Random binary tree
In computer science and probability theory, a random binary tree is a binary tree selected at random from some probability distribution on binary trees. Different distributions have been used, leading to different properties for these trees.
Random binary trees have been used for analyzing the average-case complexity of data structures based on binary search trees. For this application it is common to use random trees formed by inserting nodes one at a time according to a random permutation.[1] The resulting trees are very likely to have logarithmic depth and logarithmic Strahler number. The treap and related balanced binary search trees use update operations that maintain this random structure even when the update sequence is non-random.
Other distributions on random binary trees include the uniform discrete distribution in which all distinct trees are equally likely, distributions on a given number of nodes obtained by repeated splitting, binary tries and radix trees for random data, and trees of variable size generated by branching processes.
For random trees that are not necessarily binary, see random tree.
Background
[edit]A binary tree is a rooted tree in which each node may have up to two children (the nodes directly below it in the tree), and those children are designated as being either left or right. It is sometimes convenient instead to consider extended binary trees in which each node is either an external node with zero children, or an internal node with exactly two children. A binary tree that is not in extended form may be converted into an extended binary tree by treating all its nodes as internal, and adding an external node for each missing child of an internal node. In the other direction, an extended binary tree with at least one internal node may be converted back into a non-extended binary tree by removing all its external nodes. In this way, these two forms are almost entirely equivalent for the purposes of mathematical analysis, except that the extended form allows a tree consisting of a single external node, which does not correspond to anything in the non-extended form. For the purposes of computer data structures, the two forms differ, as the external nodes of the first form may be represented explicitly as objects in a data structure.[2]
In a binary search tree the internal nodes are labeled by numbers or other ordered values, called keys, arranged so that an inorder traversal of the tree lists the keys in sorted order. The external nodes remain unlabeled.[3] Binary trees may also be studied with all nodes unlabeled, or with labels that are not given in sorted order. For instance, the Cartesian tree data structure uses labeled binary trees that are not necessarily binary search trees.[4]
A random binary tree is a random tree drawn from a certain probability distribution on binary trees. In many cases, these probability distributions are defined using a given set of keys, and describe the probabilities of binary search trees having those keys. However, other distributions are possible, not necessarily generating binary search trees, and not necessarily giving a fixed number of nodes.[5]
From random permutations
[edit]For any sequence of distinct ordered keys, one may form a binary search tree in which each key is inserted in sequence as a leaf of the tree, without changing the structure of the previously inserted keys. The position for each insertion can be found by a binary search in the previous tree. The random permutation model, for a given set of keys, is defined by choosing the sequence randomly from the permutations of the set, with each permutation having equal probability.[6]
For instance, if the three keys 1,3,2 are inserted into a binary search 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 keys 1,2, and 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. Thus, this tree has probability of being generated, whereas the other four trees each have probability .[5]
Expected depth of a node
[edit]For any key in a given set of keys, the expected value of the length of the path from the root to in a random binary search tree is at most , where "" denotes the natural logarithm function and the introduces big O notation. By linearity of expectation, the expected number of ancestors of equals the sum, over other keys , of the probability that is an ancestor of . A key is an ancestor of exactly when is the first key to be inserted from the interval . Because each key in the interval is equally likely to be first, this happens with probability inverse to the length of the interval. Thus, the keys that are adjacent to in the sorted sequence of keys have probability of being an ancestor of , the keys one step away have probability , etc. The sum of these probabilities forms two copies of the harmonic series extending away from in both directions in the sorted sequence, giving the bound above. This bound also holds for the expected search path length for a value that is one of the given keys.[7]
The longest path
[edit]The longest root-to-leaf path, in a random binary search tree, is longer than the expected path length, but only by a constant factor. Its length, for a tree with nodes, is with high probability approximately
where is the unique number in the range satisfying the equation
Expected number of leaves
[edit]In the random permutation model, each key except the smallest and largest has probability of being a leaf in the tree. This is because it is a leaf when it inserted after its two neighbors, which happens for two out of the six permutations of it and its two neighbors, all of which are equally likely. By similar reasoning, the smallest and largest key have probability of being a leaf. Therefore, the expected number of leaves is the sum of these probabilities, which for is exactly .[9]
Strahler number
[edit]The Strahler number of vertices in any tree is a measure of the complexity of the subtrees under those vertices. A leaf (external node) has Strahler number one. For any other node, the Strahler number is defined recursively from the Strahler numbers of its children. In a binary tree, if two children have different Strahler numbers, the Strahler number of their parent is the larger of the two child numbers. But if two children have equal Strahler numbers, their parent has a number that is greater by one. The Strahler number of the whole tree is the number at the root node. For -node random binary search trees, simulations suggest that the expected Strahler number is . A weaker upper bound has been proven.[10]
Treaps and randomized binary search trees
[edit]In applications of binary search tree data structures, it is rare for the keys to be inserted without deletion in a random order, limiting the direct applications of random binary trees. However, algorithm designers have devised data structures that allow arbitrary insertions and deletions to preserve the property that the shape of the tree is random, as if the keys had been inserted randomly.[11]
If a given set of keys is assigned numeric priorities (unrelated to their values), these priorities may be used to construct a Cartesian tree for the numbers, the binary search tree that would result from inserting the keys in priority order. By choosing the priorities to be independent random real numbers in the unit interval, and by maintaining the Cartesian tree structure using tree rotations after any insertion or deletion of a node, it is possible to maintain a data structure that behaves like a random binary search tree. Such a data structure is known as a treap or a randomized binary search tree.[11]
Variants of the treap including the zip tree and zip-zip tree replace the tree rotations by "zipping" operations that split and merge trees, and that limit the number of random bits that need to be generated and stored alongside the keys. The result of these optimizations is still a tree with a random structure, but one that does not exactly match the random permutation model.[12]
Uniformly random binary trees
[edit]The number of binary trees with nodes is a Catalan number.[13] For these numbers of trees are
Thus, if one of these trees is selected uniformly at random, its probability is the reciprocal of a Catalan number. Trees generated from a model in this distribution are sometimes called random binary Catalan trees.[14] They have expected depth proportional to the square root of , rather than to the logarithm.[15] More precisely, the expected depth of a randomly chosen node in an -node tree of this type is
The expected Strahler number of a uniformly random -node binary tree is , lower than the expected Strahler number of random binary search trees.[17]
Due to their large heights, this model of equiprobable random trees is not generally used for binary search trees. However, it has other applications, including:
- Modeling the parse trees of algebraic expressions in compiler design.[18] Here the internal nodes of the tree represent binary operations in an expression and the external nodes represent the variables or constants on which the expressions operate. The bound on Strahler number translates into the number of registers needed to evaluate an expression.[19]
- Modeling river networks, the original application for which the Strahler number was developed.[20]
- Modeling possible evolutionary trees for a fixed number of species. In this application, an extended binary tree is used, with the species at its external nodes.[21]
An algorithm of Jean-Luc Rémy generates a uniformly random binary tree of a specified size in time linear in the size, by the following process. Start with a tree consisting of a single external node. Then, while the current tree has not reached the target size, repeatedly choose one of its nodes (internal or external) uniformly at random. Replace the chosen node by a new internal node, having the chosen node as one of its children (equally likely left or right), and having a new external node as its other child. Stop when the target size is reached.[22]
Branching processes
[edit]The Galton–Watson process describes a family of distributions on trees in which the number of children at each node is chosen randomly, independently of other nodes. For binary trees, two versions of the Galton–Watson process are in use, differing only in whether an extended binary tree with only one node, an external root node, is allowed:
- In the version where the root node may be external, it is chosen to be internal with some specified probability or external with probability . If it is internal, its two children are trees generated recursively by the same process.
- In the version where the root node must be internal, its left and right children are determined to be internal with probability or external with probability , independently of each other. In the case where they are internal, they are the roots of trees that are generated recursively by the same process.
Trees generated in this way have been called binary Galton–Watson trees. In the special case where they are called critical binary Galton–Watson trees.[23]
Analysis
[edit]The probability marks a phase transition for the binary Galton–Watson process: for the resulting tree is almost certainly finite, whereas for it is infinite with positive probability. More precisely, for any , the probability that the tree remains finite is
Another way to generate the same trees is to make a sequence of coin flips, with probability of heads and probability of tails, until the first flip at which the number of tails exceeds the number of heads (for the model in which an external root is allowed) or exceeds one plus the number of heads (when the root must be internal), and then use this sequence of coin flips to determine the choices made by the recursive generation process, in depth-first order.[25]
Because the number of internal nodes equals the number of heads in this coin flip sequence, all trees with a given number of nodes are generated from (unique) coin flip sequences of the same length, and are equally likely, regardless of . That is, the choice of affects the variation in the size of trees generated by this process, but for a given size the trees are generated uniformly at random.[26] For values of below the critical probability , smaller values of will produce trees with a smaller expected size, while larger values of will produce trees with a larger expected size. At the critical probability there is no finite bound on the expected size of trees generated by this process. More precisely, for any , the expected number of nodes at depth in the tree is , and the expected size of the tree can be obtained by summing the expected numbers of nodes at each depth. For this gives a geometric series
for the expected tree size, but for this gives 1 + 1 + 1 + 1 + ⋯, a divergent series.[27]
For , any particular tree with internal nodes is generated with probability , and the probability that a random tree has this size is this probability multiplied by a Catalan number,
Applications
[edit]Galton–Watson processes were originally developed to study the spread and extinction of human surnames, and have been widely applied more generally to the dynamics of human or animal populations. These processes have been generalized to models where the probability of being an internal or external node at a given level of the tree (a generation, in the population dynamics application) is not fixed, but depends on the number of nodes at the previous level.[29] A version of this process, with the critical probability , has been studied as a model for speciation, where it is known as the critical branching process. In this process, each species has an exponentially distributed lifetime, and over the course of its lifetime produces child species at a rate equal to the lifetime. When a child is produced, the parent continues as the left branch of the evolutionary tree, and the child becomes the right branch.[30]
Another application of critical Galton–Watson trees (in the version where the root must be internal) arises in the Karger–Stein algorithm for finding minimum cuts in graphs, using a recursive edge contraction process. This algorithm calls itself twice recursively, with each call having probability at least of preserving the correct solution value. The random tree models the subtree of correct recursive calls. The algorithm succeeds on a graph of vertices whenever this random tree of correct recursive calls has a branch of depth at least , reaching the base case of its recursion. The success probability is , producing one of the logarithmic factors in the algorithm's runtime.[31]
Yule process
[edit]Devroye and Robson consider a related continuous-time random process in which each external node is eventually replaced by an internal node with two external children, at an exponentially distributed time after its first appearance as an external node. The number of external nodes in the tree, at any time, is modeled by a simple birth process or Yule process in which the members of a population give birth at a constant rate: giving birth to one child, in the Yule process, corresponds to being replaced by two children, in Devroye and Robson's model. If this process is stopped at any fixed time, the result is a binary tree of a random size (depending on the stopping time), distributed according to the random permutation model for that size. Devroye and Robson use this model as part of an algorithm to quickly generate trees in the random permutation model, described by their numbers of nodes at each depth rather than by their exact structure.[32] A discrete variant of this process starts with a tree consisting of a single external node, and repeatedly replaces a randomly-chosen external node by an internal node with two external children. Again, if this is stopped at a fixed time (with a fixed size), the resulting tree is distributed according to the random permutation model for that size.[1]
Binary tries
[edit]Another form of binary tree, the binary trie or digital search tree, has a collection of binary numbers labeling some of its external nodes. The internal nodes of the tree represent prefixes of their binary representations that are shared by two or more of the numbers. The left and right children of an internal node are obtained by extending the corresponding prefix by one more bit, a zero or a one bit respectively. If this extension does not match any of the given numbers, or it matches only one of them, the result is an external node; otherwise it is another internal node. Random binary tries have been studied, for instance for sets of random real numbers generated independently in the unit interval. Despite the fact that these trees may have some empty external nodes, they tend to be better balanced than random binary search trees. For uniformly random real numbers in the unit interval, or more generally for any square-integrable probability distribution on the unit interval, the average depth of a node is asymptotically , and the average height of the whole tree is asymptotically . The analysis of these trees can be applied to the computational complexity of trie-based sorting algorithms.[33]
A variant of the trie, the radix tree or compressed trie, eliminates empty external nodes and their parent internal nodes. The remaining internal nodes correspond to prefixes for which both possible extensions, by a zero or a one bit, are used by at least one of the randomly chosen numbers. For a radix tree for uniformly distributed binary numbers, the shortest leaf-root path has length and the longest leaf-root path has length both with high probability.[34]
Random split trees
[edit]Luc Devroye and Paul Kruszewski describe a recursive process for constructing random binary trees with nodes. It generates a real-valued random variable in the unit interval , assigns the first nodes (rounded down to an integer number of nodes) to the left subtree, the next node to the root, and the remaining nodes to the right subtree. Then, it continues recursively using the same process in the left and right subtrees. If is chosen uniformly at random in the interval, the result is the same as the random binary search tree generated by a random permutation of the nodes, as any node is equally likely to be chosen as root. However, this formulation allows other distributions to be used instead. For instance, in the uniformly random binary tree model, once a root is fixed each of its two subtrees must also be uniformly random, so the uniformly random model may also be generated by a different choice of distribution (depending on ) for . As they show, by choosing a beta distribution on and by using an appropriate choice of shape to draw each of the branches, the mathematical trees generated by this process can be used to create realistic-looking botanical trees.[35]
Notes
[edit]- ^ a b Drmota (2009), p. 19.
- ^ Knuth (1997).
- ^ Knuth (1973).
- ^ Vuillemin (1980).
- ^ a b Sedgewick & Flajolet (2013), p. 286.
- ^ Morin (2014).
- ^ Hibbard (1962); Knuth (1973); Mahmoud (1992), p. 75.
- ^ Robson (1979); Pittel (1985); Devroye (1986); Mahmoud (1992), pp. 91–99; Reed (2003).
- ^ Brown & Shubert (1984).
- ^ Kruszewski (1999).
- ^ a b Martínez & Roura (1998); Seidel & Aragon (1996); Morin (2014).
- ^ Tarjan, Levy & Timmel (2021); Gila, Goodrich & Tarjan (2023).
- ^ Drmota (2009), p. 26.
- ^ Sedgewick & Flajolet (2013), p. 287.
- ^ Knuth (2005), p. 15.
- ^ Sedgewick & Flajolet (2013), p. 288.
- ^ Devroye & Kruszewski (1995).
- ^ Mahmoud (1992), p. 63.
- ^ Flajolet, Raoult & Vuillemin (1979).
- ^ Shreve (1966).
- ^ Aldous (1996).
- ^ Rémy (1985); Mäkinen & Siltaneva (2003); Knuth (2005), pp. 16–17.
- ^ Burd, Waymire & Winn (2000).
- ^ This is a special case of a general theorem about criticality and extinction probabilities in Galton–Watson processes, according to which the extinction probability is the smallest positive root of the formula , where is the probability-generating function of the distribution on the number of children, here . See e.g. Jagers (2011), Theorem 2.1, p. 92. Jagers carries out the calculation of this root for the binary case on p. 97.
- ^ For the connection between trees and random walks (as generated by random coin flips) see e.g. Section 6, "Walks and trees" pp. 483–486, of Harris (1952).
- ^ Broutin, Devroye & Fraiman (2020). More generally, every Galton–Watson process, conditioned on producing trees of a certain size, produces the same probability distribution as a critical Galton–Watson process: see section 2 of Kennedy (1975).
- ^ For the expected number of nodes at each level of the tree, see e.g. Athreya & Ney (1972), Section I.A.2: Moments, p. 4.
- ^ By the equivalence between trees and random walks, this is the same as the probability of first returning to zero after steps in a simple random walk, for which see e.g. Bertin (2021), 2.5.1 Statistics of First Return Times to the Origin of a Random Walk, pp. 70–72.
- ^ Jagers (2011).
- ^ Popovic (2004).
- ^ Karger & Stein (1996).
- ^ Devroye & Robson (1995).
- ^ Devroye (1984).
- ^ Devroye (1992).
- ^ Devroye & Kruszewski (1996).
References
[edit]- Aldous, David (1996), "Probability distributions on cladograms", in Aldous, David; Pemantle, Robin (eds.), Random Discrete Structures, The IMA Volumes in Mathematics and its Applications, vol. 76, Springer-Verlag, pp. 1–18, doi:10.1007/978-1-4612-0719-1_1, ISBN 978-1-4612-6881-9
- Athreya, Krishna B.; Ney, Peter E. (1972), Branching Processes, Berlin: Springer-Verlag, pp. 199–206, doi:10.1007/978-3-642-65371-1, ISBN 978-3-642-65371-1
- Bertin, Eric (2021), Statistical Physics of Complex Systems: A Concise Introduction, Springer Series in Synergetics (3rd ed.), Springer International Publishing, Bibcode:2021spcs.book.....B, doi:10.1007/978-3-030-79949-6, ISBN 9783030799496
- Brown, Gerald G.; Shubert, Bruno O. (1984), "On random binary trees", Mathematics of Operations Research, 9: 43–65, doi:10.1287/moor.9.1.43
- Broutin, Nicolas; Devroye, Luc; Fraiman, Nicolas (April 2020), "Recursive functions on conditional Galton–Watson trees" (PDF), Random Structures & Algorithms, 57 (2), Wiley: 304–316, arXiv:1805.09425, doi:10.1002/rsa.20921
- Burd, Gregory A.; Waymire, Edward C.; Winn, Ronald D. (February 2000), "A self-similar invariance of critical binary Galton–Watson trees", Bernoulli, 6 (1): 1–21, doi:10.2307/3318630, JSTOR 3318630
- Devroye, Luc (1984), "A probabilistic analysis of the height of tries and of the complexity of triesort" (PDF), Acta Informatica, 21 (3): 229–237, doi:10.1007/BF00264248
- Devroye, Luc (1986), "A note on the height of binary search trees" (PDF), Journal of the ACM, 33 (3): 489–498, doi:10.1145/5925.5930
- Devroye, Luc (January 1992), "A note on the probabilistic analysis of patricia trees" (PDF), Random Structures & Algorithms, 3 (2): 203–214, doi:10.1002/rsa.3240030209
- Devroye, Luc; Kruszewski, Paul (1995), "A note on the Horton–Strahler number for random trees" (PDF), Information Processing Letters, 56 (2): 95–99, doi:10.1016/0020-0190(95)00114-R
- Devroye, Luc; Kruszewski, Paul (1996), "The botanical beauty of random binary trees" (PDF), in Brandenburg, Franz J. (ed.), Graph Drawing: 3rd Int. Symp., GD'95, Passau, Germany, September 20–22, 1995, Lecture Notes in Computer Science, vol. 1027, Springer-Verlag, pp. 166–177, doi:10.1007/BFb0021801, ISBN 978-3-540-60723-6
- Devroye, Luc; Robson, John Michael (December 1995), "On the generation of random binary aearch trees" (PDF), SIAM Journal on Computing, 24 (6): 1141–1156, doi:10.1137/s0097539792224954
- Drmota, Michael (2009), Random Trees: An Interplay between Combinatorics and Probability, Springer-Verlag, doi:10.1007/978-3-211-75357-6, ISBN 978-3-211-75355-2
- Flajolet, P.; Raoult, J. C.; Vuillemin, J. (1979), "The number of registers required for evaluating arithmetic expressions" (PDF), Theoretical Computer Science, 9 (1): 99–125, doi:10.1016/0304-3975(79)90009-4
- Gila, Ofek; Goodrich, Michael T.; Tarjan, Robert E. (2023), "Zip-zip trees: making zip trees more balanced, biased, compact, or persistent", in Morin, Pat; Suri, Subhash (eds.), Algorithms and Data Structures – 18th International Symposium, WADS 2023, Montreal, QC, Canada, July 31 – August 2, 2023, Proceedings, Lecture Notes in Computer Science, vol. 14079, Springer, pp. 474–492, arXiv:2307.07660, doi:10.1007/978-3-031-38906-1_31, ISBN 978-3-031-38905-4
- Harris, T. E. (1952), "First passage and recurrence distributions", Transactions of the American Mathematical Society, 73 (3): 471–486, doi:10.1090/s0002-9947-1952-0052057-2
- Hibbard, Thomas N. (1962), "Some combinatorial properties of certain trees with applications to searching and sorting", Journal of the ACM, 9 (1): 13–28, doi:10.1145/321105.321108
- Jagers, Peter (2011), "Extinction, persistence, and evolution", in Chalub, Fabio A. C. C.; Rodrigues, José Francisco (eds.), The Mathematics of Darwin's Legacy, Mathematics and Biosciences in Interaction, Basel: Birkhäuser, pp. 91–104, doi:10.1007/978-3-0348-0122-5_5, ISBN 9783034801225
- Karger, David R.; Stein, Clifford (1996), "A new approach to the minimum cut problem" (PDF), Journal of the ACM, 43 (4): 601, doi:10.1145/234533.234534
- Kennedy, Douglas P. (1975), "The Galton–Watson process conditioned on the total progeny", Journal of Applied Probability, 12 (4): 800–806, doi:10.2307/3212730, JSTOR 3212730
- Knuth, Donald E. (1973), "6.2.2 Binary Tree Searching", The Art of Computer Programming, Vol. III: Sorting and Searching, Addison-Wesley, pp. 422–451
- Knuth, Donald E. (1997), "2.3.4.5 Path Length", The Art of Computer Programming, Vol. I: Seminumerical Algorithms (3rd ed.), Addison-Wesley, pp. 399–406
- Knuth, Donald E. (2005), "Draft of Section 7.2.1.6: Generating All Trees", The Art of Computer Programming, vol. IV, archived from the original on 2023-01-02, retrieved 2009-03-31
- Kruszewski, Paul (1999), "A note on the Horton–Strahler number for random binary search trees", Information Processing Letters, 69 (1): 47–51, doi:10.1016/S0020-0190(98)00192-6
- Mäkinen, Erkki; Siltaneva, Jarmo (2003), "A note on Rémy's algorithm for generating random binary trees", Missouri Journal of Mathematical Sciences, 15 (2): 103–109, doi:10.35834/2003/1502103
- Mahmoud, Hosam M. (1992), Evolution of Random Search Trees, John Wiley & Sons
- Martínez, Conrado; Roura, Salvador (1998), "Randomized binary search trees", Journal of the ACM, 45 (2): 288–323, CiteSeerX 10.1.1.17.243, doi:10.1145/274787.274812
- Morin, Pat (March 22, 2014), "Chapter 7: Random Binary Search Trees", Open Data Structures (in pseudocode) (PDF) (0.1Gβ ed.), pp. 145–164
- Pittel, B. (1985), "Asymptotical growth of a class of random trees", Annals of Probability, 13 (2): 414–427, doi:10.1214/aop/1176993000
- Popovic, Lea (November 2004), "Asymptotic genealogy of a critical branching process", Annals of Applied Probability, 14 (4), arXiv:math/0503577, doi:10.1214/105051604000000486
- Reed, Bruce (2003), "The height of a random binary search tree", Journal of the ACM, 50 (3): 306–332, doi:10.1145/765568.765571
- Rémy, Jean-Luc (1985), "Un procédé itératif de dénombrement d'arbres binaires et son application à leur génération aléatoire", RAIRO Informatique théorique (in French), 19 (2): 179–195, doi:10.1051/ita/1985190201791
- Robson, J. M. (1979), "The height of binary search trees", Australian Computer Journal, 11: 151–153
- Seidel, Raimund; Aragon, Cecilia R. (1996), "Randomized search trees", Algorithmica, 16 (4–5): 464–497, doi:10.1007/s004539900061 (inactive 2024-11-05)
{{citation}}
: CS1 maint: DOI inactive as of November 2024 (link) - Sedgewick, Robert; Flajolet, Philippe (2013), "Chapter 6: Trees", An Introduction to the Analysis of Algorithms (2nd ed.), Addison-Wesley, ISBN 9780133373486
- Shreve, Ronald L. (January 1966), "Statistical law of stream numbers", The Journal of Geology, 74 (1): 17–37, Bibcode:1966JG.....74...17S, doi:10.1086/627137, JSTOR 30075174
- Tarjan, Robert E.; Levy, Caleb C.; Timmel, Stephen (2021), "Zip trees", ACM Transactions on Algorithms, 17 (4): 34:1–34:12, arXiv:1806.06726, doi:10.1145/3476830
- Vuillemin, Jean (1980), "A unifying look at data structures", Communications of the ACM, 23 (4): 229–239, doi:10.1145/358841.358852