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

Fire - Roshni #23

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

Fire - Roshni #23

wants to merge 2 commits into from

Conversation

roshni-patel
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 is different from a binary tree in that in a heap each node is either bigger (max heap) or smaller (min heap) than its children, whereas in a binary search tree, all nodes to the left of the parent are smaller and all nodes to the right of the parent are bigger.
Could you build a heap with linked nodes? Yes, it's possible to build a heap with linked nodes but it's much easier to do so with an array. We can use the following formulas, left_child = i * 2 + 1, right_child = i * 2 + 2, for example, to determine the indices of a node's children.
Why is adding a node to a heap an O(log n) operation? Adding a node is an O(log n) operation because in order to add a node at most we are doing one swap per level of the tree.
Were the heap_up & heap_down methods useful? Why? Yes, they were useful as helper methods for the add and remove methods.

Copy link

@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.

Nice work Roshni! You hit the learning goals here. Well done.

Comment on lines +1 to +4
# This method uses a heap to sort an array.
# Time Complexity: O(n log n)
# Space Complexity: O(n)
def heapsort(list)

Choose a reason for hiding this comment

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

👍 Nice work!

Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(key, value = key)

Choose a reason for hiding this comment

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

👍 Nice work!

Comment on lines +26 to 28
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def remove()

Choose a reason for hiding this comment

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

👍

Comment on lines +54 to 56
# Time complexity: O(1)
# Space complexity: O(1)
def empty?

Choose a reason for hiding this comment

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

👍

Comment on lines +65 to 67
# Time complexity: O(log n)
# Space complexity: O(log n)
def heap_up(index)

Choose a reason for hiding this comment

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

👍

Comment on lines +80 to 82
# Time complexity: O(log n)
# Space complexity: O(log n)
def heap_down(index)

Choose a reason for hiding this comment

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

👍

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