-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.py
66 lines (58 loc) · 1.76 KB
/
BinaryTree.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
65
66
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
def print_tree(self):
if self.left:
self.left.print_tree()
print(self.data)
if self.right:
self.right.print_tree()
def inorder_traversal(self, node):
res = []
if node:
res = self.inorder_traversal(node.left)
res.append(node.data)
res = res + self.inorder_traversal(node.right)
return res
def pre_order_traversal(self, node):
res = []
if node:
res.append(node.data)
res += self.pre_order_traversal(node.left)
res += self.pre_order_traversal(node.right)
return res
def post_order_traversal(self, node):
res = []
if node:
res = self.post_order_traversal(node.left)
res += self.post_order_traversal(node.right)
res.append(node.data)
return res
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)
root.insert(1)
root.insert(19)
root.insert(18)
root.insert(13)
root.print_tree()
print("In Order Traversal ==>", root.inorder_traversal(root))
print("Pre Order Traversal ==>", root.pre_order_traversal(root))
print("Post Order Traversal ==>", root.post_order_traversal(root))