Created
December 31, 2014 20:30
-
-
Save AlexAbraham1/0b7d42fde7cb010e5262 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//---------------------------------------------------- | |
protected void iterativeWalk(Visitor v) | |
{ | |
Node node = root; | |
if (node != nil) { | |
while (node.left != nil) { | |
node = node.left; | |
} | |
while (node != nil) { | |
//Visit node | |
v.visit(node); | |
//If there is a right node, go right and then go to leftmost node (successor) | |
if (node.right != nil) { | |
node = node.right; | |
while (node.left != nil) { | |
node = node.left; | |
} | |
} | |
//If no right node, first parent with left child as node is the new node to visit (successor) | |
else { | |
while (node.parent != nil && node == node.parent.right) { | |
node = node.parent; | |
} | |
node = node.parent; | |
} | |
} | |
} | |
} | |
//---------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment