-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.py
67 lines (53 loc) · 2.01 KB
/
Heap.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
class Heap:
def __init__(self):
self.items = []
self.currentIndex = 0
def push(self,item):
item.heapIndex = self.currentIndex
self.items.append(item)
self.SortUp(item)
self.currentIndex+=1
def pop(self):
retItem = self.items.pop(0)
self.currentIndex -=1
if self.currentIndex > 0:
self.items.insert(0,self.items.pop())
self.items[0].heapIndex = 0
self.SortDown(self.items[0])
return retItem
def updateItem(self,item):
self.SortUp(item)
def contains(self,item):
return self.items[item.heapIndex] == item
def count(self):
return self.currentIndex
def clear(self):
self.currentIndex = 0
self.items = []
def SortDown(self,item):
while True:
childIndexLeft = item.heapIndex * 2 + 1
childIndexRight = item.heapIndex * 2 + 2
swapIndex = 0
if childIndexLeft < self.currentIndex:
swapIndex = childIndexLeft
if childIndexRight < self.currentIndex:
if self.items[childIndexRight] < self.items[childIndexLeft]:
swapIndex = childIndexRight
if self.items[swapIndex] < item:
self.Swap(item,self.items[swapIndex])
else:
break
else:
break
def SortUp(self,item):
parentIndex = (item.heapIndex-1) // 2
while parentIndex >= 0:
if item < self.items[parentIndex]:
self.Swap(self.items[parentIndex],item)
else:
break
parentIndex = (item.heapIndex-1) // 2
def Swap(self,itemA,itemB):
self.items[itemA.heapIndex],self.items[itemB.heapIndex] = self.items[itemB.heapIndex] , self.items[itemA.heapIndex]
itemA.heapIndex , itemB.heapIndex = itemB.heapIndex, itemA.heapIndex