Skip to content

Instantly share code, notes, and snippets.

@hillscottc
Created May 30, 2014 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillscottc/ae03d5dc2599b0c0c6ea to your computer and use it in GitHub Desktop.
Save hillscottc/ae03d5dc2599b0c0c6ea to your computer and use it in GitHub Desktop.
Binary tree traversal operations.
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