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

Elle #14

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

Elle #14

wants to merge 2 commits into from

Conversation

dev-elle-up
Copy link

@dev-elle-up dev-elle-up commented Sep 29, 2019

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? They are different in structure and in the way they order themselves. Binary search trees keep nodes strictly in order by value. This can lead to lopsided, unbalanced trees (unless they are re-balanced). Heaps only require the children be < or > the parent (depending upon if they are min- or max-heaps) but do not measure the difference between siblings, and they require each level to be filled left-to-right.
Could you build a heap with linked nodes? You mean a linked list? Yes, you could. This would increase the time complexity, though.
Why is adding a node to a heap an O(log n) operation? At each level, you will only visit one branch of the tree.
Were the heap_up & heap_down methods useful? Why? These were essential to making the structure work. Without them, you would need to put code into the add and remove methods that traverse and reorganize the tree, and this probably couldn't be called recursively.

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! You hit all the learning goals here. See my notes on space complexity. When you have a recursive method, you have to account for the use of the call stack.

# Time Complexity: O(nlogn),where n is the number of items in the list,
# because the add and remove methods are O(nlogn)
# Space Complexity: O(2n), where n is the number of items in the list

Choose a reason for hiding this comment

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

So really O(n)

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nlogn) because heap_up is nlogn, where n is the number of nodes in the heap
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since heap_up is recursive, you have to account for the space complexity of the system stack.

Comment on lines +103 to +116
# These helper methods find the index of the
# current item's parent node, and the indices
# of its left and right children
def find_parent_index(node_index)
return ((node_index-1)/2).floor
end

def find_left_child_index(node_index)
return node_index * 2 + 1
end

def find_right_child_index(node_index)
return node_index * 2 + 2
end

Choose a reason for hiding this comment

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

Good set of helper methods.

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.

None yet

2 participants