-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryHeap.py
109 lines (88 loc) · 3.79 KB
/
binaryHeap.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
class BinaryHeap:
def __init__(self):
self.size = 0
self.heap = [0]
# self.heap[0] = -1 * math.inf
def put(self, k):
self.heap.append(k)
self.size += 1
self.percUp(self.size)
def remove(self, i):
for item in self.heap:
if i == item:
self.heap.remove(i)
self.size -= 1
self.percDown(1)
def peek(self):
return self.heap[1]
def pop(self):
root = self.heap[1]
self.heap[1] = self.heap[self.size]
self.size -= 1
self.heap.pop()
self.percDown(1)
return root
def percUp(self, i):
while i//2 > 0:
# print(i)
parent_index = i//2
# print(self.heap, self.heap[parent_index], self.heap[i])
if self.heap[i][0] < self.heap[parent_index][0] or (self.heap[i][0] == self.heap[parent_index][0] and self.heap[i][1] > self.heap[parent_index][1]):
self.heap[i], self.heap[parent_index] = self.heap[parent_index], self.heap[i]
i = parent_index
def percDown(self, i):
while (i * 2) <= self.size:
child = i*2
if child + 1 <= self.size and (self.heap[child + 1][0] < self.heap[child][0] or (self.heap[child + 1][0] == self.heap[child][0] and self.heap[child + 1][1] > self.heap[child][1])):
child = 2*i + 1
if self.heap[i][0] > self.heap[child][0] or (self.heap[i][0] == self.heap[child][0] and self.heap[i][1] < self.heap[child][1]):
self.heap[i], self.heap[child] = self.heap[child], self.heap[i]
i = child
class RevGBinaryHeap:
def __init__(self):
self.size = 0
self.heap = [0]
# self.heap[0] = -1 * math.inf
def put(self, k):
self.heap.append(k)
self.size += 1
self.percUp(self.size)
def remove(self, i):
for item in self.heap:
if i == item:
self.heap.remove(i)
self.size -= 1
self.percDown(1)
def peek(self):
return self.heap[1]
def pop(self):
root = self.heap[1]
self.heap[1] = self.heap[self.size]
self.size -= 1
self.heap.pop()
self.percDown(1)
return root
def percUp(self, i):
while i//2 > 0:
# print(i)
parent_index = i//2
# print(self.heap, self.heap[parent_index], self.heap[i])
if self.heap[i][0] < self.heap[parent_index][0] or (self.heap[i][0] == self.heap[parent_index][0] and self.heap[i][1] < self.heap[parent_index][1]):
self.heap[i], self.heap[parent_index] = self.heap[parent_index], self.heap[i]
i = parent_index
def percDown(self, i):
while (i * 2) <= self.size:
child = i*2
if child + 1 <= self.size and (self.heap[child + 1][0] < self.heap[child][0] or (self.heap[child + 1][0] == self.heap[child][0] and self.heap[child + 1][1] < self.heap[child][1])):
child = 2*i + 1
if self.heap[i][0] > self.heap[child][0] or (self.heap[i][0] == self.heap[child][0] and self.heap[i][1] > self.heap[child][1]):
self.heap[i], self.heap[child] = self.heap[child], self.heap[i]
i = child
def search(self, point):
for i in range(1, self.size):
if point[0] == self.heap[[i]][2].x and point[1] == self.heap[[i]][2].y:
popped = self.heap[i]
self.heap[i] = self.heap[self.size]
self.percDown(1)
return [True, popped]
return [False]