Skip to content

Fire - Roshni #23

Open
roshni-patel wants to merge 2 commits intoAda-C14:masterfrom
roshni-patel:master
Open

Fire - Roshni #23
roshni-patel wants to merge 2 commits intoAda-C14:masterfrom
roshni-patel:master

Conversation

@roshni-patel
Copy link
Copy Markdown

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 different from a binary tree in that in a heap each node is either bigger (max heap) or smaller (min heap) than its children, whereas in a binary search tree, all nodes to the left of the parent are smaller and all nodes to the right of the parent are bigger.
Could you build a heap with linked nodes? Yes, it's possible to build a heap with linked nodes but it's much easier to do so with an array. We can use the following formulas, left_child = i * 2 + 1, right_child = i * 2 + 2, for example, to determine the indices of a node's children.
Why is adding a node to a heap an O(log n) operation? Adding a node is an O(log n) operation because in order to add a node at most we are doing one swap per level of the tree.
Were the heap_up & heap_down methods useful? Why? Yes, they were useful as helper methods for the add and remove methods.

Copy link
Copy Markdown

@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 Roshni! You hit the learning goals here. Well done.

Comment thread lib/heap_sort.rb
Comment on lines +1 to +4
# This method uses a heap to sort an array.
# Time Complexity: O(n log n)
# Space Complexity: O(n)
def heapsort(list)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Nice work!

Comment thread lib/min_heap.rb
Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(key, value = key)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Nice work!

Comment thread lib/min_heap.rb
Comment on lines +26 to 28
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def remove()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread lib/min_heap.rb
Comment on lines +54 to 56
# Time complexity: O(1)
# Space complexity: O(1)
def empty?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread lib/min_heap.rb
Comment on lines +65 to 67
# Time complexity: O(log n)
# Space complexity: O(log n)
def heap_up(index)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread lib/min_heap.rb
Comment on lines +80 to 82
# Time complexity: O(log n)
# Space complexity: O(log n)
def heap_down(index)
Copy link
Copy Markdown

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