Skip to content

Commit a79982f

Browse files
author
IanDoarn
committed
Updates to data_structures:
- queue.py - Updated docstrings at top of file - Updated docstrings for functions - Optimized import statements - Removed redundant import statments in methods - linked_list.py - Updated docstrings at top of file - Updated docstrings for functions - Optimized import statements - Removed redundant import statments in methods - Updated conditionals and syntaxing - Removed redundant parenthesis around conditionals - Removed camel casing on variables - heap.py - Updated docstrings at top of file - Updated docstrings for functions - Optimized import statements - Removed redundant import statments in methods - Updated parent_idx, left_child_idx, right_child_idx, time_complexities to be static - removed unused variable 'ROOT' from heapify_down() - stack.py - Updated docstrings at top of file - Updated docstrings for functions - Optimized import statements - Removed redundant import statments in methods - Removed camel casing on methods and variables - Updated get_code() in Stack(object) to be static - Updated _isOperand(), _precedence() in InfixToPostfix() to be static and private - Updated get_code() to be static - Updated method _isOperand() - Removed redundant parenthesis around conditionals - tree.py - Updated docstrings at top of file - Updated docstrings for functions - Optimized import statements - Removed redundant import statments in methods - Removed redundant parenthesis around conditionals - Updated min_val_bst_node(), get_code() - Removed camel casing on variables and parameter names Signed-off-by: IanDoarn <[email protected]>
1 parent 07b4685 commit a79982f

File tree

5 files changed

+545
-333
lines changed

5 files changed

+545
-333
lines changed

pygorithm/data_structures/heap.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,57 @@
33
Contributed: OMKAR PATHAK
44
Created On: 11th August 2017
55
"""
6+
from __future__ import division
67
from pygorithm.data_structures import queue
8+
import inspect
79

810

911
class Heap(queue.Queue):
10-
''' min-heap implementation as queue '''
11-
def parent_idx(self, idx):
12-
''' retrieve the parent '''
12+
"""
13+
min-heap implementation as queue
14+
"""
15+
16+
@staticmethod
17+
def parent_idx(idx):
18+
"""
19+
retrieve the parent
20+
"""
1321
return idx // 2
1422

15-
def left_child_idx(self, idx):
16-
''' retrieve the left child '''
23+
@staticmethod
24+
def left_child_idx(idx):
25+
"""
26+
retrieve the left child
27+
"""
1728
return (idx * 2) + 1
1829

19-
def right_child_idx(self, idx):
20-
''' retrieve the right child '''
30+
@staticmethod
31+
def right_child_idx(idx):
32+
"""
33+
retrieve the right child
34+
"""
2135
return (idx * 2) + 2
2236

2337
def insert(self, data):
24-
''' inserting an element in the heap '''
38+
"""
39+
inserting an element in the heap
40+
"""
41+
# TODO: Fix this if we want this compatible with 2.7
2542
super().enqueue(data)
2643
if self.rear >= 1: # heap may need to be fixed
2744
self.heapify_up()
2845

2946
def heapify_up(self):
30-
'''
47+
"""
3148
Start at the end of the tree (last enqueued item).
3249
3350
Compare the rear item to its parent, swap if
3451
the parent is larger than the child (min-heap property).
3552
Repeat until the min-heap property is met.
3653
3754
Best Case: O(1), item is inserted at correct position, no swaps needed
38-
Worst Case: O(logn), item needs to be swapped throughout all levels of tree
39-
'''
55+
Worst Case: O(log n), item needs to be swapped throughout all levels of tree
56+
"""
4057
child = self.rear
4158
parent = self.parent_idx(child)
4259
while self.queue[child] < self.queue[self.parent_idx(child)]:
@@ -46,38 +63,45 @@ def heapify_up(self):
4663
parent = self.parent_idx(child)
4764

4865
def pop(self):
49-
''' Removes the lowest value element (highest priority, at root) from the heap '''
66+
"""
67+
Removes the lowest value element (highest priority, at root) from the heap
68+
"""
5069
min = super().dequeue()
5170
if self.rear >= 1: # heap may need to be fixed
5271
self.heapify_down()
5372
return min
5473

5574
def favorite(self, parent):
56-
''' Determines which child has the highest priority by 3 cases '''
75+
"""
76+
Determines which child has the highest priority by 3 cases
77+
"""
5778
left = self.left_child_idx(parent)
5879
right = self.right_child_idx(parent)
5980

60-
if left <= self.rear and right <= self.rear: # case 1: both nodes exist
81+
# case 1: both nodes exist
82+
if left <= self.rear and right <= self.rear:
6183
if self.queue[left] <= self.queue[right]:
6284
return left
6385
else:
6486
return right
65-
elif left <= self.rear: # case 2: only left exists
87+
# case 2: only left exists
88+
elif left <= self.rear:
6689
return left
67-
else: # case 3: no children (if left doesn't exist, neither can the right)
90+
# case 3: no children (if left doesn't exist, neither can the right)
91+
else:
6892
return None
6993

7094
def heapify_down(self):
71-
'''
95+
"""
7296
Select the root and sift down until min-heap property is met.
7397
7498
While a favorite child exists, and that child is smaller
7599
than the parent, swap them (sift down).
76100
77101
Best Case: O(1), item is inserted at correct position, no swaps needed
78102
Worst Case: O(logn), item needs to be swapped throughout all levels of tree
79-
'''
80-
cur = ROOT = 0 # start at the root
103+
"""
104+
cur = 0 # start at the root
81105
fav = self.favorite(cur) # determine favorite child
82106
while self.queue[fav] is not None:
83107
if self.queue[cur] > self.queue[fav]:
@@ -88,10 +112,13 @@ def heapify_down(self):
88112
else:
89113
return
90114

91-
def time_complexities(self):
92-
return '''[Insert & Pop] Best Case: O(1), Worst Case: O(logn)'''
115+
# TODO: Is this necessary?
116+
@staticmethod
117+
def time_complexities():
118+
return "[Insert & Pop] Best Case: O(1), Worst Case: O(logn)"
93119

94120
def get_code(self):
95-
''' returns the code for the current class '''
96-
import inspect
121+
"""
122+
returns the code for the current class
123+
"""
97124
return inspect.getsource(Heap)

0 commit comments

Comments
 (0)