Repository: JuliaCollections/DataStructures.jl
Pull request #470 (2018-12-20 20:25:51)

fix identity map
MWE: when there is a `LinkedList{Any}`, the following will error:

```julia
ex = :(a+b+2 * 2 + foo(2))
tl = list(ex.args...)
map(x->x, tl)
```

This PR will fix it, and will allow more generic (each node has different generic types) linked list.

codecov[bot]: # [Codecov](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=h1) Report
> Merging [#470](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=desc) into [master](https://codecov.io/gh/JuliaCollections/DataStructures.jl/commit/31044efe5130b8d1980e2b6650337da08f27274e?src=pr&el=desc) will **decrease** coverage by `31.54%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/graphs/tree.svg?width=650&token=7FHvoCSQ7k&height=150&src=pr)](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           master     #470       +/-   ##
===========================================
- Coverage    98.8%   67.26%   -31.55%     
===========================================
  Files          27       27               
  Lines        1336     1964      +628     
===========================================
+ Hits         1320     1321        +1     
- Misses         16      643      +627
```


| [Impacted Files](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/list.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2xpc3Quamw=) | `93.84% <100%> (-6.16%)` | :arrow_down: |
| [src/container\_loops.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2NvbnRhaW5lcl9sb29wcy5qbA==) | `35.1% <0%> (-64.9%)` | :arrow_down: |
| [src/balanced\_tree.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2JhbGFuY2VkX3RyZWUuamw=) | `45.08% <0%> (-54.92%)` | :arrow_down: |
| [src/delegate.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2RlbGVnYXRlLmps) | `50% <0%> (-50%)` | :arrow_down: |
| [src/dict\_support.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2RpY3Rfc3VwcG9ydC5qbA==) | `50% <0%> (-50%)` | :arrow_down: |
| [src/heaps/mutable\_binary\_heap.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2hlYXBzL211dGFibGVfYmluYXJ5X2hlYXAuamw=) | `50% <0%> (-50%)` | :arrow_down: |
| [src/deque.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2RlcXVlLmps) | `57.35% <0%> (-42.65%)` | :arrow_down: |
| [src/tokens2.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL3Rva2VuczIuamw=) | `64.1% <0%> (-35.9%)` | :arrow_down: |
| [src/sorted\_set.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL3NvcnRlZF9zZXQuamw=) | `65.24% <0%> (-34.76%)` | :arrow_down: |
| [src/heaps/binary\_heap.jl](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree#diff-c3JjL2hlYXBzL2JpbmFyeV9oZWFwLmps) | `68.25% <0%> (-31.75%)` | :arrow_down: |
| ... and [15 more](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=footer). Last update [31044ef...160455a](https://codecov.io/gh/JuliaCollections/DataStructures.jl/pull/470?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/Project.toml b/Project.toml
index 6db8c39..14c06b6 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "DataStructures"
 uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
-version = "0.12.0"
+version = "0.14.0"
 
 [deps]
 InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
diff --git a/docs/src/heaps.md b/docs/src/heaps.md
index 53d57a8..d603a91 100644
--- a/docs/src/heaps.md
+++ b/docs/src/heaps.md
@@ -51,6 +51,39 @@ h = mutable_binary_minheap([1,4,3,2])
 h = mutable_binary_maxheap([1,4,3,2])    # create a mutable min/max heap from a vector
 ```
 
+## Min-max heaps
+Min-max heaps maintain the minimum _and_ the maximum of a set, 
+allowing both to be retrieved in constant (`O(1)`) time.
+The min-max heaps in this package are subtypes of `AbstractMinMaxHeap <: AbstractHeap`
+and have the same interface as other heaps with the following additions:
+```julia
+# Let h be a min-max heap, k an integer
+minimum(h)     # return the smallest element
+maximum(h)     # return the largest element
+
+popmin!(h)     # remove and return the smallest element
+popmin!(h, k)  # remove and return the smallest k elements
+
+popmax!(h)     # remove and return the largest element
+popmax!(h, k)  # remove and return the largest k elements
+
+popall!(h)     # remove and return all the elements, sorted smallest to largest
+popall!(h, o)  # remove and return all the elements according to ordering o
+```
+The usual `top(h)` and `pop!(h)` are defined to be `minimum(h)` and `popmin!(h)`, 
+respectively.
+
+This package includes an implementation of a binary min-max heap (`BinaryMinMaxHeap`).
+> Atkinson, M.D., Sack, J., Santoro, N., & Strothotte, T. (1986). Min-Max > Heaps and Generalized Priority Queues. Commun. ACM, 29, 996-1000.
+> doi: [10.1145/6617.6621](https://doi.org/10.1145/6617.6621)
+
+Examples:
+```julia
+h = BinaryMinMaxHeap{Int}()          # create an empty min-max heap with integer values
+
+h = BinaryMinMaxHeap([1, 2, 3, 4]) # create a min-max heap from a vector
+```
+
 # Functions using heaps
 
 Heaps can be used to extract the largest or smallest elements of an
diff --git a/src/DataStructures.jl b/src/DataStructures.jl
index ea49a4f..c3436f3 100644
--- a/src/DataStructures.jl
+++ b/src/DataStructures.jl
@@ -13,7 +13,7 @@ module DataStructures
                  isless,
                  union, intersect, symdiff, setdiff, issubset,
                  searchsortedfirst, searchsortedlast, in,
-                 eachindex, keytype, valtype
+                 eachindex, keytype, valtype, minimum, maximum
     import Base: iterate
 
     using OrderedCollections
@@ -36,6 +36,7 @@ module DataStructures
     export BinaryHeap, binary_minheap, binary_maxheap, nlargest, nsmallest
     export MutableBinaryHeap, mutable_binary_minheap, mutable_binary_maxheap
     export heapify!, heapify, heappop!, heappush!, isheap
+    export BinaryMinMaxHeap, popmin!, popmax!, popall!
 
     export OrderedDict, OrderedSet
     export DefaultDict, DefaultOrderedDict
diff --git a/src/heaps.jl b/src/heaps.jl
index 4d6ca93..a1bcec5 100644
--- a/src/heaps.jl
+++ b/src/heaps.jl
@@ -50,6 +50,8 @@ abstract type AbstractHeap{VT} end
 
 abstract type AbstractMutableHeap{VT,HT} <: AbstractHeap{VT} end
 
+abstract type AbstractMinMaxHeap{VT} <: AbstractHeap{VT} end
+
 # comparer
 
 struct LessThan
@@ -66,6 +68,7 @@ compare(c::GreaterThan, x, y) = x > y
 include("heaps/binary_heap.jl")
 include("heaps/mutable_binary_heap.jl")
 include("heaps/arrays_as_heaps.jl")
+include("heaps/minmax_heap.jl")
 
 # generic functions
 
diff --git a/src/heaps/minmax_heap.jl b/src/heaps/minmax_heap.jl
new file mode 100644
index 0000000..17b0d77
--- /dev/null
+++ b/src/heaps/minmax_heap.jl
@@ -0,0 +1,277 @@
+################################################
+#
+# minmax heap type and constructors
+#
+################################################
+
+mutable struct BinaryMinMaxHeap{T} <: AbstractMinMaxHeap{T}
+    valtree::Vector{T}
+    
+    BinaryMinMaxHeap{T}() where {T} = new{T}(Vector{T}())
+    
+    function BinaryMinMaxHeap(xs::AbstractVector{T}) where {T}
+        valtree = _make_binary_minmax_heap(xs)
+        new{T}(valtree)
+    end
+end
+
+################################################
+#
+# core implementation
+#
+################################################
+
+function _make_binary_minmax_heap(xs)
+    valtree = copy(xs)
+    for i in length(xs):-1:1
+        _minmax_heap_trickle_down!(valtree, i)
+    end
+    valtree
+end
+
+function _minmax_heap_bubble_up!(A::AbstractVector, i::Integer)
+    if on_minlevel(i)
+        if i > 1 && A[i] > A[hparent(i)]
+            # swap to parent and bubble up max
+            tmp = A[i]
+            A[i] = A[hparent(i)]
+            A[hparent(i)] = tmp
+            _minmax_heap_bubble_up!(A, hparent(i), Reverse)
+        else
+            # bubble up min
+            _minmax_heap_bubble_up!(A, i, Forward)
+        end
+        
+    else
+        # max level
+        if i > 1 && A[i] < A[hparent(i)]
+            # swap to parent and bubble up min
+            tmp = A[i]
+            A[i] = A[hparent(i)]
+            A[hparent(i)] = tmp
+            _minmax_heap_bubble_up!(A, hparent(i), Forward)
+        else
+            # bubble up max
+            _minmax_heap_bubble_up!(A, i, Reverse)
+        end
+    end
+   return 
+end
+
+function _minmax_heap_bubble_up!(A::AbstractVector, i::Integer, o::Ordering, x=A[i])
+    if hasgrandparent(i)
+        gparent = hparent(hparent(i))
+        if lt(o, x, A[gparent])
+            A[i] = A[gparent]
+            A[gparent] = x
+            _minmax_heap_bubble_up!(A, gparent, o)
+        end
+    end
+    return          
+end
+
+function _minmax_heap_trickle_down!(A::AbstractVector, i::Integer)
+    if on_minlevel(i)
+        _minmax_heap_trickle_down!(A, i, Forward)
+    else
+        _minmax_heap_trickle_down!(A, i, Reverse)
+    end
+    return
+end
+
+function _minmax_heap_trickle_down!(A::AbstractVector, i::Integer, o::Ordering, x=A[i])
+                    
+    if haschildren(i, A)
+        # get the index of the extremum (min or max) descendant
+        extremum = o === Forward ? minimum : maximum
+        _, m = extremum((A[j], j) for j in children_and_grandchildren(length(A), i))
+
+        if isgrandchild(m, i)
+            if lt(o, A[m], A[i])
+                A[i] = A[m]
+                A[m] = x
+                if lt(o, A[hparent(m)], A[m])
+                    t = A[m]
+                    A[m] = A[hparent(m)]
+                    A[hparent(m)] = t
+                end
+                _minmax_heap_trickle_down!(A, m, o)
+            end
+        else
+            if lt(o, A[m], A[i])
+                A[i] = A[m]
+                A[m] = x
+            end
+        end
+    end
+    return
+end
+                    
+################################################
+#
+# utilities
+#
+################################################
+
+@inline level(i) = floor(Int, log2(i))
+@inline lchild(i) = 2*i
+@inline rchild(i) = 2*i+1
+@inline children(i) = (lchild(i), rchild(i))
+@inline hparent(i) = i ÷ 2
+@inline on_minlevel(i) = level(i) % 2 == 0 
+@inline haschildren(i, A) = lchild(i) ≤ length(A)
+@inline isgrandchild(j, i) = j > rchild(i)
+@inline hasgrandparent(i) = i ≥ 4 
+
+"""
+    children_and_grandchildren(maxlen, i)
+
+Return the indices of all children and grandchildren of
+position `i`.
+"""
+function children_and_grandchildren(maxlen::T, i::T) where {T <: Integer}
+    _children_and_grandchildren = T[]
+    for child in children(i)
+        for desc in (child, lchild(child), rchild(child))
+            if desc ≤ maxlen
+                push!(_children_and_grandchildren, desc)
+            end
+        end
+    end
+    return _children_and_grandchildren
+end
+
+"""
+    is_minmax_heap(h::AbstractVector) -> Bool
+
+Return `true` if `A` is a min-max heap. A min-max heap is a
+heap where the minimum element is the root and the maximum
+element is a child of the root.
+"""
+function is_minmax_heap(A::AbstractVector)
+
+    for i in 1:length(A)
+        if on_minlevel(i)
+            # check that A[i] < children A[i]
+            #    and grandchildren A[i]
+            for j in children_and_grandchildren(length(A), i)
+                A[i] ≤ A[j] || return false
+            end
+        else
+            # max layer
+            for j in children_and_grandchildren(length(A), i)
+                A[i] ≥ A[j] || return false
+            end
+        end
+    end
+    return true
+end
+
+################################################
+#
+# interfaces
+#
+################################################
+
+length(h::BinaryMinMaxHeap) = length(h.valtree)
+
+isempty(h::BinaryMinMaxHeap) = isempty(h.valtree)
+
+"""
+    pop!(h::BinaryMinMaxHeap) = popmin!(h)
+"""
+@inline pop!(h::BinaryMinMaxHeap) = popmin!(h)
+
+"""
+    popmin!(h::BinaryMinMaxHeap) -> min
+                        
+Remove the minimum value from the heap.
+"""
+function popmin!(h::BinaryMinMaxHeap)
+    valtree = h.valtree
+    !isempty(valtree) || throw(ArgumentError("heap must be non-empty"))
+    x = valtree[1]
+    y = pop!(valtree)
+    if !isempty(valtree)
+        valtree[1] = y
+        _minmax_heap_trickle_down!(valtree, 1)
+    end
+    return x
+end
+
+                        
+"""
+    popmin!(h::BinaryMinMaxHeap, k::Integer) -> vals
+                        
+Remove up to the `k` smallest values from the heap.
+"""
+@inline function popmin!(h::BinaryMinMaxHeap, k::Integer) 
+    return [popmin!(h) for _ in 1:min(length(h), k)]
+end
+
+"""
+    popmax!(h::BinaryMinMaxHeap) -> max
+                        
+Remove the maximum value from the heap.
+"""
+function popmax!(h::BinaryMinMaxHeap)
+    valtree = h.valtree
+    !isempty(valtree) || throw(ArgumentError("heap must be non-empty"))
+    @inbounds x, i = maximum(((valtree[j], j) for j in 1:min(length(valtree), 3)))
+    y = pop!(valtree)
+    if !isempty(valtree) && i <= length(valtree)
+        @inbounds valtree[i] = y
+        _minmax_heap_trickle_down!(valtree, i)
+    end
+    return x    
+end
+
+"""
+    popmax!(h::BinaryMinMaxHeap, k::Integer) -> vals
+                        
+Remove up to the `k` largest values from the heap.
+"""
+@inline function popmax!(h::BinaryMinMaxHeap, k::Integer) 
+    return [popmax!(h) for _ in 1:min(length(h), k)]                    
+end
+                        
+                        
+function push!(h::BinaryMinMaxHeap, v)
+    valtree = h.valtree        
+    push!(valtree, v)
+    _minmax_heap_bubble_up!(valtree, length(valtree))
+end
+
+"""
+    top(h::BinaryMinMaxHeap)
+                        
+Get the top (minimum) of the heap.
+"""
+@inline top(h::BinaryMinMaxHeap) = minimum(h)
+
+@inline function minimum(h::BinaryMinMaxHeap) 
+    valtree = h.valtree
+    !isempty(h) || throw(ArgumentError("heap must be non-empty"))
+    return @inbounds h.valtree[1]
+end
+
+@inline function maximum(h::BinaryMinMaxHeap) 
+    valtree = h.valtree
+    !isempty(h) || throw(ArgumentError("heap must be non-empty"))
+    return @inbounds maximum(valtree[1:min(end, 3)])
+end
+                        
+empty!(h::BinaryMinMaxHeap) = (empty!(h.valtree); h)
+                        
+
+"""
+    popall!(h::BinaryMinMaxHeap, ::Ordering = Forward)
+                        
+Remove and return all the elements of `h` according to
+the given ordering. Default is `Forward` (smallest to 
+largest).
+"""
+popall!(h::BinaryMinMaxHeap) = popall!(h, Forward)
+popall!(h::BinaryMinMaxHeap, ::ForwardOrdering) = popmin!(h, length(h))
+popall!(h::BinaryMinMaxHeap, ::ReverseOrdering) = popmax!(h, length(h))
+
diff --git a/src/list.jl b/src/list.jl
index d35e082..ff3b294 100644
--- a/src/list.jl
+++ b/src/list.jl
@@ -67,9 +67,9 @@ end
 
 map(f::Base.Callable, l::Nil) = l
 
-function map(f::Base.Callable, l::Cons)
+function map(f::Base.Callable, l::Cons{T}) where T
     first = f(l.head)
-    l2 = cons(first, nil(typeof(first)))
+    l2 = cons(first, nil(typeof(first) <: T ? T : typeof(first)))
     for h in l.tail
         l2 = cons(f(h), l2)
     end


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index c18f267..daf11c8 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -18,6 +18,7 @@ tests = ["int_set",
          "disjoint_set",
          "binheap",
          "mutable_binheap",
+         "minmax_heap",
          "default_dict",
          "trie",
          "list",
diff --git a/test/test_list.jl b/test/test_list.jl
index 696cab2..2f28d77 100644
--- a/test/test_list.jl
+++ b/test/test_list.jl
@@ -103,4 +103,10 @@
         @test collect(l10) == [2; 4; 5.6; 10.5]
     end
 
+    @testset "test identity map" begin
+        ex = :(a+b+2 * 2 + foo(2))
+        l11 = list(ex.args...)
+        @test collect(map(x->x, l11)) == collect(l11)
+    end
+
 end # @testset LinkedList
diff --git a/test/test_minmax_heap.jl b/test/test_minmax_heap.jl
new file mode 100644
index 0000000..8a8bdb7
--- /dev/null
+++ b/test/test_minmax_heap.jl
@@ -0,0 +1,164 @@
+# tests for min-max heaps
+using DataStructures: _make_binary_minmax_heap, is_minmax_heap, children_and_grandchildren
+using Base.Order: Forward, Reverse
+
+@testset "Binary MinMax Heaps" begin
+    
+    @testset "is_minmax_heap tests" begin
+        mmheap = [0, 10, 9, 2, 3, 4, 5]
+        @test is_minmax_heap(mmheap)
+        @test is_minmax_heap([])
+        @test is_minmax_heap([rand()])
+    end
+    
+    @testset "make heap tests" begin
+        vs = [10, 4, 6, 1, 16, 2, 20, 17, 13, 5]
+        
+        @testset "make from array tests" begin
+            h = BinaryMinMaxHeap(vs)
+            @test length(h) == 10
+            @test !isempty(h)
+            @test top(h) == minimum(h) == 1
+            @test maximum(h) == 20
+            @test is_minmax_heap(h.valtree)
+        end
+        
+        @testset "make from random arrays tests" begin
+            for i = 1:20
+                A = rand(50)
+                vtree = _make_binary_minmax_heap(A)
+                @test is_minmax_heap(vtree)
+            end
+        end
+        
+        @testset "push! tests" begin
+            h = BinaryMinMaxHeap{Int}()
+            
+            @test length(h) == 0
+            @test isempty(h)
+            
+            push!(h, 1)
+            @test top(h) == 1
+            push!(h, 2)
+            @test is_minmax_heap(h.valtree)
+            push!(h, 10)
+            @test is_minmax_heap(h.valtree)
+            push!(h, rand(Int))
+            @test is_minmax_heap(h.valtree)
+            for i = 1:5
+                A = rand(20)
+                hs = BinaryMinMaxHeap(A)
+                for j = 1:4
+                    push!(hs, rand())
+                    @test is_minmax_heap(hs.valtree)
+                end
+            end
+        end
+    end
+    
+    @testset "pop! tests" begin
+        @testset "popmin! tests" begin
+            h = BinaryMinMaxHeap([1])
+            @test popmin!(h) == 1
+            @test length(h) == 0
+            @test isempty(h)
+            h = BinaryMinMaxHeap([1, 3, 2])
+            @test popmin!(h) == 1
+            @test length(h) == 2
+            @test is_minmax_heap(h.valtree)
+            @test minimum(h) == 2
+            for i = 1:20
+                A = rand(50)
+                h = BinaryMinMaxHeap(A)
+                minval = minimum(A)
+                @test popmin!(h) == minval
+                @test length(h) == 49
+                @test is_minmax_heap(h.valtree)
+            end
+        end
+        @testset "popmax! tests" begin
+            h = BinaryMinMaxHeap([1])
+            @test popmax!(h) == 1
+            @test length(h) == 0
+            @test isempty(h)
+            h = BinaryMinMaxHeap([1, 2, 3])
+            @test popmax!(h) == 3
+            @test length(h) == 2
+            @test is_minmax_heap(h.valtree)
+            @test maximum(h) == 2
+            h = BinaryMinMaxHeap([1, 3, 2])
+            @test popmax!(h) == 3
+            @test length(h) == 2
+            @test is_minmax_heap(h.valtree)
+            @test maximum(h) == 2
+        end
+    end
+    
+    @testset "empty!" begin
+        h = BinaryMinMaxHeap([1, 4, 3, 10, 2])
+        ret = empty!(h)
+        @test ret === h
+        @test length(ret) == 0
+        @test isempty(ret)
+    end
+
+    @testset "popall!" begin
+        @testset "popmin! tests" begin
+            A = rand(Int, 50)
+            sorted_A = sort(A)
+            h = BinaryMinMaxHeap(A)
+            @test popall!(h) == sorted_A
+            @test isempty(h)
+            @test length(h) == 0
+            
+            h = BinaryMinMaxHeap(A)
+            @test popmin!(h, 10) == sorted_A[1:10]
+            @test !isempty(h)
+            @test length(h) == 40
+        end
+        @testset "popmax! tests" begin
+            A = rand(Int, 50)
+            sorted_A = sort(A, order=Reverse)
+            h = BinaryMinMaxHeap(A)
+            @test popall!(h, Reverse) == sorted_A
+            @test length(h) == 0
+            @test isempty(h)
+            
+            h = BinaryMinMaxHeap(A)
+            @test popmax!(h, 10) == sorted_A[1:10]
+            @test length(h) == 40
+            @test !isempty(h)
+        end
+    end
+    
+    @testset "type conversion" begin
+        h = BinaryMinMaxHeap{Float64}()
+        push!(h, 3.)
+        push!(h, 5)
+        push!(h, Rational(4, 8))
+        push!(h, Complex(10.1, 0.0))
+        
+        @test h.valtree == [0.5, 10.1, 3.0, 5.0]
+    end
+    
+    @testset "children_and_grandchildren tests" begin
+        @test children_and_grandchildren(1, 1) == Int[]
+        @test children_and_grandchildren(2, 1) == [2]
+        @test children_and_grandchildren(3, 1) == [2, 3]
+        @test children_and_grandchildren(7, 1) == [2, 4, 5, 3, 6, 7]
+        @test children_and_grandchildren(10, 2) == [4, 8, 9, 5, 10]
+    end
+    
+    @testset "throw errors tests" begin
+        h = BinaryMinMaxHeap{Int}()
+        @test_throws ArgumentError pop!(h)
+        @test_throws ArgumentError popmin!(h)
+        
+        @test_throws ArgumentError popmax!(h)
+        
+        @test_throws ArgumentError top(h)
+        @test_throws ArgumentError minimum(h)
+        @test_throws ArgumentError maximum(h)
+        
+    end
+end