2. Build a Simple Binary Tree
In [1]:
Copied!
from utils.nodes import BinaryTreeNode as Node
from utils.tree_traversal import TreeTraversal as tt
from utils.nodes import BinaryTreeNode as Node
from utils.tree_traversal import TreeTraversal as tt
In [2]:
Copied!
# Inherited base BinaryTreeNode class
# just for clarity but not required
class BinaryTree(Node):
pass
# Inherited base BinaryTreeNode class
# just for clarity but not required
class BinaryTree(Node):
pass
In [3]:
Copied!
root = BinaryTree(1)
root.left = Node(2)
root.left.left = Node(3)
root.left.right = Node(4)
root.left.right.left = Node(5)
root.right = Node(6)
root.right.left = Node(7)
root.right.right = Node(8)
root = BinaryTree(1)
root.left = Node(2)
root.left.left = Node(3)
root.left.right = Node(4)
root.left.right.left = Node(5)
root.right = Node(6)
root.right.left = Node(7)
root.right.right = Node(8)
In [4]:
Copied!
tt.traverse_preorder(root)
tt.traverse_preorder(root)
1 2 3 4 5 6 7 8
In [5]:
Copied!
tt.traverse_inorder(root)
tt.traverse_inorder(root)
3 2 5 4 1 7 6 8
In [6]:
Copied!
tt.traverse_postorder(root)
tt.traverse_postorder(root)
3 5 4 2 7 8 6 1
In [ ]:
Copied!