In computer science and probability theory, a random binary tree is a binary tree selected at random from some probability distribution on binary trees. Two different distributions are commonly used: binary trees formed by inserting nodes one at a time according to a random permutation, and binary trees chosen from a uniform discrete distribution in which all distinct trees are equally likely. It is also possible to form other distributions, for instance by repeated splitting. Adding and removing nodes directly in a random binary tree will in general disrupt its random structure, but the treap and related randomized binary search tree data structures use the principle of binary trees formed from a random permutation in order to maintain a balanced binary search tree dynamically as nodes are inserted and deleted.

For random trees that are not necessarily binary, see random tree.

Random binary tree

Binary trees from random permutations

For any set of numbers (or, more generally, values from some total order), one may form a binary search tree in which each number is inserted in sequence as a leaf of the tree, without changing the structure of the previously inserted numbers. The position into which each number should be inserted is uniquely determined by a binary search in the tree formed by the previous numbers. For instance, if the three numbers (1,3,2) are inserted into a tree in that sequence, the number 1 will sit at the root of the tree, the number 3 will be placed as its right child, and the number 2 as the left child of the number 3. There are six different permutations of the numbers (1,2,3), but only five trees may be constructed from them. That is because the permutations (2,1,3) and (2,3,1) form the same tree.

Random binary tree

Expected depth of a node

For any fixed choice of a value in a given set of numbers, if one randomly permutes the numbers and forms a binary tree from them as described above, the expected value of the length of the path from the root of the tree to is at most , where "" denotes the natural logarithm function and the introduces big O notation. For, the expected number of ancestors of is by linearity of expectation equal to the sum, over all other values in the set, of the probability that is an ancestor of . And a value is an ancestor of exactly when is the first element to be inserted from the elements in the interval . Thus, the values that are adjacent to in the sorted sequence of values have probability of being an ancestor of , the values one step away have probability , etc. Adding these probabilities for all positions in the sorted sequence gives twice a Harmonic number, leading to the bound above. A bound of this form holds also for the expected search length of a path to a fixed value that is not part of the given set.

Random binary tree

To understand it by using min-max records. The number in a random permutation is the min (max) record means it is the min (max) value from the first position to its position. Consider a simple example = (2, 4, 3, 6, 5, 1). The min records in are 2, 1 by searching the min value from start to the end. Similarly, the max recodes in are 2, 4, 6. The expected number of min (max) records is all probability of each number in a random permutation. For position , it has the probability of 1/. Therefore, the expected number of min (max) records is a Harmonic number. To search 3 in , all numbers in (2, 1) is less than 3, and (4, 6, 5, 1) is bigger than 3. The searching on 's random binary tree only counts max records on (2, 1) and min records on (4, 6, 5, 1)—this is why it is twice a Harmonic number.

The longest path

Although not as easy to analyze as the average path length, there has also been much research on determining the expectation (or high probability bounds) of the length of the longest path in a binary search tree generated from a random insertion order. It is now known that this length, for a tree with nodes, is almost surely

where is the unique number in the range satisfying the equation

Expected number of leaves

In the random permutation model, each of the numbers from the set of numbers used to form the tree, except for the smallest and largest of the numbers, has probability of being a leaf in the tree, for it is a leaf when it inserted after its two neighbors, and any of the six permutations of these two neighbors and it are equally likely. By similar reasoning, the smallest and largest of the numbers have probability of being a leaf. Therefore, the expected number of leaves is the sum of these probabilities, which for is exactly .