Advertisement
ilevishinov

Нерекурзивно изминување во preorder

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