-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.py
More file actions
224 lines (175 loc) · 4.52 KB
/
Copy pathbinaryTree.py
File metadata and controls
224 lines (175 loc) · 4.52 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
223
224
from queue import Queue
class Node:
def __init__(self, val) -> None:
self.val = val
self.left = None
self.right = None
"""
A
/ \
B C
/ \ \
D E F
"""
class BinaryTree:
def breadthFirst(self, root):
if root == None:
return []
res = []
queue = Queue()
queue.put(root)
while queue.qsize() > 0:
current = queue.get()
res.append(current.val)
if current.left != None:
queue.put(current.left)
if current.right != None:
queue.put(current.right)
return res
# Depth first iteratively
def depthFirstIt(self, root):
if root == None:
return []
res = []
stack = [root]
while len(stack) > 0:
current = stack.pop()
res.append(current.val)
if current.right != None:
stack.append(current.right)
if current.left != None:
stack.append(current.left)
return res
# Depth first recursively
def depthFirstRe(self, root):
if root == None:
return []
leftValues = self.depthFirstRe(root.left)
rightValues = self.depthFirstRe(root.right)
# return (leftValues + [root.val] + rightValues) # In order
# return (leftValues + rightValues + [root.val]) # Post order
return ([root.val] + leftValues + rightValues) # Pre order
# Similar methods
def printInOrder(self, root):
if root:
self.printInOrder(root.left)
print(root.val, end=" "),
self.printInOrder(root.right)
def printPostOrder(self, root):
if root:
self.printPostOrder(root.left)
self.printPostOrder(root.right)
print(root.val, end=" ")
def printPreOrder(self, root):
if root:
print(root.val, end=" ")
self.printPreOrder(root.left)
self.printPreOrder(root.right)
# Check if element exists using breadth first method
def includesBF(self, root, ele):
if root == None:
return False
queue = Queue()
queue.put(root)
while queue.qsize() > 0:
current = queue.get()
if current.val == ele:
return True
if current.left:
queue.put(current.left)
if current.right:
queue.put(current.right)
return False
# Check if element exists using depth first method
def includesDP(self, root, ele):
if root == None:
return False
if root.val == ele:
return True
return self.includesDP(root.left, ele) or self.includesDP(
root.right, ele)
def sum(self, root):
if root == None:
return 0
return root.val + self.sum(root.left) + self.sum(root.right)
def sumIt(self, root):
if root == None:
return 0
queue = Queue()
queue.put(root)
s = 0
while queue.qsize() > 0:
current = queue.get()
s += current.val
if current.left != None:
queue.put(current.left)
if current.right != None:
queue.put(current.right)
return s
def minValRe(self, root):
if root == None:
return float('inf')
return min(root.val, self.minValRe(root.left),
self.minValRe(root.right))
def minValIt(self, root):
if root == None:
return None
q = Queue()
q.put(root)
minVal = root.val
while q.qsize() > 0:
current = q.get()
if current.val < minVal:
minVal = current.val
if current.left != None:
q.put(current.left)
if current.right != None:
q.put(current.right)
return minVal
def maxRootToLeafRe(self, root):
if root == None:
return float('-inf')
if root.left == None and root.right == None:
return root.val
maxChildPath = max(self.maxRootToLeafRe(root.left),
self.maxRootToLeafRe(root.right))
return root.val + maxChildPath
if __name__ == "__main__":
a = Node(5)
b = Node(12)
c = Node(34)
d = Node(45)
e = Node(58)
f = Node(0)
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
biTree = BinaryTree()
print("\n----------DepthFirst----------\n")
print(biTree.depthFirstRe(a))
print(biTree.depthFirstIt(a))
print("\n----------BreadthFirst----------\n")
print(biTree.breadthFirst(a))
print("\n\n----------In-order----------\n\n")
biTree.printInOrder(a)
print("\n\n----------Post-order----------\n\n")
biTree.printPostOrder(a)
print("\n\n----------Pre-order----------\n\n")
biTree.printPreOrder(a)
searchEle = int(input("\n\nEnter a element to search : "))
print(
f"{searchEle} does{'' if biTree.includesBF(a, searchEle) else ' not'} exist"
)
print(
f"{searchEle} does{'' if biTree.includesDP(a, searchEle) else ' not'} exist"
)
print("\n\n----------Sum----------\n\n")
print(biTree.sum(a))
print(biTree.sumIt(a))
print("\n\n----------Minimum Value----------\n\n")
print(biTree.minValRe(a))
print(biTree.minValIt(a))
print("\n\n----------Max root to leaf Value----------\n\n")
print(biTree.maxRootToLeafRe(a))