-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.py
More file actions
81 lines (63 loc) · 2.23 KB
/
Heap.py
File metadata and controls
81 lines (63 loc) · 2.23 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Course: CS2302 Data Structures
Author: Javier Navarro
Assignment: Lab 4 Option B
Instructor: Diego Aguirre
TA: Manoj Saha
Last modified: 11/30/18
Purpose: Hold contents for a heap class
"""
class Heap:
def __init__(self):
self.heap_array = []
#inserts k at appropriate index
def insert(self, k):
self.heap_array.append(k) #adds k to the list
index = len(self.heap_array) - 1
while index > 0:
parentIndex = (index - 1) // 2
#if child is greater than parent
if (self.heap_array[index] >= self.heap_array[parentIndex]):
return
else:
self.swap(parentIndex, index)
index = parentIndex
#swaps positions of node1 and node2
def swap(self, node1, node2):
temp = self.heap_array[node1]
self.heap_array[node1] = self.heap_array[node2]
self.heap_array[node2] = temp
#checks the min heap property
def heapify(self, size, index):
parent = index
left = index * 2 + 1
right = index * 2 + 2
#checks if child is larger than the parent
if left < size and self.heap_array[index] > self.heap_array[left]:
parent = left
#checks if child is larger than the parent
if right < size and self.heap_array[parent] > self.heap_array[right]:
parent = right
#swaps if parent isn't root
if parent != index:
self.swap(index, parent)
self.heapify(size, parent)
#sorts the heap in descending order
def heap_sort(self):
if self.is_empty():
return
for i in range(len(self.heap_array) - 1, 0, -1):
self.swap(0, i)
self.heapify(i, 0)
#removes and returns min
def extract_min(self):
if self.is_empty():
return None
min_elem = self.heap_array.pop(0)
return min_elem
#checks if the heap is empty
def is_empty(self):
return len(self.heap_array) == 0
#prints all elements in the heap
def printHeap(self):
print(self.heap_array)