-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBT.py
More file actions
226 lines (200 loc) · 6.74 KB
/
BT.py
File metadata and controls
226 lines (200 loc) · 6.74 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
225
226
# binary tree code
# TO DO:
# 1. Add a method for deleting (removing) a node no matter where it's located
# 2. Add the traversal methods (inorder, postorder, and preorder) using recursion
# 3. Print the tree level by level so it kind of looks like a tree as the __str__ method
# (feel free to get online/AI help for this one)
# 4. In a separate file, create your own testing program. That program should
# try every method in this class in multiple scenarios (like the LLTester does).
#Final Program
#Binary Tree
#Sneha Patel
#Dec 11, 2023
from binNode import binNode
class binaryTree:
def __init__(self, r = None, c = None):
self._root = r
self._curr = c
@property
def root(self):
return self._root
@root.setter
def root(self, n):
self._root = n
@property
def curr(self):
return self._curr
@curr.setter
def curr(self, n):
self._curr = n
def isEmpty(self):
if self.root == None:
return True
else:
return False
def isLeaf(self):
if self.curr.left == None and self.curr.right == None:
return True
else:
return False
def getData(self):
if self.curr == None:
return None
else:
return self.curr.data
def goRoot(self):
self.curr = self.root
def goLeft(self):
self.curr = self.curr.left
def goRight(self):
self.curr = self.curr.right
def getMin(self):
self.goRoot()
while self.curr.left != None:
self.goLeft()
return self.getData()
def getMax(self):
self.goRoot()
while self.curr.right != None:
self.goRight()
return self.getData()
# recursive function to insert
# only used inside binaryTree class!!!
def insertNode(self, here, n):
if here == None:
here = n
else:
if n.data < here.data:
here.left = self.insertNode(here.left, n)
else:
here.right = self.insertNode(here.right, n)
return here
# inserts a node (n)
# This is the method you'll call outside of this class
def insert(self, n):
if self.isEmpty() == True:
self.root = n
self.curr = n
else:
self.goRoot()
self.curr = self.insertNode(self.curr, n)
# does the actual counting
# again, used only in-class
def count(self, n):
if n == None:
return 0
else:
l = 1
l += self.count(n.left)
l += self.count(n.right)
return l
# use this outside of the class
def getSize(self):
return self.count(self.root)
'''
def findIt(self, n, what, attribute):
#print(attribute)
if n is None:
return None # not found
if getattr(n.data, attribute) == what:
return n
elif what < getattr(n.data, attribute):
#print(n.data)
return self.findIt(n.left, what, attribute)
elif what > getattr(n.data, attribute):
#(n.data)
return self.findIt(n.right, what, attribute)
def search(self, n, what, attribute):
self.curr = self.findIt(n, what, attribute)
return self.curr
#above I updated my search and findit method
'''
# next 2 methods find a node in the tree or returns None if not there
def findIt(self, n, what):
if n == None:
return None # not found
if n.data == what:
return n
elif what < n.data:
return self.findIt(n.left, what)
elif what > n.data:
return self.findIt(n.right, what)
def search(self,what):
n = self.findIt(self.root, what)
return n
#helper method to remove node
def helper_remove(self, node,node_to_remove):
if node is None:
return None
if node_to_remove < node.data:
node.left = self.helper_remove(node.left, node_to_remove)
elif node_to_remove > node.data:
node.right = self.helper_remove(node.right, node_to_remove)
else: # Node to be removed is found
if node.left is None: # Case 1: Node has one or no child
return node.right
elif node.right is None: # Case 2: Node has one child
return node.left
else: # Case 3: Node has two children
swap = self.getMin()
node.data = swap #replaces the data of the current node
node.right = self.helper_remove(node.right, swap)
return node
# remove a node from the tree
# "what" is the data - use it to find the node to kill
def remove(self, what):
value = what
node_to_remove = value
#node_to_remove = self.search(what)
self.root = self.helper_remove(self.root, node_to_remove)
### traversal methods ###
#Inorder = "LNR"
def inOrder(self, n):
if n is not None:
self.inOrder(n.left)
print(n.data)
self.inOrder(n.right)
def traverseInOrder(self):
self.goRoot()
self.inOrder(self.curr)
#Preorder = "NLR"
def PreOrder(self, n):
if n is not None:
print(n.data)
if n.left:
self.PreOrder(n.left)
if n.right:
self.PreOrder(n.right)
def traversePreOrder(self):
self.goRoot()
self.PreOrder(self.curr)
#PostOrder = "LRN"
def PostOrder(self, n):
if n is not None:
if n.left:
self.PostOrder(n.left)
if n.right:
self.PostOrder(n.right)
print(n.data)
def traversePostOrder(self):
self.goRoot()
self.PostOrder(self.curr)
# returns a string that when printed
# shows the tree in tree form
# for example:
# M
# F T
# B H R W
#took help from stockexchange site
#helper method for str
def str(self,node,level=0):
result = ""
if node is not None:
result += self.str(node.left, level + 1)
result += ' ' * 4 * level + '-> ' + str(node.data) + "\n"
result += self.str(node.right, level + 1)
return result
def __str__(self):
if self.root is None:
return "Empty tree"
return self.str(self.root)