-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearchTree.py
More file actions
138 lines (95 loc) · 3.32 KB
/
BinarySearchTree.py
File metadata and controls
138 lines (95 loc) · 3.32 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
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
# Function to perform inorder traversal on the BST
def inorder(root):
if root is None:
return
inorder(root.left)
print(root.data, end=' ')
inorder(root.right)
# Function to find the maximum value node in the subtree rooted at `ptr`
def maximumKey(ptr):
while ptr.right:
ptr = ptr.right
return ptr
# Recursive function to insert a key into a BST
def insert(root, key):
# if the root is None, create a new node and return it
if root is None:
print("{} has been added as a new node of the tree".format(key))
return Node(key)
# if the given key is less than the root node, recur for the left subtree
if key < root.data:
print("{} has been inserted to the left subtree.".format(key))
root.left = insert(root.left, key)
# if the given key is more than the root node, recur for the right subtree
else:
print("{} has been inserted to the right subtree.".format(key))
root.right = insert(root.right, key)
return root
# Function to delete a node from a BST
def deleteNode(root, key):
# base case: the key is not found in the tree
if root is None:
print("[!]Element not found on Tree.")
return root
if key < root.data:
root.left = deleteNode(root.left, key)
elif key > root.data:
root.right = deleteNode(root.right, key)
# key found
else:
if root.left is None and root.right is None:
print("[#]Node has no children.")
return None
elif root.left and root.right:
print("[#]Node has children on both sides.")
predecessor = maximumKey(root.left)
root.data = predecessor.data
root.left = deleteNode(root.left, predecessor.data)
print("[-] Deleting Node.")
else:
print("[#]Node has children.")
child = root.left if root.left else root.right
root = child
print("[-] {} element deleted.".format(key))
return root
def printOption():
print("-="*18)
print("Binary Search Tree")
print("1.) Add elements to the tree")
print("2.) Delete elements to the tree")
print("3.) Exit")
print("-="*18)
if __name__ == '__main__':
option = 0
root = None
while(option != 3):
print("")
printOption()
option = int(input("Enter your Choice: "))
if (option == 1):
print("")
print("[Please Enter Numbers Separated by Comma]")
nums = input("Enter numbers:\n").split(',')
keys = [int(num) for num in nums]
for key in keys:
root = insert(root, key)
print("*"*35)
inorder(root)
print("")
print("*"*35)
elif (option == 2):
print("[Please Enter the Number to be Deleted]")
numDel = int(input("Enter Number: "))
root = deleteNode(root, numDel)
print("*"*35)
inorder(root)
print("")
print("*"*35)
else:
print("[!]Please choose from options above.")
print("K. Bye")