# 'BinaryMaxHeap' of tuples ordered on second element?

**URL:** <https://discourse.julialang.org/t/binarymaxheap-of-tuples-ordered-on-second-element/70524>\
**Category:** General Usage\
**Tags:** data\_structures\
**Created:** [October 27, 2021, 11:42pm UTC](https://discourse.julialang.org/t/binarymaxheap-of-tuples-ordered-on-second-element/70524 "2021-10-27T23:42:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [October 27, 2021, 11:42pm UTC](https://discourse.julialang.org/t/binarymaxheap-of-tuples-ordered-on-second-element/70524/1 "2021-10-27T23:42:51Z")

</div>

I have some Float64 data:

```julia
julia> data = rand(10)
10-element Vector{Float64}:
 0.7812532624261221
 0.21171331164805607
 0.17972757757421154
 0.18741269983325282
 0.42157861540422026
 0.7922059340808225
 0.258799529427459
 0.5883959190694283
 0.13260864460551547
 0.47544080823605883

```

I can put this data into a `BinaryMaxHeap` as follows:

```julia
julia> using DataStructures

julia> binmaxheap = BinaryMaxHeap(data)
BinaryMaxHeap{Float64}(Base.Order.ReverseOrdering{Base.Order.ForwardOrdering}(Base.Order.ForwardOrdering()), [0.7922059340808225, 0.5883959190694283, 0.7812532624261221, 0.21171331164805607, 0.47544080823605883, 0.17972757757421154, 0.258799529427459, 0.18741269983325282, 0.13260864460551547, 0.42157861540422026])

julia> first(binmaxheap)
0.7922059340808225

```

Now suppose I want to preserve the indices in `data` in my heap. According to the [DataStructures.jl docs](https://juliacollections.github.io/DataStructures.jl/latest/heaps/#Using-alternate-orderings-1) I can create a generic `BinaryHeap` that does this as follows:

```julia
julia> heapwithindex = BinaryHeap(Base.By(last), collect(enumerate(data)))
BinaryHeap{Tuple{Int64, Float64}, Base.Order.By{typeof(last), Base.Order.ForwardOrdering}}(Base.Order.By{typeof(last), Base.Order.ForwardOrdering}(last, Base.Order.ForwardOrdering()), [(9, 0.13260864460551547), (4, 0.18741269983325282), (3, 0.17972757757421154), (2, 0.21171331164805607), (5, 0.42157861540422026), (6, 0.7922059340808225), (7, 0.258799529427459), (8, 0.5883959190694283), (1, 0.7812532624261221), (10, 0.47544080823605883)])

julia> first(heapwithindex)
(9, 0.13260864460551547)

julia> ans[1] == argmin(data)
true

```

However, this is a min heap, not a max heap as required.

**How can I modify the call to `BinaryHeap()` to create a max heap? How about a max-min heap?**

- Calling `BinaryMaxHeap(Base.By(last), data)` produces an error.

Furthermore, **how can I create an _empty_ (min or max) heap ordered on the second entry of the tuple?**

- Calling `BinaryHeap(Base.By(last))` without the second argument produces an error (as it should, because there is no input data to show that the entries will be `Tuple{Int64,Float64}}`s).

- Calling `BinaryMinHeap{Tuple{Int64, Float64}}(Base.By(last))` also errors.

- My current workaround for an empty such min heap is `h = BinaryHeap(Base.By(last), [(1, 0.0)]); extract_all!(h)`.
