# ⓘ Weight-balanced tree. In computer science, weight-balanced binary trees are a type of self-balancing binary search trees that can be used to implement dynamic s ..

## ⓘ Weight-balanced tree

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

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 stores the size of the subtree rooted at the node, and the sizes of left and right subtrees are kept within some factor of each other. 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 elements index in sorted order.

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.

## 1. 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

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

By definition, the size of a leaf typically represented by a nil pointer is zero. The size of an internal node is the sum of sizes of its two children, plus one, t)

Here, Split is presumed to return two trees: one holding the keys less its input key, one holding the greater keys. The algorithm is non-destructive, but an in-place destructive version exists as well.

The algorithm for intersection or difference is similar, but requires the Join2 helper routine that is the same as Join but without the middle key. Based on the new functions for union, intersection or difference, either one key or multiple keys can be inserted to or deleted from the weight-balanced tree. Since Split and Union call Join but do not deal with the balancing criteria of weight-balanced trees directly, such an implementation is usually called the join-based algorithms.

The complexity of each of union, intersection and difference is O m log ⁡ n m + 1) {\displaystyle O\leftm\log \left{n \over m}+1\right\right)} for two weight-balanced trees of sizes m {\displaystyle m} and n ≥ m {\displaystyle n\geq m}. This complexity is optimal in terms of the number of comparisons. More importantly, since the recursive calls to union, intersection or difference are independent of each other, they can be executed in parallel with a parallel depth O log ⁡ m log ⁡ n {\displaystyle O\log m\log n}. When m = 1 {\displaystyle m=1}, the join-based implementation has the same computational directed acyclic graph DAG as single-element insertion and deletion if the root of the larger tree is used to split the smaller tree.