-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.py
More file actions
222 lines (195 loc) · 5.84 KB
/
Copy pathBST.py
File metadata and controls
222 lines (195 loc) · 5.84 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.parent = None
def __str__(self):
return str(self.data)
class BST:
def __init__(self):
self.root = None
def insert(self,x):
if self.root == None:
self.root = Node(x)
else:
self.insert_rec(self.root,x)
def insert_rec(self,root,x):
"""
recursively insert x into the BST rooted at root.
"""
if root == None:
return Node(x)
elif root.data > x:
root.left = self.insert_rec(root.left,x)
root.left.parent = root
else:
root.right = self.insert_rec(root.right,x)
root.right.parent = root
return root
def insert_iterative(self,x):
"""
Insert x into the BST rooted at root.
There are 2 cases:
1. x is smaller than root.left, insert x to the left of root.left.
2. x is greater than root.right, insert x to the right of root.right.
"""
root = self.root
if root == None:
self.root = Node(x)
return
while root != None:
if root.data > x:
if root.left == None:
root.left = Node(x)
root.left.parent = root
break
else:
root = root.left
else:
if root.right == None:
root.right = Node(x)
root.right.parent = root
break
else:
root = root.right
def search(self,root,x):
if root == None:
return False
if root.data == x:
return root
if root.data > x:
return self.search(root.left,x)
else:
return self.search(root.right,x)
def search_iterative(self,x):
root=self.root
while root != None:
if root.data == x:
return root
elif root.data > x:
root = root.left
else:
root = root.right
def minimum(self,root):
if root == None:
return None
else:
while root.left != None:
root = root.left
return root
def successor(self,x):
"""
Return the successor of x in the BST.
Successor is the node with the smallest key greater than x.
"""
if x.right!=None:
return self.minimum(x.right)
else:
y = x.parent
while y!=None and x==y.right:
x = y
y = y.parent
return y
def transplant(self,u,v):
"""
Replace subtree rooted at u with subtree rooted at v."""
if u.parent == None:
self.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
if v != None:
v.parent = u.parent
def delete(self,x):
"""
Delete the node containing x from the BST."""
if x.left == None:
self.transplant(x,x.right)
elif x.right == None:
self.transplant(x,x.left)
else:
y = self.minimum(x.right)
if y.parent != x:
self.transplant(y,y.right)
y.right = x.right
y.right.parent = y
self.transplant(x,y)
y.left = x.left
y.left.parent = y
def deletev2(self,x):
x=self.search_iterative(x)
print("found {}".format(x.data))
if x.left == None and x.right == None:
if x.parent.left == x:
x.parent.left = None
else:
x.parent.right = None
if x.left==None:
y=x.right
y.parent=x.parent
y.parent.left=y
elif x.right==None:
y=x.left
y.parent=x.parent
y.parent.right=y
else:
z=self.successor(x)
z_bar=z.parent
if z_bar.left==z:
z_bar.left=None
else:
z_bar.right=None
z.parent=x.parent
z.left=x.left
z.right=x.right
if x.parent.left==x:
x.parent.left=z
else:
x.parent.right=z
def inorder(self,root):
if root == None:
return
self.inorder(root.left)
print(root.data)
self.inorder(root.right)
def preorder(self,root):
if root == None:
return
print(root.data)
self.preorder(root.left)
self.preorder(root.right)
def postorder(self,root):
if root == None:
return
self.postorder(root.left)
self.postorder(root.right)
print(root.data)
def inorder_iterative(self,root):
stack = []
while True:
if root != None:
stack.append(root)
root = root.left
elif len(stack) > 0:
root = stack.pop()
print(root.data)
root = root.right
else:
break
def inorder(x):
if x is None:
return
inorder(x.left)
print(x.data, end=' ')
inorder(x.right)
bst = BST()
def addTo_BST(list):
for i in list:
bst.insert(i)
return bst
l=[43,23,545,3,431,54,65,76,87,2]
addTo_BST(l)
bst.delete(bst.search_iterative(43))
bst.inorder_iterative(bst.root)