Ropes: local balance and the height bound #
Rope.balanceNode l r joins two well-formed ropes whose heights differ by at most two into one
well-formed rope, using a single or a double rotation when one side is two levels taller. It is
the constructor that join uses on its way back up the spine, following the AVL join of
Blelloch, Ferizovic and Sun, Figure 2. The result represents
l.toList ++ r.toList, and its height is between max l.height r.height and one more.
A well-formed rope of height h has at least Nat.fib (h + 2) leaves, since its sibling heights
differ by at most one and its internal nodes have non-empty children. Each leaf holds at least one
element, so the height is at most 2 * Nat.log 2 length. No + 1 is needed, because empty and
a leaf both have height 0.
Main definitions #
Rope.balanceNode: a node overlandrthat rotates when their heights differ by two.Rope.balanceL,Rope.balanceR: the rotations for a left or right child that is two levels taller.
Main results #
Rope.toList_balanceNode,Rope.chunks_balanceNode,Rope.isWF_balanceNode,Rope.height_balanceNode: the list model, the leaf chunks, well-formedness and the height effect ofbalanceNode.Rope.balanceNode_eq_mkNode:balanceNodeismkNodewhen the heights differ by at most one.Rope.leaves_le_length: a chunked rope has at most as many leaves as elements.Rope.fib_height_le_leaves: the Fibonacci lower bound on the number of leaves of a non-empty well-formed rope.Rope.height_le_two_mul_log: a well-formed rope has height at most2 * Nat.log 2 length.
References #
Rotations #
A node over l and r when l is two levels taller than r. A single right rotation is
used when the outer grandchild ll is at least as tall as the inner one lr, and otherwise a
double rotation through lr.
The list model and the leaf chunks #
balanceNode keeps the leaf chunks of its arguments, in order.
balanceNode represents the concatenation of its arguments, with no hypotheses.
Well-formedness and the height effect #
The height of balanceNode is between max l.height r.height and one more. When one side is
empty, the result is the other side, so the lower bound is an equality.
The height bound #
A chunked rope has at most as many leaves as elements.