-
Notifications
You must be signed in to change notification settings - Fork 497
/
BinarySearchTree.py
134 lines (108 loc) · 2.9 KB
/
BinarySearchTree.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
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
class Node(object):
# Node for Binary search Tree and set the Node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# insert data in tree
def insert(node, data):
if node is None:
# create a new node
node = Node(data)
return node
else:
# go to the left child
if data <= node.data:
node.left = insert(node.left, data)
# got to the right child
else:
node.right = insert(node.right, data)
return node
# seraching data
def search(node, data):
# if data not present
if node is None:
return None
# go to the left if data is lesser from root
if data < node.data:
node = search(node.left, data)
# go to the right if data is greater from root
elif data > node.data:
node = search(node.right, data)
# data found
elif data == node.data:
return node
return node
# find the minimum
def minright(node):
if node.left is None:
return node
else:
node = minright(node.left)
return node
# delete a node
def delete(root, data):
if root is None:
return root
if data < root.data:
root.left = delete(root.left, data)
elif data > root.data:
root.right = delete(root.right, data)
# data node is found
else:
if root.left is None:
return root.right
elif root.right is None:
return root.left
# if left or right child is Not None find the the minimum node in right child
temp = minright(root.right)
root.data = temp.data
# recursive delete the minimum node in right child
root.right = delete(root.right, temp.data)
return root
# print inorder
def inorder(root):
if root is not None:
inorder(root.left)
print(root.data)
inorder(root.right)
# print preorder
def preorder(root):
if root is not None:
print(root.data)
preorder(root.left)
preorder(root.right)
# print postorder
def postorder(root):
if root is not None:
preorder(root.left)
preorder(root.right)
print(root.data)
# check the method
def main():
root = None
root = insert(root, 5)
root = insert(root, 11)
root = insert(root, 3)
root = insert(root, 1)
root = insert(root, 50)
root = insert(root, 45)
root = insert(root, 30)
root = insert(root, 35)
print("****** InOrder ******")
inorder(root)
print("****** PreOrder ******")
preorder(root)
print("****** PostOrder ******")
postorder(root)
print("***** search Node ******")
temp = search(root, 28)
if temp is not None:
print("node search==> ", temp.data)
else:
print("node is not present")
print("***** delete Node ******")
root = delete(root, 11)
inorder(root)
if __name__ == '__main__':
main()