-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.py
76 lines (62 loc) · 1.94 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
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class BinarySearchTree:
def insert(self, node, val):
# if tree empty
if node is None:
return Node(val)
# traverse and insert
if val < node.val:
node.left = self.insert(node.left, val)
else:
node.right = self.insert(node.right, val)
return node
def delete(self, node, val):
if node is None:
return None
else:
if val < node.val:
node.left = self.delete(node.left, val)
elif val > node.val:
node.right = self.delete(node.right, val)
else:
if node.left is None and node.right is None:
return None
elif node.left is None:
node = node.right
elif node.right is None:
node = node.left
else:
temp = self.minNode(node.right)
node.val = temp.val
node.right = self.delete(node.right, temp.val)
return node
def minNode(self, root):
if root.left is None:
return root
return self.minNode(root.left)
def inorder(self, root):
if root is None:
return
self.inorder(root.left)
print(str(root.val) + "->", end=' ')
self.inorder(root.right)
bst = BinarySearchTree()
root = None
# Insert nodes
values_to_insert = [50, 30, 20, 40, 70, 60, 80]
for val in values_to_insert:
root = bst.insert(root, val)
print("In-order traversal before deletion:")
bst.inorder(root)
print("\n")
# Delete nodes
values_to_delete = [20, 30, 50]
for val in values_to_delete:
root = bst.delete(root, val)
print("In-order traversal after deletion:")
bst.inorder(root)
print("\n")