-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.py
167 lines (138 loc) · 4.21 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
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
from queue import Queue
from stack import Stack
class Node:
def __init__(self, value):
self.value = value
self.right = None
self.left = None
class BinarySearchTree:
def __init__(self, initial=None):
self.root = Node(initial) if initial else None
def insert(self, value):
if not self.root:
self.root = Node(value)
else:
self._insert(self.root, value)
def _insert(self, node, value):
if value > node.value:
if not node.right:
node.right = Node(value)
else:
self._insert(node.right, value)
elif value < node.value:
if not node.left:
node.left = Node(value)
else:
self._insert(node.left, value)
else:
raise Exception(
'Value %s is already presented in the tree' % value)
def is_empty(self):
return not self.root
def size(self, use='recursion'):
if use == 'recursion':
return self.size_recursion()
elif use == 'stack':
return self.size_stack()
raise Exception('Can not calculate size using %s' % use)
def size_recursion(self, node=-1):
if node == -1:
node = self.root
if not node:
return 0
return 1 + self.size_recursion(node.left) + self.size_recursion(node.right)
def size_stack(self):
size = 0
stack = Stack()
if self.root:
stack.push(self.root)
while not stack.is_empty():
current = stack.pop()
if current.left:
stack.push(current.left)
if current.right:
stack.push(current.right)
size += 1
return size
def height(self, node=-1):
if node == -1:
node = self.root
if not node:
return -1
return 1 + max(self.height(node.left), self.height(node.right))
def inorder(self):
arr = []
self._inorder(self.root, arr)
return arr
def _inorder(self, node, arr):
if node:
self._inorder(node.left, arr)
arr.append(node.value)
self._inorder(node.right, arr)
def preorder(self):
arr = []
self._preorder(self.root, arr)
return arr
def _preorder(self, node, arr):
if node:
arr.append(node.value)
self._preorder(node.left, arr)
self._preorder(node.right, arr)
def postorder(self):
arr = []
self._postorder(self.root, arr)
return arr
def _postorder(self, node, arr):
if node:
self._postorder(node.left, arr)
self._postorder(node.right, arr)
arr.append(node.value)
def levelorder(self):
arr = []
queue = Queue()
if self.root:
queue.enqueue(self.root)
while not queue.is_empty():
current = queue.dequeue()
arr.append(current.value)
if current.left:
queue.enqueue(current.left)
if current.right:
queue.enqueue(current.right)
return arr
def validate_bst(self, node=-1, mini=None, maxi=None):
# validate tree for bst property
if node == -1:
node = self.root
if not node:
return True
if mini is not None and node.value < mini:
return False
if maxi is not None and node.value > maxi:
return False
return (
self.validate_bst(node=node.left, mini=mini, maxi=node.value-1) and
self.validate_bst(node=node.right, mini=node.value+1, maxi=maxi)
)
tree = BinarySearchTree(initial=4)
tree.root.left = Node(2)
tree.root.right = Node(5)
tree.root.left.left = Node(1)
tree.root.left.right = Node(3)
tree.insert(6)
tree.insert(7)
tree.insert(-1)
tree.insert(-3)
tree.insert(12)
tree.insert(14)
if (tree.validate_bst()):
print("Valid binary search tree")
else:
print("Invalid binary search tree")
print(tree.inorder())
print(tree.preorder())
print(tree.postorder())
print(tree.levelorder())
print(tree.size())
print(tree.size(use='stack'))
print(tree.height())