algoritmauzmanı
Sign in
TREN
6.1Binary Tree
Lesson 6.1.1

What is a binary tree? Full, complete and perfect trees

In a tree a node can have any number of children. In a 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.

What you will learn
  • 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
6 min

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.

10201020
Two different binary trees. On the left, 20 is the left child of 10; on the right, 20 is the right child of 10. As general trees they are the same; as binary trees they differ.

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

1020306070
A full binary tree: 10 and 30 have two children, the others none. The leaves 20, 60 and 70 are on different levels.

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 . There may be room left on the right of the last level, but no gap on the left.

102030405060
A complete binary tree: 10, 20, 30, 40, 50 and 60 placed in order, level by level and left to right. 60 (orange) is in the third place of the last level; the fourth place is empty.

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.

10203040506070
A perfect binary tree: height 2, seven nodes. All four leaves (green) are on the bottom level.

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.

10203040
A right-skewed tree: four nodes, height 3. This tree is really a linked list.

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

KindRuleExample tree
FullEvery node has 0 or 2 children10, 20, 30, 60, 70
CompleteAll levels full but the last, which fills from the leftThe six nodes 10 to 60
PerfectEvery level is fullThe seven nodes 10 to 70
SkewedEvery node has one child10, 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?

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?

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.