-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbinary-search-tree.py
84 lines (68 loc) · 1.88 KB
/
binary-search-tree.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
# Python implementation of binary search tree operations
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.key = key
# Inorder Traversal
def inOrder(root):
if root is not None:
inOrder(root.left)
print(str(root.key) + "->", end=' ')
inOrder(root.right)
# Insert a node
def insertNode(node, key):
if node is None:
return Node(key)
if key < Node.key:
node.left = insertNode(node.left, key)
else:
node.right = insertNode(node.right, key)
return node
# INORDER SUCCESSOR
def inOS(node):
current = node
while current.left is not None:
current = current.left
return current
# Delete Node
def delNode(root, key):
if root is None:
return root
if key < root.key:
root.left = delNode(root.left, key)
elif(key > root.key):
root.right = delNode(root.right, key)
else:
# If the node is with only one child or no child
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
# If the node has two children,
# place the inorder successor in position of the node to be deleted
temp = inOS(root.right)
root.key = temp.key
# Delete the inorder successor
root.right = delNode(root.right, temp.key)
return root
# Driver Code
root = None
root = insertNode(root, 8)
root = insertNode(root, 3)
root = insertNode(root, 1)
root = insertNode(root, 6)
root = insertNode(root, 7)
root = insertNode(root, 10)
root = insertNode(root, 14)
root = insertNode(root, 4)
print("Inorder traversal: ", end=' ')
inOrder(root)
print("\nDelete 10")
root = delNode(root, 10)
print("Inorder traversal: ", end=' ')
inOrder(root)