Algorithm Search⌘K

Proofs: Balanced parentheses representation

These are the human proofs of Balanced parentheses representation, at full length.

Correctness of navigation on the BP representation full

This proof shows: correctness.

Let PP be written by a preorder traversal, ( on entering a node and ) on leaving it, let o(v)o(v) and c(v)c(v) be the positions of the two parentheses of vv, and let E(i)E(i) be the excess of P[0..i]P[0..i], with E(−1)=0E(-1) = 0.

Lemma 1 (nesting). P[o(v)..c(v)]P[o(v)..c(v)] consists of the parentheses of the descendants of vv, and of nothing else. The traversal enters vv, then visits the subtrees of its children one after another, then leaves vv. By induction on the height of vv, each child's subtree writes exactly its own pairs, and nothing else is written between o(v)o(v) and c(v)c(v).

Lemma 2 (excess). E(o(v))=depth(v)+1E(o(v)) = \text{depth}(v) + 1 and E(c(v))=depth(v)E(c(v)) = \text{depth}(v), with the root at depth 0. The nodes entered and not yet left right after o(v)o(v) are vv and its ancestors, each with one unmatched ( and no other open pair; after c(v)c(v), vv is closed too. Lemma 1 also shows that E(i)>E(c(v))E(i) > E(c(v)) for o(v)≤i<c(v)o(v) \le i < c(v): inside the subtree, vv itself stays open. So c(v)=findclose(o(v))c(v) = \text{findclose}(o(v)), the first position after o(v)o(v) with excess E(o(v))−1E(o(v)) - 1.

FIRST-CHILD. If vv has children, its first child is entered right after vv, so P[o(v)+1]=(P[o(v) + 1] = ( is its opening. If not, vv is left right away and P[o(v)+1]=)P[o(v)+1] = ).

NEXT-SIBLING. After leaving vv at c(v)=findclose(o(v))c(v) = \text{findclose}(o(v)), the traversal enters the next child of the parent if one exists, writing ( at c(v)+1c(v) + 1; otherwise it leaves the parent, writing ). The root has no next position.

PARENT. By Lemma 2 only the root has E(o(v))=1E(o(v)) = 1. Otherwise let pp be the parent. By Lemma 1, the pair of pp contains the pair of vv, and so do the pairs of all ancestors of vv, and no other pair does. The tightest of them is the deepest ancestor, pp, which is enclose(o(v))\text{enclose}(o(v)).

SUBTREE-SIZE. By Lemma 1 the range [o(v),c(v)][o(v), c(v)] holds two parentheses for each node of the subtree, so its length c(v)−o(v)+1c(v) - o(v) + 1 is twice the size.

DEPTH. Lemma 2.

IS-ANCESTOR. uu is an ancestor of vv or u=vu = v exactly when the pair of vv lies in the pair of uu, by Lemma 1, that is, when o(u)≤o(v)≤c(u)o(u) \le o(v) \le c(u); the pair of a non-descendant either ends before o(u)o(u) or starts after c(u)c(u).

LCA. Let o(u)<o(v)o(u) < o(v), with uu not an ancestor of vv, and let aa be their lowest common ancestor. Then uu lies in the subtree of a child a1a_1 of aa and vv in the subtree of a later child aka_k, k≥2k \ge 2, since aa is the lowest common ancestor and uu is not an ancestor of vv. Let d=depth(a)d = \text{depth}(a). On [o(u),o(v)][o(u), o(v)]:

  • every position inside the pair of a child of aa has excess at least d+1d + 1, by Lemma 2 applied to that child, since its own ( is still open;
  • the positions c(a1),…,c(ak−1)c(a_1), \ldots, c(a_{k-1}) lie in the range, because c(a1)>o(u)c(a_1) > o(u) (Lemma 1) and c(ak−1)<o(ak)≤o(v)c(a_{k-1}) < o(a_k) \le o(v), and each has excess exactly dd;
  • the range lies inside the pair of aa and after o(a1)o(a_1), and c(aj)c(a_j) is followed directly by o(aj+1)o(a_{j+1}), so every position of the range is either inside the pair of some child aja_j or is one of the closings c(aj)c(a_j).

So the minimum of EE on [o(u),o(v)][o(u), o(v)] is dd, reached first at m=c(a1)m = c(a_1), or at another c(aj)c(a_j) with j<kj < k. In every case P[m+1]=(P[m + 1] = ( is the opening of aj+1a_{j+1}, a child of aa, and enclose(m+1)=a\text{enclose}(m + 1) = a by the PARENT case. If uu is an ancestor of vv, the answer is uu, which line L12 returns. ■\blacksquare

Back to Balanced parentheses representation