Advertisement
mvCode

NonRecursive , Nerekurzivno izminuvanje vo preorder AIPS

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