Skip to content

Instantly share code, notes, and snippets.

@robertDurst

robertDurst/Tree.py

Created Sep 28, 2019
Embed
What would you like to do?
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