Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.  
  2. public class Traversals {
  3.     public <T> void iterativeInorderTraverse(BinaryNode<T> root){
  4.         StackInterface<BinaryNode<T>> nodeStack = new LinkedStack<>();
  5.         BinaryNode<T> currentNode = root;
  6.  
  7.         while(!nodeStack.isEmpty() || (currentNode != null)){
  8.             while(currentNode != null){
  9.                 nodeStack.push(currentNode);
  10.                 currentNode = currentNode.getLeftChild();
  11.             }
  12.             if(!nodeStack.isEmpty()){
  13.                 BinaryNode<T> nextNode = nodeStack.pop();
  14.                 assert nextNode != null;
  15.  
  16.                 System.out.println(nextNode.getData());
  17.                 currentNode = nextNode.getRightChild();
  18.             }
  19.         }
  20.     }
  21.    
  22.     public static void main(String[] args) {
  23.         BinaryNode g = new BinaryNode("G");
  24.         BinaryNode f = new BinaryNode("F", null, g);
  25.         BinaryNode e = new BinaryNode("E");
  26.         BinaryNode d = new BinaryNode("D");
  27.         BinaryNode c = new BinaryNode("C", f, null);
  28.         BinaryNode b = new BinaryNode("B", d, e);
  29.         BinaryNode a = new BinaryNode("A", b, c);
  30.         Traversals t = new Traversals();
  31.         t.iterativeInorderTraverse(a);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement