Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C16 Digital - Gabe K #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

GabeKelemen
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap has to be a complete binary tree. It has a faster insertion and deletion -- O(log n) -- than a BST, O(n). A can be ordered as a max or min heap where the root/parent is either greater or smaller than the child, whereas in a BST all nodes in the left subtree are smaller, and all nodes in the right subtree are larger.
Could you build a heap with linked nodes? A heap can implement a priority queue using a singly linked list but it makes more sense to implement a heap with an array because you can iterate over the array and use the array's index for the children / nodes.
Why is adding a node to a heap an O(log n) operation? A new node is always added at the bottom of the heap and bubbles up. The number of comparisons and swaps to move the new element to its correct position will be at worst-case one swap per level of the heap. Since there Log n levels to the heap, then adding a node is 𝑂(log𝑛).
Were the heap_up & heap_down methods useful? Why? These methods make the comparison and swapping between the child and parent easier per level until the node in question ends up in its correct position.

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.

✨💫 Very nice! I left a few comments on space complexity below. Let me know what questions you have.

🟢

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n 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.

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)

Choose a reason for hiding this comment

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

✨ Nice, however the space complexity is O(log n) because of the recursive call stack of heap_up


def remove(self):
""" This method removes and returns an element from the heap
maintaining the heap structure
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)

Choose a reason for hiding this comment

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

✨ Nice, however like above, space complexity is O(log n) because of the recursive call stack of heap_down

@@ -44,10 +66,10 @@ def __str__(self):

def empty(self):

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(log n)
Space complexity: makes a recursive call

Choose a reason for hiding this comment

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

✨ What does the recursive call imply the space complexity should be??

if self.store[index].key < self.store[parent].key:
self.swap(index, parent)
#continue to heap_up until the root is reached and every node ordered correctly
self.heap_up(parent)

def heap_down(self, index):

Choose a reason for hiding this comment

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

✨ Very clean!

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