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

Space - Shonda #5

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

Space - Shonda #5

wants to merge 3 commits into from

Conversation

Shonda860
Copy link

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

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

Overall pretty well done, you hit most of the learning goals.

Take a look at my inline comments especially on remove. You lose some of the advantages of a heap because your remove method is O(n) in time complexity.

@@ -6,7 +6,7 @@ Congratulations! You're submitting your assignment!

Question | Answer
:------------- | :-------------
How is a Heap different from a Binary Search Tree? |
How is a Heap different from a Binary Search Tree? | heaps can be ordered by max and min and for

Choose a reason for hiding this comment

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

No other answers for comprehension questions?

Comment on lines 3 to 6
// This method uses a heap to sort an array.
// Time Complexity: ?
// Space Complexity: ?
function heapsort(list) {

Choose a reason for hiding this comment

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

👍 Space & Time Complexity?

Comment on lines +14 to +16
// Time Complexity: O(n)
// Space Complexity: O(1)
add(key, value = key) {

Choose a reason for hiding this comment

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

👍 , This works, but the time complexity is O(log n)

Comment on lines +109 to +117
heap = new MinHeap()
heap.add(6, 'Pasta');
heap.add(12, 'Soup');
heap.add(20, 'Pizza');
heap.add(25, 'Donuts');
heap.add(13, 'Cookies');

console.log(heap.remove())

Choose a reason for hiding this comment

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

Suggested change
heap = new MinHeap()
heap.add(6, 'Pasta');
heap.add(12, 'Soup');
heap.add(20, 'Pizza');
heap.add(25, 'Donuts');
heap.add(13, 'Cookies');
console.log(heap.remove())

Comment on lines +87 to 89
// Time complexity: O(n)
// Space complexity: O(1)
heapUp(index) {

Choose a reason for hiding this comment

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

This method has a time complexity of O(1), and it's really more of a swap with parent method.

Comment on lines +78 to 80
// Time complexity: O(1)
// Space complexity: O(1)
isEmpty() {

Choose a reason for hiding this comment

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

👍

Comment on lines +37 to +38
let last = this.store.pop()
this.store.unshift(last)

Choose a reason for hiding this comment

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

This unshift is an O(n) operation, but all you really need to do is:

  1. Swap the 1st and last element
  2. Pop the last element (to return later).

Then you can heap_down as an O(log n) operation.

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