Mitrezzz

[АПС] Лаб 7: Дрва Нерекурзивно изминување во preorder

Dec 19th, 2018 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. //package lab6nonRecursivePreorder;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.StringTokenizer;
  6. import java.util.NoSuchElementException;
  7.  
  8. import javax.xml.transform.Templates;
  9.  
  10. class BNode<E> {
  11.    
  12.     public E info;
  13.     public BNode<E> left;
  14.     public BNode<E> right;
  15.    
  16.     static int LEFT = 1;
  17.     static int RIGHT = 2;
  18.    
  19.     public BNode(E info) {
  20.         this.info = info;
  21.         left = null;
  22.         right = null;
  23.     }
  24.    
  25.     public BNode() {
  26.         this.info = null;
  27.         left = null;
  28.         right = null;
  29.     }
  30.    
  31.     public BNode(E info, BNode<E> left, BNode<E> right) {
  32.         this.info = info;
  33.         this.left = left;
  34.         this.right = right;
  35.     }
  36.    
  37. }
  38.  
  39. class BTree<E> {
  40.    
  41.     public BNode<E> root;
  42.    
  43.     public BTree() {
  44.         root = null;
  45.     }
  46.    
  47.     public BTree(E info) {
  48.         root = new BNode<E>(info);
  49.     }
  50.    
  51.     public void makeRoot(E elem) {
  52.         root = new BNode(elem);
  53.     }
  54.    
  55.     public void makeRootNode(BNode<E> node) {
  56.         root = node;
  57.     }
  58.    
  59.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  60.        
  61.         BNode<E> tmp = new BNode<E>(elem);
  62.        
  63.         if (where == BNode.LEFT) {
  64.             if (node.left != null)  // veke postoi element
  65.                 return null;
  66.             node.left = tmp;
  67.         } else {
  68.             if (node.right != null) // veke postoi element
  69.                 return null;
  70.             node.right = tmp;
  71.         }
  72.        
  73.         return tmp;
  74.     }
  75.    
  76.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  77.        
  78.         if (where == BNode.LEFT) {
  79.             if (node.left != null)  // veke postoi element
  80.                 return null;
  81.             node.left = tmp;
  82.         } else {
  83.             if (node.right != null) // veke postoi element
  84.                 return null;
  85.             node.right = tmp;
  86.         }
  87.        
  88.         return tmp;
  89.     }
  90.    
  91.     public void PreorderNonRecursive() {
  92.         ArrayStack<BNode<E>> stack = new ArrayStack<BNode<E>>(100);
  93.         BNode<E> p;
  94.         stack.push(root);
  95.        
  96.         while(true) {
  97.             p = stack.pop();
  98.             System.out.print(p.info + " ");
  99.            
  100.             if (p.right != null) {
  101.                 stack.push(p.right);
  102.             }
  103.            
  104.             if (p.left != null) {
  105.                 stack.push(p.left);
  106.             }
  107.            
  108.             if (stack.isEmpty()) {
  109.                 break;
  110.             }
  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