forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BST_Construction_Recursive.py
184 lines (161 loc) · 5.63 KB
/
BST_Construction_Recursive.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
183
184
# My solution. Failing on 2 test case but do't know why
class BST:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
def insert(self, value):
if value < self.value:
if self.left is None:
self.left = BST(value)
else:
self.left.insert(value)
else:
if self.right is None:
self.right = BST(value)
else:
self.right.insert(value)
return self
def contains(self, value):
if value < self.value:
if self.left is None:
return False
else:
self.left.contains(value)
elif value > self.value:
if self.right is None:
return False
else:
return self.right.contains(value)
else:
return True
def remove(self, value, parent = None):
if value < self.value:
if self.left is not None:
self.left.remove(value, self)
elif value > self.value:
if self.right is not None:
self.right.remove(value, self)
else:
if self.left is not None and self.right is not None:
self.value = self.right.get_min_value()
self.right.remove(self.value, self)
elif parent is None:
if self.left is not None:
self.value = self.left.value
self.right = self.left.right
self.left = self.left.left
elif self.right is not None:
self.value = self.right.value
self.left = self.right.left
self.right = self.right.right
else:
pass # This is a single-node tree. nothing to do
elif parent.left == self:
parent.left = self.left if self.left is not None else self.right
elif parent.right == self:
parent.right = self.left if self.left is not None else self.right
return self
def get_min_value(self):
if self.left is None:
return self.value
else:
return self.left.get_min_value()
# Source: https://tinyurl.com/rxnq4sz
def traverse_list(node):
visit_order = list()
if node:
visit_order.append(node.value)
visit_order += traverse_list(node.left)
visit_order += traverse_list(node.right)
return visit_order
def traverse(node):
visit_order = list()
if node:
visit_order.append(node)
visit_order += traverse(node.left)
visit_order += traverse(node.right)
return visit_order
def get_min_node_value(node):
while node.left:
node = node.left
return node.value
class BST:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def compare(self, target):
if self.value > target:
return -1
elif self.value < target:
return 1
else:
return 0
def insert(self, value):
node = self
while True:
comparision = node.compare(value)
if comparision == -1:
if node.left:
node = node.left
else:
node.left = BST(value)
break
else: # comparision == 1 or equals
if node.right:
node = node.right
else:
node.right = BST(value)
break
return self
def contains(self, value):
node = self
while node:
comparision = node.compare(value)
if comparision == -1:
node = node.left
elif comparision == 1:
node = node.right
else:
return True
return False
def remove(self, value, parent_node=None):
node = self
while True:
comparision = node.compare(value)
if comparision == -1:
if node.left:
parent_node = node
node = node.left
else:
print('Value not found')
break
elif comparision == 1:
if node.right:
parent_node = node
node = node.right
else:
print('Value not found')
break
else:
if node.left and node.right: # node with left and child
node.value = get_min_node_value(node.right)
node.right.remove(node.value, node)
elif parent_node is None: # parent node
if node.left:
node.value = node.left.value
node.right = node.left.right
node.left = node.left.left
elif node.right:
node.value = node.right.value
node.left = node.right.left
node.right = node.right.right
else: # parent node with no children
node.value = None
elif parent_node.left == node: # found in the left node with right None
parent_node.left = node.left if node.left else node.right
elif parent_node.right == node: # found in the right node with left None
parent_node.right = node.left if node.left else node.right
break
return self