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 - Afina Walton #1

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

Conversation

afinawalton
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? The children of a parent in a heap don't need to be in a specific order. They just need to be either less than their parent (max-heap) or more than their parent (min-heap).
Could you build a heap with linked nodes? You could, but heaps are usually built with an array. A linked node implementation would be more difficult, as you couldn't calculate the indices of parents and children easily. The concept of parents and children are also not part of a typical linked list.
Why is adding a node to a heap an O(log n) operation? Because of the bubble-up method, which is only comparing 1 of 2 possible items in each level of the tree.
Were the heap_up & heap_down methods useful? Why? Yes. Heap down lets you get the items in the correct, sorted spot. Heap up does the same and is used to initialize the heap.

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.

✨💫 Nice work, Afina. I left a couple comments, mostly on space complexity. Let me know what questions you have.

🟢

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

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

✨ However, because of the recursive call stack space complexity will be O(log n)


// base case
if (index === 0 || parent.key < element.key) {
// pass

Choose a reason for hiding this comment

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

This works however, it's generally considered bad style to leave the body of an if statement empty

Suggested change
// pass
return

// Time complexity: ?
// Space complexity: ?
// 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.

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n)
// Space Complexity: O(1)
remove() {

Choose a reason for hiding this comment

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

✨ However space complexity will be O(log n) here because of the recursive call stack of heap_down

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

✨ However space complexity will be O(log n) here because of the recursive call stack of heap_up

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