metalni

APS Labs 7 Nerekurzivno izminuvanje vo preorder

Dec 27th, 2020 (edited)
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4. import java.util.NoSuchElementException;
  5.  
  6. class BNode<E> {
  7.  
  8.     public E info;
  9.     public BNode<E> left;
  10.     public BNode<E> right;
  11.  
  12.     static int LEFT = 1;
  13.     static int RIGHT = 2;
  14.  
  15.     public BNode(E info) {
  16.         this.info = info;
  17.         left = null;
  18.         right = null;
  19.     }
  20.  
  21.     public BNode() {
  22.         this.info = null;
  23.         left = null;
  24.         right = null;
  25.     }
  26.  
  27.     public BNode(E info, BNode<E> left, BNode<E> right) {
  28.         this.info = info;
  29.         this.left = left;
  30.         this.right = right;
  31.     }
  32.  
  33. }
  34.  
  35. class BTree<E> {
  36.  
  37.     public BNode<E> root;
  38.  
  39.     public BTree() {
  40.         root = null;
  41.     }
  42.  
  43.     public BTree(E info) {
  44.         root = new BNode<E>(info);
  45.     }
  46.  
  47.     public void makeRoot(E elem) {
  48.         root = new BNode(elem);
  49.     }
  50.  
  51.     public void makeRootNode(BNode<E> node) {
  52.         root = node;
  53.     }
  54.  
  55.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  56.  
  57.         BNode<E> tmp = new BNode<E>(elem);
  58.  
  59.         if (where == BNode.LEFT) {
  60.             if (node.left != null)  // veke postoi element
  61.                 return null;
  62.             node.left = tmp;
  63.         } else {
  64.             if (node.right != null) // veke postoi element
  65.                 return null;
  66.             node.right = tmp;
  67.         }
  68.  
  69.         return tmp;
  70.     }
  71.  
  72.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  73.  
  74.         if (where == BNode.LEFT) {
  75.             if (node.left != null)  // veke postoi element
  76.                 return null;
  77.             node.left = tmp;
  78.         } else {
  79.             if (node.right != null) // veke postoi element
  80.                 return null;
  81.             node.right = tmp;
  82.         }
  83.  
  84.         return tmp;
  85.     }
  86.  
  87.     public void PreorderNonRecursive() {
  88.         ArrayStack<BNode<E>> st = new ArrayStack<BNode<E>>(500);
  89.         BNode<E> node = root;
  90.  
  91.         st.push(this.root);
  92.  
  93.         if(node == null)
  94.             return;
  95.  
  96.         while(!st.isEmpty()){
  97.             BNode<E> curr = st.peek();
  98.             System.out.print(curr.info + " ");
  99.             st.pop();
  100.  
  101.             if(curr.right != null)
  102.                 st.push(curr.right);
  103.             if(curr.left != null)
  104.                 st.push(curr.left);
  105.         }
  106.     }
  107.  
  108. }
  109.  
  110. interface Stack<E> {
  111.  
  112.     // Elementi na stekot se objekti od proizvolen tip.
  113.  
  114.     // Metodi za pristap:
  115.  
  116.     public boolean isEmpty ();
  117.     // Vrakja true ako i samo ako stekot e prazen.
  118.  
  119.     public E peek ();
  120.     // Go vrakja elementot na vrvot od stekot.
  121.  
  122.     // Metodi za transformacija:
  123.  
  124.     public void clear ();
  125.     // Go prazni stekot.
  126.  
  127.     public void push (E x);
  128.     // Go dodava x na vrvot na stekot.
  129.  
  130.     public E pop ();
  131.     // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  132. }
  133.  
  134. class ArrayStack<E> implements Stack<E> {
  135.     private E[] elems;
  136.     private int depth;
  137.  
  138.     @SuppressWarnings("unchecked")
  139.     public ArrayStack (int maxDepth) {
  140.         // Konstrukcija na nov, prazen stek.
  141.         elems = (E[]) new Object[maxDepth];
  142.         depth = 0;
  143.     }
  144.  
  145.  
  146.     public boolean isEmpty () {
  147.         // Vrakja true ako i samo ako stekot e prazen.
  148.         return (depth == 0);
  149.     }
  150.  
  151.  
  152.     public E peek () {
  153.         // Go vrakja elementot na vrvot od stekot.
  154.         if (depth == 0)
  155.             throw new NoSuchElementException();
  156.         return elems[depth-1];
  157.     }
  158.  
  159.  
  160.     public void clear () {
  161.         // Go prazni stekot.
  162.         for (int i = 0; i < depth; i++)  elems[i] = null;
  163.         depth = 0;
  164.     }
  165.  
  166.  
  167.     public void push (E x) {
  168.         // Go dodava x na vrvot na stekot.
  169.         elems[depth++] = x;
  170.     }
  171.  
  172.  
  173.     public E pop () {
  174.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  175.         if (depth == 0)
  176.             throw new NoSuchElementException();
  177.         E topmost = elems[--depth];
  178.         elems[depth] = null;
  179.         return topmost;
  180.     }
  181. }
  182.  
  183. public class PreorderNonRecursive {
  184.  
  185.     public static void main(String[] args) throws Exception {
  186.         int i, j, k;
  187.         int index;
  188.         String action;
  189.  
  190.         String line;
  191.  
  192.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  193.         StringTokenizer st;
  194.  
  195.         int N = Integer.parseInt(br.readLine());
  196.  
  197.         BNode<String> nodes[] = new BNode[N];
  198.         BTree<String> tree = new BTree<String>();
  199.  
  200.         for (i=0;i<N;i++)
  201.             nodes[i] = new BNode<String>();
  202.  
  203.         for (i = 0; i < N; i++) {
  204.             line = br.readLine();
  205.             st = new StringTokenizer(line);
  206.             index = Integer.parseInt(st.nextToken());
  207.             nodes[index].info = st.nextToken();
  208.             action = st.nextToken();
  209.             if (action.equals("LEFT")) {
  210.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  211.             } else if (action.equals("RIGHT")) {
  212.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  213.             } else {
  214.                 // this node is the root
  215.                 tree.makeRootNode(nodes[index]);
  216.             }
  217.         }
  218.  
  219.         br.close();
  220.  
  221.         tree.PreorderNonRecursive();
  222.  
  223.     }
  224. }
  225.  
Add Comment
Please, Sign In to add comment