1. Binary trees: terms and counting results
A binary tree is empty or a root with a left and a right subtree. The depth of a node is the number of edges from the root; the height of a tree is the largest depth (a single node has height 0). Check which convention the question uses; some count levels from 1.
- A full (strict) binary tree: every node has 0 or 2 children.
- A complete binary tree: every level is full except possibly the last, which is filled from the left.
- A perfect binary tree: all levels full.
Key results for a binary tree with nodes:
- It has edges and NULL child pointers.
- If , , are the numbers of nodes with 0, 1, 2 children, then (from counting edges: ).
- Level has at most nodes; a tree of height has between and nodes.
- The minimum height for nodes is ; a complete binary tree reaches it.
- A full binary tree with internal nodes has leaves and nodes.
Counting shapes. The number of distinct binary tree shapes with nodes is the Catalan number (1, 2, 5, 14, 42). The number of BSTs on distinct keys is also , because each shape can be filled with sorted keys in exactly one way.