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

Aimee 👾 - Fire 🔥 #2

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

Aimee 👾 - Fire 🔥 #2

wants to merge 4 commits into from

Conversation

marks214
Copy link

@marks214 marks214 commented May 2, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap maintains a complete binary tree, but not a binary search tree (BST). A complete binary tree is a structure in which every level of the tree is fully filled, with exception of the last level. Each level in the complete binary tree is filled from left to right. A BST can be unbalanced, where as a heap cannot be.
Could you build a heap with linked nodes? Yes.
Why is adding a node to a heap an O(log n) operation? The height of a complete binary tree is log2(n+1), therefore adding a node is an O(log n) operation.
Were the heap_up & heap_down methods useful? Why? Yes, the methods were useful because they make recursive calls on the stack with a space and time complexity of O(log n).

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 Aimee, I had a small comment, but this is quite well done.

Comment on lines +4 to 6
// Time Complexity: O(nlog(n))- both for loops are O(n), heap.add and heap.remove are O(log(n))
// Space Complexity: O(n) - the store array of minHeap has a space complexity of O(n)
function heapsort(list) {

Choose a reason for hiding this comment

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

👍

Comment on lines +14 to 16
// Time Complexity: O(log(n)) - Recursive calls (uses heapUp)
// Space Complexity: O(log(n)) - Recursive calls, log(n) calls in memory (because of heapUp)
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 +53 to 55
// Time complexity: O(1) - the length property is a quick look up
// Space complexity: O(1) - saving the length of the array will be constant memory
isEmpty() {

Choose a reason for hiding this comment

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

👍

Comment on lines +26 to 28
// Time Complexity: O(log(n)), recursive (uses heapDown, similar to heapUp)
// Space Complexity: O(log(n)), log(n) recursion calls (uses heapDown)
remove() {

Choose a reason for hiding this comment

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

👍

Comment on lines +76 to +80
if (index % 2 === 0) {
parentIndex = (index - 2) / 2;
} else {
parentIndex = (index - 1) / 2;
}

Choose a reason for hiding this comment

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

Could you use floor to simplify this a bit?

Comment on lines 93 to 96
// 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.

👍

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