Skip to content

Conversation

mac6551
Copy link

@mac6551 mac6551 commented May 15, 2022

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap is not ordered, a BST is. Heaps are also almost complete, where a BST is not required to meet that condition (it's just better if they do). Heaps are also filled left to right whereas BST fills based on order
Could you build a heap with linked nodes? No. Because of the way we add and remove from a heap, using a linked node structure makes things way too convoluted.
Why is adding a node to a heap an O(log n) operation? Because you add to the next available position at the end of the array then heap_up till it is in the correct place (based on whether it's a min/max heap). The way you move of the array only goes through half, worst case scenario, so it is log n
Were the heap_up & heap_down methods useful? Why? Yes, it keeps the heap valid

Copy link

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

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

✨ Nice work, Mac! I left a few comments on complexity below. Also technically you could create a heap with a linked list, it's just, as you said, much more convoluted and therefore not advisable.

🟢

@@ -1,8 +1,19 @@

from heapq import heappush, heappop

def heap_sort(list):

Choose a reason for hiding this comment

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

✨ Time and space complexity? It would also be great to see you implement heap_sort using the MinHeap you implement below!

larger than either of its children and continues until
the heap property is reestablished.
"""
pass
left_index = (index * 2) + 1

Choose a reason for hiding this comment

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

Time complexity: ?
Space complexity: ?
Time complexity: O(1)
Space complexity: O(n)

Choose a reason for hiding this comment

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

✨ However time and space complexity are O(log n) here because of the recursive call. heap_up will be called (log n) times because each time you are halving the index

Time complexity: ?
Space complexity: ?
Time complexity: O(1)
Space complexity: O(1)

Choose a reason for hiding this comment

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


self.store.append(node)

self.heap_up(len(self.store) - 1)

def remove(self):

Choose a reason for hiding this comment

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

✨ However space complexity will be O(log n) because of the recursive call stack of heap_down

""" Adds a HeapNode instance to the heap
If value == None the new node's value is set to key
Time Complexity: O(log n)
Space Complexity: O(n)

Choose a reason for hiding this comment

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

✨ However space complexity will just be O(log n) here because of the recursive call stack of heap_up. You aren't creating any other significant data structures.

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