A binary search tree puts one key in each node and gives it two children. An m-way search tree puts several keys in each node and gives it up to m children. Each comparison now splits the keys m ways instead of two, so the tree can be much shorter. But it has the same weakness as a plain BST: its shape depends on the order the keys arrive in.
Think of a BST node as one key with two "ways" out of it: left for smaller keys, right for larger. An m-way search tree generalizes this. Each node holds up to m − 1 keys, kept in sorted order, and has up to m children, one for each gap around the keys:
A BST is just the case m = 2: one key, two children. The number m is called the order of the tree.
The node [40 45] hangs from the gap between 35 and 50, so everything in it must be between 35 and 50. 45 is; 55 is not. 55 does fit higher up, between 50 and 60, which would be the gap to the right of 50.
124. A full tree has 1 node at the top, 5 below it and 25 below those, 31 nodes in all, each with 4 keys. In general a full m-way tree of height h holds mʰ − 1 keys. For a BST (m = 2) that is 2ʰ − 1, which you have seen before.
Searching works like a BST, but each node takes a few comparisons instead of one. Scan the node's keys from left to right until you find the first key that is not smaller than the one you want:
To find 45 in the 3-way tree: 45 is between 30 and 60, so take the middle child. 45 is between 35 and 50, so take the middle child again. 45 is in [40 45]: found, after visiting three nodes.
Each level visited costs up to m − 1 comparisons (or about log₂ m with a binary search inside the node). The win is that there are fewer levels. That matters most when each node lives in a block on disk: reading a block takes far longer than comparing the keys inside it, so a tree that is a few levels tall can answer a search with just a few disk reads.
The simplest insert search for the key, then:
So nodes fill from the top down, and a node only gets children after it is full. Nothing in this rule looks at the shape of the tree.
50 and 20 fill the root: [20 50]. 80 is greater than 50, so it starts the right child: [80]. 10 starts the left child: [10]. 30 is between 20 and 50, so it starts the middle child: [30]. 60 joins [80] to make [60 80], 90 goes below it as [90], and 40 joins [30] to make [30 40]. Height 3 with eight keys. Check it in the lab with the matching preset.
Try "Sorted 1–20" with m = 3. Each new key is larger than everything before it, so it always heads for the rightmost gap. The root fills with 1 and 2, then 3 and 4 go in a node below it, then 5 and 6 below that, and so on: a chain of 10 nodes, each with one child.
This is exactly what happens to a BST with sorted input, only m − 1 times shorter. The height is about n / (m − 1) instead of about logm n, so searching is O(n) again.
There are other problems too. Nodes near the bottom can be nearly empty, wasting space (and on disk, a whole block per node). Deleting keys can leave more half-empty nodes behind. The simple rules never move a key once it is placed, so the tree has no way to repair itself.
The chain has 4 keys per node, so 20 keys make a chain of 5 nodes: height 5. The best possible is 2, because a 5-way tree of height 2 holds up to 5² − 1 = 24 keys. A larger m only makes the chain shorter by a constant factor; the height still grows in proportion to n.
The AVL tree. Each node tracks its height, and after every insert, rotations restore the rule that sibling subtrees differ in height by at most one. That keeps the height O(log n) whatever order the keys arrive in.
Two ideas work together. Require every node (except the root) to be at least about half full, so nodes cannot sit nearly empty. And require every leaf to be at the same depth, so no branch can grow longer than another. Keeping both rules true while inserting means changing how the tree grows. That is the B-tree.
An AVL tree is a binary search tree with extra rules and a repair step that keep it balanced. A B-tree is the same idea applied to an m-way search tree:
Both guarantee a height of O(log n), so search, insert and delete are O(log n) no matter the input order. Instead of growing downward from the leaves, a B-tree grows upward at the root, which is how it keeps every leaf at the same depth.