-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaryTree.py
396 lines (318 loc) · 8.62 KB
/
binaryTree.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Binary Trees - Binary Search Tree and Binary Tree
class TreeNode:
def __init__(self, x=None):
self.val = x
self.left = None
self.right = None
#create treenode
def getNode(x):
node = TreeNode(x)
return node
# insert
def insert_node(root, x):
if root is None:
root = getNode(x)
elif x <= root.val:
if root.left is None:
root.left = getNode(x)
else:
insert_node(root.left, x)
else:
if root.right is None:
root.right = getNode(x)
else:
insert_node(root.right, x)
# bft - breadth first
def bft(root):
def bft_trav(q):
while q:
temp = q[-1]
if temp.left:
q.insert(0, temp.left)
if temp.right:
q.insert(0, temp.right)
print(temp.val)
q.pop()
bft = bft_trav([root])
# dft - depth first search
# DRL - PreOrder : root -> left -> right
def drl(root):
if root is None:
return
else:
print(root.val)
drl(root.left)
drl(root.right)
# LDR - InOrder : left -> root -> right
def ldr(root):
if root is None:
return
else:
ldr(root.left)
print(root.val)
ldr(root.right)
# LRD - PostOrder : left -> right -> root
def lrd(root):
if root is None:
return
else:
lrd(root.left)
lrd(root.right)
print(root.val)
def search(root, val):
if root is None:
return None
else:
if root.val == val:
return root
elif val < root.val:
return search(root.left, val)
else:
return search(root.right, val)
def findMin(root):
if root is None:
return None
while root.left:
root = root.left
return root
def findMax(root):
if root is None:
return None
while root.right:
root = root.right
return root
# delete a node
# case 1 - leaf node
# case 2 - node with single child - set move the parent of deleted down
# case 3 - Has both left and right child
def delete_node(root, val):
if root is None:
return None
elif val < root.val:
root.left = delete_node(root.left, val)
elif val > root.val:
root.right = delete_node(root.right, val)
else:
if root.left is None and root.right is None:
del root
root = None
return root
elif root.left is None:
temp = root
root = root.right
del temp
elif root.right is None:
temp = root
root = root.left
del temp
else:
min_ = findMin(root.right)
root.val = min_.val
root.right = delete_node(root.right, min_.val)
return root
def height(root):
if root is None:
return 0
else:
left_height = height(root.left)
right_height = height(root.right)
return max(left_height, right_height) + 1
# @param A : root node of tree
# @return an integer
def minDepth(A):
root = A
if root is None:
return 0
if root.left is None and root.right is None:
return 1
if root.right is None:
return minDepth(root.left) + 1
if root.left is None:
return minDepth(root.right) + 1
return min(minDepth(root.left), minDepth(root.right))+1
# Function to find LCA of n1 and n2. The function assumes
# that both n1 and n2 are present in BST
def lca(A, B, C):
def lca_(root, n1, n2):
if root is None:
return None
elif n1 < root.val and n2 < root.val:
return lca(root.left, n1, n2)
elif n1 > root.val and n2 > root.val:
return lca(root.right, n1, n2)
return root
chk1 = search(A, B)
chk2 = search(A, C)
if chk1 and chk2:
return lca_(A, B, C).val
else:
return None
# @param A : root node of tree
# @param B : integer
# @return the root node in the tree
def getSuccessor(A, B):
def findMin(A):
head = A
while head.left:
head = head.left
return head
x = B
root = A
if root:
while root.val != x:
if x < root.val:
root = root.left
else:
root = root.right
if root.right:
# find the leftmost node in right subtree
return findMin(root.right)
else:
# find the deepest ancestor for which x is in left subtree
ancestor = A
successor = None
while ancestor:
if root.val < ancestor.val:
successor = ancestor
ancestor = ancestor.left
else:
ancestor = ancestor.right
return successor
else:
return None
def isValidBST(A):
import sys
def validate(node, minm, maxm):
if not node:
return 1
return 1 if node.val > minm and node.val < maxm and validate(node.left, minm, node.val) and validate(node.right, node.val, maxm) else 0
return validate(A, -sys.maxsize - 1, sys.maxsize)
head = TreeNode(10)
insert_node(head, 5)
insert_node(head, 7)
insert_node(head, 2)
insert_node(head, 15)
insert_node(head, 25)
insert_node(head, 11)
# unbalanced tree
# head = insert_node(head, 7)
# head = insert_node(head, 4)
# head = insert_node(head, -1)
# head = insert_node(head, 5)
# head = insert_node(head, 3)
# head = insert_node(head, -1)
# head = insert_node(head, -1)
# head = insert_node(head, -1)
# ans = search_node(head, 11)
# head = delete_node(head, 50)
# bft(head)
# drl(head)
# ldr(head)
# lrd(head)
# print(search(head, 11))
# print(findMin(head).val)
# print(height(head))
# print(lca(head, 5, 11).val)
# drl(head)
# Finds the path from root node to given root of the tree.
# Stores the path in a list path[], returns true if path
# exists otherwise false
# Returns LCA if node n1 , n2 are present in the given
# binary tre otherwise return -1
def findLCA(head, n1, n2):
# Code here
p1 = []
p2 = []
def path(root, ls, n):
if root is None:
return False
ls.append(root.val)
if root.val == n:
return True
if (root.left != None and path(root.left, ls, n) != False) or (
root.right != None and path(root.right, ls, n) != False):
return True
ls.pop()
return False
path(head, p1, n1)
path(head, p2, n2)
if n1 in p1 and n2 in p2:
i = 0
while (i < (len(p1) - 1)) and (i < (len(p1) - 1)):
if p1[i] != p2[i]:
break
i += 1
return p1[i - 1]
else:
return -1
# print(findLCA(head, 5, 11))
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class BSTIterator:
# @param root, a binary search tree's root node
ls = []
def __init__(self, root):
st = []
self.ls = self.ldr(root, st)
def ldr(self, root, st):
if root is None:
return
else:
self.ldr(root.left, st)
st.insert(0, root.val)
self.ldr(root.right, st)
return st
# @return a boolean, whether we have a next smallest number
def hasNext(self):
return True if self.ls else False
# @return an integer, the next smallest number
def next(self):
return self.ls.pop()
# Your BSTIterator will be called like this:
# i = BSTIterator(root)
# while i.hasNext(): print i.next(),
test = BSTIterator(head)
print(test.ls)
print(test.next())
print(test.next())
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def insert_node(root, val):
if root is None or root.val is None:
root = getNode(val)
else:
if val <= root.val:
if root.left is None:
root.left = getNode(val)
else:
root.left = insert_node(root.left, val)
else:
if root.right is None:
root.right = getNode(val)
else:
root.right = insert_node(root.right, val)
return root
def traverse(root):
if root is None or root.val is None:
return
traverse(root.left)
print(root.val)
traverse(root.right)
tree = TreeNode(None)
tree = insert_node(tree, 15)
tree = insert_node(tree, 10)
tree = insert_node(tree, 50)
tree = insert_node(tree, 7)
tree = insert_node(tree, 13)
tree = insert_node(tree, 25)
tree = insert_node(tree, 17)
tree = insert_node(tree, 55)
tree = insert_node(tree, 56)
traverse(tree)