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

Ringo - Earth #1

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

Ringo - Earth #1

wants to merge 3 commits into from

Conversation

ringolingo
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 always balanced and always as full as possible. A binary search tree is not necessarily balanced (though that's a plus when it is). Also, in a binary search tree, values are sorted among sibling nodes, whereas in a heap you only have a guarantee of the value relationship between a parent and child node.
Could you build a heap with linked nodes? No. Nodes in a linked list only point to one other node. Nodes in a binary tree must be able to have two children.
Why is adding a node to a heap an O(log n) operation? The adding a node part is only O(1), since it only means putting a value at the end of the heap. The O(log n) part is because, to keep the heap sorted, you must compare the new node with its parent node, and possibly with every subsequent parent node up to the root, to be sure that the values are in the correct order and you do not have a parent node that ought to be lower down than its child. In worst case scenario, you will have to run as many comparisons and swaps as there are levels to the heap. However, the height of the heap only increases logarithmically with the overall size of the heap.
Were the heap_up & heap_down methods useful? Why? heapUp and heapDown were useful because they handled all of the comparisons and swapping, making the addition/removal methods simpler.

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 Ringo, you hit all the learning goals here. Well done.

lib/heapsort.js Outdated
Comment on lines 4 to 15
// Time Complexity: O(n log n). The while loops both run for
// every element in list, so on their own have O(n). Within them
// they call either heap.add or heap.remove, which has O(log n)
// time complexity.
// Space Complexity: O(1). The function does make a new data structure --
// a heap -- but it only adds to the heap one element for every element it
// pops off of the list, so the list is taking less space as the heap takes
// more. Then, the process is reversed with the heap taking less space
// so the list can be rebuilt and take more space. Increasing the size of
// the list is not going to change the additional amount of space the function uses.

function heapsort(list) {

Choose a reason for hiding this comment

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

👍 But the space complexity of your solution is O(n) because you're building a heap here.

Comment on lines +14 to 18
// Time Complexity: O(log n). Pushing the new value to array is O(1),
// then it calls heapUp which is O(log n).
// Space Complexity: O(log n). The space for actually adding is O(1),
// but it calls heapUp which is O(log n).
add(key, value = key) {

Choose a reason for hiding this comment

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

👍

Comment on lines +25 to 29
// Time Complexity: O(log n). The swap and pop are both O(1),
// then it calls heapDown which is O(log n).
// Space Complexity: O(log n). The space for removing is O(1),
// but it calls heapDown which is O(log n).
remove() {

Choose a reason for hiding this comment

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

👍

Comment on lines +52 to 54
// Time complexity: O(1), just checks array length.
// Space complexity: O(1), doesn't need any additional data structures.
isEmpty() {

Choose a reason for hiding this comment

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

👍

Comment on lines +61 to 69
// Time complexity: O(log n). The calculation, swap, and comparison
// are all constant, but the function will need to be called as
// many times as there are levels in the heap, which scales
// logarithmically with the heap's size.
// Space complexity: O(log n). Since it is a recursive function
// it will have at worst as many function calls on the stack
// as times it will be called, which is controlled by number of levels
// in the heap and scales logarithmically with size.
heapUp(index) {

Choose a reason for hiding this comment

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

👍

if (this.store[parentIndex].key < this.store[index].key) return;

this.swap(index, parentIndex);
this.heapUp(parentIndex);
}

// This helper method takes an index and
// moves it up the heap if it's smaller
// than it's parent node.
heapDown(index) {

Choose a reason for hiding this comment

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

👍 , nicely compact!

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