6.1Binary Tree
What is a binary tree? Full, complete and perfect trees
In a tree a node can have any number of children. In a binary tree every node has at most two children, and they are told apart as left and right. This limit makes trees much easier to code, which is why most tree lessons use binary trees. In this lesson you meet the binary tree and the kinds that exams often mix up.
- The definition of a binary tree, and why the left and right child are different
- How to tell full, complete and perfect binary trees apart
- Why a skewed (degenerate) tree turns into a linked list
1At most two children
In a binary tree every node has 0, 1 or 2 children. The children are ordered: one is the left child, the other the right child. For a node with one child, it matters whether that child is on the left or on the right.
The recursive definition of a binary tree also explains the code: a binary tree is either empty, or it is a root with a left subtree and a right subtree, both of which are binary trees. In code an empty tree is NULL.
2Full binary tree
If every node has either 0 or 2 children, the tree is full (also called a strict binary tree). No node has exactly one child. The leaves do not have to be on the same level.
3Complete binary tree
If every level except the last is full and the last level fills from left to right with no gaps, the tree is complete. There may be room left on the right of the last level, but no gap on the left.
This tree is not full: 30 has only one child. A complete tree matters for this reason: if you write its nodes into an array level by level, the array has no holes. The binary tree with an array lesson uses this; a heap is a complete tree too.
An easy way to check for completeness: read the nodes level by level, left to right. If you meet another node after a gap, the tree is not complete.
4Perfect binary tree
If every internal node has two children and all leaves are on the same level, the tree is perfect. Every level is filled to the brim. A perfect tree is both full and complete.
A perfect tree of height h has exactly 2^(h+1) - 1 nodes: 7 for height 2, 15 for height 3. This formula and others like it are in Number of nodes and height.
Books do not all use these names the same way. Some call a complete tree "almost complete" and a perfect tree "complete"; some call a full tree "proper" or "strict". In an exam, think in terms of the definition, not the name: 0 or 2 children (full), the last level fills from the left (complete), every level is full (perfect)?
5Skewed tree
If every node has only one child, the tree does not branch; it runs down as a single line. This is called a skewed or degenerate tree. One that always goes down to the right child is right-skewed, one that always goes left is left-skewed.
In a skewed tree the height is n - 1, the largest it can be. Since the running time of tree operations usually depends on the height, this is the worst case. If you insert sorted data into a binary search tree, you get exactly this kind of tree; Unbalanced trees covers it.
6Summary
| Kind | Rule | Example tree |
|---|---|---|
| Full | Every node has 0 or 2 children | 10, 20, 30, 60, 70 |
| Complete | All levels full but the last, which fills from the left | The six nodes 10 to 60 |
| Perfect | Every level is full | The seven nodes 10 to 70 |
| Skewed | Every node has one child | 10, 20, 30, 40 going right |
7Try it
Read level by level, a binary tree gives A, B, C, D, empty, F (the 5th place is empty, F is in the 6th). Is it complete?
The answer is B. In a complete tree the last level fills from left to right with no gaps. Having F in the 6th place while the 5th is empty breaks that rule.
Show the answer
The answer is B. In a complete tree the last level fills from left to right with no gaps. Having F in the 6th place while the 5th is empty breaks that rule.
Which statement is always true?
The answer is C. In a perfect tree every level is full, so no node has a single child (full) and the last level is full from the left (complete). This lesson has a counterexample for each of the other two: the tree 10 to 60 is complete but not full, and the tree 10, 20, 30, 60, 70 is full but not perfect.
Show the answer
The answer is C. In a perfect tree every level is full, so no node has a single child (full) and the last level is full from the left (complete). This lesson has a counterexample for each of the other two: the tree 10 to 60 is complete but not full, and the tree 10, 20, 30, 60, 70 is full but not perfect.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.