Skip to content

Commit 0ad029c

Browse files
committed
Added getNode function and made changes to insert function
1 parent 535cbb7 commit 0ad029c

File tree

2 files changed

+194
-37
lines changed

2 files changed

+194
-37
lines changed

Diff for: data_structures/Binary Tree/binary_seach_tree.py

+64-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
A binary search Tree
33
'''
44

5-
65
class 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
'''
8296
Example
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+

Diff for: data_structures/Binary Tree/binary_search_tree.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
'''
2+
A binary search Tree
3+
'''
4+
5+
class Node:
6+
7+
def __init__(self, label):
8+
self.label = label
9+
self.left = None
10+
self.right = None
11+
12+
def getLabel(self):
13+
return self.label
14+
15+
def setLabel(self, label):
16+
self.label = label
17+
18+
def getLeft(self):
19+
return self.left
20+
21+
def setLeft(self, left):
22+
self.left = left
23+
24+
def getRight(self):
25+
return self.right
26+
27+
def setRight(self, right):
28+
self.right = right
29+
30+
31+
class BinarySearchTree:
32+
33+
def __init__(self):
34+
self.root = None
35+
36+
def insert(self, label):
37+
# Create a new Node
38+
new_node = Node(label)
39+
# If Tree is empty
40+
if self.empty():
41+
self.root = new_node
42+
else:
43+
#If Tree is not empty
44+
parent_node = None
45+
curr_node = self.root
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()
76+
else:
77+
#Else we go right
78+
curr_node = curr_node.getRight()
79+
return curr_node
80+
81+
def empty(self):
82+
if self.root is None:
83+
return True
84+
return False
85+
86+
def preShow(self, curr_node):
87+
if curr_node is not None:
88+
print(curr_node.getLabel())
89+
self.preShow(curr_node.getLeft())
90+
self.preShow(curr_node.getRight())
91+
92+
def getRoot(self):
93+
return self.root
94+
95+
'''
96+
Example
97+
8
98+
/ \
99+
3 10
100+
/ \ \
101+
1 6 14
102+
/ \ /
103+
4 7 13
104+
'''
105+
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

Comments
 (0)