Binary Trees Overview


Formal Definition of a Binary Tree

A binary tree consists of a finite set of nodes that is either empty, or consists of one specially designated node called the root of the binary tree, and the elements of two disjoint binary trees called the left subtree and right subtree of the root.

Note that the definition above is recursive: we have defined a binary tree in terms of binary trees. This is appropriate since recursion is an innate characteristic of tree structures.

Figure 1: A binary tree

Binary Tree Terminology

Tree terminology is generally derived from the terminology of family trees (specifically, the type of family tree called a lineal chart).

Other Tree Terms

Special Forms of Binary Trees

There are a few special forms of binary tree worth mentioning.

If every non-leaf node in a binary tree has nonempty left and right subtrees, the tree is termed a strictly binary tree. Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero or two, never degree one. A strictly binary tree with N leaves always contains 2N - 1 nodes.

Some texts call this a "full" binary tree.

A complete binary tree of depth d is the strictly binary tree all of whose leaves are at level d.

The total number of nodes in a complete binary tree of depth d equals 2d+1 - 1. Since all leaves in such a tree are at level d, the tree contains 2d leaves and, therefore, 2d - 1 internal nodes.

Figure 2: A complete binary tree

A binary tree of depth d is an almost complete binary tree if:

Figure 3: An almost complete binary tree

An almost complete strictly binary tree with N leaves has 2N - 1 nodes (as does any other strictly binary tree). An almost complete binary tree with N leaves that is not strictly binary has 2N nodes. There are two distinct almost complete binary trees with N leaves, one of which is strictly binary and one of which is not.

There is only a single almost complete binary tree with N nodes. This tree is strictly binary if and only if N is odd.

Some texts do not make a distinction between complete and almost complete binary trees, considering both to be complete binary trees.

Representing Binary Trees in Memory

Array Representation

For a complete or almost complete binary tree, storing the binary tree as an array may be a good choice.

One way to do this is to store the root of the tree in the first element of the array. Then, for each node in the tree that is stored at subscript k, the node's left child can be stored at subscript 2k+1 and the right child can be stored at subscript 2k+2. For example, the almost complete binary tree shown in Figure 3 can be stored in an array like this:

However, if this scheme is used to store a binary tree that is not complete or almost complete, we can end up with a great deal of wasted space in the array. For example, the following binary tree

would be stored using this technique like this:

Linked Representation

If a binary tree is not complete or almost complete, a better choice for storing it is to use a linked representation similar to the linked list structures covered earlier in the semester:

Each tree node has two pointers (usually named left and right). The tree class has a pointer to the root node of the tree (labeled root in the diagram above).

Any pointer in the tree structure that does not point to a node will normally contain the value nullptr. A linked tree with N nodes will always contain N + 1 null links.