# Scala algorithm: Check a binary tree is balanced

Published

## Algorithm goal

A binary tree is considered to be balanced when the height of each sub-branch of a tree has a height that differs by no more than 1, and its children also are balanced.

## Test cases in Scala

``````assert(BinaryTree.Node[Int](1).isBalanced)
assert(BinaryTree.Node[Int](1).copy(right = BinaryTree.Node(2)).isBalanced)
assert(
!BinaryTree
.Node[Int](1)
.copy(right = BinaryTree.Node[Int](2).copy(right = BinaryTree.Node(3)))
.isBalanced
)
assert(
!BinaryTree
.Node[Int](1)
.copy(
left = BinaryTree
.Node[Int](2)
.copy(left =
BinaryTree.Node[Int](4).copy(left = BinaryTree.Node[Int](6))
),
right = BinaryTree
.Node[Int](3)
.copy(right =
BinaryTree
.Node[Int](4)
.copy(right =
BinaryTree.Node[Int](5).copy(right = BinaryTree.Node[Int](7))
)
)
)
.isBalanced
)
assert(
BinaryTree
.Node[Int](
value = 1,
left = BinaryTree.Node[Int](4),
right = BinaryTree.Node[Int](2).copy(right = BinaryTree.Node[Int](3))
)
.isBalanced
)
``````

## Algorithm in Scala

31 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!

## Explanation

To check if a tree is balanced, we need to check the height and also that both of the sub-trees are balanced.

Here we demonstrate two approaches to iterating a tree: 'isBalanced' uses a recursive approach where it would call both 'left.isBalanced' and 'right.isBalanced', which would create a stack of size of the height of the tree, so is not the most efficient stack-wise; for 'heightIterative', we use an iterative approach which uses more heap but less stack (no recursion). . (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. ### 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")
``````
3. ### State machine

A state machine is the use of `sealed trait` to represent all the possible states (and transitions) of a 'machine' in a hierarchical form.

# 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.