Unable to run an example code using DataStructures.jl

I am trying to run a simple example code from the DataStructures.jl package but I get an error.

data = collect(enumerate(["foo", "bar", "baz"]))

h1 = BinaryHeap(data) # Standard lexicographic ordering for tuples
first(h1)             # => (1, "foo")

h2 = BinaryHeap(Base.By(last), data) # Order by 2nd element only
first(h2)                            # => (2, "bar")

ERROR: MethodError: no method matching BinaryHeap(::Vector{Tuple{Int64, String}})
Closest candidates are:
  BinaryHeap(::Base.Order.Ordering, ::AbstractVector{T}) where T at ~/.julia/packages/DataStructures/59MD0/src/heaps/binary_heap.jl:47
 [1] top-level scope
   @ ~/Desktop/ShortestPath/heaptest.jl:4

I have been checking online, some people solved the problem by renaming the environment, others had to update the packages. But I still cannot make this work. If anyone has an idea I will be really grateful, I wrote a code that was working perfectly were the main line is precisely a BinaryHeap.


1 Like

It seems, that the default method without ordering is removed:

julia> methods(BinaryHeap)
# 1 method for type constructor:
[1] BinaryHeap(ordering::Base.Order.Ordering, xs::AbstractVector{T}) where T in DataStructures at C:\Users\Oli\.julia\packages\DataStructures\59MD0\src\heaps\binary_heap.jl:47

So you may work around with the default ordering give explicitly:

h1 = BinaryHeap(Base.Order.ForwardOrdering(),data)

I didn’t check the package change history or issues for more information about it, but either it’s a bug so that the default method doesn’t appear or it is by purpose than the documentation needs to be updated at this example. There is an open issue already: Bug when using Base.By(last) in a BinaryHeap · Issue #828 · JuliaCollections/DataStructures.jl · GitHub (by you, well done!).