Access, rank, select and quantile on the wavelet tree are correct full
This proof shows: correctness.
Notation as on the page: node v covers [av,bv), Sv is the subsequence of the symbols of S in that range, in order, and for an inner node mv=⌊(av+bv)/2⌋, Bv[i]=[Sv[i]≥mv], with left child ℓ(v) covering [av,mv) and right child r(v) covering [mv,bv). Since bv−av≥2 at an inner node, av<mv<bv, so both children cover nonempty ranges and the descent ends at a leaf after at most ⌈lgσ⌉ steps.
Lemma (mapping). Let v be an inner node and 0≤i≤∣Sv∣. The symbols of Sv[0…i−1] that are less than mv, in order, are Sℓ(v)[0…rank0(Bv,i)−1], and those that are at least mv are Sr(v)[0…rank1(Bv,i)−1]. In particular, if i<∣Sv∣ and Bv[i]=0, then Sv[i]=Sℓ(v)[rank0(Bv,i)], and symmetrically for 1.
Proof. Sℓ(v) is the subsequence of S of the symbols in [av,mv), which is also the subsequence of Sv of the symbols less than mv, because [av,mv)⊆[av,bv) and taking subsequences keeps order. The symbols of Sv less than mv are those at the positions with Bv=0. So the first rank0(Bv,i) of them are those at the positions with Bv=0 below i. For the second claim, apply the first with i+1: the symbol Sv[i] is the last one of the prefix of length rank0(Bv,i+1)=rank0(Bv,i)+1. The right side is symmetric. □
Access. Invariant before each test of line L2: v is the node covering [a,b), 0≤i<∣Sv∣ and Sv[i]=S[i0], where i0 is the input. It holds at the root. At an inner node, if Bv[i]=0 then Sv[i]<m and, by the lemma, Sv[i]=Sℓ(v)[rank0(Bv,i)]; line L4 moves to exactly that node and position. Line L5 is symmetric. At a leaf, [a,b)={a}, and every symbol of Sv is a, so S[i0]=a, which line L6 returns.
Rank. Let c∈[0,σ) and 0≤i0≤n. Invariant: c∈[a,b), v covers [a,b), and the number of c in S[0…i0−1] equals the number of c in Sv[0…i−1]. At the root it is trivial. At an inner node with c<m, all copies of c in Sv[0…i−1] are less than m, so by the lemma they are exactly the copies of c in Sℓ(v)[0…rank0(Bv,i)−1]; line L10 keeps the invariant, and line L11 is symmetric. At a leaf, every symbol of Sv is c, so the count is i, which line L12 returns.
Select. Claim: for a node v with c∈[av,bv) and 1≤j≤ the number of c in Sv, SELECT(v,av,bv,c,j) returns the position in Sv of the j-th c of Sv. By induction on the height of v. At a leaf, all symbols are c and the j-th is at j−1 (line L13). At an inner node with c<mv, the copies of c in Sv all lie in Sℓ(v) in the same order, so by induction $p = $ SELECT(ℓ(v),…) is the position in Sℓ(v) of the j-th c. By the lemma, position p of Sℓ(v) is the position q of Sv with Bv[q]=0 and rank0(Bv,q)=p, which is the (p+1)-th 0 of Bv, that is select0(Bv,p+1), line L15. Line L16 is symmetric. At the root, Sv=S.
Quantile. Invariant: v covers [a,b), the multiset M of the symbols of Sv[l…r−1] is the multiset of the symbols of S[l0…r0−1] that lie in [a,b), and the answer, the k0-th smallest of S[l0…r0−1], is the k-th smallest of M (in particular 1≤k≤∣M∣). Initially it holds. At an inner node, by the lemma applied to r and to l, the symbols of Sv[l…r−1] less than m are those of Sℓ(v)[rank0(Bv,l)…rank0(Bv,r)−1], and there are z=rank0(Bv,r)−rank0(Bv,l) of them; the others are in the corresponding range of Sr(v). In sorted order, the z symbols less than m come first. If k≤z, the k-th smallest of M is the k-th smallest of the left part, line L20. Otherwise it is the (k−z)-th smallest of the right part, line L21. At a leaf M consists of copies of a, and k≤∣M∣, so the answer is a, line L22. ■