# Order statistics binary search tree

Contrary to what its nicknames suggest, this data structure can be very useful, and is also known to be easy to implement. Since the only extra piece of information that needs to be stored is sizes of the nodes instead of other "useless" fields such as randomized weights in treaps or colours in red—black tress , this makes it very convenient to implement the select-by-rank and get-rank operations easily transforming it into an order statistic tree.

It supports standard binary search tree operations such as insertion, deletion, and searching in O log n time. The size balanced tree examines each node's size i. Each node in the tree satisfies the following two properties:. In other words, each child node of is not smaller in size than the child nodes of its sibling. Clearly, we should consider the sizes of nonexistent children and siblings to be 0.

Consider the following example where is the node in question, are its child nodes, and are subtrees which also satisfy the above SBT properties on their own. Then, the node must satisfy:. After insertions and deletions, the new sizes of subtrees may violate the two properties above. Thus, we define a procedure maintain T to rebalance the SBT rooted at the node. This should be called with the precondition that 's children are already SBTs themselves.

Since property 1 and 2 are symmetrical, we will only discuss property 1. With this casework being taken care of, it becomes straightforward to actually implement maintain.

This pseudocode is slightly slow and redundant. Since we know that the two SBT properties will usually be satisfied, the following is an optimization. If the flag is TRUE, then we examine cases 1 and 2, otherwise we examine cases 3 and 4. Doing so will eliminate many unnecessary comparisons.

The proof for why maintain t. Furthermore, the running time of maintain is O 1 amortized which means that you do not have to worry about it not terminating. Searching in SBTs is exactly the same as searching in other binary search trees.

Order-statistic trees can be further amended with bookkeeping information to maintain balance e. Alternatively, the size field can be used in conjunction with a weight-balancing scheme at no additional storage cost.

Another way to implement an order statistic tree is an implicit data structure derived from the min-max heap. From Wikipedia, the free encyclopedia. In computer science , an order statistic tree is a variant of the binary search tree or more generally, a B-tree [1] that supports two additional operations beyond insertion, lookup and deletion: Select i — find the i' th smallest element stored in the tree Rank x — find the rank of element x in the tree, i. Retrieved 18 January Introduction to Algorithms 2nd ed.

Introduction to Algorithms 3rd ed. A new method for balancing binary search trees. Lecture Notes in Computer Science. Communications of the ACM.