forked from clair3st/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinheap.py
More file actions
62 lines (50 loc) · 1.77 KB
/
binheap.py
File metadata and controls
62 lines (50 loc) · 1.77 KB
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
"""Python implementation of Binary Heap."""
class Binheap(object):
"""Python implementation of max binary heap.
supports the following method
push(): puts a new value into the heap, maintaining the heap property.
pop(): removes the top value in the heap, maintaining the heap property.
dislplay(): displays the heap as a string representation of a tree.
"""
def __init__(self, data=None):
"""Initialize bin heap."""
self.container = [None]
if data:
for val in data:
self.push(val)
def _balance(self):
"""Helper function to balance heap."""
size = len(self.container) - 1
while size // 2 > 0:
if self.container[size] > self.container[size // 2]:
tmp = self.container[size // 2]
self.container[size // 2] = self.container[size]
self.container[size] = tmp
size = size // 2
def push(self, val):
"""Put a new value into the heap."""
self.container.append(val)
self._balance()
def pop(self):
"""Remove the top value of the heap."""
if not self.container:
raise IndexError('Can\'t pop from and empty heap')
self.container.pop(1)
self._balance()
def display(self):
"""Display the heap as a tree."""
cols = []
col = 1
to_show = ''
l = self.container[1:]
while len(self.container) > col:
cols.append(col)
col *= 2
for i, v in enumerate(cols):
buff = cols[-1 - i] // 2
to_show += buff * ' '
for idx in range(v):
if l:
to_show += str(l.pop(0)) + ' '
to_show += '\n'
return to_show