# 15-150: Principles of Functional Programming

# Lecture 8: Sorting Trees

We implemented mergesort for trees.

We showed that there is considerable opportunity for parallelism
compared with mergesort for lists.

We mentioned the need for tree rebalancing, in order to constrain the
depth of trees returned by recursive class in our mergesort.

### Key Concepts

- Sorted Trees
- Divide and Conquer for Trees
- Span Analysis
- Tree Balance

A tree is said to be *balanced* if `d` is
`O(log n)`, with `d` the *depth* of the tree
and with `n` the `size` of the tree. The size could be
measured either in terms of the number of nodes or the number of
leaves (e.g., `Empty`s), depending on the particular
application code. (A binary tree has `n` nodes if and only if
it has `n+1` leaves.) For a balanced tree we often view the
left subtree and right subtrees of the root as each containing roughly
half the overall number of nodes, i.e., roughly `n/2`. We say
that a tree of depth `d` is *perfectly balanced* if each
immediate subtree is perfectly balanced and has depth `d-1`.
We say that a tree is *unbalanced* if it is not balanced.