Created
May 30, 2014 16:27
-
-
Save hillscottc/ae03d5dc2599b0c0c6ea to your computer and use it in GitHub Desktop.
Binary tree traversal operations.
This file contains 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
tree_walk(x): | |
if x: | |
tree_walk(x:left) | |
print x:key | |
tree_walk(x:right) | |
tree_search(x, k): | |
"""Recursive search.""" | |
if not x or k == x.key: | |
return x | |
if k < x.key | |
return tree_search(x.left, k) | |
else | |
return tree_search(x.right, k) | |
tree_search_i(x, k): | |
""""Iterative search.""" | |
while x and k != x.key | |
if k < x:key | |
x = x:left | |
else | |
x = x:right | |
return x | |
tree_min(x): | |
while x.left: | |
x = x.left | |
return x | |
tree_max(x): | |
while x.right: | |
x = x.right | |
return x | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment