Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Traversals {
- public <T> void iterativeInorderTraverse(BinaryNode<T> root){
- StackInterface<BinaryNode<T>> nodeStack = new LinkedStack<>();
- BinaryNode<T> currentNode = root;
- while(!nodeStack.isEmpty() || (currentNode != null)){
- while(currentNode != null){
- nodeStack.push(currentNode);
- currentNode = currentNode.getLeftChild();
- }
- if(!nodeStack.isEmpty()){
- BinaryNode<T> nextNode = nodeStack.pop();
- assert nextNode != null;
- System.out.println(nextNode.getData());
- currentNode = nextNode.getRightChild();
- }
- }
- }
- public static void main(String[] args) {
- BinaryNode g = new BinaryNode("G");
- BinaryNode f = new BinaryNode("F", null, g);
- BinaryNode e = new BinaryNode("E");
- BinaryNode d = new BinaryNode("D");
- BinaryNode c = new BinaryNode("C", f, null);
- BinaryNode b = new BinaryNode("B", d, e);
- BinaryNode a = new BinaryNode("A", b, c);
- Traversals t = new Traversals();
- t.iterativeInorderTraverse(a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement