-
Notifications
You must be signed in to change notification settings - Fork 41
Space- Yieni #15
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- Yieni #15
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,21 +14,33 @@ def initialize | |
| end | ||
|
|
||
| # This method adds a HeapNode instance to the heap | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: 0(1)? | ||
| # Space Complexity: o(n)? | ||
| 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. 👍 The time complexity would be O(log n) because of how you heap_up going up through the levels of the tree. Similarly because |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| node = HeapNode.new(key, value) | ||
| if @store.empty? | ||
| @store << node | ||
| else | ||
| @store << node | ||
| current_index = @store.length - 1 | ||
| @store = heap_up(current_index) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # This method removes and returns an element from the heap | ||
| # maintaining the heap structure | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def remove() | ||
|
Comment on lines
33
to
35
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. 👍 Time & space complexity? |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| removed = @store[0].value | ||
| @store[0] = @store[@store.length - 1] | ||
| parent_node = @store[0] | ||
| @store.pop | ||
| @store = heap_down(0) | ||
| return removed | ||
| end | ||
|
|
||
|
|
||
| # Used for Testing | ||
| def to_s | ||
| return "[]" if @store.empty? | ||
|
|
@@ -47,7 +59,7 @@ def to_s | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def empty? | ||
|
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 | ||
|
|
@@ -58,14 +70,56 @@ def empty? | |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def heap_up(index) | ||
|
Comment on lines
70
to
72
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. 👍 Time & space complexity? |
||
| node = @store[index] | ||
| parent_index = (index - 1)/2 | ||
| parent_node = @store[parent_index] | ||
| return @store if parent_node.key <= node.key || index == 0 | ||
|
|
||
| @store[index] = parent_node | ||
| @store[parent_index] = node | ||
|
|
||
| index = parent_index | ||
| return heap_up(index) | ||
| 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. This works, but you've overcomplicated some of the if statements. |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| parent_node = @store[index] | ||
| left_child_index = index * 2 + 1 | ||
| right_child_index = index * 2 + 2 | ||
| left_child = @store[left_child_index] | ||
| right_child = @store[right_child_index] | ||
| if left_child.nil? == false && right_child.nil? == false | ||
| return @store if (parent_node.key <= left_child.key && parent_node.key <= right_child.key) | ||
|
|
||
| if left_child.key < right_child.key | ||
| @store[index] = left_child | ||
| @store[left_child_index] = parent_node | ||
| index = left_child_index | ||
| return heap_down(index) | ||
| else | ||
| @store[index] = right_child | ||
| @store[right_child_index] = parent_node | ||
| index = right_child_index | ||
| return heap_down(index) | ||
| end | ||
| elsif left_child.nil? == true && right_child.nil? == true | ||
| return @store | ||
| elsif left_child.nil? == true && right_child.nil? == false | ||
|
Comment on lines
+108
to
+110
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 don't think the right child can't be similarly I don't think the left child can be nil and the right not nil. |
||
| return @store if parent_node.key <= right_child.key | ||
| @store[index] = right_child | ||
| @store[right_child_index] = parent_node | ||
| index = right_child_index | ||
| return heap_down(index) | ||
| else left_child.nil? == false && right_child.nil? == true | ||
|
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. Good catch checking if the right child is past the end of the array and the left isn't. |
||
| return @store if parent_node.key <= left_child.key | ||
| @store[index] = left_child | ||
| @store[left_child_index] = parent_node | ||
| index = left_child_index | ||
| return 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.
👍 Time & Space complexity?