Graph theorists use the following definition. A binary tree is a connected acyclic graph such that the degree of each vertex is no more than 3. A rooted binary tree is such a graph that has one of its vertices of degree no more than 2 singled out as the root. With the root thus chosen, each vertex will have a uniquely defined parent, and up to two children; however, so far there is insufficient information to distinguish a left or right child. If we drop the connectedness requirement, and there are multiple connected component in the graph, we call such a structure a forest.

Table of contents |

2 Methods of iterating over binary trees 3 Encoding n-ary trees as binary trees |

Binary trees can be constructed from programming language primitives in several ways. In a language with structures and either pointers or references, binary trees are constructed by having a tree node structure which contains some data and pointers or references to its left child and its right child. The order this imposes on the children is sometimes useful, particularly for in-order traversal. Sometimes it also contains a pointer to its unique parent. If a node has less than two children, some of the child pointers may be set to a special null value, or to a special sentinel node.

Binary trees can also be stored in arrays, and if the tree is a complete binary tree, this method wastes no space, and the resulting data structure is called a heap. In this compact arrangement, a node has an index `i`, and (assuming zero-based arrays) its children are found at indices 2`i`+1 and 2`i`+2, while its parent (if any) is found at index *floor(i/2)*. This method benefits from more compact storage and better locality of reference, particularly during a preorder traversal.

In languages with tagged unions such as ML, a tree node is often a tagged union of two types of nodes, one of which is a 3-tuple of data, left child, and right child, and the other of which is a "leaf" node, which contains no data and functions much like the null value in a language with pointers.

visit(TreeNode node) {print node.value if node.left != null then visit(node.left) if node.right != null then visit(node.right)}

visit(TreeNode node) {if node.left != null then visit(node.left) print node.value if node.right != null then visit(node.right)}

Finally, an in-order traversal, as above, visits each node between the nodes in its left subtree and the nodes in its right subtree. This is a particularly common way of traversing a binary search tree, because it gives the values in increasing order.

There is a one-to-one mapping between general ordered trees and binary trees, which in particular is used by Lisp to represent general ordered trees as binary trees. Each node `N` in the ordered tree corresponds to a node `N`' in the binary tree; the *left* child of `N`' is the node corresponding to the first child of `N`, and the *right* child of `N`' is the node corresponding to the N's next sibling --- that is, the next node in order among the children of the parent of `N`

One way of thinking about this is that each node's children are in a linked list, chained together with their *right* fields, and the node only has a pointer to the beginning or head of this list, through its *left* field.

For example, in the tree on the left, A has the 6 children {B,C,D,E,F,G}. It can be converted into the binary tree on the right.

The binary tree can be thought of as the original tree tilted sideways, with the black left edges representing *first child* and the blue right edges representing *next sibling*. The leaves of the tree on the left would be written in Lisp as:

- (((M N) H I) C D ((O) (P)) F (L))

See also: AVL tree, B-tree, binary space partition, red-black tree\n