# Checking if a value is contained in a MutableBinaryHeap?

**URL:** <https://discourse.julialang.org/t/checking-if-a-value-is-contained-in-a-mutablebinaryheap/82069>\
**Category:** General Usage\
**Created:** [June 1, 2022, 6:21pm UTC](https://discourse.julialang.org/t/checking-if-a-value-is-contained-in-a-mutablebinaryheap/82069 "2022-06-01T18:21:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [June 1, 2022, 6:21pm UTC](https://discourse.julialang.org/t/checking-if-a-value-is-contained-in-a-mutablebinaryheap/82069/1 "2022-06-01T18:21:34Z")

</div>

Hello guys, I need to test if a value is contained in a MutableBinaryHeap.

I may have the following Tuple t:

```julia
6-element Vector{Tuple{Int64, Float64}}:
 (1, Inf)
 (2, Inf)
 (3, Inf)
 (4, Inf)
 (5, Inf)
 (6, Inf)

```

For the Tuple case I can just do:

```julia
a=first.(t)

```

And check whether a value is contained in a.  
The problem is that I need to do this test on a MutableBinaryHeap.

I have the heap h

```julia
MutableBinaryHeap((1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf))

```

and doing

```julia
c=first.(h)

```

gives the following error:

```julia
ERROR: MethodError: no method matching iterate(::MutableBinaryHeap{Tuple{Int64, Float64}, Base.Order.By{typeof(last), Base.Order.ForwardOrdering}})

```

Any idea on how to test is a value is contained on the first element of a MutableBinaryHeap?

Thank you so much in advance.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 19, 2023, 1:14pm UTC](https://discourse.julialang.org/t/checking-if-a-value-is-contained-in-a-mutablebinaryheap/82069/2 "2023-02-19T13:14:31Z")

</div>

Waiting for a more specific answer you could try this workaround

```julia
a=[(1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf)]

mbh=MutableBinaryMinHeap(a)

findfirst(t->first(t)==(2),[pop!(mbh) for _ in 1:length(mbh)])

#or

searchsorted([pop!(mbh) for _ in 1:length(mbh)],2; by=first)

```

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 19, 2023, 4:28pm UTC](https://discourse.julialang.org/t/checking-if-a-value-is-contained-in-a-mutablebinaryheap/82069/3 "2023-02-19T16:28:37Z")

</div>

Would this work for your case? (I wouldn’t usually go for directly accessing fields of structs defined in other packages, as the implementation might change. But it seems unlikely that the implementation in this case will change and it’s a pretty compact solution in my opinion)

```julia
using DataStructures

heap = MutableBinaryMinHeap([(1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf)])
get_value(node) = node.value

first.(get_value.(heap.nodes))

# gives
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6

```

The corresponding structs are defined here

> <https://github.com/JuliaCollections/DataStructures.jl/blob/334f840d2bcde16e1cbafab194993d06ea8519d9/src/heaps/mutable_binary_heap.jl#L159>
