-
Notifications
You must be signed in to change notification settings - Fork 37
Rachael-Waterrrrr #32
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?
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,24 @@ 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. 👍 Space/Time Complexity? |
||
| 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: ? | ||
| def remove() | ||
|
Comment on lines
25
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. 👍 Space/Time Complexity? |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return nil if @store.empty? | ||
|
|
||
| swap(0, @store.length - 1) | ||
| removed = @store.pop | ||
|
|
||
| heap_down(0) unless @store.empty? | ||
|
|
||
| removed.value | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -47,7 +56,7 @@ def to_s | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def empty? | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| @store.empty? | ||
| end | ||
|
|
||
| private | ||
|
|
@@ -58,14 +67,37 @@ def empty? | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def heap_up(index) | ||
|
Comment on lines
67
to
69
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. 👍 Space/Time Complexity? |
||
|
|
||
| if index == 0 | ||
| return | ||
| end | ||
|
|
||
| head = (index - 1 ) / 2 | ||
|
|
||
| if index == 0 | ||
| return | ||
| elsif @store[index].key < @store[head].key | ||
| swap(head,index) | ||
| heap_up(head) | ||
| 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
85
to
88
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. 👍 Space/Time Complexity? Nice work handling all three scenarios here. |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| left = (index * 2) + 1 | ||
| right = (index * 2) + 2 | ||
| if left >= @store.length - 1 | ||
| return | ||
| end | ||
| if @store[right] && @store[right].key < @store[left].key && @store[right].key < @store[index].key | ||
| swap(right, index) | ||
| heap_down(right) | ||
| elsif @store[left].key < @store[index].key | ||
| swap(left, index) | ||
| heap_down(left) | ||
| 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.
👍 Space/Time Complexity?