# Weight-balanced tree

For other uses, see Optimal binary search tree.

In computer science, weight-balanced binary trees (WBTs) are a type of self-balancing binary search trees that can be used to implement dynamic sets, dictionaries (maps) and sequences.[1] These trees were introduced by Nievergelt and Reingold in the 1970s as trees of bounded balance, or BB[α] trees.[2][3] Their more common name is due to Knuth.[4]

Like other self-balancing trees, WBTs store bookkeeping information pertaining to balance in their nodes and perform rotations to restore balance when it is disturbed by insertion or deletion operations. Specifically, each node has a weight that is either the size of the subtree rooted at the node, or (in the original presentation) the size plus one. Unlike the balance information in AVL trees (which store the height of subtrees) and red-black trees (which store a fictional "color" bit), the bookkeeping information in a WBT is an actually useful property for applications: the number of elements in a tree is equal to the size of its root, and the size information is exactly the information needed to implement the operations of an order statistic tree, viz., getting the n'th largest element in a set or determining an element's index in sorted order.[5]

Weight-balanced trees are popular in the functional programming community and are used to implement sets and maps in MIT Scheme, SLIB and implementations of Haskell.[6][4]

## Description

A weight-balanced tree is a binary search tree that stores the sizes of subtrees in the nodes. That is, a node has fields

• key, of any ordered type
• value (optional, only for mappings)
• left, right, pointer to node
• size, of type integer.

By definition, the size of a leaf (typically representing by a nil pointer) is zero. The size of an internal node is the sum of sizes of its two children, plus one (size[n] = size[n.left] + size[n.right] + 1). Operations that modify the tree must make sure that the sizes of the left and right subtrees of every node remain within some factor of each other. The exact factor is a parameter.

The rebalancing operations used to do so are the same as those for an AVL tree, rotations and double rotations. Applying them correctly guarantees a tree of n elements will have height O(log n); the number of balancing operations required in a sequence of n insertions and deletions is linear in n, i.e., constant in an amortized sense.[7]

## References

1. ^ Tsakalidis, A. K. (1984). "Maintaining order in a generalized linked list". Acta Informatica 21: 101. doi:10.1007/BF00289142. edit
2. ^ Nievergelt, J.; Reingold, E. M. (1973). "Binary Search Trees of Bounded Balance". SIAM Journal on Computing 2: 33. doi:10.1137/0202005. edit
3. ^ Black, Paul E. "BB(α) tree". Dictionary of Algorithms and Data Structures. NIST.
4. ^ a b Hirai, Y.; Yamamoto, K. (2011). "Balancing weight-balanced trees". Journal of Functional Programming 21 (3): 287. doi:10.1017/S0956796811000104. edit
5. ^ Roura, Salvador (2001). A new method for balancing binary search trees. ICALP. Lecture Notes in Computer Science 2076. pp. 469–480. doi:10.1007/3-540-48224-5_39. ISBN 978-3-540-42287-7. edit
6. ^ Adams, Stephen (1993). "Functional Pearls: Efficient sets—a balancing act". Journal of Functional Programming 3 (4): 553–561. doi:10.1017/S0956796800000885. edit
7. ^ Blum, Norbert; Mehlhorn, Kurt (1980). "On the average number of rebalancing operations in weight-balanced trees". Theoretical Computer Science 11 (3): 303–320. doi:10.1016/0304-3975(80)90018-3. edit