Skip to content

Fire- Gessica#18

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

Fire- Gessica#18
GeMath18 wants to merge 2 commits intoAda-C14:masterfrom
GeMath18:master

Conversation

@GeMath18
Copy link
Copy Markdown

@GeMath18 GeMath18 commented May 5, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps are a great way to store information in a semi-order and BST must be sorted.
Could you build a heap with linked nodes? It is possible but the time complexity to search for an element would be linear.
Why is adding a node to a heap an O(log n) operation? Adding a node involves placing a node in the next available leaf node and then conducting a series of swaps with its parent until heap order is achieved. Since there are Log n levels to the heap, then adding a node is a O(log n) operation.
Were the heap_up & heap_down methods useful? Why? Yes, they were very useful as helper methods to add or remove a node in a heap.

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.

Overall nice work Gessica, I had one comment on heap_down. Take a look and let me know what questions you have. Otherwise well done.

Comment thread lib/heap_sort.rb
Comment on lines +4 to +6
# Time Complexity: O(nlog(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.

👍

Comment thread lib/min_heap.rb
Comment on lines +28 to 30
# 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 +60 to 62
# 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 +72 to 74
# 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
left_child = (index * 2) + 1
right_child = (index * 2) + 2

return @store if @store[left_child].nil? || @store[right_child].nil?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What about if the left child is NOT nil and the right child is? Could the parent need to swap with the sole left child?

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