# Scala algorithm: Binary heap (min-heap)

Published

## Algorithm goal

A binary heap implements a priority queue in a binary tree fashion. A binary tree has nodes that have at most 2 children. A priority queue is a data structure that given some ordering of children, will enable immediate access to the smallest (or largest, by reversing the Ordering) item.

The ordering in a binary heap is: the parent is always less than either of its two children.The left child is less than the right child (if there is more than 1 child).

Here are some recommended references with additional imagery, but may think in a more imperative way:

## Test cases in Scala

``````assert(BinaryHeap.empty[Char].min.isEmpty)
assert(BinaryHeap.empty[Char].withoutMin.isEmpty)
assert(
BinaryHeap
.empty[Char]
.withoutMin
.flatMap(_.min)
.contains('N')
)
assert(
BinaryHeap
.empty[Char]
.withoutMin
.flatMap(_.min)
.contains('N')
)
assert(
BinaryHeap
.empty[Char]
.withoutMin
.flatMap(_.withoutMin)
.flatMap(_.min)
.isEmpty
)
assert(
BinaryHeap
.empty[Char]
.withoutMin
.flatMap(_.min)
.contains('M')
)
assert(
)
assert(
BinaryHeap
.empty[Char]
.toList
.mkString == "ABLMNOQ"
)
``````

## Algorithm in Scala

69 lines of Scala (compatible versions 2.13 & 3.0).

## Explanation

Item153798
Index123456

Typical solutions of the Binary Heap use a Vector or an Array. Binary Heaps are complex to implement already (SO), so for ease of comprehension, we are using a Map as it simplifies the solution in having a more uniform look-up method.

The algorithm initially makes no sense, especially when we see references to 'i', 'i / 2' and 'i * 2'. The reason for this is that whenever updating the tree, we are comparing a parent with its children, or a child with its parent or sibling. In the array representation, the first level takes size 1, the 2nd level takes size 4, and so forth, because of the nature of it being a binary tree. The parent of '8' in the diagram above, at index 6, is 3. If we added another element, such as '10', its index would be 7, and both 6 / 2, and 7 / 2 = 3 (in integers), which is the index of the element '3'. (this is Â© from www.scala-algorithms.com)

## Scala concepts & Hints

1. ### Collect

'collect' allows you to use Pattern Matching, to filter and map items.

``````assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")
``````
2. ### Option Type

The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!

``````assert(Option(1).flatMap(x => Option(x + 2)) == Option(3))

assert(Option(1).flatMap(x => None) == None)
``````
3. ### Ordering

In Scala, the 'Ordering' type is a 'type class' that contains methods to determine an ordering of specific types.

``````assert(List(3, 2, 1).sorted == List(1, 2, 3))

assert(List(3, 2, 1).sorted(Ordering[Int].reverse) == List(3, 2, 1))

assert(Ordering[Int].lt(1, 2))

assert(!Ordering[Int].lt(2, 1))
``````
4. ### Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

``````assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")
``````
5. ### Stack Safety

Stack safety is present where a function cannot crash due to overflowing the limit of number of recursive calls.

This function will work for n = 5, but will not work for n = 2000 (crash with java.lang.StackOverflowError) - however there is a way to fix it :-)

In Scala Algorithms, we try to write the algorithms in a stack-safe way, where possible, so that when you use the algorithms, they will not crash on large inputs. However, stack-safe implementations are often more complex, and in some cases, overly complex, for the task at hand.

``````def sum(from: Int, until: Int): Int =
if (from == until) until else from + sum(from + 1, until)

def thisWillSucceed: Int = sum(1, 5)

def thisWillFail: Int = sum(1, 300)
``````
6. ### Tail Recursion

In Scala, tail recursion enables you to rewrite a mutable structure such as a while-loop, into an immutable algorithm.

``````def fibonacci(n: Int): Int = {
@scala.annotation.tailrec
def go(i: Int, previous: Int, beforePrevious: Int): Int =
if (i >= n) previous else go(i + 1, previous + beforePrevious, previous)

go(i = 1, previous = 1, beforePrevious = 0)
}

assert(fibonacci(8) == 21)
``````
7. ### Type Class

Type classes are one of Scala's most important super-powers: they enable you to add new behaviour to existing classes, without modifying those classes. In many languages, to add a behaviour to a class, you would typically extend it with an interface, and then implement methods against this interface.This, however, does not scale: especially when you have older libraries, you would be forced to make them depend on a new interface, and have to re-build everything.

Type classes are used heavily in Apple's SwiftUI as "extensions" to enable powerful abstraction capabilities.

Type classes enable you to do things like this:

``````import Ordering.Implicits._

type CommonType = (Int, String, Option[String])

val a: CommonType = (1, "X", None)

val b: CommonType = (2, "A", Some("B"))

assert(a < b, "We can order tuples using Scala-provided type classes")
``````

# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

## How our 100 algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.

### Study our 100 Scala Algorithms: 6 fully free, 100 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 22 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.