-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.py
More file actions
59 lines (47 loc) · 1.36 KB
/
bst.py
File metadata and controls
59 lines (47 loc) · 1.36 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
class Node:
def __init__(self,val):
self.val = val
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def insert(self,node,rt):
if rt == None:
rt = node
else:
if node.val < rt.val:
rt.left = self.insert(node,rt.left)
elif node.val > rt.val:
rt.right = self.insert(node,rt.right)
return rt
def remove(self,val):
print(self.root.val)
def search(self,val,root):
if root == None:
print("Item not found")
else:
if val < root.val:
self.search(val,root.left)
elif val > root.val:
self.search(val,root.right)
else:
print("Item found")
myBst = BST()
node1 = Node(10)
node2 = Node(20)
node3 = Node(30)
node4 = Node(15)
node5 = Node(5)
node6 = Node(1)
myBst.root = myBst.insert(node1,myBst.root)
myBst.root = myBst.insert(node2,myBst.root)
myBst.root = myBst.insert(node3,myBst.root)
myBst.root = myBst.insert(node4,myBst.root)
myBst.root = myBst.insert(node5,myBst.root)
myBst.root = myBst.insert(node6,myBst.root)
myBst.search(10,myBst.root)
myBst.search(15,myBst.root)
myBst.search(99,myBst.root)
myBst.search(1,myBst.root)
myBst.remove(34)