Skip to content

Instantly share code, notes, and snippets.

@robertDurst
Created September 28, 2019 20:24
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 robertDurst/efd8ca0f3d1be724a5e9b763a32addfa to your computer and use it in GitHub Desktop.
Save robertDurst/efd8ca0f3d1be724a5e9b763a32addfa to your computer and use it in GitHub Desktop.
Traversal Practice
class Tree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def inorder(node):
# fill in code
return
def preorder(node):
# fill in code
return
def postorder(node):
# fill in code
return
root = Tree("R")
A = Tree("A")
B = Tree("B")
C = Tree("C")
D = Tree("D")
E = Tree("E")
F = Tree("F")
G = Tree("G")
root.left = A
root.right = B
A.left = C
C.right = D
B.left = E
B.right = F
F.right = G
"""
R
A B
C E F
D G
"""
print "These should all be true!"
print inorder(root) == "CDAREBFG"
print preorder(root) == "RACDBEFG"
print postorder(root) == "DCAEGFBR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment