-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.py
307 lines (257 loc) · 8.56 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
class TreeNode:
def __init__(self, value, left = None, right = None):
self.value = value
self.left = left
self.right = right
def bst(self):
result = []
queue = []
queue.append(self)
while len(queue) > 0:
node = queue.pop(0)
if node.left != None:
queue.append(node.left)
if node.right != None:
queue.append(node.right)
result.append(node.value)
return result
def preOrder(self):
result = []
result.append(self.value)
if self.left != None:
for x in self.left.preOrder():
result.append(x)
if self.right != None:
for x in self.right.preOrder():
result.append(x)
return result
def inOrder(self):
result = []
if self.left != None:
for x in self.left.inOrder():
result.append(x)
result.append(self.value)
if self.right != None:
for x in self.right.inOrder():
result.append(x)
return result
def postOrder(self):
result = []
if self.left != None:
for x in self.left.postOrder():
result.append(x)
if self.right != None:
for x in self.right.postOrder():
result.append(x)
result.append(self.value)
return result
def printFullNodes(self):
if self.left and self.right:
print(self.value)
if self.left:
self.left.printFullNodes()
if self.right:
self.right.printFullNodes()
def iterativePreOrder(self):
traversed = []
stack = []
stack.append(self)
while len(stack) > 0:
popped = stack.pop(-1)
traversed.append(popped.value)
if popped.right:
stack.append(popped.right)
if popped.left:
stack.append(popped.left)
return traversed
def printTree(self):
# return an array demonstrating the BST
result = []
parents = []
parents.append([self])
while len(parents) != 0:
parentsList = parents.pop(0)
thisLevel = []
nextLevelParents = []
for i in range(len(parentsList)):
parentNode = parentsList[i]
thisLevel.append(parentNode.value)
if parentNode.left != None:
nextLevelParents.append(parentNode.left)
if parentNode.right != None:
nextLevelParents.append(parentNode.right)
result.append(thisLevel)
if len(nextLevelParents) > 0:
parents.append(nextLevelParents)
return result
def isBstHelper(self, minValue, maxValue):
retValue = False
if self.value > minValue and self.value <= maxValue :
retValue = True
if self.left:
retValue = retValue and self.left.isBstHelper(minValue, self.Value)
if self.right:
retValue = retValue and self.right.isBstHelper(self.value, maxValue)
return retValue
def sumOfLeavePaths(self, context, accSum):
if self.left == None and self.right == None:
#this is a leaf, return value
return context+accSum
else:
if self.left:
accSum = self.left.sumOfLeavePaths(context*10+self.left.value, accSum)
if self.right:
accSum = self.right.sumOfLeavePaths(context*10+self.right.value, accSum)
return accSum
def getSumOfLeaves(self):
return self.sumOfLeavePaths(self.value, 0)
def serializeHelper(self):
vSerialized = []
vSerialized.append(self.value)
if self.left != None:
vSerialized += self.left.serializeHelper()
else:
vSerialized.append("#")
if self.right != None:
vSerialized += self.right.serializeHelper()
else:
vSerialized.append("#")
return vSerialized
def serialize(self):
result = ''
serializedList = self.serializeHelper()
for i, c in enumerate(serializedList):
result += str(c)
if i != len(serializedList) - 1:
result += ','
return result
@staticmethod
def deserialize(serialized):
splittedValues = serialized.split(",")
dVal = int(splittedValues.pop(0))
dRoot = TreeNode(dVal)
dRoot.left = TreeNode.deserializeHelper(splittedValues, dRoot)
dRoot.right = TreeNode.deserializeHelper(splittedValues, dRoot)
return dRoot
@staticmethod
def deserializeHelper(listOfValues, node):
dVal = listOfValues.pop(0)
if dVal == '#':
return None
else:
dNode = TreeNode(dVal)
dNode.left = TreeNode.deserializeHelper(listOfValues, dNode)
dNode.right = TreeNode.deserializeHelper(listOfValues, dNode)
return dNode
def printTargetSumPathFromRoot(self, target, contextPath=[], goodPaths=[]):
if self.value > target:
return False
if self.value == target:
contextPath.append(self.value)
goodPaths.append(str(contextPath))
contextPath.pop(-1)
return True
if self.value < target and self.left == None and self.right == None:
return False
leftCheck = False
rightCheck = False
if self.left:
contextPath.append(self.value)
leftCheck = self.left.printTargetSumPathFromRoot(target-self.value, contextPath, goodPaths)
contextPath.pop(-1)
if self.right:
contextPath.append(self.value)
rightCheck = self.right.printTargetSumPathFromRoot(target-self.value, contextPath, goodPaths)
contextPath.pop(-1)
return leftCheck or rightCheck
def pathTargetSum(self, target):
paths = []
self.printTargetSumPathFromRoot(target, goodPaths=paths)
print(paths)
def findTargetSum(self, target):
if target < 0:
return False
if self.value == target:
return True
if self.left == None and self.right == None and self.value != target:
return False
foundTarget = False
if self.left:
foundTarget = self.left.findTargetSum(target-self.value) or self.left.findTargetSum(target)
if self.right and foundTarget == False:
foundTarget = self.right.findTargetSum(target-self.value) or self.right.findTargetSum(target)
return foundTarget
def toListOfList(self):
result = []
treeLevelStack = []
treeLevelStack.append([self])
while len(treeLevelStack) > 0:
parents = treeLevelStack.pop(0)
parentsValues = []
nextTreeLevel = []
for i in range(len(parents)):
parentsValues.append(parents[i].value)
if parents[i].left != None:
nextTreeLevel.append(parents[i].left)
if parents[i].right != None:
nextTreeLevel.append(parents[i].right)
if len(parentsValues) > 0:
result.append(parentsValues)
if len(nextTreeLevel) > 0:
treeLevelStack.append(nextTreeLevel)
return result
'''
root = TreeNode(17)
l1 = TreeNode(10)
r1 = TreeNode(25)
l11 = TreeNode(6)
l12 = TreeNode(13)
r11 = TreeNode(20)
r12 = TreeNode(34)
l111 = TreeNode(2)
l121 = TreeNode(15)
r111 = TreeNode(18)
r112 = TreeNode(22)
root.left = l1
root.right = r1
l1.left = l11
l1.right = l12
r1.left = r11
r1.right = r12
l11.left = l111
l12.left = l121
r11.left = r111
r11.right = r112
print(root.bst())
print(TreeNode.deserialize(root.serialize()).serialize())
'''
root = TreeNode(7)
left = TreeNode(1)
right = TreeNode(2)
leftleft = TreeNode(3)
leftright = TreeNode(1)
leftrightleft = TreeNode(5)
rightleft = TreeNode(9)
rightright = TreeNode(7)
rightleftright = TreeNode(6)
root.left = left
root.right = right
left.left = leftleft
left.right = leftright
leftright.left = leftrightleft
right.left = rightleft
right.right = rightright
rightleft.right = rightleftright
#713, 7115, 7296, 727
print(713+7115+7296+727)
print(root.getSumOfLeaves())
print(root.findTargetSum(14))
print(root.findTargetSum(6))
print(root.findTargetSum(523))
root.pathTargetSum(16)
print(root.findTargetSum(16))
print(root.findTargetSum(17))
print(root.toListOfList())
root.printFullNodes()
print(root.preOrder())
print(root.iterativePreOrder())