-
Notifications
You must be signed in to change notification settings - Fork 41
Space - Antonia #29
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?
Space - Antonia #29
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 |
|---|---|---|
|
|
@@ -17,15 +17,22 @@ def initialize | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| 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) | ||
| return heap_up(@store.length - 1) | ||
| end | ||
|
|
||
| # This method removes and returns an element from the heap | ||
| # maintaining the heap structure | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def remove() | ||
|
Comment on lines
26
to
28
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. 👍 , noting you're missing time/space complexity |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return nil if @store.empty? | ||
|
|
||
| swap(0, @store.length - 1) | ||
| removed = @store.pop | ||
|
|
||
| heap_down(0) if [email protected]? | ||
| return removed.value | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -47,7 +54,7 @@ def to_s | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| 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. 👍 , noting you're missing time/space complexity |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return @store.empty? | ||
| end | ||
|
|
||
| private | ||
|
|
@@ -58,14 +65,38 @@ def empty? | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def heap_up(index) | ||
|
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. 👍 , noting you're missing time/space complexity |
||
|
|
||
| return nil if index == 0 | ||
| parent = (index -1 ) / 2 | ||
| if @store[index].key < @store[parent].key | ||
| swap(index, parent) | ||
| heap_up(parent) | ||
| end | ||
| 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) | ||
|
Comment on lines
76
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. 👍 , noting you're missing time/space complexity |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| left_child = (index * 2) + 1 | ||
| right_child= (index * 2) + 2 | ||
|
|
||
| return if !@store[left_child] && !@store[right_child] | ||
|
|
||
| if @store[left_child].nil? | ||
| smallest_index = right_child | ||
| elsif @store[right_child].nil? | ||
| smallest_index = left_child | ||
| elsif @store[left_child].key < @store[right_child].key | ||
| smallest_index = left_child | ||
| else | ||
| smallest_index = right_child | ||
| end | ||
|
|
||
| if @store[index].key > @store[smallest_index].key | ||
| swap(index, smallest_index) | ||
| end | ||
|
|
||
| return heap_down(smallest_index) | ||
| 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.
👍