# Function to check if a value is contained in a MutableBinaryHeap

**URL:** <https://discourse.julialang.org/t/function-to-check-if-a-value-is-contained-in-a-mutablebinaryheap/82159>\
**Category:** General Usage\
**Created:** [June 2, 2022, 9:52pm UTC](https://discourse.julialang.org/t/function-to-check-if-a-value-is-contained-in-a-mutablebinaryheap/82159 "2022-06-02T21:52:41Z")\
**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 2, 2022, 9:52pm UTC](https://discourse.julialang.org/t/function-to-check-if-a-value-is-contained-in-a-mutablebinaryheap/82159/1 "2022-06-02T21:52:42Z")

</div>

Hello everyone.

Yesterday I posted this question

> [@Checking if a value is contained in a MutableBinaryHeap?](https://discourse.julialang.org/t/checking-if-a-value-is-contained-in-a-mutablebinaryheap/82069):
>
> Hello guys, I need to test if a value is contained in a MutableBinaryHeap. I may have the following Tuple t: 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: 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 MutableBinaryHeap((1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf)) and doing c=first.(h) giv…

I found a solution that I would like to share, given an input heap (minhp) and a value that I want to look for (node):

```julia
function searchhp(minhp,node)
    #this function tests if a node is contained in a heap
    b=extract_all!(minhp)
    c=first.(b)
    d=node in b
    minhp=MutableBinaryHeap(Base.By(last),b)
    return d,minhp
end

```

Basically, what this is doing is turning the heap into a tuple, searching the value, and then transforming back the tuple into the heap.

I worries me that going back and forth from tuple to heap may be computationally expensive for big heaps, but is the only solution that I could find.

If anyone has any idea on a better way to test if a value is contained in a MutableBinaryHeap, I would really appreciate it.

Regards

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 2, 2022, 11:38pm UTC](https://discourse.julialang.org/t/function-to-check-if-a-value-is-contained-in-a-mutablebinaryheap/82159/2 "2022-06-02T23:38:08Z")

</div>

First, you could have posted in that same thread, instead of creating a new one.

Second, if you often need to check if a heap contain a value, then a heap is probably not the most adequate data structure, I recommend using another one, like an [`OrderedDict`](https://juliacollections.github.io/DataStructures.jl/stable/ordered_containers/) or my own `TrackingHeap` (see the fourth point below).

Third, yes, this is horrendous for performance, I really do not recommend doing it. At this point you would be better with a simple unordered `Vector` and searching it with `findmin` and `findfirst`. You should also not be creating a `b` just to use `in`, use `findfirst(x -> first(x) == node, b) !== nothing` instead.

Fourth, if you need to use a heap, I recommend my own competing solution [`TrackingHeaps.jl`](https://github.com/henriquebecker91/TrackingHeaps.jl). It conforms to the `Dict` interface, and using it you could just do `findfirst(x -> first(x) == node, values(minhp)) !== nothing` to do a linear search without any performance penalty (`values` does not copy, so you are just directly accessing the `Vector` with the values).

---

<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 6, 2022, 4:48pm UTC](https://discourse.julialang.org/t/function-to-check-if-a-value-is-contained-in-a-mutablebinaryheap/82159/3 "2022-06-06T16:48:26Z")

</div>

Hi Henrique, thank you for taking the time to reply.  
Your feedback is highly appreciated. I will definitely try TrackingHeaps.jl

Thanks.
