Crazy

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

Dec 3rd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.ArrayList;
  4. import java.util.StringTokenizer;
  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.         // vasiot kod ovde
  90.         ArrayStack<BNode<E>> s = new ArrayStack<BNode<E>>(100);
  91.         BNode<E> p = root;
  92.         while(true){
  93.             if(p!=null){
  94.                 s.push(p);
  95.             System.out.print(p.info+" ");
  96.             }
  97.             while(p!=null){
  98.                 p=p.left;
  99.  
  100.                 if(p!=null){
  101.                     System.out.print(p.info+" ");
  102.                     s.push(p);
  103.                 }
  104.  
  105.             }
  106.             if(s.isEmpty())
  107.                 break;
  108.             p=s.peek();
  109.             s.pop();
  110.             p=p.right;
  111.         }
  112.     }
  113.  
  114. }
  115.  
  116. interface Stack<E> {
  117.  
  118.     // Elementi na stekot se objekti od proizvolen tip.
  119.  
  120.     // Metodi za pristap:
  121.  
  122.     public boolean isEmpty ();
  123.     // Vrakja true ako i samo ako stekot e prazen.
  124.  
  125.     public E peek ();
  126.     // Go vrakja elementot na vrvot od stekot.
  127.  
  128.     // Metodi za transformacija:
  129.  
  130.     public void clear ();
  131.     // Go prazni stekot.
  132.  
  133.     public void push (E x);
  134.     // Go dodava x na vrvot na stekot.
  135.  
  136.     public E pop ();
  137.     // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  138. }
  139.  
  140. class ArrayStack<E> implements Stack<E> {
  141.     private E[] elems;
  142.     private int depth;
  143.  
  144.     @SuppressWarnings("unchecked")
  145.     public ArrayStack (int maxDepth) {
  146.         // Konstrukcija na nov, prazen stek.
  147.         elems = (E[]) new Object[maxDepth];
  148.         depth = 0;
  149.     }
  150.  
  151.  
  152.     public boolean isEmpty () {
  153.         // Vrakja true ako i samo ako stekot e prazen.
  154.         return (depth == 0);
  155.     }
  156.  
  157.  
  158.     public E peek () {
  159.         // Go vrakja elementot na vrvot od stekot.
  160.         if (depth == 0)
  161.             throw new NoSuchElementException();
  162.         return elems[depth-1];
  163.     }
  164.  
  165.  
  166.     public void clear () {
  167.         // Go prazni stekot.
  168.         for (int i = 0; i < depth; i++)  elems[i] = null;
  169.         depth = 0;
  170.     }
  171.  
  172.  
  173.     public void push (E x) {
  174.         // Go dodava x na vrvot na stekot.
  175.         elems[depth++] = x;
  176.     }
  177.  
  178.  
  179.     public E pop () {
  180.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  181.         if (depth == 0)
  182.             throw new NoSuchElementException();
  183.         E topmost = elems[--depth];
  184.         elems[depth] = null;
  185.         return topmost;
  186.     }
  187. }
  188.  
  189. public class PreorderNonRecursive {
  190.  
  191.     public static void main(String[] args) throws Exception {
  192.         int i, j, k;
  193.         int index;
  194.         String action;
  195.  
  196.         String line;
  197.  
  198.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  199.         StringTokenizer st;
  200.  
  201.         int N = Integer.parseInt(br.readLine());
  202.  
  203.         BNode<String> nodes[] = new BNode[N];
  204.         BTree<String> tree = new BTree<String>();
  205.  
  206.         for (i=0;i<N;i++)
  207.             nodes[i] = new BNode<String>();
  208.  
  209.         for (i = 0; i < N; i++) {
  210.             line = br.readLine();
  211.             st = new StringTokenizer(line);
  212.             index = Integer.parseInt(st.nextToken());
  213.             nodes[index].info = st.nextToken();
  214.             action = st.nextToken();
  215.             if (action.equals("LEFT")) {
  216.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  217.             } else if (action.equals("RIGHT")) {
  218.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  219.             } else {
  220.                 // this node is the root
  221.                 tree.makeRootNode(nodes[index]);
  222.             }
  223.         }
  224.  
  225.         br.close();
  226.  
  227.         tree.PreorderNonRecursive();
  228.  
  229.     }
  230. }
Add Comment
Please, Sign In to add comment