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].add('M').min.contains('M'))
assert(BinaryHeap.empty[Char].add('M').withoutMin.flatMap(_.min).isEmpty)
assert(
BinaryHeap
.empty[Char]
.add('M')
.add('N')
.withoutMin
.flatMap(_.min)
.contains('N')
)
assert(
BinaryHeap
.empty[Char]
.add('N')
.add('M')
.withoutMin
.flatMap(_.min)
.contains('N')
)
assert(
BinaryHeap
.empty[Char]
.add('M')
.add('N')
.withoutMin
.flatMap(_.withoutMin)
.flatMap(_.min)
.isEmpty
)
assert(BinaryHeap.empty[Char].add('M').add('N').min.contains('M'))
assert(BinaryHeap.empty[Char].add('M').add('N').add('L').min.contains('L'))
assert(
BinaryHeap
.empty[Char]
.add('M')
.add('N')
.add('L')
.withoutMin
.flatMap(_.min)
.contains('M')
)
assert(
BinaryHeap.empty[Char].add('M').add('N').add('L').toList.mkString == "LMN"
)
assert(
BinaryHeap
.empty[Char]
.add('M')
.add('N')
.add('L')
.add('O')
.add('Q')
.add('B')
.add('A')
.toList
.mkString == "ABLMNOQ"
)
Algorithm in Scala
69 lines of Scala (compatible versions 2.13 & 3.0).
Explanation
Item | 1 | 5 | 3 | 7 | 9 | 8 |
---|---|---|---|---|---|---|
Index | 1 | 2 | 3 | 4 | 5 | 6 |
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
Collect
'collect' allows you to use Pattern Matching, to filter and map items.
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!
Ordering
In Scala, the 'Ordering' type is a 'type class' that contains methods to determine an ordering of specific types.
Pattern Matching
Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.
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.
Tail Recursion
In Scala, tail recursion enables you to rewrite a mutable structure such as a while-loop, into an immutable algorithm.
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: