Kame3

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

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