-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_tree.py
173 lines (131 loc) · 4.23 KB
/
binary_tree.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
from collections import deque
import random
class Node:
data = None
left = None
right = None
def __init__(self,data):
self.data = data
def __repr__(self):
return f"Node data: {self.data}"
class BinaryTree:
def __init__(self):
self.root = None
def add(self,data):
# Uses BFS
new_node = Node(data)
if self.root is None:
self.root = new_node
return
queue = deque()
queue.append(self.root)
while queue:
current = queue.popleft()
if current.left is None:
current.left = new_node
return
else:
queue.append(current.left)
if current.right is None:
current.right = new_node
return
else:
queue.append(current.right)
def depth_first_search(self):
#DFS
if self.root == None:
return
stack = [self.root]
while stack:
current = stack.pop()
if current.right != None:
stack.append(current.right)
if current.left != None:
stack.append(current.left)
print(current)
def breath_first_search(self):
#BFS
# It's like a search in levels
# it finishes searching a level of the tree before going below
if self.root == None:
return
queue = deque()
queue.append(self.root)
while queue:
current = queue.popleft()
if current.left != None:
queue.append(current.left)
if current.right != None:
queue.append(current.right)
print(current)
def search(self,key):
# Aka tree includes
# search for a value with BFS
# It can also be done with DFS
if self.root == None:
return
queue = deque()
queue.append(self.root)
while queue:
current = queue.popleft()
if current.data == key:
return 'Value found'
if current.left != None:
queue.append(current.left)
if current.right != None:
queue.append(current.right)
return 'Value not found'
def search_recursive(self,root,key):
if root == None:
return False
if root.data == key:
return True
return self.search_recursive(root.left, key) or self.search_recursive(root.right, key)
def max_path_sum(self,root):
if root == None:
return 0
if root.left == None and root.right == None:
return root.data
max_value = max(self.max_path_sum(root.left) , self.max_path_sum(root.right))
return root.data + max_value
def min_value(self):
if self.root == None:
return
lowest = self.root.data
stack = [self.root]
while stack:
current = stack.pop()
if current.data < lowest:
lowest = current.data
if current.right != None:
stack.append(current.right)
if current.left != None:
stack.append(current.left)
return lowest
def tree_sum(self):
total = 0
if self.root == None:
return
queue = deque()
queue.append(self.root)
while queue:
current = queue.popleft()
total += current.data
if current.left != None:
queue.append(current.left)
if current.right != None:
queue.append(current.right)
return total
def main():
tree = BinaryTree()
for i in range(0,15):
tree.add(random.randint(0,40))
tree.depth_first_search()
print('----------------------')
tree.breath_first_search()
print(f'search :{tree.search(3)}')
print(f'recursive search : {tree.search_recursive(tree.root,3)}')
print(f'max path sum :{tree.max_path_sum(tree.root)}')
print(f'min value : {tree.min_value()}')
print(f'tree sum : {tree.tree_sum()}')
main()