-
Notifications
You must be signed in to change notification settings - Fork 41
Jeta - Space #11
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?
Jeta - Space #11
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 |
|---|---|---|
|
|
@@ -14,18 +14,24 @@ def initialize | |
| end | ||
|
|
||
| # This method adds a HeapNode instance to the heap | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(logn) | ||
| # Space Complexity: O(1) | ||
| 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. 👍 , nice work with a non-recursive heap_up method. |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| @store.push(HeapNode.new(key, value)) | ||
| new_node_index = @store.length - 1 | ||
| heap_up(new_node_index) | ||
| 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(1) | ||
| 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 if empty? | ||
| swap(0, @store.length - 1) | ||
| removed_element = @store.pop | ||
| heap_down(0) | ||
| return removed_element.value | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -47,25 +53,58 @@ def to_s | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def empty? | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return @store[0].nil? | ||
| 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: ? | ||
| # Time complexity: O(logn) | ||
| # Space complexity: O(1) | ||
| def heap_up(index) | ||
|
Comment on lines
+64
to
66
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_i = (index - 1) / 2 | ||
| return if parent_i < 0 | ||
| parent = @store[parent_i] | ||
| current = @store[index] | ||
|
|
||
| while parent_i >= 0 && parent.key > current.key | ||
| swap(index, parent_i) | ||
| index = parent_i | ||
| parent_i = (index - 1) / 2 | ||
| parent = @store[parent_i] | ||
| end | ||
| end | ||
|
|
||
| # This helper method takes an index and | ||
| # moves it up the heap if it's smaller | ||
| # than it's parent node. | ||
| # moves it up the heap if it's smaller | ||
| # than it's parent node. | ||
| def heap_down(index) | ||
|
Comment on lines
+81
to
83
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..." | ||
| while index < @store.length | ||
| left_i = index * 2 + 1 | ||
| right_i = index * 2 + 2 | ||
| child_index = nil | ||
| left = @store[left_i] | ||
| right = @store[right_i] | ||
|
|
||
| if left && right | ||
| child_index = left.key < right.key ? left_i : right_i | ||
| if @store[child_index].key < @store[index].key | ||
| swap(index, child_index) | ||
| index = child_index | ||
| else | ||
| return | ||
| end | ||
| elsif left && !right | ||
|
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. Nice work catching the scenario where the right is not in the array, but left is. |
||
| return unless left.key < @store[index].key | ||
| child_index = left_i | ||
| swap(index, child_index) | ||
| index = child_index | ||
| else | ||
| return | ||
| end | ||
| 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.
No heapsort method?