Skip to content

Commit ad27fa1

Browse files
authored
Merge pull request #689 from neha030/dev17
BST added
2 parents 9538dde + cbc2d24 commit ad27fa1

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

Data Structures/Trees/BST.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
'''
2+
IMPLEMENTATION OF BINARY SEARCH TREE (BST)
3+
Binary Search Tree is a special type of binary tree where:
4+
1. The value of all the nodes in the left sub-tree is less than or equal to the value of the root.
5+
2. The value of all the nodes in the right sub-tree is greater than value of the root.
6+
3. This rule will be recursively applied to all the left and right sub-trees of the root.
7+
This program contains the menu-based implementation of the basic operations on a BST:
8+
1. Insert Node into BST
9+
2. Search Node in BST
10+
3. Delete Node in BST
11+
4. Print BST (inorder way)
12+
'''
13+
14+
15+
class BSTNode:
16+
# A Node of the Binary Search Tree
17+
def __init__(self, val=None, left=None, right=None):
18+
self.val = val
19+
self.left = left
20+
self.right = right
21+
22+
23+
def insertNode(root, val):
24+
# Function to insert a node to a BST
25+
26+
# if the given BST is empty
27+
if root is None:
28+
return BSTNode(val)
29+
30+
# if the value to insert already exists in the tree
31+
if root.val == val:
32+
return root
33+
34+
# if the value to insert should be inserted to the left subtree
35+
if val < root.val:
36+
root.left = insertNode(root.left, val)
37+
38+
# if the value to insert should be inserted to the right subtree
39+
else:
40+
root.right = insertNode(root.right, val)
41+
42+
return root
43+
44+
45+
def searchNode(root, val):
46+
# Function to search a node in a BST
47+
48+
# if the BST is empty or if the node doesn't exist
49+
if root is None:
50+
return False
51+
52+
# if the value exists in the BST
53+
if root.val == val:
54+
return True
55+
56+
# if the value might exist in the right subtree
57+
if val > root.val:
58+
return searchNode(root.right, val)
59+
60+
# if the value might exist in the left subtree
61+
return searchNode(root.left, val)
62+
63+
64+
def inorderSuccessor(node):
65+
# Function to find the inorder successor of a given node
66+
67+
curr = node
68+
# Loop through the BST until a left most node is reached
69+
while curr.left is not None:
70+
curr = curr.left
71+
return curr
72+
73+
74+
def deleteNode(root, val):
75+
# Function to delete a node in a BST
76+
77+
# if the BST is empty
78+
if root is None:
79+
return root
80+
81+
# if the node to delete might exist in the left subtree
82+
if root.val > val:
83+
root.left = deleteNode(root.left, val)
84+
85+
# if the node to delete might exist in the right subtree
86+
elif root.val < val:
87+
root.right = deleteNode(root.right, val)
88+
89+
# if the node is found
90+
else:
91+
92+
# if the node has one child or no children
93+
if root.right is None:
94+
tempNode = root.left
95+
root = None
96+
return tempNode
97+
98+
if root.left is None:
99+
tempNode = root.right
100+
root = None
101+
return tempNode
102+
103+
# if the node has two children
104+
tempNode = inorderSuccessor(root.right)
105+
root.val = tempNode.val
106+
root.right = deleteNode(root.right, tempNode.val)
107+
108+
return root
109+
110+
111+
def inorder(root):
112+
# Function to print the nodes in the BST inorder
113+
114+
# if the BST is not empty
115+
if root:
116+
# visit the left subtree (or leaf)
117+
inorder(root.left)
118+
# visit the parent
119+
print(root.val, end=' ')
120+
# visit the right subtree (or leaf)
121+
inorder(root.right)
122+
123+
124+
# A menu based implementation
125+
if __name__ == '__main__':
126+
bst = None
127+
op = 1
128+
while op != 5:
129+
print()
130+
print("1: Insert")
131+
print("2: Search")
132+
print("3: Delete")
133+
print("4: Print (Inorder)")
134+
print("5: Exit")
135+
print("Enter option: ", end="")
136+
137+
op = int(input())
138+
139+
if op == 1:
140+
print("Enter value to insert: ", end="")
141+
x = int(input())
142+
bst = insertNode(bst, x)
143+
print()
144+
145+
elif op == 2:
146+
print("Enter element to search for: ", end="")
147+
x = int(input())
148+
if searchNode(bst, x):
149+
print("The element is present!")
150+
else:
151+
print("The element is not present")
152+
print()
153+
154+
elif op == 3:
155+
print("Enter the value to delete: ", end="")
156+
x = int(input())
157+
bst = deleteNode(bst, x)
158+
print()
159+
160+
elif op == 4:
161+
if bst:
162+
inorder(bst)
163+
else:
164+
print("Empty BST!")
165+
print()
166+
167+
168+
'''
169+
Sample Input/Output:
170+
1: Insert
171+
2: Search
172+
3: Delete
173+
4: Print (Inorder)
174+
5: Exit
175+
Enter option: 1
176+
Enter value to insert: 5
177+
1: Insert
178+
2: Search
179+
3: Delete
180+
4: Print (Inorder)
181+
5: Exit
182+
Enter option: 2
183+
Enter element to search for: 5
184+
The element is present!
185+
1: Insert
186+
2: Search
187+
3: Delete
188+
4: Print (Inorder)
189+
5: Exit
190+
Enter option: 3
191+
Enter the value to delete: 5
192+
1: Insert
193+
2: Search
194+
3: Delete
195+
4: Print (Inorder)
196+
5: Exit
197+
Enter option: 4
198+
Empty BST!
199+
200+
Time Complexity:
201+
Insert: O(n)
202+
Search: O(n)
203+
Delete: O(n)
204+
Print (Inorder): O(n)
205+
Space Complexity:
206+
Insert: O(n)
207+
Search: O(n)
208+
Delete: O(n)
209+
Print (Inorder): O(n)
210+
'''

0 commit comments

Comments
 (0)