Class: Algorithms::Containers::Heap
- Inherits:
-
Object
- Object
- Algorithms::Containers::Heap
- Includes:
- Enumerable
- Defined in:
- lib/containers/heap.rb
Defined Under Namespace
Classes: Node
Instance Method Summary collapse
-
#change_key(key, new_key, delete = false) ⇒ Object
call-seq: change_key(key, new_key) -> [new_key, value] change_key(key, new_key) -> nil.
-
#clear ⇒ Object
call-seq: clear -> nil.
-
#delete(key) ⇒ Object
call-seq: delete(key) -> value delete(key) -> nil.
-
#empty? ⇒ Boolean
call-seq: empty? -> true or false.
-
#has_key?(key) ⇒ Boolean
call-seq: has_key?(key) -> true or false.
-
#initialize(ary = [], &block) ⇒ Heap
constructor
call-seq: Heap.new(optional_array) { |x, y| optional_comparison_fn } -> new_heap.
-
#merge!(otherheap) ⇒ Object
call-seq: merge!(otherheap) -> merged_heap.
-
#next ⇒ Object
call-seq: next -> value next -> nil.
-
#pop ⇒ Object
(also: #next!)
call-seq: pop -> value pop -> nil.
-
#push(key, value = key) ⇒ Object
(also: #<<)
call-seq: push(key, value) -> value push(value) -> value.
-
#size ⇒ Object
(also: #length)
call-seq: size -> int.
Constructor Details
#initialize(ary = [], &block) ⇒ Heap
call-seq:
Heap.new(optional_array) { |x, y| optional_comparison_fn }
-> new_heap
If an optional array is passed, the entries in the array are inserted into the heap with equal key and value fields. Also, an optional block can be passed to define the function that maintains heap property. For example, a min-heap can be created with:
minheap = Heap.new { |x, y| (x <=> y) == -1 }
minheap.push(6)
minheap.push(10)
minheap.pop #=> 6
Thus, smaller elements will be parent nodes. The heap defaults to a min-heap if no block is given.
44 45 46 47 48 49 50 51 |
# File 'lib/containers/heap.rb', line 44 def initialize(ary=[], &block) @compare_fn = block || lambda { |x, y| (x <=> y) == -1 } @next = nil @size = 0 @stored = {} ary.each { |n| push(n) } unless ary.empty? end |
Instance Method Details
#change_key(key, new_key, delete = false) ⇒ Object
call-seq:
change_key(key, new_key) -> [new_key, value]
change_key(key, new_key) -> nil
Changes the key from one to another. Doing so must not violate the heap property or an exception will be raised. If the key is found, an array containing the new key and value pair is returned, otherwise nil is returned.
In the case of duplicate keys, an arbitrary key is changed. This will be investigated more in the future.
Complexity: amortized O(1)
minheap = MinHeap.new([1, 2])
minheap.change_key(2, 3) #=> raise error since we can't increase the value in a min-heap
minheap.change_key(2, 0) #=> [0, 2]
minheap.pop #=> 2
minheap.pop #=> 1
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/containers/heap.rb', line 256 def change_key(key, new_key, delete=false) return if @stored[key].nil? || @stored[key].empty? || (key == new_key) # Must maintain heap property raise "Changing this key would not maintain heap property!" unless (delete || @compare_fn[new_key, key]) node = @stored[key].shift if node node.key = new_key @stored[new_key] ||= [] @stored[new_key] << node parent = node.parent if parent # if heap property is violated if delete || @compare_fn[new_key, parent.key] cut(node, parent) cascading_cut(parent) end end if delete || @compare_fn[node.key, @next.key] @next = node end return [node.key, node.value] end nil end |
#clear ⇒ Object
call-seq:
clear -> nil
Removes all elements from the heap, destructively.
Complexity: O(1)
134 135 136 137 138 139 |
# File 'lib/containers/heap.rb', line 134 def clear @next = nil @size = 0 @stored = {} nil end |
#delete(key) ⇒ Object
call-seq:
delete(key) -> value
delete(key) -> nil
Deletes the item with associated key and returns it. nil is returned if the keyis not found. In the case of nodes with duplicate keys, an arbitrary one is deleted.
Complexity: amortized O(log n)
minheap = MinHeap.new([1, 2])
minheap.delete(1) #=> 1
minheap.size #=> 1
295 296 297 |
# File 'lib/containers/heap.rb', line 295 def delete(key) pop if change_key(key, nil, true) end |
#empty? ⇒ Boolean
call-seq:
empty? -> true or false
Returns true if the heap is empty, false otherwise.
145 146 147 |
# File 'lib/containers/heap.rb', line 145 def empty? @next.nil? end |
#has_key?(key) ⇒ Boolean
107 108 109 |
# File 'lib/containers/heap.rb', line 107 def has_key?(key) @stored[key] && !@stored[key].empty? ? true : false end |
#merge!(otherheap) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/containers/heap.rb', line 161 def merge!(otherheap) raise ArgumentError, "Trying to merge a heap with something not a heap" unless otherheap.kind_of? Heap other_root = otherheap.instance_variable_get("@next") if other_root @stored = @stored.merge(otherheap.instance_variable_get("@stored")) { |key, a, b| (a << b).flatten } # Insert othernode's @next node to the left of current @next @next.left.right = other_root ol = other_root.left other_root.left = @next.left ol.right = @next @next.left = ol @next = other_root if @compare_fn[other_root.key, @next.key] end @size += otherheap.size end |
#next ⇒ Object
123 124 125 |
# File 'lib/containers/heap.rb', line 123 def next @next && @next.value end |
#pop ⇒ Object Also known as: next!
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/containers/heap.rb', line 190 def pop return nil unless @next popped = @next if @size == 1 clear return popped.value end # Merge the popped's children into root node if @next.child @next.child.parent = nil # get rid of parent sibling = @next.child.right until sibling == @next.child sibling.parent = nil sibling = sibling.right end # Merge the children into the root. If @next is the only root node, # make its child the @next node if @next.right == @next @next = @next.child else next_left, next_right = @next.left, @next.right current_child = @next.child @next.right.left = current_child @next.left.right = current_child.right current_child.right.left = next_left current_child.right = next_right @next = @next.right end else @next.left.right = @next.right @next.right.left = @next.left @next = @next.right end consolidate unless @stored[popped.key].delete(popped) raise "Couldn't delete node from stored nodes hash" end @size -= 1 popped.value end |
#push(key, value = key) ⇒ Object Also known as: <<
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/containers/heap.rb', line 67 def push(key, value=key) raise ArgumentError, "Heap keys must not be nil." unless key node = Node.new(key, value) # Add new node to the left of the @next node if @next node.right = @next node.left = @next.left node.left.right = node @next.left = node if @compare_fn[key, @next.key] @next = node end else @next = node end @size += 1 arr = [] w = @next.right until w == @next do arr << w.value w = w.right end arr << @next.value @stored[key] ||= [] @stored[key] << node value end |
#size ⇒ Object Also known as: length
call-seq:
size -> int
Return the number of elements in the heap.
23 24 25 |
# File 'lib/containers/heap.rb', line 23 def size @size end |