-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path(1)_binary-search-tree.py
145 lines (132 loc) · 4.56 KB
/
(1)_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
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
# Binary Search Tree
# ==================
# Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers.
#
# It is called a binary tree because each tree node has a maximum of two children.
# It is called a search tree because it can be used to search for the presence of a number in O(log(n)) time.
#
# The properties that separate a binary search tree from a regular binary tree is
#
# All nodes of left subtree are less than the root node
# All nodes of right subtree are more than the root node
# Both subtrees of each node are also BSTs i.e. they have the above two properties
# Implement Node Class
class _Node:
__slots__ = '_element', '_left', '_right'
def __init__(self, element, left=None, right=None):
self._element = element
self._left = left
self._right = right
# Implement Binary Search Tree using Linked representation. It is similar in structure to
# a Linked list but not a Linked List.
class BinarySearchTree:
def __init__(self):
self._root = None
def inorder(self, root):
if root:
self.inorder(root._left)
print(root._element, end='-->')
self.inorder(root._right)
# Iterative Insert function
def insert_it(self, root, element):
temp = None
while root:
temp = root
if element == root._element:
return
elif element < root._element:
root = root._left
elif element > root._element:
root = root._right
n = _Node(element)
if self._root:
if element < temp._element:
temp._left = n
else:
temp._right = n
else:
self._root = n
# Recursive Insert function
def insert_recur(self, root, e):
if root:
if e < root._element:
root._left = self.insert_recur(root._left, e)
elif e > root._element:
root._right = self.insert_recur(root._right, e)
else:
n = _Node(e)
root = n
return root
# Iterative Search function
def search_it(self, key):
root = self._root
while root:
if key == root._element:
return True
elif key < root._element:
root = root._left
elif key > root._element:
root = root._right
return False
# Recursive Search function
def search_recur(self, root, key):
if root:
if key == root._element:
return True
elif key < root._element:
return self.search_recur(root._left, key)
elif key > root._element:
return self.search_recur(root._right, key)
def delete(self, e):
p = self._root
pp = None
while p and p._element != e:
pp = p
if e < p._element:
p = p._left
else:
p = p._right
if not p:
return False
if p._left and p._right:
s = p._left
ps = p
while s._right:
ps = s
s = s._right
p._element = s._element
p = s
pp = ps
c = None
if p._left:
c = p._left
else:
c = p._right
if p == self._root:
self._root = None
else:
if p == pp._left:
pp._left = c
else:
pp._right = c
BinarySearchTree = BinarySearchTree()
BinarySearchTree.insert_it(BinarySearchTree._root, 50)
BinarySearchTree.insert_it(BinarySearchTree._root, 30)
BinarySearchTree.insert_it(BinarySearchTree._root, 80)
BinarySearchTree.insert_it(BinarySearchTree._root, 10)
BinarySearchTree.insert_it(BinarySearchTree._root, 40)
BinarySearchTree.insert_it(BinarySearchTree._root, 60)
BinarySearchTree.inorder(BinarySearchTree._root)
print()
BinarySearchTree._root = BinarySearchTree.insert_recur(BinarySearchTree._root, 50)
BinarySearchTree.insert_recur(BinarySearchTree._root, 30)
BinarySearchTree.insert_recur(BinarySearchTree._root, 80)
BinarySearchTree.insert_recur(BinarySearchTree._root, 10)
BinarySearchTree.insert_recur(BinarySearchTree._root, 40)
BinarySearchTree.insert_recur(BinarySearchTree._root, 60)
BinarySearchTree.inorder(BinarySearchTree._root)
print()
print(BinarySearchTree.search_it(10))
print(BinarySearchTree.search_recur(BinarySearchTree._root, 10))
BinarySearchTree.delete(60)
BinarySearchTree.inorder(BinarySearchTree._root)