-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap copy.py
90 lines (69 loc) · 1.6 KB
/
heap copy.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
import math
import operator
class Heap(object):
def __init__(self, heap=None, min_heap=False):
self.heap = [0] + (heap if heap is not None else [])
self.size = 0
self.min_heap = min_heap
def height(self):
return int(math.ceil(math.log(self.size + 1,2)) - 1)
def max_heapify(self, heap, i, min_heap=False):
l = len(heap)
s = l / 2
op = operator.gt if min_heap else operator.lt
print op
while i < s:
left = i * 2
right = i * 2 + 1
swap = i
if op(heap[left], heap[i]):
swap = left
if right < l and op(heap[right], heap[swap]):
swap = right
if swap != i:
heap[i], heap[swap], i = heap[swap], heap[i], swap
else:
return
def build_max_heap(self, items):
for i in range(len(items) / 2, 0, -1):
max_heapify(items)
def extract_root(self):
root, self.heap[1] = self.heap[1], self.heap[self.size]
del self.heap[self.size]
self.size-=1
self.max_heapify(self.heap, 1)
return root
def bubble_up(self, heap, i):
'''bubble key i up to correct position'''
parent = i / 2
while parent > 0:
if heap[i] > heap[parent]:
break
heap[i], heap[parent] = heap[parent], heap[i]
i, parent = parent, parent / 2
def insert(self, item):
self.heap.append(item)
self.size += 1
self.bubble_up(self.heap, self.size)
h = Heap()
h.insert(10)
h.insert(12)
h.insert(14)
h.insert(8)
h.insert(6)
h.insert(4)
h.insert(3)
print h.heap, h.height(), Heap([10,12,14,8,6,4,3]).heap
'''
[0, 3, 8, 4, 12, 10, 14, 6]
3
8 4
12 10 14 6
'''
print h.extract_root(), h.heap, h.height()
'''
3 [0, 4, 8, 6, 12, 10, 14]
4
8 6
12 10 14
'''