AlgoViz

Binary tree

easy 18 min

A binary tree is a hierarchy of nodes where each node has at most two children, left and right, hanging from a single root.

Where it shows up

The same pattern appears across everyday software.

  • Expression trees in compilers

    A parser turns (3 + 4) × 2 into a tree with operators inside and numbers at the leaves. Postorder evaluates it, and the same abstract syntax tree drives type checking and code generation.

  • Database and file indexes

    Binary search trees and their balanced forms (AVL, red-black) back ordered maps in many language libraries, and the B-trees behind database indexes generalise the same idea to many children per node.

  • Huffman compression

    ZIP and JPEG use Huffman codes: a full binary tree where each left edge is a 0, each right edge a 1, and frequent symbols sit near the root so they get shorter codes.

  • Decision trees in machine learning

    A classifier asks one yes/no question per node and follows the branch to a leaf holding the prediction. Random forests are many such trees voting together.

  • Game AI

    Minimax searches a tree of moves and replies, scoring leaves and passing the best score back up to the root: a postorder computation.

  • Folders, the DOM and JSON

    File systems, HTML documents and nested JSON are general trees, and are often stored as binary trees with a first-child link and a next-sibling link. Disk-usage tools total folder sizes in postorder.