-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7. Binary_Search_Tree.py
186 lines (137 loc) · 5.67 KB
/
7. Binary_Search_Tree.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class BinarySearchTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def add_child_node(self, data):
if data == self.data:
return
if data < self.data:
if self.left:
self.left.add_child_node(data)
else:
self.left = BinarySearchTreeNode(data)
else: #'else data > self.data' works too
if self.right:
self.right.add_child_node(data)
else:
self.right = BinarySearchTreeNode(data)
def depth_first_search_in_order_traversal(self):
elements = []
#visiting left node
if self.left:
elements += self.left.depth_first_search_in_order_traversal()
#visiting root/base/current node
elements.append(self.data)
#visiting right node
if self.right:
elements += self.right.depth_first_search_in_order_traversal()
return elements
def depth_first_search_pre_order_traversal(self):
elements = []
#visiting root/base/current node
elements.append(self.data)
#visiting left node
if self.left:
elements += self.left.depth_first_search_pre_order_traversal()
#visiting right node
if self.right:
elements += self.right.depth_first_search_pre_order_traversal()
return elements
def depth_first_search_post_order_traversal(self):
elements = []
#visiting left node
if self.left:
elements += self.left.depth_first_search_post_order_traversal()
#visiting right node
if self.right:
elements += self.right.depth_first_search_post_order_traversal()
#visiting root/base/current node
elements.append(self.data)
return elements
def search_binary_search_tree(self, value_to_find):
if self.data == value_to_find:
return True
if value_to_find < self.data:
#'value_to_find' might be in left sub-tree
if self.left:
return self.left.search_binary_search_tree(value_to_find)
else:
return False
else:
#'value_to_find' might be in right sub-tree
if self.right:
return self.right.search_binary_search_tree(value_to_find)
else:
return False
def print_binary_search_tree(self, level=0):
if self.data:
if self.right:
self.right.print_binary_search_tree(level + 1)
print(' ' * 4 * level + '-> ' + str(self.data))
if self.left:
self.left.print_binary_search_tree(level + 1)
def calculate_sum(self):
total = 0
for i in self.depth_first_search_in_order_traversal():
total += i
return total
def find_min(self):
if self.data:
if self.left:
return self.left.find_min()
else:
return self.data
def find_max(self):
if self.data:
if self.right:
return self.right.find_max()
else:
return self.data
def delete_node(self, data):
if data < self.data:
if self.left:
self.left = self.left.delete_node(data)
elif data > self.data:
if self.right:
self.right = self.right.delete_node(data)
else: #'elif data == self.data:' works too
if self.left is None and self.right is None:
self = None
return self
if self.left is None:
self = self.right
return self
if self.right is None:
self = self.left
return self
if self.left is not None and self.right is not None:
minimum_value_in_right_child_sub_tree = self.right.find_min()
self.data = minimum_value_in_right_child_sub_tree
self.right = self.right.delete_node(minimum_value_in_right_child_sub_tree)
return self
return self
def build_binary_search_tree(elements):
root_node = BinarySearchTreeNode(elements[0])
for i in range(1, len(elements)):
root_node.add_child_node(elements[i])
return root_node
if __name__ == '__main__':
numbers = [15, 27, 12, 14, 20, 7, 88, 23]
numbers_binary_search_tree = build_binary_search_tree(numbers)
print(numbers_binary_search_tree.depth_first_search_in_order_traversal())
numbers_binary_search_tree.print_binary_search_tree()
print(numbers_binary_search_tree.search_binary_search_tree(24))
print(numbers_binary_search_tree.search_binary_search_tree(23))
numbers_binary_search_tree.delete_node(27)
numbers_binary_search_tree.print_binary_search_tree()
print(numbers_binary_search_tree.depth_first_search_in_order_traversal())
numbers_binary_search_tree.delete_node(14)
numbers_binary_search_tree.print_binary_search_tree()
print(numbers_binary_search_tree.depth_first_search_in_order_traversal())
numbers_binary_search_tree.delete_node(15)
numbers_binary_search_tree.print_binary_search_tree()
print(numbers_binary_search_tree.depth_first_search_in_order_traversal())
numbers_binary_search_tree.delete_node(88)
numbers_binary_search_tree.print_binary_search_tree()
print(numbers_binary_search_tree.depth_first_search_in_order_traversal())