Skip to content

Instantly share code, notes, and snippets.

@AlexAbraham1
Created December 31, 2014 20:30
Show Gist options
  • Save AlexAbraham1/0b7d42fde7cb010e5262 to your computer and use it in GitHub Desktop.
Save AlexAbraham1/0b7d42fde7cb010e5262 to your computer and use it in GitHub Desktop.
//----------------------------------------------------
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