algorithms icon indicating copy to clipboard operation
algorithms copied to clipboard

Heap is broken with objects

Open fbernier opened this issue 11 years ago • 6 comments

Works when the heap length is < 10 but fails with 10+

min_heap = ::Containers::Heap.new(10.times.map{Object.new}){|x, y| (x <=> y) == -1}

while val = min_heap.pop do
  p val
end

RuntimeError: Couldn't delete node from stored nodes hash

fbernier avatar May 01 '14 17:05 fbernier

any updates on this issue?

dirkholzapfel avatar Aug 05 '15 17:08 dirkholzapfel

@dirkholzapfel I ended up using https://github.com/rubyworks/pqueue instead

fbernier avatar Aug 05 '15 17:08 fbernier

If you let the elements not same, this err wont be raised.

min_heap = ::Containers::Heap.new(10.times.map{rand(100)}){|x, y| (x <=> y) == -1}

while val = min_heap.pop do
  p val
end

But if you need elements to be same, just replace Heap with MaxHeap or MinHeap.

min_heap = ::Containers::MinHeap.new(10.times.map{Object.new}){|x, y| (x <=> y) == -1}

while val = min_heap.pop do
  p val
end

shiduanguang avatar Jul 22 '16 08:07 shiduanguang

This Heap is slower very much than Ruby's built in Array sorting. I'm using Ruby 2.2.4.

shiduanguang avatar Jul 27 '16 04:07 shiduanguang