-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
278 lines (231 loc) · 8.75 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import random
import math
from MKnode import Node
class BST_Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# Insert Node
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = BST_Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = BST_Node(data)
else:
self.right.insert(data)
else:
self.data = data
# Print the Tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data),
if self.right:
self.right.PrintTree()
def PreorderTraversal(self, root):
res = []
if root:
res.append(root.data)
res = res + self.PreorderTraversal(root.left)
res = res + self.PreorderTraversal(root.right)
return res
def display(self):
lines, *_ = self._display_aux()
for line in lines:
print(line)
def _display_aux(self):
"""Returns list of strings, width, height, and horizontal coordinate of the root."""
# No child.
if self.right is None and self.left is None:
line = '%s' % self.data
width = len(line)
height = 1
middle = width // 2
return [line], width, height, middle
# Only left child.
if self.right is None:
lines, n, p, x = self.left._display_aux()
s = '%s' % self.data
u = len(s)
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s
second_line = x * ' ' + '/' + (n - x - 1 + u) * ' '
shifted_lines = [line + u * ' ' for line in lines]
return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2
# Only right child.
if self.left is None:
lines, n, p, x = self.right._display_aux()
s = '%s' % self.data
u = len(s)
first_line = s + x * '_' + (n - x) * ' '
second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' '
shifted_lines = [u * ' ' + line for line in lines]
return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2
# Two children.
left, n, p, x = self.left._display_aux()
right, m, q, y = self.right._display_aux()
s = '%s' % self.data
u = len(s)
first_line = (x + 1) * ' ' + (n - x - 1) * \
'_' + s + y * '_' + (m - y) * ' '
second_line = x * ' ' + '/' + \
(n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' '
if p < q:
left += [n * ' '] * (q - p)
elif q < p:
right += [m * ' '] * (p - q)
zipped_lines = zip(left, right)
lines = [first_line, second_line] + \
[a + u * ' ' + b for a, b in zipped_lines]
return lines, n + m + u, max(p, q) + 2, n + u // 2
class Min_Heap:
def __init__(self, capacity):
self.storage = [None]*capacity
self.capacity = capacity
self.size = 0
def getParentIndex(self, index):
return (index-1) // 2
def getLeftchildIndex(self, index):
return (2*index)+1
def getRightchildIndex(self, index):
return (2*index) + 2
def hasParent(self, index):
return self.getParentIndex(index) >= 0
def hasLeftchild(self, index):
return self.getLeftchildIndex(index) < self.size
def hasRightchild(self, index):
return self.getRightchildIndex(index) < self.size
def parent(self, index):
return self.storage[self.getParentIndex(index)]
def leftChild(self, index):
return self.storage[self.getLeftchildIndex(index)]
def RightChild(self, index):
return self.storage[self.getRightchildIndex(index)]
def isFull(self):
return self.size == self.capacity
def swap(self, index1, index2):
self.storage[index1], self.storage[index2] = self.storage[index2], self.storage[index1]
def insert(self, data):
if (self.isFull()):
raise ("HEAP IS FULL")
self.storage[self.size] = data
self.size += 1
self.heapifyUp()
def heapifyUp(self):
index = self.size - 1
while (self.hasParent(index) and self.parent(index) > self.storage[index]):
self.swap(self.getParentIndex(index), index)
index = self.getParentIndex(index)
def removeMin(self):
if (self.size == 0):
raise ("EMPTY HEAP")
data = self.storage[0]
self.storage[0]=self.storage[self.size-1]
self.storage.pop(self.size-1)
self.size -= 1
self.heapifyDown()
return data
def heapifyDown(self):
index = 0
while (self.hasLeftchild(index)):
smallerChildIndex = self.getLeftchildIndex(index)
if (self.hasRightchild(index) and self.RightChild(index) < self.leftChild(index)):
smallerChildIndex = self.getRightchildIndex(index)
if (self.storage[index] < self.storage[smallerChildIndex]):
break
else:
self.swap(index, smallerChildIndex)
index = smallerChildIndex
def prints(self):
return self.storage
# array = [5548, 87, 7, 787, 1, 465, 97, 787, 454, 15, 445, 487, 64, 4, 11, 2, 3]
# minh = Min_Heap(len(array))
# for i in array:
# minh.insert(i)
# minheaplist = minh.prints()
# doubly_linked_list = Node(minheaplist[0])
# doubly_linked_list.convert_array_to_doubly_linked_list(minheaplist)
# doubly_linked_list.display()
# minh.removeMin()
# minheaplist = minh.prints()
# doubly_linked_list = Node(minheaplist[0])
# doubly_linked_list.convert_array_to_doubly_linked_list(minheaplist)
# doubly_linked_list.display()
class Max_heap:
def __init__(self, capacity):
self.storage = [None]*capacity
self.capacity = capacity
self.size = 0
def getParentIndex(self, index):
return (index-1) // 2
def getLeftchildIndex(self, index):
return (2*index)+1
def getRightchildIndex(self, index):
return (2*index) + 2
def hasParent(self, index):
return self.getParentIndex(index) >= 0
def hasLeftchild(self, index):
return self.getLeftchildIndex(index) < self.size
def hasRightchild(self, index):
return self.getRightchildIndex(index) < self.size
def parent(self, index):
return self.storage[self.getParentIndex(index)]
def leftChild(self, index):
return self.storage[self.getLeftchildIndex(index)]
def RightChild(self, index):
return self.storage[self.getRightchildIndex(index)]
def isFull(self):
return self.size == self.capacity
def swap(self, index1, index2):
self.storage[index1], self.storage[index2] = self.storage[index2], self.storage[index1]
def insert(self, data):
if (self.isFull()):
raise ("HEAP IS FULL")
self.storage[self.size] = data
self.size += 1
self.heapifyUp()
def heapifyUp(self):
index = self.size - 1
while (self.hasParent(index) and self.parent(index) < self.storage[index]):
self.swap(self.getParentIndex(index), index)
index = self.getParentIndex(index)
def removeMax(self):
if (self.size == 0):
raise ("EMPTY HEAP")
data = self.storage[0]
self.storage[0]= self.storage[self.size-1]
self.storage.pop()
self.size -= 1
self.heapifyDown()
return data
def heapifyDown(self):
index = 0
while (self.hasLeftchild(index)):
smallerChildIndex = self.getLeftchildIndex(index)
if (self.hasRightchild(index) and self.RightChild(index) > self.leftChild(index)):
smallerChildIndex = self.getRightchildIndex(index)
if (self.storage[index] > self.storage[smallerChildIndex]):
break
else:
self.swap(index, smallerChildIndex)
index = smallerChildIndex
def prints(self):
return self.storage
# maxh = Max_heap(5)
# # for i in range(50):
# # d = random.randint(0,100)
# # maxh.insert(d)
# maxh.insert(1)
# maxh.insert(5)
# maxh.insert(6)
# maxh.insert(54)
# maxh.insert(33)
# maxh.removeMax()
# x = maxh.prints();y = Node(x[0])
# y.convert_array_to_doubly_linked_list(x)
# y.display()