22A binary search Tree
33'''
44
5-
65class Node :
76
87 def __init__ (self , label ):
@@ -35,32 +34,49 @@ def __init__(self):
3534 self .root = None
3635
3736 def insert (self , label ):
38-
3937 # Create a new Node
40-
41- node = Node (label )
42-
38+ new_node = Node (label )
39+ # If Tree is empty
4340 if self .empty ():
44- self .root = node
41+ self .root = new_node
4542 else :
46- dad_node = None
43+ #If Tree is not empty
44+ parent_node = None
4745 curr_node = self .root
48-
49- while True :
50- if curr_node is not None :
51-
52- dad_node = curr_node
53-
54- if node .getLabel () < curr_node .getLabel ():
55- curr_node = curr_node .getLeft ()
56- else :
57- curr_node = curr_node .getRight ()
46+ #While we don't get to a leaf
47+ while curr_node is not None :
48+ #We keep reference of the parent node
49+ parent_node = curr_node
50+ #If node label is less than current node
51+ if new_node .getLabel () < curr_node .getLabel ():
52+ #We go left
53+ curr_node = curr_node .getLeft ()
54+ else :
55+ #Else we go right
56+ curr_node = curr_node .getRight ()
57+ #We insert the new node in a leaf
58+ if new_node .getLabel () < parent_node .getLabel ():
59+ parent_node .setLeft (new_node )
60+ else :
61+ parent_node .setRight (new_node )
62+
63+ def getNode (self , label ):
64+ curr_node = None
65+ #If the tree is not empty
66+ if (not self .empty ()):
67+ #Get tree root
68+ curr_node = self .getRoot ()
69+ #While we don't find the node we look for
70+ #I am using lazy evaluation here to avoid NoneType Attribute error
71+ while curr_node is not None and curr_node .getLabel () is not label :
72+ #If node label is less than current node
73+ if label < curr_node .getLabel ():
74+ #We go left
75+ curr_node = curr_node .getLeft ()
5876 else :
59- if node .getLabel () < dad_node .getLabel ():
60- dad_node .setLeft (node )
61- else :
62- dad_node .setRight (node )
63- break
77+ #Else we go right
78+ curr_node = curr_node .getRight ()
79+ return curr_node
6480
6581 def empty (self ):
6682 if self .root is None :
@@ -69,15 +85,13 @@ def empty(self):
6985
7086 def preShow (self , curr_node ):
7187 if curr_node is not None :
72- print (curr_node .getLabel (), end = " " )
73-
88+ print (curr_node .getLabel ())
7489 self .preShow (curr_node .getLeft ())
7590 self .preShow (curr_node .getRight ())
7691
7792 def getRoot (self ):
7893 return self .root
7994
80-
8195'''
8296Example
8397 8
@@ -89,15 +103,28 @@ def getRoot(self):
89103 4 7 13
90104'''
91105
92- t = BinarySearchTree ()
93- t .insert (8 )
94- t .insert (3 )
95- t .insert (1 )
96- t .insert (6 )
97- t .insert (4 )
98- t .insert (7 )
99- t .insert (10 )
100- t .insert (14 )
101- t .insert (13 )
102-
103- t .preShow (t .getRoot ())
106+
107+ if __name__ == "__main__" :
108+ t = BinarySearchTree ()
109+ t .insert (8 )
110+ t .insert (3 )
111+ t .insert (1 )
112+ t .insert (6 )
113+ t .insert (4 )
114+ t .insert (7 )
115+ t .insert (10 )
116+ t .insert (14 )
117+ t .insert (13 )
118+
119+ t .preShow (t .getRoot ())
120+
121+ if (t .getNode (6 ) is not None ):
122+ print ("The label 6 exists" )
123+ else :
124+ print ("The label 6 doesn't exist" )
125+
126+ if (t .getNode (- 1 ) is not None ):
127+ print ("The label -1 exists" )
128+ else :
129+ print ("The label -1 doesn't exist" )
130+
0 commit comments