designwithfocus

Programming basics

What Is a Binary Tree? A Node with Two Children

By Design With Focus Team 6 min read
A root node with a left child and a right child

Short answer

A binary tree is a tree in which each node has at most two children. NIST defines it as empty, or as a root plus a left binary tree and a right binary tree. A binary search tree adds order: left keys are smaller and right keys are larger. Binary search is a separate algorithm that halves a sorted array.

Last verified against the sources listed below.

Key takeaways

  • NIST defines a binary tree as a tree with at most two children for each node.
  • OpenDSA describes a binary tree as a root plus disjoint left and right subtrees, or as empty.
  • A binary search tree keeps smaller keys in the left subtree and larger keys in the right.
  • NIST defines binary search as halving a sorted array, with run time O(ln n).
  • OpenDSA names the heap as an example of a complete binary tree, which is a shape restriction.

What is binary tree, in data structures, is a question about how many children a node may have. NIST defines a binary tree as a tree with at most two children for each node. It is also called a dyadic tree.

The same entry gives a formal version. A binary tree either is empty, or it has a root node, a left binary tree, and a right binary tree. Wikipedia states the computer-science version the same way: each node has at most two children, called the left child and the right child.

It also warns that some mathematics texts instead require every non-leaf to have exactly two children.

A programming paradigm does not change this definition. The tree is a data structure you can build in more than one style of code.

What does a node contain?

A node is one element, and it has at most two children. OpenDSA says a binary tree is a finite set of nodes. The set is empty, or it is a root together with two binary trees, the left and right subtrees, which share no nodes with each other or with the root.

The roots of those subtrees are the children of the root. There is an edge from a node to each child, and the node is the parent of its children. A path is a sequence of nodes where each is the parent of the next.

The length of a path with k nodes is k minus 1. The depth of a node is the length of the path from the root to that node. The height of the tree is the depth of its deepest node.

OpenDSA says a leaf has two empty children. An internal node has at least one non-empty child. The root is the only node at level 0, and its depth is 0.

What does a three-node tree look like?

A three-node example is a root with two children. Call the root P, the left child L, and the right child R. P has two children. L and R have none, so each of those children is a leaf under the OpenDSA wording.

That picture matches NIST’s “at most two children.” It is not a claim about speed. Nothing in the NIST binary-tree entry assigns a run time to this shape.

If the tree is also a search tree, the keys have to be ordered. Put 2 at P, 1 at L, and 3 at R. NIST’s binary search tree entry requires every left subtree’s keys to be less than the node’s key, and every right subtree’s keys to be greater.

Here 1 is less than 2, and 3 is greater than 2. Swap 1 and 3 and the picture is still a binary tree, but it is no longer a binary search tree.

How does a search tree use order?

A binary search tree stores keys so a search can walk left or right. Princeton’s Algorithms text defines it as a binary tree where each node’s key is larger than the keys in its left subtree and smaller than the keys in its right subtree.

Their search reads directly from that rule. An empty tree is a miss. A key equal to the root is a hit.

Otherwise the search continues in the left subtree or the right subtree. Inserting a missing key ends at an empty link and replaces that link with a new node.

The same chapter says running times depend on the shape of the tree, which depends on insertion order. One proposition says search hits in a tree built from N random keys take about 2 ln N compares on average. Another says search, insertion, and several order queries take time proportional to the height in the worst case. Those are the book’s statements, not a promise for every insertion order.

Wikipedia notes that left and right still matter when a node has only one child, and that this matters in particular for binary search trees. The placement of nodes can change, for example by balancing, without changing the set of keys.

What is binary search, then?

Binary search is an algorithm on a sorted array, not a tree. NIST says you start with an interval covering the whole array. If the search key is less than the middle item, you keep the lower half.

Otherwise you keep the upper half. You repeat until the value is found or the interval is empty.

NIST notes the run time as O(ln n). It also shows a midpoint formula, mid = (high + low)/2, and warns that this overflows when high and low are near the largest expressible integer. The alternative it prints is mid = low + (high - low)/2, for non-negative high and low.

The same entry says binary search may be used on an ordered linked list. That version still makes O(n) traversals, as linear search does, but it performs O(log n) comparisons. Those bounds are NIST’s notes. They are not the definition of a binary tree.

Structure What the cited page requires
Binary tree At most two children for each node, or empty (NIST)
Binary search tree Left subtree keys smaller, right subtree keys larger (NIST)
Binary search Halve a sorted array until the key is found or the interval is empty (NIST)
Complete binary tree Filled by levels from the left; a heap is one example (OpenDSA)

Which shapes have extra names?

Full and complete are restrictions on shape, not extra children. OpenDSA says each node in a full binary tree is either an internal node with exactly two non-empty children or a leaf. A complete binary tree is filled level by level from the left. All levels except possibly the bottom are full, and the bottom is filled from the left.

The page says a tree can be full but not complete, or complete but not full. It names a Huffman coding tree as a full example. It names the heap as a complete example. A heap is that shape plus heap order, which is a separate definition from “at most two children.”

A word counter can count the words in a short writeup of the three-node example. It does not check whether a left child is smaller than its parent. For that check, compare keys with the NIST search-tree rule, or walk the OpenDSA definitions of leaf, depth, and height.

Frequently asked questions

Is an empty tree still a binary tree?

Yes. NIST says a binary tree either is empty, with no nodes, or has a root, a left binary tree, and a right binary tree. OpenDSA says the set of nodes is empty, or it is a root plus left and right subtrees that share no nodes. An empty child is how a node can have fewer than two real children and still fit the definition.

What is a leaf in a binary tree?

OpenDSA says a leaf has two empty children. An internal node has at least one non-empty child. In a picture with a root and two children, those children are leaves if their own subtrees are empty. The root is internal because its children are not empty. NIST's basic definition does not need a separate leaf sentence. At most two children is enough.

How is a binary search tree different?

NIST says a binary search tree is a binary tree where every left subtree's keys are less than the node's key, and every right subtree's keys are greater. Princeton's Algorithms text uses the same order. Left and right positions matter. A plain binary tree may put any values there. A search tree may not, or it stops matching that definition.

Is binary search the same as a binary tree?

No. NIST lists binary search as an algorithm on a sorted array. It keeps the lower half when the key is less than the middle item, and the upper half otherwise, until the value is found or the interval is empty. NIST notes the run time as O of ln n. That procedure is not the definition of a binary tree.

What do full and complete mean?

OpenDSA describes two shapes. A full binary tree node is an internal node with exactly two non-empty children, or a leaf. A complete binary tree is filled level by level from the left, and only the bottom level may be incomplete. A tree can be one of those shapes and not the other. The page names a heap as a complete example.

Sources