-
Notifications
You must be signed in to change notification settings - Fork 34
Niv #10
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?
Niv #10
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,5 +1,3 @@ | ||
|
|
||
|
|
||
| # This method uses a heap to sort an array. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| class HeapNode | ||
| attr_reader :key, :value | ||
|
|
||
| # compare by key, priority field | ||
| def initialize(key, value) | ||
| @key = key | ||
| @value = value | ||
|
|
@@ -17,16 +17,25 @@ def initialize | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def add(key, value = key) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| new_node = HeapNode.new(key, value) | ||
| new_node_index = @store.length | ||
| @store.push(new_node) | ||
| heap_up(@store.length - 1) | ||
|
|
||
| return new_node | ||
| end | ||
|
|
||
| # This method removes and returns an element from the heap | ||
| # maintaining the heap structure | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def remove() | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| end | ||
| removed = @store[0].value | ||
| swap(0, @store.length - 1) | ||
| @store.pop | ||
| heap_down(0) | ||
| return removed | ||
| end | ||
|
|
||
|
|
||
| # Used for Testing | ||
|
|
@@ -52,20 +61,48 @@ def empty? | |
|
|
||
| private | ||
|
|
||
| def find_parent(index) | ||
| parent_index = (index - 1)/2 | ||
| return parent_index | ||
| end | ||
|
Comment on lines
+64
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. I like this helper method. |
||
|
|
||
| # 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) | ||
|
|
||
| puts to_s | ||
| parent_index = find_parent(index) | ||
| if @store[parent_index].key <= @store[index].key || index == 0 | ||
| return | ||
| end | ||
| if @store[parent_index].key > @store[index].key | ||
|
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. Since your prior |
||
| # puts @store[index].value | ||
| swap(parent_index, index) | ||
| index = parent_index | ||
| heap_up(index) | ||
| 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) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| puts 'heap down' | ||
| puts to_s | ||
| child_index = 2*index + 1 | ||
|
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. You are only finding the left child here. You are not finding the right child. |
||
| if index >= @store.length - 1 || child_index >= @store.length - 1 | ||
| return | ||
| end | ||
| if @store[child_index].key >= @store[index].key | ||
| return | ||
| end | ||
| if @store[child_index].key < @store[index].key | ||
| swap(child_index, index) | ||
| index = child_index | ||
| heap_down(index) | ||
| 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.
Since you're doing recursion it should be O(log n) for both.