class BinaryHeap(T)
- BinaryHeap(T)
- Reference
- Object
Included Modules
- Enumerable(T)
- Iterable(T)
Defined in:
datastructure/binary_heap.crConstructors
-
.new(enumerable : Enumerable(T))
Creates a new heap from the elements in enumerable.
-
.new(initial_capacity : Int = 0)
Creates a new empty heap backed by a buffer that is initially initial_capacity big (default:
0
). -
.new(enumerable : Enumerable(T), &block : T, T -> Int32?)
Creates a new empty heap with the custom comperator.
-
.new(initial_capacity : Int = 0, &block : T, T -> Int32?)
Creates a new empty heap with the custom comperator.
Instance Method Summary
-
#<<(object : T) : self
Adds object to the heap and returns
self
. -
#==(other : BinaryHeap(T)) : Bool
Returns true if both heap have the same elements.
-
#add(object : T) : self
Adds object to the heap and returns
self
. -
#clear : self
Removes all elements from the heap and returns
self
. -
#clone
Returns a copy of
self
with all instance variables cloned. -
#each
Returns an iterator for each element of the heap.
-
#each(&) : Nil
Yields each element of the heap, and returns
nil
. -
#empty? : Bool
Returns
true
ifself
is empty,false
otherwise. -
#inspect(io : IO) : Nil
Writes a string representation of the heap to
io
. -
#pop(n : Int) : Array(T)
Removes the last n values from
self
ahd returns the removed values. -
#pop : T
Removes the lowest value from
self
and returns the removed value. -
#pop(&)
Removes the lowest value from
self
and returns the removed value. -
#pop? : T?
Like
#pop
, but returnsnil
ifself
is empty. -
#size : Int32
Returns the number of elements in the heap.
-
#sort : Array(T)
Returns a new array with all elements sorted.
-
#to_a : Array(T)
Returns the elements as an Array.
-
#to_s(io : IO) : Nil
Writes a string representation of the heap to
io
. -
#top : T
Returns the lowest value in the
self
. -
#top(&)
Returns the lowest value in the
self
. -
#top? : T?
Returns the lowest value in the
self
.
Instance methods inherited from module Enumerable(T)
accumulate(init : U) : Array(U) forall Uaccumulate : Array(T)
accumulate(init : U, &block : U, T -> U) : Array(U) forall U
accumulate(&block : T, T -> T) : Array(T) accumulate, mex : T mex, mex_sorted : T mex_sorted, tally(*, default : Int32) : Hash(T, Int32) tally, unique : self
unique(&) : self unique
Constructor Detail
Creates a new heap from the elements in enumerable.
a = BinaryHeap.new [3, 1, 2]
a.pop # => 1
a.pop # => 2
a.pop # => 3
Creates a new empty heap backed by a buffer that is initially initial_capacity big (default: 0
).
a = BinaryHeap.new(3)
a << 3 << 1 << 2
a.pop # => 1
a.pop # => 2
a.pop # => 3
Creates a new empty heap with the custom comperator.
The block must implement a comparison between two elements a and b, where a < b
returns -1
,
a == b
returns 0
, and a > b
returns 1
. The comparison operator #<=>
can be used for this.
a = BinaryHeap.new [3, 1, 2]
a.pop # => 1
b = BinaryHeap.new [3, 1, 2] { |a, b| b <=> a }
b.pop # => 3
Creates a new empty heap with the custom comperator.
The block must implement a comparison between two elements a and b, where a < b
returns -1
,
a == b
returns 0
, and a > b
returns 1
. The comparison operator #<=>
can be used for this.
a = BinaryHeap.new [3, 1, 2]
a.pop # => 1
b = BinaryHeap.new [3, 1, 2] { |a, b| b <=> a }
b.pop # => 3
Instance Method Detail
Writes a string representation of the heap to io
.
BinaryHeap{1, 2}.inspect # => "BinaryHeap{1, 2}"
Removes the last n values from self
ahd returns the removed values.
Removes the lowest value from self
and returns the removed value.
Raises IndexError
if heap is of 0 size.
Removes the lowest value from self
and returns the removed value.
If the array is empty, the given block is called.
Returns a new array with all elements sorted.
a = BinaryHeap.new [3, 1, 2]
a.sort # => [1, 2, 3]
b = BinaryHeap.new [3, 1, 2] { |a, b| b <=> a }
b.sort # => [3, 2, 1]
Returns the elements as an Array.
BinaryHeap{3, 1, 2}.to_a # => [1, 3, 2]
Writes a string representation of the heap to io
.
BinaryHeap{1, 2}.to_s # => "BinaryHeap{1, 2}"
Returns the lowest value in the self
.
If the self
is empty, calls the block and returns its value.