-
Notifications
You must be signed in to change notification settings - Fork 37
Iris Lux - Water #25
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
base: master
Are you sure you want to change the base?
Iris Lux - Water #25
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
|
|
||
|
|
||
| require_relative "min_heap" | ||
| # This method uses a heap to sort an array. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: o(nlogn) | ||
| # Space Complexity: o(nlogn) | ||
| def heap_sort(list) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| #add elements to a heap | ||
| #remove elements from heap one-by one, placing them in order | ||
| sort_heap = MinHeap.new | ||
| sorted = [] | ||
| list.each{|element| sort_heap.add(element)} | ||
| list.length.times{|i| sorted.push(sort_heap.remove)} | ||
| return sorted | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,18 +14,25 @@ def initialize | |
| end | ||
|
|
||
| # This method adds a HeapNode instance to the heap | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(logn) | ||
| # Space Complexity: O(logn) | ||
| def add(key, value = key) | ||
|
Comment on lines
+17
to
19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| @store << HeapNode.new(key, value) | ||
|
|
||
| heap_up(@store.length - 1) | ||
| end | ||
|
|
||
| # This method removes and returns an element from the heap | ||
| # maintaining the heap structure | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(logn) | ||
| # Space Complexity: O(logn) | ||
| def remove() | ||
|
Comment on lines
+27
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return nil if @store.empty? | ||
| swap(0, @store.length - 1) | ||
| result = @store.pop.value | ||
|
|
||
| heap_down(0) unless @store.empty? | ||
| return result | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -44,28 +51,46 @@ def to_s | |
| end | ||
|
|
||
| # This method returns true if the heap is empty | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(1) | ||
| # Space complexity: O(1) | ||
| def empty? | ||
|
Comment on lines
+54
to
56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return @store.empty? | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # This helper method takes an index and | ||
| # moves it up the heap, if it is less than it's parent node. | ||
| # It could be **very** helpful for the add method. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def heap_up(index) | ||
|
|
||
| # Time complexity: O(logn) | ||
| # Space complexity: O(logn) | ||
| def heap_up(idx) | ||
|
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| parent_idx = (idx - 1) / 2 | ||
| return if idx <= 0 || @store[parent_idx].key <= @store[idx].key | ||
| swap(parent_idx, idx) | ||
| heap_up(parent_idx) | ||
| end | ||
|
|
||
| # This helper method takes an index and | ||
| # moves it up the heap if it's smaller | ||
| # than it's parent node. | ||
| def heap_down(index) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| # Time complexity: O(logn) | ||
| # Space complexity: O(logn) | ||
| def heap_down(idx) | ||
|
Comment on lines
74
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Very elegant |
||
|
|
||
| left_idx = (idx * 2) + 1 | ||
| right_idx = (idx * 2) + 2 | ||
| smaller_idx = idx | ||
|
|
||
| smaller_idx = left_idx if @store[left_idx] && @store[left_idx].key < @store[smaller_idx].key | ||
|
|
||
| smaller_idx = right_idx if @store[right_idx] && @store[right_idx].key < @store[smaller_idx].key | ||
|
|
||
| if (smaller_idx != idx) | ||
| swap(idx, smaller_idx) | ||
| heap_down(smaller_idx) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # If you want a swap method... you're welcome | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 The space complexity is O(n) since you are making a Heap with n elements.