Chap4_BinaryTree
Chap4_BinaryTree
def InOrder(root):
if root == None:
return
InOrder(root.left)
print(root.id, end = ' ')
InOrder(root.right)
def PreOrder(root):
if root == None:
return
print(root.id, end = ' ')
PreOrder(root.left)
PreOrder(root.right)
def PostOrder(root):
if root == None:
return
PostOrder(root.left)
PostOrder(root.right)
print(root.id, end = ' ')
def Depth(node):
if node == None:
return
if node == root:
return 1
return Height(root) - Height(node) + 1
def Height(node):
if node == None:
return 0
lheight = Height(node.left)
rheight = Height(node.right)
return max(lheight + 1, rheight + 1)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
InOrder(root)
print()
Insert(root, 10, 3)
Insert(root, 20, 3)
InOrder(root)