Lably
Skip to content

Binary tree

Limited form of tree data structure

  • 18 min read
  • 7 chapters

Learn it on Lably

From a graph theory perspective, binary trees as defined here are arborescences. A binary tree may thus be also called a bifurcating arborescence, a term which appears in some early programming books before the modern computer science terminology prevailed. It is also possible to interpret a binary tree as an undirected, rather than directed graph, in which case a binary tree is an ordered, rooted tree. Some authors use rooted binary tree instead of binary tree to emphasize the fact that the tree is rooted, but as defined above, a binary tree is always rooted.

In mathematics, what is termed binary tree can vary significantly from author to author. Some use the definition commonly used in computer science, but others define it as every non-leaf having exactly two children and don't necessarily label the children as left and right either.

In computing, binary trees can be used in two very different ways:

  • First, as a means of accessing nodes based on some value or label associated with each node. Binary trees labelled this way are used to implement binary search trees and binary heaps, and are used for efficient searching and sorting. The designation of non-root nodes as left or right child even when there is only one child present matters in some of these applications, in particular, it is significant in binary search trees. However, the arrangement of particular nodes into the tree is not part of the conceptual information. For example, in a normal binary search tree the placement of nodes depends almost entirely on the order in which they were added, and can be re-arranged (for example by balancing) without changing the meaning.
  • Second, as a representation of data with a relevant bifurcating structure. In such cases, the particular arrangement of nodes under and/or to the left or right of other nodes is part of the information (that is, changing it would change the meaning). Common examples occur with Huffman coding and cladograms. The everyday division of documents into chapters, sections, paragraphs, and so on is an analogous example with n-ary rather than binary trees.

Definitions

Recursive definition

Recursive full tree definition

A simple, informal approach to describe a binary tree might be as follows:

a binary tree has a root node, which has 0 or 2 children nodes (which in turn may have their 0 or 2 children, and so on).

More formally:

  • (base case) There exists a full tree consisting of a single node;
  • (recursive step) If T1 and T2 are full binary trees, which do not share any node, and r is a node not belonging to T1 or T2, then the ordered triple (r, T1, T2) is a full binary tree.

This definition implies two limitations: a full binary tree contains at least one node, and no node can have only one child. This is resolved with the next definition.

Recursive extended tree definition

The extended tree definition starts with the assumption the tree can be empty.

  • (base case) An empty set of nodes is an extended binary tree.
  • (recursive step) If T1 and T2 are extended binary trees, which do not share any node, and r is a node not belonging to any of them, then the ordered triple (r, T1, T2) is an extended binary tree.

To be complete from the graph point of view, both definitions of a tree should be extended by a definition of the corresponding branches sets. Informally, the branches set can be described as a set of all ordered pairs of nodes (r, s) where r is a root node of any subtree appearing in the definition, and s is a root node of any of its T1 and T2 subtrees (in case the respective subtree is not empty).

Another way of imagining this construction (and understanding the terminology) is to consider instead of the empty set a different type of node—for instance square nodes if the regular ones are circles.

Using graph theory concepts

A binary tree is a rooted tree that is also an ordered tree (a.k.a. plane tree) in which every node has at most two children. A rooted tree naturally imparts a notion of levels (distance from the root); thus, for every node, a notion of children may be defined as the nodes connected to it a level below. Ordering of these children (e.g., by drawing them on a plane) makes it possible to distinguish a left child from a right child. But this still does not distinguish between a node with left but not a right child from a node with right but no left child.

The necessary distinction can be made by first partitioning the edges; i.e., defining the binary tree as triplet (V, E1, E2), where (V, E1 ∪ E2) is a rooted tree (equivalently arborescence) and E1 ∩ E2 is empty, and also requiring that for all j ∈ { 1, 2 }, every node has at most one Ej child. A more informal way of making the distinction is to say, quoting the Encyclopedia of Mathematics, that "every node has a left child, a right child, neither, or both" and to specify that these "are all different" binary trees.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Computing and communication