Skip to content

Christian C15 - Scissors#44

Open
Crintion wants to merge 1 commit into
Ada-C15:masterfrom
Crintion:master
Open

Christian C15 - Scissors#44
Crintion wants to merge 1 commit into
Ada-C15:masterfrom
Crintion:master

Conversation

@Crintion
Copy link
Copy Markdown

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree?
Could you build a heap with linked nodes?
Why is adding a node to a heap an O(log n) operation?
Were the heap_up & heap_down methods useful? Why?

Copy link
Copy Markdown

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little awkward, but I think it mostly works. I made a few comments and let me know what questions you have.

Comment thread heaps/heap_sort.py
Comment on lines 3 to 7
def heap_sort(list):
""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(nlogn) (Each add is log(n), n nodes must be added)
Space Complexity: O(n)
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread heaps/min_heap.py
Comment on lines +35 to +36

print(self.store)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(self.store)

Comment thread heaps/min_heap.py
Comment on lines +22 to 27
def add(self, key, value=None):
""" This method adds a HeapNode instance to the heap
If value == None the new node's value should be set to key
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log(n)) : add tail is constant rebalancing is log(n)
Space Complexity: O(N+1)
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However space complexity should be O(log n) due to the recursive call stack.

Comment thread heaps/min_heap.py
Comment on lines +87 to +90
if index == 1 or index == 2:
parent_index = 0
else:
parent_index = int(math.ceil((index-2)/2))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the if statement, just the arithmetic for the parent_index would work.

Comment thread heaps/min_heap.py
Comment on lines +94 to +99
print("Parent")
print(parent_index)
print(type(parent_index))
print("index")
print(index)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("Parent")
print(parent_index)
print(type(parent_index))
print("index")
print(index)

Comment thread heaps/min_heap.py
left_index = (index*2)+1
right_index = (index*2)+2

if left_index >= len(self.store) or right_index >= len(self.store):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have a left child with no right child.

Suggested change
if left_index >= len(self.store) or right_index >= len(self.store):
if left_index >= len(self.store) and right_index >= len(self.store):

Comment thread heaps/min_heap.py
Comment on lines +120 to +126
if self.store[left_index][0] < self.store[index][0]:
self.swap(left_index, index)
self.heap_down(index)

elif self.store[right_index][0] < self.store[index][0]:
self.swap(right_index, index)
self.heap_down(index)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to do two swaps here. I think you should compare the left and right children and swap the current index with the min child.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants