User blog:Vel!/Dollar function redux

The dollar function \(\$(n, T)\) takes two arguments; the first is a positive integer and the second is a dollar tree as defined below.

Money don't grow on trees
A dollar tree is a tree data structure where each leaf is labeled with a nonnegative integer. The root node is never labeled.

Basic rules
"Leftmost node" refers to the first leaf in the tree.


 * 1) If \(T\) contains only a root node, then \($(n, T) = n\).
 * 2) If the first child of \(T\) is a leaf with label \(a\), chop it off to form \(T'\). Then \(\$(n, T) = \$(n + a, T')\).
 * 3) If the leftmost node of \(T\) has label 0, and its parent has no other children, then chop it off and label its parent with \(n\).
 * 4) If the leftmost node of \(T\) has label 0, and its parent does have other children, then chop it off.
 * 5) If the leftmost node of \(T\) has label \(a + 1\), then relabel it with \(a\) and add \(n - 1\) more copies of its parent node.