Optimal binary search tree
In computer science, an optimal binary search tree (Optimal BST), sometimes called a weight-balanced binary tree, is a binary search tree which provides the smallest possible search time (or expected search time) for a given sequence of accesses (or access probabilities).[src] Optimal BSTs are generally divided into two types: static and dynamic.[src] In the static optimality problem, the tree cannot be modified after it has been constructed.[src] In this case, there exists some particular layout of the nodes of the tree which provides the smallest expected search time for the given access probabilities.[src] Various algorithms exist to construct or approximate the statically optimal tree given the information on the access probabilities of the elements.[src] In the dynamic optimality problem, the tree can be modified at any time, typically by permitting tree rotations.[src] The tree is considered to have a cursor starting at the root which it can move or use to perform modifications.[src] In this case, there exists some minimal-cost sequence of these operations which causes the cursor to visit every node in the target access sequence in order.[src] The splay tree is conjectured to have a constant competitive ratio compared to the dynamically optimal tree in all cases, though this has not yet been proven.[src]
In computer science, an optimal binary search tree (Optimal BST), sometimes called a weight-balanced binary tree, is a binary search tree which provides the smallest possible search time (or expected search time) for a given sequence of accesses (or access probabilities).[src] Optimal BSTs are generally divided into two types: static and dynamic.[src] In the static optimality problem, the tree cannot be modified after it has been constructed.[src] In this case, there exists some particular layout of the nodes of the tree which provides the smallest expected search time for the given access probabilities.[src] Various algorithms exist to construct or approximate the statically optimal tree given the information on the access probabilities of the elements.[src] In the dynamic optimality problem, the tree can be modified at any time, typically by permitting tree rotations.[src] The tree is considered to have a cursor starting at the root which it can move or use to perform modifications.[src] In this case, there exists some minimal-cost sequence of these operations which causes the cursor to visit every node in the target access sequence in order.[src] The splay tree is conjectured to have a constant competitive ratio compared to the dynamically optimal tree in all cases, though this has not yet been proven.[src] In 1971, Knuth published a relatively straightforward dynamic programming algorithm capable of constructing the statically optimal tree in only O(n 2 ) time.[src] In this work, Knuth extended and improved the dynamic programming algorithm by Edgar Gilbert and Edward F.[src] Moore introduced in 1958.[src] Gilbert's and Moore's algorithm required O(n^3) time and O(n^2) space and was designed for a particular case of optimal binary search trees construction (known as optimal alphabetic tree problem ) that considers only the probability of unsuccessful searches, that is, \sum _{i = 1}^{n} A_i = 0 .[src] Knuth's work relied upon the following insight: the static optimality problem exhibits optimal substructure; that is, if a certain tree is statically optimal for a given probability distribution, then its left and right subtrees must also be statically optimal for their appropriate subsets of the distribution (known as monotonicity property of the roots).[src] To see this, consider what Knuth calls the "weighted path length" of a tree.[src] The weighted path length of a tree of n elements is the sum of the lengths of all 2n+1 possible search paths, weighted by their respective probabilities.[src] The tree with the minimal weighted path length is, by definition, statically optimal.[src] But weighted path lengths have an interesting property.[src] Let E be the weighted path length of a binary tree, be the weighted path length of its left subtree, and be the weighted path length of its right subtree.[src] Also let W be the sum of all the probabilities in the tree.[src] Observe that when either subtree is attached to the root, the depth of each of its elements (and thus each of its search paths) is increased by one.[src] Also observe that the root itself has a depth of one.[src] This means that the difference in weighted path length between a tree and its two subtrees is exactly the sum of every single probability in the tree, leading to the following recurrence: E = E_L + E_R + W This recurrence leads to a natural dynamic programming solution.[src] Let E_{ij} be the weighted path length of the statically optimal search tree for all values between and , let W_{ij} be the total weight of that tree, and let R_{ij} be the index of its root.[src] The algorithm can be built using the following formulas: \begin{align} E_{i,i-1} = W_{i,i-1} &= B_{i-1} \operatorname{for} 1 \leq i \leq n+1 \\ W_{i,j} &= W_{i,j-1} + A_j + B_j \\ E_{i, j} &= \min_{i\leq r \leq j}(E_{i,r-1} + E_{r+1,j}+W_{i,j}) \operatorname{for} 1 \leq i \leq j \leq n \end{align} The naive implementation of this algorithm actually takes O(n 3 ) time, but Knuth's paper includes some additional observations which can be used to produce a modified algorithm taking only O(n 2 ) time.[src] In addition to its dynamic programming algorithm, Knuth proposed two heuristics (or rules) to produce nearly (approximation of) optimal binary search trees.[src] Studying nearly optimal binary search trees was necessary since Knuth's algorithm time and space complexity can be prohibitive when n is substantially large.[src] Knuth's rules can be seen as the following: Rule I (Root-max): Place the most frequently occurring name at the root of the tree, then proceed similarly on the subtrees.[src] Rule II (Bisection): Choose the root so as to equalize the total weight of the left and right subtree as much as possible, then proceed similarly on the subtrees.[src] Knuth's heuristics implements nearly optimal binary search trees in O(n \log n) time and O(n) space.[src] The analysis on how far from the optimum Knuth's heuristics can be was further proposed by Kurt Mehlhorn.[src] \begin{align} n = 2^k - 1,~~A_i = 2^{-k} + \varepsilon_i~~\operatorname{with}~~\sum^{n}_{i = 1} \varepsilon_i = 2^{-k} \end{align} and \begin{align} \varepsilon_1,\varepsilon_2,\dots,\varepsilon_n>0~~\operatorname{for}~~1 \leqq i \leqq n~~\operatorname{and}~~B_j = 0 \operatorname{for}~~0 \leqq j \leqq n.[src] \end{align} Considering the weighted path length P of the tree constructed based on the previous definition, we have the following: \begin{align} P &= \sum_{i=1}^{n} A_i(a_i + 1) + \sum_{j=1}^{n} B_j b_j \\ &= \sum_{i=1}^{n} A_i i \\ &\geqq 2^{-k} \sum_{i=1}^{n} i = 2^{-k} \frac{n(n+1)}{2}\geqq \frac{n}{2}.[src] \end{align} Thus, the resulting tree by the root-max rule will be a tree that grows only on the right side (except for the deepest level of the tree), and the left side will always have terminal nodes.[src] This tree has a path length bounded by \Omega (\frac{n}{2}) and, when compared with a balanced search tree (with path bounded by O (2 \log n) ), will perform substantially worse for the same frequency distribution.[src] In addition, Mehlhorn improved Knuth's work and introduced a much simpler algorithm that uses Rule II and closely approximates the performance of the statically optimal tree in only time.[src] The algorithm follows the same idea of the bisection rule by choosing the tree's root to balance the total weight (by probability) of the left and right subtrees most closely.[src] And the strategy is then applied recursively on each subtree.[src] That this strategy produces a good approximation can be seen intuitively by noting that the weights of the subtrees along any path form something very close to a geometrically decreasing sequence.[src] In fact, this strategy generates a tree whose weighted path length is at most 2+(1 - \log(\sqrt{5} - 1))^{-1}H = 2 + \frac H {1 - \log(\sqrt{5} - 1)} where H is the entropy of the probability distribution.[src] Since no optimal binary search tree can ever do better than a weighted path length of (1/\log3)H = \frac H {\log 3} this approximation is very close.[src] ===Hu–Tucker and Garsia–Wachs algorithms=== In the special case that all of the A_i values are zero, the optimal tree can be found in time O(n\log n) .[src] This was first proved by T.[src] C.[src] Hu and Alan Tucker in a paper that they published in 1971.[src] A later simplification by Garsia and Wachs, the Garsia–Wachs algorithm, performs the same comparisons in the same order.[src] The algorithm works by using a greedy algorithm to build a tree that has the optimal height for each leaf, but is out of order, and then constructing another binary search tree with the same heights.[src] float[][] costMatrix = new float[numNodes + 2][numNodes + 1]; for (int i = 1; i <= numNodes; i++) { costMatrix[i][i - 1] = 0; costMatrix[i][i] = probabilities[i]; roots[i][i] = i; roots[i][i - 1] = 0; } for (int diagonal = 1; diagonal <= numNodes; diagonal++) { for (int i = 1; i <= numNodes - diagonal; i++) { int j = i + diagonal; costMatrix[i][j] = findMinCost(costMatrix, i, j) + sumProbabilities(probabilities, i, j); // Note: roots[i][j] assignment is missing, this needs to be fixed if you want // to reconstruct the tree.[src] } } return costMatrix[1][numNodes]; } ==See also== Trees Splay tree Tango tree Geometry of binary search trees Interleave lower bound ==Notes==[src]
58 sentences · 58 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Optimal binary search tree." encyclopegia.org, 2026, https://encyclopegia.org/wiki/optimal-binary-search-tree. Accessed 02 December 2026.
Chicago
"Optimal binary search tree." encyclopegia.org. Accessed December 2, 2026. https://encyclopegia.org/wiki/optimal-binary-search-tree.
APA
encyclopegia.org. (2026). Optimal binary search tree. Retrieved December 2, 2026, from https://encyclopegia.org/wiki/optimal-binary-search-tree
Accessed December 2, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| publication date | 1971 | Wikipedia |