-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathbst-construction.py
204 lines (184 loc) · 5 KB
/
bst-construction.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# BST CONSTRUCTION
# Do not edit the class below except for
# the insert, contains, and remove methods.
# Feel free to add new properties and methods
# to the class.
class BST:
def __init__(self, value, parent=None):
self.value = value
self.left = None
self.right = None
def insert(self, value):
# Write your code here.
# Do not edit the return statement of this method.
if not self:
return BST(value)
current = self
while current:
if value < current.value:
if current.left:
current = current.left
else:
current.left = BST(value)
break
else:
if current.right:
current = current.right
else:
current.right = BST(value)
break
return self
def contains(self, value):
# Write your code here.
current = self
while current:
if value == current.value:
return True
elif value < current.value:
current = current.left
else:
current = current.right
return False
def getMin(self):
current = self
while current.left is not None:
current = current.left
return current.value
def remove(self, value, parent=None):
# Write your code here.
# Do not edit the return statement of this method.
if value < self.value:
if self.left:
self.left.remove(value, self)
elif value > self.value:
if self.right:
self.right.remove(value, self)
else:
if self.left is not None and self.right is not None:
self.value = self.right.getMin()
self.right.remove(self.value, self)
elif self.left is None and self.right is None:
if not parent:
pass
else:
if parent.left == self:
parent.left = None
else:
parent.right = None
elif self.left is None:
if not parent:
self.value = self.right.value
self.left = self.right.left
self.right = self.right.right
else:
if parent.left == self:
parent.left = self.right
else:
parent.right = self.right
else:
if not parent:
self.value = self.left.value
self.right = self.left.right
self.left = self.left.left
else:
if parent.left == self:
parent.left = self.left
else:
parent.right = self.left
# SECOND WAY
"""
# Do not edit the class below except for
# the insert, contains, and remove methods.
# Feel free to add new properties and methods
# to the class.
class BST:
def __init__(self, value, parent=None):
self.value = value
self.left = None
self.right = None
self.parent = parent
def insert(self, value):
# Write your code here.
# Do not edit the return statement of this method.
if not self:
return BST(value)
current = self
while current:
if value < current.value:
if current.left:
current = current.left
else:
current.left = BST(value, current)
break
else:
if current.right:
current = current.right
else:
current.right = BST(value, current)
break
return self
def contains(self, value):
# Write your code here.
current = self
while current:
if value == current.value:
return True
elif value < current.value:
current = current.left
else:
current = current.right
return False
def getMin(self):
current = self
while current.left is not None:
current = current.left
return current
def remove(self, value):
# Write your code here.
# Do not edit the return statement of this method.
toBeDeleted = self
while toBeDeleted and toBeDeleted.value != value:
if value < toBeDeleted.value:
toBeDeleted = toBeDeleted.left
else:
toBeDeleted = toBeDeleted.right
print('Deleting', value)
if toBeDeleted.parent:
print('Found node', toBeDeleted.value, 'with parent', toBeDeleted.parent.value)
if toBeDeleted.left is None and toBeDeleted.right is None:
if not toBeDeleted.parent:
pass
else:
if toBeDeleted.parent.left == toBeDeleted:
toBeDeleted.parent.left = None
else:
toBeDeleted.parent.right = None
elif toBeDeleted.left is None:
if not toBeDeleted.parent:
self.value = self.right.value
self.left = self.right.left
self.right = self.right.right
else:
if toBeDeleted.parent.left == toBeDeleted:
toBeDeleted.parent.left = toBeDeleted.right
toBeDeleted.right.parent = toBeDeleted.parent
else:
toBeDeleted.parent.right = toBeDeleted.right
toBeDeleted.right.parent = toBeDeleted.parent
elif toBeDeleted.right is None:
if not toBeDeleted.parent:
self.value = self.left.value
self.right = self.left.right
self.left = self.left.left
else:
if toBeDeleted.parent.left == toBeDeleted:
toBeDeleted.parent.left = toBeDeleted.left
toBeDeleted.left.parent = toBeDeleted.parent
else:
toBeDeleted.parent.right = toBeDeleted.left
toBeDeleted.left.parent = toBeDeleted.parent
else:
successor = toBeDeleted.right.getMin()
toBeDeleted.value = successor.value
toBeDeleted.right.remove(successor.value)
"""