Skip to content

Conversation

@sjscotton
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 only guarantees the relationship of the parents and children, not siblings. So an inorder traversal will not actually be inorder.
Could you build a heap with linked nodes? You could, but it would be more difficult because you cant use indexing.
Why is adding a node to a heap an O(log n) operation? Because the max number of swaps needed is the height of the tree, which is 0(log n).
Were the heap_up & heap_down methods useful? Why? It made the add and remove methods cleaner.

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.

Well done. You hit the learning goals here. Let me know if you have questions.

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: 0(n log n) where n is the length of the list. heapify iterates through the list and for each calls add() which is 0(log n) so thats n log n for heapify, heap sort is the same, n log n. So total its 0(2 * nlogn) which reduces to n log n
# Space Complexity: 0(1), we create a new heap, but each time we add one, we remove one from the list. and then westart moving items one at a time back to the list. the length of the heap and the length of the list combined are always equal to the original list size, so we dont actually use any extra space.

Choose a reason for hiding this comment

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

You are building a heap here, so this sort has O(n) space complexity.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(log n) where n is the index
# Space complexity: O(1) only use constant sized variables

Choose a reason for hiding this comment

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

Nice, an iterative solution.

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