-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.py
213 lines (178 loc) · 6.17 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
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
# Python program for bunary tree traversals
# A class that represents an individual node in a Binary Tree
from logging import root
class Node:
def __init__(self, value):
self.value = value
self.leftChild = None
self.rightChild = None
def __str__(self):
return f"Node={self.value}"
class Tree:
def __init__(self):
self.root = None
def __str__(self):
return str(self.root.value)
def insert(self, value):
# If the tree is empty, set the root to the new node
if not self.root:
self.root = Node(value)
return
# Other scenario: find the parent of the new node
# Start from the root node and traverse the tree until we find a target node
current = self.root
while True:
if value < current.value:
if not current.leftChild:
# Found the parent
current.leftChild = Node(value)
break
current = current.leftChild
else:
if not current.rightChild:
# Found the parent
current.rightChild = Node(value)
break
current = current.rightChild
def find(self, value):
current = self.root
while current:
if value < current.value:
current = current.leftChild
elif value > current.value:
current = current.rightChild
else:
return True
return False
def traversePreOrder(self):
self.__traversePreOrder(self.root)
def __traversePreOrder(self, root):
# Base condition to avoid the cycle
if not root:
return
# Depth-First, Pre-order
# Root, left, Right
print(root.value)
self.__traversePreOrder(root.leftChild)
self.__traversePreOrder(root.rightChild)
def traverseInOrder(self):
self.__traverseInOrder(self.root)
def __traverseInOrder(self, root):
# Base condition to avoid the cycle
if not root:
return
# Depth-First, In-order
# Left, Root, Right
self.__traverseInOrder(root.leftChild)
print(root.value)
self.__traverseInOrder(root.rightChild)
def traversePostOrder(self):
self.__traversePostOrder(self.root)
def __traversePostOrder(self, root):
# Base condition to avoid the cycle
if not root:
return
# Depth-First, Post-order
# Left, Right, Root
self.__traversePostOrder(root.leftChild)
self.__traversePostOrder(root.rightChild)
print(root.value)
# Depth
# Calculate the depth by counting the number of edges
# from root to target node
def height(self):
return self.__height(self.root)
# Height
# Find the longest path from the leaf node to the root node
# The height of the tree = the height of the root node
# Calculate the height of the left and right subtrees and add 1
# 1 + max(height(L),height(R))
# Post order traversal - visit the leaves first and pass the value up until we get the root node
def __height(self, root):
if not root:
return -1
# Base Condition
if not root.leftChild and not root.rightChild:
# The leaf node has 0 height
return 0
return 1 + max(self.__height(root.leftChild), self.__height(root.rightChild))
def min(self):
# Return the minimum value in a tree
return self.__min(self.root)
def __min(self, root: Node):
# Base Condition
if not root.leftChild and not root.rightChild:
# The leaf node, return the value of that node
return root.value
left = self.__min(root.leftChild)
right = self.__min(root.rightChild)
return min(min(left, right), root.value)
def equals(self, other):
if not other:
return False
return self.__equals(self.root, other.root)
def __equals(self, first: Node, second: Node):
if not first and not second:
return True
if first and second:
# Pre-order
# Compare the root first, follow by the left, and right subtrees
return (
first.value == second.value
and self.__equals(first.leftChild, second.leftChild)
and self.__equals(first.rightChild, second.rightChild)
)
return False
def getNodesAtDistance(self, distance: int):
list = []
self.__getNodesAtDistance(self.root, distance, list)
return list
def __getNodesAtDistance(self, root, distance: int, list):
if not root:
return
if distance == 0:
list.append(root.value)
return
# If we reach this point,
# The distance is greater than zero
# As we go down, we decrement the distance by 1
self.__getNodesAtDistance(root.leftChild, distance - 1, list)
self.__getNodesAtDistance(root.rightChild, distance - 1, list)
# The level that we have is the height of the tree
# The height of the root node
# Once we know the height, we can use the for loops
# to print nodes at each level
def traverseLevelOrder(self):
self.__traverseLevelOrder(self.root)
def __traverseLevelOrder(self, root):
print(range(self.height() + 1))
for i in range(self.height() + 1):
print("i", i)
# Get the nodes at a given level
for value in self.getNodesAtDistance(i):
print("value", value)
tree = Tree()
tree.insert(7)
tree.insert(4)
tree.insert(9)
tree.insert(1)
tree.insert(6)
tree.insert(8)
tree.insert(10)
# print(tree.find(11))
# print(tree.traverseInOrder())
# print(tree.traversePreOrder())
# print(tree.traversePostOrder())
# print(tree.height())
# print(tree.min())
tree2 = Tree()
tree2.insert(7)
tree2.insert(4)
tree2.insert(9)
tree2.insert(1)
tree2.insert(6)
tree2.insert(8)
tree2.insert(10)
# print("Equality Checking", tree.equals(tree2))
# print("Nodes at distance", 2, tree.getNodesAtDistance(2))
tree.traverseLevelOrder()