What is a merge-sort tree, and what does each of its nodes hold?
A binary tree with one node per recursive call: each node holds the call's unsorted input (before the split) and its sorted output (after the merge). The root is the first call on the whole sequence; the leaves are the calls on sequences of size 0 or 1.
The tree makes a recursion visible that otherwise only exists on the call stack. How to read it:
- Top-down you see the divide steps: every node's input is cut into the inputs of its two children.
- Bottom-up you see the conquer steps: every node's output is the merge of its two children's outputs.
- A leaf holds a sequence of one element (or none), the base case: input and output are the same.
For $n$ a power of two the tree is a perfect binary tree. With $n = 8$ it has 8 leaves, $8 - 1 = 7$ internal nodes, each of them one merge, and height $\log_2 8 = 3$. In general the height is $\lceil \log_2 n \rceil$, because every level halves the sequences.
The tree is never built in memory. It is a picture of the calls: at any moment only one path of it exists, as the open calls on the call stack, and a debugger with a breakpoint on return would stop for the first time only at the leftmost leaf.
Go deeper:
Comparison sorting visualisation (USF) โ merge-sort, quick-sort and four simpler sorts animated step by step on an array.