-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST_basic_operations.py
66 lines (57 loc) · 1.18 KB
/
BST_basic_operations.py
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
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
self.parent=None
def BST_insert(k,x,y):
while x!=None:
y=x
if k.data<x.data:
x=x.left
else:
x=x.right
k.parent=y
if k.data<y.data:
y.left=k
else:
y.right=k
def BST_search(k,x):
if x==None:
return -1
elif x.data==k:
return (x.parent,x.left,x.right)
else:
if k<x.data:
return BST_search(k,x.left)
else:
return BST_search(k,x.right)
roote=int(input("Enter the root node\n"))
root=Node(roote)
while True:
a=int(input("Enter the node to be inserted.Enter -1 to quit\n"))
if a==-1:
break
obj=Node(a)
BST_insert(obj,root,None)
while True:
a=int(input("Enter the node to be searched.Enter -1 to quit\n"))
if a==-1:
break
ret=BST_search(a,root)
if ret==-1:
print("Node not found\n")
else:
print("Node found! {}\n".format(a))
if ret[0]==None:
print("Parent Node: Nill")
else:
print("Parent Node: {}".format(ret[0].data))
if ret[1]==None:
print("Left Child Node: Nill")
else:
print("Left Child Node: {}".format(ret[1].data))
if ret[2]==None:
print("Right Child Node: Nill")
else:
print("Right Child Node: {}".format(ret[2].data))