1
1
'''
2
2
A binary search Tree
3
3
'''
4
-
5
4
class Node :
6
5
7
- def __init__ (self , label ):
6
+ def __init__ (self , label , parent ):
8
7
self .label = label
9
8
self .left = None
10
9
self .right = None
10
+ #Added in order to delete a node easier
11
+ self .parent = parent
11
12
12
13
def getLabel (self ):
13
14
return self .label
@@ -27,6 +28,11 @@ def getRight(self):
27
28
def setRight (self , right ):
28
29
self .right = right
29
30
31
+ def getParent (self ):
32
+ return self .parent
33
+
34
+ def setParent (self , parent ):
35
+ self .parent = parent
30
36
31
37
class BinarySearchTree :
32
38
@@ -35,13 +41,12 @@ def __init__(self):
35
41
36
42
def insert (self , label ):
37
43
# Create a new Node
38
- new_node = Node (label )
44
+ new_node = Node (label , None )
39
45
# If Tree is empty
40
46
if self .empty ():
41
47
self .root = new_node
42
48
else :
43
49
#If Tree is not empty
44
- parent_node = None
45
50
curr_node = self .root
46
51
#While we don't get to a leaf
47
52
while curr_node is not None :
@@ -58,8 +63,14 @@ def insert(self, label):
58
63
if new_node .getLabel () < parent_node .getLabel ():
59
64
parent_node .setLeft (new_node )
60
65
else :
61
- parent_node .setRight (new_node )
62
-
66
+ parent_node .setRight (new_node )
67
+ #Set parent to the new node
68
+ new_node .setParent (parent_node )
69
+ '''
70
+ def delete(self):
71
+ if (not self.empty()):
72
+ if()
73
+ '''
63
74
def getNode (self , label ):
64
75
curr_node = None
65
76
#If the tree is not empty
@@ -78,6 +89,24 @@ def getNode(self, label):
78
89
curr_node = curr_node .getRight ()
79
90
return curr_node
80
91
92
+ def getMax (self ):
93
+ #We go deep on the right branch
94
+ curr_node = None
95
+ if (not self .empty ()):
96
+ curr_node = self .getRoot ()
97
+ while (curr_node .getRight () is not None ):
98
+ curr_node = curr_node .getRight ()
99
+ return curr_node
100
+
101
+ def getMin (self ):
102
+ #We go deep on the left branch
103
+ curr_node = None
104
+ if (not self .empty ()):
105
+ curr_node = self .getRoot ()
106
+ while (curr_node .getLeft () is not None ):
107
+ curr_node = curr_node .getLeft ()
108
+ return curr_node
109
+
81
110
def empty (self ):
82
111
if self .root is None :
83
112
return True
@@ -92,19 +121,19 @@ def preShow(self, curr_node):
92
121
def getRoot (self ):
93
122
return self .root
94
123
95
- '''
96
- Example
97
- 8
98
- / \
99
- 3 10
100
- / \ \
101
- 1 6 14
102
- / \ /
103
- 4 7 13
104
- '''
105
124
125
+ def testBinarySearchTree ():
126
+ '''
127
+ Example
128
+ 8
129
+ / \
130
+ 3 10
131
+ / \ \
132
+ 1 6 14
133
+ / \ /
134
+ 4 7 13
135
+ '''
106
136
107
- if __name__ == "__main__" :
108
137
t = BinarySearchTree ()
109
138
t .insert (8 )
110
139
t .insert (3 )
@@ -128,3 +157,9 @@ def getRoot(self):
128
157
else :
129
158
print ("The label -1 doesn't exist" )
130
159
160
+ if (not t .empty ()):
161
+ print ("Max Value: " , t .getMax ().getLabel ())
162
+ print ("Min Value: " , t .getMin ().getLabel ())
163
+
164
+ if __name__ == "__main__" :
165
+ testBinarySearchTree ()
0 commit comments