-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeTraversalRecursion.py
64 lines (45 loc) · 1.4 KB
/
BinaryTreeTraversalRecursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class Tree:
def __init__(self, head):
self.head = head
def pre_order_traversal(self, node):
print(node.data, end=" ")
if node.left is not None:
self.pre_order_traversal(node.left)
if node.right is not None:
self.pre_order_traversal(node.right)
def in_order_traversal(self, node):
if node.left is not None:
self.in_order_traversal(node.left)
print(node.data, end=" ")
if node.right is not None:
self.in_order_traversal(node.right)
def post_order_traversal(self, node):
if node.left is not None:
self.post_order_traversal(node.left)
if node.right is not None:
self.post_order_traversal(node.right)
print(node.data, end=" ")
if __name__ == "__main__":
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)
node6 = Node(6)
node1.left = node2
node2.left = node3
node2.right = node4
node1.right = node5
node5.right = node6
tree = Tree(node1)
print("\nPre Order Traversal: ")
tree.pre_order_traversal(node1)
print("\nIn Order Traversal: ")
tree.in_order_traversal(node1)
print("\nPost Order Traversal: ")
tree.post_order_traversal(node1)