@@ -22,11 +22,14 @@ def insert(self, data):
22
22
23
23
def heapify_up (self ):
24
24
'''
25
- Start at the end of the tree (first enqueued item).
25
+ Start at the end of the tree (last enqueued item).
26
26
27
27
Compare the rear item to its parent, swap if
28
28
the parent is larger than the child (min-heap property).
29
29
Repeat until the min-heap property is met.
30
+
31
+ Best Case: O(1), item is inserted at correct position, no swaps needed
32
+ Worst Case: O(logn), item needs to be swapped throughout all levels of tree
30
33
'''
31
34
child = self .rear
32
35
parent = self .parent_idx (child )
@@ -37,7 +40,7 @@ def heapify_up(self):
37
40
parent = self .parent_idx (child )
38
41
39
42
def pop (self ):
40
- ''' Removes the lowest value element (highest priority) from the heap '''
43
+ ''' Removes the lowest value element (highest priority, at root ) from the heap '''
41
44
min = self .dequeue ()
42
45
if self .rear >= 1 : # heap may need to be fixed
43
46
self .heapify_down ()
@@ -64,6 +67,9 @@ def heapify_down(self):
64
67
65
68
While a favorite child exists, and that child is smaller
66
69
than the parent, swap them (sift down).
70
+
71
+ Best Case: O(1), item is inserted at correct position, no swaps needed
72
+ Worst Case: O(logn), item needs to be swapped throughout all levels of tree
67
73
'''
68
74
cur = ROOT = 0 # start at the root
69
75
fav = self .favorite (cur ) # determine favorite child
@@ -76,6 +82,9 @@ def heapify_down(self):
76
82
else :
77
83
return
78
84
85
+ def time_complexities (self ):
86
+ return '''[Insert & Pop] Best Case: O(1), Worst Case: O(logn)'''
87
+
79
88
def get_code (self ):
80
89
''' returns the code for the current class '''
81
90
import inspect
0 commit comments