-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.py
136 lines (111 loc) · 4.01 KB
/
BinarySearchTree.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class BinarySearchNode():
def __init__(self,data):
self.data=data
self.right=None
self.left=None
# Add child to the tree asper value
def add_child(self,data):
if data==self.data:
return
if data < self.data:
if self.left:
self.left.add_child(data) # recursive call
else:
self.left=BinarySearchNode(data) # newly added node with value data
else:
if self.right:
self.right.add_child(data) # recursive call
else:
self.right = BinarySearchNode(data) # newly added node with value data
# find minimum value in binary search Tree
def find_min(self):
if self.left==None:
return self.data
else:
left_elements = self.In_order_Traversal()
return left_elements[0]
# find max value in binary search Tree
def find_max(self):
if self.right==None:
return self.data
else:
left_elements = self.In_order_Traversal()
return left_elements[-1]
# Binary Search Tree Traversal Techniques 1-In order traversal, 2- pre-order traversal 3- post-order traversal
# In-order traversal produces ascending sorted list(set)
def In_order_Traversal(self):
elements=[]
# visit left sub_tree
if self.left:
elements+=self.left.In_order_Traversal()
# base node
elements.append(self.data)
# visit right subtree
if self.right:
elements+=self.right.In_order_Traversal()
return elements
# pre-order traversal
def pre_order_traversal(self):
elements = [self.data]
if self.left:
elements += self.left.pre_order_traversal()
if self.right:
elements += self.right.pre_order_traversal()
return elements
# post-order traversal
def post_order_traversal(self):
elements = []
if self.left:
elements += self.left.post_order_traversal()
if self.right:
elements += self.right.post_order_traversal()
elements.append(self.data)
return elements
# Search for value in Binary Search Tree,True/False
def Search_Value(self,value):
if self.data==value:
return True
if value < self.left.data:
self.left.Search_Value(value)
else:
return False
if value > self.right.data :
self.right.Search_Value(value)
else:
return False
# reverse Binary Search Tree
def invertTree(self):
if self:
self.left , self.right= self.right.invertTree() , self.left.invertTree()
return self
def create_binary_search_tree(array):
root= BinarySearchNode(array[0])
for element in array[1:]:
root.add_child(element)
return root
if __name__ == '__main__':
numbers=[12,43,65,89,100,33,12,15,7]
colors=['green','red','yellow','pink']
# In order traversal check
number_tree=create_binary_search_tree(numbers)
new_number_tree=number_tree
result=number_tree.In_order_Traversal()
print('In-order-Traversal result on numbers is: ',result)
# check on strings
color_tree = create_binary_search_tree(colors)
result = color_tree.In_order_Traversal()
print('In-order-Traversal result on strings is: ', result)
# check if vvalue is found in binary search tree
number=12
print('number {} is found: {}'.format(number,new_number_tree.Search_Value(number)))
# Exercise Solutions
# find minimum value in Binary Search Tree
print('Minimum number in tree is: ',new_number_tree.find_min())
# find max value in Binary Search Tree
print('Max number in tree is: ',new_number_tree.find_max())
result = number_tree.pre_order_traversal()
print('pre-order-Traversal result on numbers is: ', result)
# inverse tree
new_number_tree.invertTree()
result = new_number_tree.In_order_Traversal()
print('In-order-Traversal result on numbers is: ', result)