From ac907d8a6f032d990a9c2be4140d226db9a2db69 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 19 Sep 2019 15:44:55 -0700 Subject: [PATCH 1/5] working on min heap --- lib/min_heap.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..158b73d 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -1,6 +1,6 @@ class HeapNode attr_reader :key, :value - + # compare by key, priority field def initialize(key, value) @key = key @value = value @@ -17,7 +17,13 @@ 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) + if new_node_index > 2 + parent_node = find_parent() + end + return new_node end # This method removes and returns an element from the heap @@ -52,6 +58,14 @@ def empty? private + def find_parent(index) + parent_index = (index - 1)/2 + return parent_index + end + + + + # 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. From f771d0b645a1f3012cba935fadc6aeadae7cb4eb Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 19 Sep 2019 15:59:14 -0700 Subject: [PATCH 2/5] working on heap up --- lib/min_heap.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 158b73d..a9afb3f 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -20,8 +20,8 @@ def add(key, value = key) new_node = HeapNode.new(key, value) new_node_index = @store.length @store.push(new_node) - if new_node_index > 2 - parent_node = find_parent() + if @store.length > 1 + heap_up(@store.length - 1) end return new_node end @@ -63,15 +63,16 @@ def find_parent(index) return parent_index end - - - # 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) + p = find_parent(index) + if @store[p].key < @store[index].key + swap(p, index) + end end From f134a04535e2a185fc71ffa2f12ddf22f13a089b Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 19 Sep 2019 16:11:06 -0700 Subject: [PATCH 3/5] recursive heap up --- lib/min_heap.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index a9afb3f..fc4fd8c 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -69,11 +69,16 @@ def find_parent(index) # Time complexity: ? # Space complexity: ? def heap_up(index) - p = find_parent(index) - if @store[p].key < @store[index].key - swap(p, index) + parent_index = find_parent(index) + if @store[parent_index].key < @store[index].key + swap(parent_index, index) + index = parent_index + end + if index == 0 || @store[parent_index].key <= @store[index].key + return + else + heap_up(index) end - end # This helper method takes an index and From bbf1f2d87cfb71bb1c7973cc3d21f3b714b76fcb Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 26 Sep 2019 16:23:38 -0700 Subject: [PATCH 4/5] fixed heap up --- lib/min_heap.rb | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index fc4fd8c..ce07a2c 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -20,9 +20,8 @@ def add(key, value = key) new_node = HeapNode.new(key, value) new_node_index = @store.length @store.push(new_node) - if @store.length > 1 - heap_up(@store.length - 1) - end + heap_up(@store.length - 1) + return new_node end @@ -31,8 +30,12 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." - end + removed = @store[0].value + swap(0, @store.length - 1) + @store.pop(@store.length - 1) + heap_down(0) + return removed + end # Used for Testing @@ -69,14 +72,15 @@ def find_parent(index) # Time complexity: ? # Space complexity: ? def heap_up(index) + puts to_s parent_index = find_parent(index) - if @store[parent_index].key < @store[index].key + if @store[parent_index].key <= @store[index].key || index == 0 + return + end + if @store[parent_index].key > @store[index].key + # puts @store[index].value swap(parent_index, index) index = parent_index - end - if index == 0 || @store[parent_index].key <= @store[index].key - return - else heap_up(index) end end @@ -85,7 +89,16 @@ def heap_up(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + parent_index = find_parent(index) + if @store[parent_index].key < @store[index].key + swap(parent_index, index) + end + if index == 0 || @store[parent_index].key >= @store[index].key + return + else + index = parent_index + heap_down(index) + end end # If you want a swap method... you're welcome From c97256982e73ca22e7587f07968090a0984644b0 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 26 Sep 2019 16:55:26 -0700 Subject: [PATCH 5/5] working on heap down --- lib/heap_sort.rb | 2 -- lib/min_heap.rb | 18 +++++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..ca6c499 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,5 +1,3 @@ - - # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? diff --git a/lib/min_heap.rb b/lib/min_heap.rb index ce07a2c..11dbf90 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -32,7 +32,7 @@ def add(key, value = key) def remove() removed = @store[0].value swap(0, @store.length - 1) - @store.pop(@store.length - 1) + @store.pop heap_down(0) return removed end @@ -89,14 +89,18 @@ def heap_up(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - parent_index = find_parent(index) - if @store[parent_index].key < @store[index].key - swap(parent_index, index) + puts 'heap down' + puts to_s + child_index = 2*index + 1 + if index >= @store.length - 1 || child_index >= @store.length - 1 + return end - if index == 0 || @store[parent_index].key >= @store[index].key + if @store[child_index].key >= @store[index].key return - else - index = parent_index + end + if @store[child_index].key < @store[index].key + swap(child_index, index) + index = child_index heap_down(index) end end