Advertisement
Guest User

Untitled

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