Advanced Data Structures – Summer 2026 – Lecture 2

A.k.a.: applications of rank and select.
Application: in-place json parsing

An ordered tree is a tree with:
How many ordered trees are there at least? At most?

There is a bijection between ordered trees on \(n\) nodes and balanced parentheses (BP) sequences of \(n-1\) "(" and ")", where each "(" has a matching ")".
"Trace" the outline of the tree in DFS-order: each time an edge goes down, write "(", and each time an edge goes up, write ")".

There are \(o(4^n)\) ordered trees on \(n\) nodes.
The number of BP sequences is at most
\[\textsf{#trees} \leq \binom{2(n-1)}{n-1} = \Theta\left(\frac{2^{2n}}{\sqrt n}\right).\]
Or simpler: each of the \(2(n-1)\) positions is ( or ), giving \(\leq
2^{2(n-1)}< 4^n\).

There are \(\Omega(4^n/n^{1.5})\) ordered trees on \(n\) nodes.
Partition the \(\binom{2(n-1)}{n-1}\) possible BP sequences in equivalence classes under rotation of the string. Take a random element of any class. Find the prefix with the largest value of \(\mathsf{count}_)(i) - \mathsf{count}_((i)\), and consider the rotation starting after this prefix. This is a BP. Thus, each class has size \(\leq 2(n-1)\), and contains at least one BP:
\[\textsf{#trees} \geq \frac 1{2(n-1)}\binom{2(n-1)}{n-1} = \Omega\left(\frac{2^{2n}}{n^{1.5}}\right).\]

We need at least \[\frac 1n\log_2(4^n/n^{1.5}) = 2 - o(1)\] bits per node, and this is tight.
Represent trees using \(2 + o(1)\) bits per node, while supporting common operations.

The exact number of ordered trees on \(n\) nodes is the Catalan number: \[ C_{n-1} := \frac1{n} \binom{2(n-1)}{n-1} \sim \frac{4^{n-1}}{n^{3/2} \sqrt \pi}. \]


Start with 10.
Then append the degree of each node in level/BFS order in unary:
1 for each child, and then a 0.
10111100110011001100000
a_bchiABdeCHjkIDfgEJKFG
\(2n+1\) bits:

How to implement operations:
10111100110011001100000
a_bchiABdeCHjkIDfgEJKFG
\[ \newcommand{\rank}{\mathsf{rank}} \newcommand{\select}{\mathsf{select}} \]

10111100110011001100000
a_bchiABdeCHjkIDfgEJKFG
Number nodes \(0\) to \(n-1\) in level/BFS-order.

Traverse tree in DFS-order.
Write a ( each time a node/subtree is first entered, and a ) a node/subtree
is left.
(()(()(()()))()(()()))
1101101101000101101000 10 instead of ()
abBcdDefFgGEChHijJkKIA
\(2n\) bits: a ( and ) for each node.
Note: the children of a are all over the place!
\(\rank\) and \(\select\) won't work here.

( at pos \(i\), get the position of the matching ).) at pos \(i\), get the position of the matching (.( at pos \(i\), get the position of the
closest ( left of it enclosing it.Can all be done in \(O(1)\) time and \(o(n)\) space
Implementation: See Navarro and Sadakane (2014)


(()(()(()()))()(()()))
1101101101000101101000 10 instead of ()
abBcdDefFgGEChHijJkKIA
Number nodes \(0\) to \(n-1\) in DFS-order.

Start with 1.
Then traverse the nodes in DFS order, and append for each its degree in unary:
a 1 for each child and then a 0.
Then convert 1 to ( and 0 to ) to get a BP sequence.
((((())(())(())))(()))
1111100110011000011000
aihcbABedCDgfEFGHkjIJK
\(2n\) bits: 1 to introduce each node,
0 to end the unary degree encoding of each node.

((((())(())(())))(()))
1111100110011000011000
aihcbABedCDgfEFGHkjIJK
): \(\mathsf{findclose}(q-1-i)+1\)) that leaves the
subtree:
\((\mathsf{findclose}(\mathsf{enclose}(p)) - p)/2 + 1\)



