Algorithm Search⌘K

Proofs: Level-order traversal

These are the human proofs of Level-order traversal, at full length.

The queue-based traversal visits a binary tree in level order full

This proof shows: correctness, termination.

Let TT be a binary tree with n≥1n \ge 1 nodes; for the empty tree, line L1 returns and the empty list is correct. Let NkN_k be the list of the nodes of depth kk sorted by the relation "left of", for k=0,1,…,hk = 0, 1, \ldots, h, where hh is the height. The level order is the concatenation N0N1⋯NhN_0 N_1 \cdots N_h.

Lemma 1 (left of, one level down). Let x≠yx \ne y be nodes of depth k+1k + 1 with parents pp and qq. If p≠qp \ne q, then xx is left of yy if and only if pp is left of qq. If p=qp = q, then xx is left of yy if and only if xx is the left child and yy is the right child.

Proof. If p=qp = q, the deepest common ancestor of xx and yy is pp, and the claim is the definition. If p≠qp \ne q, then pp and qq have the same depth kk, and neither is an ancestor of the other. A common ancestor of xx and yy is not xx or yy, so it is a common ancestor of pp and qq, and conversely. So the deepest common ancestor aa is the same, and the paths from aa to xx and to pp start with the same child of aa, and so do those to yy and to qq. So the two relations agree. □\square

Lemma 2. Write C(x)C(x) for the list of the children of xx, the left child first. If Nk=(x1,…,xr)N_k = (x_1, \ldots, x_r), then Nk+1=C(x1)C(x2)⋯C(xr)N_{k+1} = C(x_1) C(x_2) \cdots C(x_r).

Proof. Each node of depth k+1k + 1 has exactly one parent, of depth kk, so the right side lists each node of depth k+1k + 1 exactly once. Two nodes in the same block C(xi)C(x_i) are in the right order by the second part of Lemma 1, and two nodes in blocks C(xi)C(x_i) and C(xj)C(x_j) with i<ji < j are in the right order by the first part, because xix_i is left of xjx_j. □\square

FIFO order. Let EE be the sequence of the nodes in the order in which they are enqueued. A FIFO queue dequeues in the order of enqueuing, so the nodes are visited in the order EE, as far as the loop runs. By lines L2, L6 and L7, EE starts with the root, and for each visited node yy, the block C(y)C(y) is appended to EE at the visit of yy. So E=(root) C(e1) C(e2)⋯E = (\mathrm{root})\, C(e_1)\, C(e_2) \cdots, where e1,e2,…e_1, e_2, \ldots are the entries of EE in order.

Claim. E=N0N1⋯NhE = N_0 N_1 \cdots N_h.

Proof. We show by induction on kk that EE starts with N0N1⋯NkN_0 N_1 \cdots N_k. For k=0k = 0, EE starts with the root, and N0N_0 is the root. Assume that EE starts with N0⋯NkN_0 \cdots N_k. The entries of N0⋯Nk−1N_0 \cdots N_{k-1} contribute, by the FIFO order, the blocks of their children, which are N1⋯NkN_1 \cdots N_k by Lemma 2, and these come right after the root. So the children of the entries of NkN_k are appended next, in the order of NkN_k, and by Lemma 2 they form Nk+1N_{k+1}. So EE starts with N0⋯Nk+1N_0 \cdots N_{k+1}. Each node is in exactly one NkN_k and is enqueued only by its parent or at line L2, so EE has no other entries. □\square

Termination. Each node is enqueued at most once, by its unique parent or, for the root, at line L2, so there are nn enqueues, and the loop of line L3 runs nn times.

Theorem. LEVEL-ORDER(T)(T) visits each node of TT exactly once, in the level order N0N1⋯NhN_0 N_1 \cdots N_h.

Proof. The loop ends after nn iterations, when every enqueued node is dequeued. The visits follow the order EE, which is the level order by the claim. ■\blacksquare

Back to Level-order traversal