-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB5.py
More file actions
182 lines (155 loc) · 5.44 KB
/
LAB5.py
File metadata and controls
182 lines (155 loc) · 5.44 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
# Lab #5
# Due Date: 10/22/2021, 11:59PM
# REMINDERS:
# The work in this assignment must be your own original work and must be completed alone.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def __str__(self):
return ("Node({})".format(self.value))
__repr__ = __str__
class BinarySearchTree:
'''
>>> x=BinarySearchTree()
>>> x.isEmpty()
True
>>> x.insert(9)
>>> x.insert(4)
>>> x.insert(11)
>>> x.insert(2)
>>> x.insert(5)
>>> x.insert(10)
>>> x.insert(9.5)
>>> x.insert(7)
>>> x.getMin
Node(2)
>>> x.getMax
Node(11)
>>> 67 in x
False
>>> 9.5 in x
True
>>> x.isEmpty()
False
>>> x.getHeight(x.root) # Height of the tree
3
>>> x.getHeight(x.root.left.right)
1
>>> x.getHeight(x.root.right)
2
>>> x.getHeight(x.root.right.left)
1
>>> x.printInorder
2 : 4 : 5 : 7 : 9 : 9.5 : 10 : 11 :
>>> new_tree = x.mirror()
11 : 10 : 9.5 : 9 : 7 : 5 : 4 : 2 :
>>> new_tree.root.right
Node(4)
>>> x.printInorder
2 : 4 : 5 : 7 : 9 : 9.5 : 10 : 11 :
'''
def __init__(self):
self.root = None
def insert(self, value):
if self.root is None:
self.root=Node(value)
else:
self._insert(self.root, value)
def _insert(self, node, value):
if(value<node.value):
if(node.left==None):
node.left = Node(value)
else:
self._insert(node.left, value)
else:
if(node.right==None):
node.right = Node(value)
else:
self._insert(node.right, value)
@property
def printInorder(self):
if self.isEmpty():
return None
else:
self._inorderHelper(self.root)
def _inorderHelper(self, node):
if node is not None:
self._inorderHelper(node.left)
print(node.value, end=' : ')
self._inorderHelper(node.right)
def mirror(self):
# Creates a new BST that is a mirror of self: Elements greater than the root are on the left side, and smaller values on the right side
# Do NOT modify any given code
if self.root is None:
return None
else:
newTree = BinarySearchTree()
newTree.root = self._mirrorHelper(self.root)
newTree.printInorder
return newTree
#A tree is empty if there is no root, as all nodes are indirectly connected to the root
def isEmpty(self):
return not (self.root)
#Creates a mirrored copy of the tree
def _mirrorHelper(self, node):
#If the node is empty return nothing
if not node:
return
#Copy the current value
newNode = Node(node.value)
#Copy the original node's right tree to the new Node's left
newNode.left = self._mirrorHelper(node.right)
#Copy the original node's left tree to the new Node's right
newNode.right = self._mirrorHelper(node.left)
return newNode
#Returns the minimum value within the tree
@property
def getMin(self):
#Traveler node
temp = self.root
#While there is a left node
while (temp.left):
temp = temp.left
#Since tree is sorted, this is the min.
return temp
#Returns the maximum value within the tree
@property
def getMax(self):
#Traveler node
temp = self.root
#While there is a left node
while (temp.right):
temp = temp.right
#Since tree is sorted, this is the min.
return temp
#Determines whether a node with a certain value is within a tree
def __contains__(self,value):
return self.__containshelper(self.root, value)
#The recursive helper function for __contains__
def __containshelper(self,node,value):
#If there is no node, it does not contain the value
if not node:
return False
#If the node with the desired value has been found, return true
if (node.value == value):
return True
#Return true if the node has been found in the left or right subtrees
return self.__containshelper(node.left, value) or self.__containshelper(node.right, value)
#Returns the height of the tree
def getHeight(self, node):
#If the node is null, return a height of -1
#This is done such that when the parent adds +1 to each connection to children, an edge to a null child has a depth of 0
if not node:
return -1
#If the node is a leaf, it has a height of zero
if not node.left and not node.right:
return 0
#Get the height of both subtrees
leftHeight = self.getHeight(node.left)
rightHeight = self.getHeight(node.right)
#Add one to the greater subtree to account for the edge connecting the parent to the child
if leftHeight >= rightHeight:
return leftHeight+1
return rightHeight+1