-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.py
More file actions
196 lines (141 loc) · 5.02 KB
/
bst.py
File metadata and controls
196 lines (141 loc) · 5.02 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pydot
from binarytree import build
# SALAM
# PROJECT DARMORDE
# BST NODE CLASS
# BA PYTHON
# :)
#BST IN PYTHON
class Node:
# Constructor to create a new node
def __init__(self, key):
self.key = key
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, data):
if self.root is None:
self.root = Node(data)
else:
if self.root.key < data:
if self.root.right is None:
self.root.right = Node(data)
else:
self.insertNode(self.root.right, data)
else:
if self.root.left is None:
self.root.left = Node(data)
else:
self.insertNode(self.root.left, data)
def insertNode(self, currentNode, val):
if(val <= currentNode.key):
if(currentNode.left):
self.insertNode(currentNode.left, val)
else:
currentNode.left = Node(val)
elif(val > currentNode.key):
if(currentNode.right):
self.insertNode(currentNode.right, val)
else:
currentNode.right = Node(val)
def search(self, root, key):
# Base Cases: root is null or key is present at root
if root is None or root.key == key:
return root
# Key is greater than root's key
if root.key < key:
return self.search(root.right, key)
# Key is smaller than root's key
return self.search(root.left, key)
# This code is contributed by Bhavya Jain
def inorder(self, root):
if root is not None:
self.inorder(root.left)
print(root.key)
self.inorder(root.right)
def findsuccessor(self, current_node):
while current_node.left != None:
current_node = current_node.left
return current_node
def deleteNode(self, root, key):
# Base Case
if root is None:
return root
# If the key to be deleted is smaller than the root's
# key then it lies in left subtree
if key < root.key:
root.left = self.deleteNode(root.left, key)
# If the kye to be delete is greater than the root's key
# then it lies in right subtree
elif(key > root.key):
root.right = self.deleteNode(root.right, key)
# If key is same as root's key, then this is the node
# to be deleted
else:
# Node with only one child or no child
if root.left is None :
return root.right
elif root.right is None :
return root.left
# Node with two children: Get the inorder successor
# (smallest in the right subtree)
temp = self.findsuccessor(root.right)
# Copy the inorder successor's content to this node
root.key = temp.key
# Delete the inorder successor
root.right = self.deleteNode(root.right , temp.key)
return root
def delete(self, key):
return self.deleteNode(self.root, key)
def treeMinimum(self, node):
while node.left is not None:
node = node.left
return node
def treeMaximum(self, node):
while node.right is not None:
node = node.right
return node
if __name__ == '__main__':
bst = BinarySearchTree()
y = []
zzz = []
print("insert 50 30 20 40 70 60 80 ya hrchi mad nazareton hst:")
while 1:
x = input(('INPUT FOR INSERT(For stop just click on enter)=> '))
if x == '':
break
if x != '':
bst.insert(int(x))
y.append(int(x))
print("inorder Permutation:")
bst.inorder(bst.root)
search = int(input(('INPUT FOR SEARCH, just put 80 or everything you want base on inputs in tree=> ')))
print(f'search for {search} Node:')
if bst.search(bst.root, search) is None:
print(search, ' is not exist in tree')
else:
print(search, ' is in tree')
bst.delete(m:=int(input(('INPUT FOR DELETE, put it 80 or everything you want base on input in tree=> '))))
print("your number node deleted from Tree sucessfully")
print("Print Tree after Delete number node to user:")
bst.inorder(bst.root)
y.remove(m)
zzz.extend(y)
values = y
tree = build(values)
print(tree)
print(tree.values)
graph = pydot.Dot(graph_type='graph')
for i in tree.values:
edge = pydot.Edge(i, i+1)
graph.add_edge(edge)
values = zzz
tree = build(values)
print(tree)
print(tree.values)
graph = pydot.Dot(graph_type='graph')
for i in tree.values:
edge = pydot.Edge(i, i+1)
graph.add_edge(edge)