Trees

Binary Search Treetreesthe base structure AVL Treetreesstrictly balanced BST Red Black Treetreesrelaxed balance Splay Treetreesself-adjusting Cartesian TreetreesBST plus heap B Treetreesdisk-oriented KD Treetreesspatial search

Search trees keep keys in order so that lookup, insertion, deletion and range queries all run in time proportional to the tree height. The trees in this section differ in how they keep that height small, and in what they are optimised for: memory, disk, access patterns or multi-dimensional data.

Search trees and their balanced variants: BST, AVL, red-black, splay, B-tree, Cartesian and KD trees.

AVL Tree

An AVL tree (Adelson-Velsky & Landis, 1962 — the original self-balancing BST) keeps every node's subtree heights within 1 of each other. It is more rig

B Tree

A B-tree is a self-balancing search tree where each node holds many keys and has many children — designed to minimize disk reads. It's the structure behind

Binary Search Tree

A binary search tree (BST) is a binary tree where every node's left subtree contains smaller keys and right subtree contains larger keys. Average O(log n)…

Cartesian Tree

A Cartesian tree is a binary tree built from a sequence: every node is greater (or smaller, for a min-heap variant) than its descendants in heap order, while…

KD Tree

A KD-tree (k-dimensional tree) is a binary tree that partitions k-dimensional space by alternating splitting axes. It enables fast nearest-neighbor and range…

Red Black Tree

A red-black tree is a self-balancing BST where each node is colored red or black and a small set of color invariants keep the tree height ≤ 2 log(n+1). Used…

Splay Tree

A splay tree is a self-adjusting BST: every access (search, insert, delete) moves the touched node to the root through a sequence of rotations called…


1. How the trees relate

Tree family Binary Search Tree ordered, may degrade to O(n) AVL strict height balance Red-black relaxed balance Splay self-adjusting Cartesian BST + heap order B-tree many keys per node, disk pages KD tree k-dimensional points

A plain BST is the foundation: every key in the left subtree is smaller and every key in the right is larger. Inserting sorted data turns it into a linked list, so balanced variants add rotations. AVL trees keep subtree heights within one; red-black trees allow more slack and rotate less on updates; splay trees move each accessed key to the root, which is good for skewed access; Cartesian trees (treaps when priorities are random) combine BST order with heap order. B-trees generalise to many keys per node to match disk pages, and KD trees split on alternating dimensions for spatial queries.

2. Comparison

TreeSearch (worst)BalanceBest for
BSTO(n)NoneTeaching, random insert order
AVLO(log n)StrictRead-heavy in-memory lookups
Red-blackO(log n)RelaxedGeneral maps (std::map, Java TreeMap)
SplayO(log n) amortizedSelf-adjustingCaches, skewed access
Cartesian / treapO(log n) expectedRandomizedSimple balanced trees, split/merge
B-treeO(log n)Strict, wide nodesDatabases and file systems
KD treeO(n) worst, about O(log n) typicalBy median splitsNearest neighbour in low dimensions
Hash Tabledata structuresunordered O(1) alternative Skip Listlinearprobabilistic ordered structure