On Huang and Wong's Algorithm for Generalized Binary Split Trees

Huang and Wong [1984] proposed a polynomial-time dynamic-programming algorithm for computing optimal generalized binary split trees. We show that their algorithm is incorrect. Thus, it remains open whether such trees can be computed in polynomial time. Spuler [1994] proposed modifying Huang and Wong's algorithm to obtain an algorithm for a different problem: computing optimal two-way-comparison search trees. We show that the dynamic program underlying Spuler's algorithm is not valid, in that it does not satisfy the necessary optimal-substructure property and its proposed recurrence relation is incorrect. It remains unknown whether the algorithm is guaranteed to compute a correct overall solution.


Introduction
Given an ordered set K of n keys, a generalized binary split tree T is a form of binary search tree where each node N has two associated keys in K: an equality-test key and a split key [8]. For any query v ∈ K, a search for v in T starts at the root. If v equals the root's equality-test key, then the search halts. Otherwise, the search recurses in the left or right subtree, depending on whether or not v is less than the root's split key. A correct tree T must have n nodes, and the search for each query v ∈ K must halt at the node whose equality-test key is v. (There must be exactly one such node for each v ∈ K.) Given also a probability distribution p on K, the cost of a tree T is the expected number of nodes visited when searching in T for a random query v drawn from p. The goal, given K and p, is to compute a tree T of minimum cost (thus minimizing, over any tree T of this form, the expected number of two-way comparisons made when searching in T ). We denote this problem GBSPLIT. See Fig. 1 for an example. Following Huang and Wong, here we focus Fig. 1 The picture on the left shows an example of a generalized binary split tree for key interval {A,B,C,D,E,F}. Each node is labeled with its equality key and its probability, as well as the node's split key (except that split keys are omitted at leaves, where they are irrelevant). The total cost of this tree is 0.3 · 1 + 2 · (0.2 · 2) + 3 · (0.1 · 3) = 2. In all figures in the paper we use a more compact representation, shown on the right, where split keys are omitted. (Each node's split key can be any key that separates the equality keys in the left subtree from those in the right subtree.) on the so-called successful-queries variant, in which all queries are guaranteed to be in K. (In the general variant, arbitrary queries are allowed. ) Huang and Wong [8] proposed a polynomial-time algorithm for GBSPLIT. We show (in Theorem 1, Sect. 2) that their algorithm and claimed proof of correctness are wrong. The reason is that their dynamic program does not satisfy the claimed optimal-substructure property. Consequently, as far as we know, it is not known whether GBSPLIT has a polynomial-time algorithm.
A closely related problem is to find an optimal two-way comparison search tree, in which each node is associated with just one key and one binary comparison operator-equality or less-than. We use 2WCST to denote this problem. (See Fig. 4 for an example.) Spuler [14,15] proposed several 2WCST algorithms. He described two of his proposed 2WCST algorithms (for the successful-queries and general variants, respectively) as "straightforward" modifications of Huang and Wong's GBSPLIT algorithm, but he gave no formal proof of correctness, explaining only that correctness follows from the dynamic-programming formulation, in particular from the underlying recurrence relation.
We show (Theorem 3, Sect. 3) that this recurrence relation is wrong, and his algorithm computes incorrect solutions to some subproblems in the dynamic program. Here also the dynamic program does not satisfy the assumed optimal-substructure property. This counter-example is only for a subproblem, not a full instance, so the overall correctness of his proposed algorithm remains open. (Here also we focus on the successful-queries variant only.) Historical context. The study of optimal binary search trees began with three-way comparison search trees. These have only one key associated with each node, and comparing the given query to that key has three possible outcomes-less than, equal to, or greater than. Knuth's classical dynamic-programming algorithm computes a minimum-cost tree of this kind (supporting both successful and unsuccessful queries) in time O(n 2 ) [10].
Following Knuth's suggestion [11, §6.2.2 ex. 33], various authors began exploring trees based on two-way (binary) comparisons. Sheil [13] introduced median split trees-generalized binary split trees where the split key at each node N must be a median key among the set K N of keys whose search visits node N, and the equality-test key must be a most likely key among K N . He gave an O(n log n)-time algorithm to compute a median split tree (for the successful-queries variant). Other authors [9,12,7] then introduced binary split trees-generalized binary split trees with the added restriction that the equality-test key at each node must be a most likely key among keys reaching the node. These trees can be thought of as a relaxation of median split trees, without the restriction that the split key has to be a median key. Their algorithms compute minimum-cost binary split trees in O(n 5 ) time for both the successful-queries and general variants. (See also the note at the end of this paper.) Huang and Wong [8] then introduced GBSPLIT (generalized binary split trees) as defined above, and proposed an O(n 5 )-time algorithm for the problem, the one we show here to be incorrect.
Subsequently, the algorithm was extended by Chen and Liu to MULTIWAY GBSPLIT, a variant of GBSPLIT that requires multiple split keys per node [2]. Chen and Liu's algorithm and proof of correctness are directly patterned on Huang and Wong's. Their proof is invalid (and we believe their algorithm to be incorrect) for the same reason that Huang and Wong's proof and algorithm fail. (See the remark at the end of Sect. 2.) As mentioned above, Spuler [14,15] proposed several 2WCST algorithms without proof of correctness. Anderson et al. [1] gave the first proof that 2WCST is in polynomial time. Their algorithm runs in time O(n 4 ) and is restricted to the successful-queries variant. Chrobak et al. [3,5,6] gave a somewhat simpler O(n 4 )-time algorithm for the general variant.
Beyond pointing out errors in the literature on binary search trees, we hope that the constructions underlying our counter-examples will contribute to a better understanding of the difficulties involved in designing algorithms for GBSPLIT and 2WCST, leading to better algorithms or even new hardness results.

Huang and Wong's GBSPLIT algorithm is incorrect
This section gives our first main result: a proof that Huang and Wong's proposed GBSPLIT algorithm [8] has a fundamental flaw.
Theorem 1 Huang and Wong's GBSPLIT algorithm [8] is incorrect. There is a GBSPLIT instance (K, p) for which it returns a non-optimal tree.
We summarize their algorithm and analysis, give the intuition behind the failure, then prove the theorem. The basic intuition is that, for the dynamic program that Huang and Wong define, the optimal-substructure property fails. The proof gives a specific counter-example and verifies it. The counter-example can also be verified computationally by running the Python code for Huang and Wong's algorithm in Appendix A.
Fix any GBSPLIT instance (K, p). Assume without loss of generality that the keys are K = {1, 2, . . . , n}. Regarding the probability vector p, for convenience, throughout the paper we drop the constraint that the probabilities must sum to 1, and we use "probabilities" and "weights" synonymously, allowing their values to be arbitrary non-negative reals. (To represent probabilities, these values can be appropriately normalized.) During a search, the outcome of each less-than comparison narrows the current search interval, while the outcome of each (failed) equality test removes one key within the interval from consideration. Thus, at each node in any search tree, the set of keys reaching the node consists of some interval of keys, minus some so-called holes-keys removed from consideration by previous equality tests. Next we formally define an exponentially large (!) class of subproblems that arise in this way, along with a natural recurrence relation for their cost. We then discuss how Huang and Wong attempt to reduce the number of subproblems to O(n 3 ).
Abusing notation, a query interval I = [i, j] is the set of contiguous keys {i, i+1, . . . , j}. Given any query interval I and any subset H ⊆ I of "hole" keys, consider the subproblem (I, H) formed by the subset of keys I \ H, with the weight distribution obtained from p by restricting to I \ H. Let opt(I, H) denote the minimum cost of any generalized binary split tree for this subproblem. Let p(I \ H) = ∑ k∈I\H p k denote the total weight of its keys. where H e = H ∪ {e}. (Here s ∈ [i, j + 1] ranges over the possible split keys; 1 e ∈ I \ H ranges over the possible equality keys.) The goal is to compute opt(K, / 0). The recurrence above allows arbitrary equality keys e, so it gives rise to exponentially many hole sets H, resulting in a dynamic program with exponentially many subproblems. Huang and Wong propose a dynamic program with O(n 3 ) subproblems (I, h), one for each interval I and integer h ≤ |I|. Specifically, they define which is the minimum cost of any tree for interval I minus any hole set of size h. Each such tree will have |I| − h nodes. (Their paper uses "p[i − 1, j, h]" to denote opt * ([i, j], h).) We refer to any such subproblem (I, h) as an HW-subproblem.
They develop a recurrence for opt * (I, h) as follows. For any node N in an optimal tree, define N's interval I N and hole set H N in the natural way so that interval I N contains those key values that, if searched for in T with the equality tests ignored, would reach N, and H N ⊆ I N contains those keys in interval I N that are equality keys at ancestors of N. Hence, the set of keys reaching N is I N \ H N , and the subtree rooted at N is a solution for the subproblem (I N , H N ), as well as the HW-subproblem (I N , |H N |), which we refer to as the HW-subproblem arising at N. Huang and Wong's Lemma 1 states: Lemma 1 from [8] (ambiguous) "Subtrees of an optimal generalized binary split tree are optimal generalized binary split trees." This statement is ambiguous in that it doesn't specify for which subproblem the subtree is optimal. Consider any subtree T ′ of an optimal tree T * . Let T ′ have root N, interval I N and hole set H N . The first interpretation of their Lemma 1 is that T ′ must be an optimal solution for (I N , H N ). With this interpretation (following the first recurrence above), the lemma is indeed true. But another interpretation is that T ′ must be an optimal solution for the HW-subproblem (I N , |H N |) arising at N. This interpretation is not the same-the HW-subproblem specifies only the number of holes, and choosing different holes can give a cheaper tree, so it can be that opt * (I N , |H N |) < opt(I N , H N ). As we shall see below, it is the second interpretation that underlies the recurrence relation that Huang and Wong propose, but, with that interpretation, as our Theorem 2 shows, the above lemma is false because the HW-subproblems do not have optimal substructure.
The ambiguity in Lemma 1 appears to be their first misstep. They follow it with the following (correct) observation: [8] (correct) Let N be the root of a subtree T ′ with interval I in an optimal generalized binary split tree T * . The equality-test key e N of N must be the least frequent key among those in N's interval I N that do not occur (as an equality-test key) in the left and right subtrees of N. 2 Proof The proof is a simple exchange argument. Suppose for contradiction that e N is more likely than some key k in I N and k does not occur as an equality-test key in the left and right subtrees of N. Then k is a hole at N, so it must be the equality-test key k = e N ′ of some ancestor N ′ of N. A contradiction is obtained by observing that exchanging e N and e N ′ gives a correct tree cheaper than T * .

⊓ ⊔
Huang and Wong's Lemma 2 above (with the second, incorrect interpretation of their Lemma 1) suggests the following idea. To find a hopefully optimal tree τ(I, h) for the HW-subproblem (I, h), consider each possible root split key and each possible split of the h hole slots. For each, first find optimal left and right subtrees for their respective subproblems, and then take the equality key at the root to be the least-likely key in I that is not an equality test in either subtree. Among trees obtained in this way, take τ(I, h) to be one of minimum cost. Following this idea, their algorithm (as detailed on pages 118-120 of their paper) solves any given HW-subproblem (I, h), where I = [i, j] is non-empty, as follows: To justify the algorithm, Huang and Wong proceed as follows. Fix any execution of the algorithm (breaking ties arbitrarily; see the remarks below). For any HW-subproblem (I, h) that it solves, let opt * (I, h) denote the minimum cost of any tree for the subproblem. Recall that τ(I, h) denotes the algorithm's solution (tree) for the subproblem, presumably of cost opt * (I, h). Huang and Wong first state a correct base case: But their Lemma 4 then claims that, for any non-empty interval I = [i, j] and any number of holes h ≤ |I|, the following recurrence relation holds: where the minimum is over all legal combinations of s and h 1  More significantly, our statement of their Lemma 4 corrects what we believe is an error. Namely, their statement of the lemma has "w(I, h)" where we have "p(T (s, h 1 , h 2 ))", with w(I, h) (on their page 118) defined as the "total weight of the optimal GBST for" the HW-subproblem (I, h). We believe that they had in mind the recurrence as we give it (using p(T (s, h 1 , h 2 ))), mainly because this recurrence is the one that their algorithm, as defined on pages 118-120 of their paper, actually uses.
Our Theorem 2, next, refutes their Lemma 4 regardless of this issue-it refutes any recurrence based on the class of HW-subproblems {(I, h)}, by showing that the class doesn't have the optimal-substructure property. In Theorem 1 and elsewhere, by "Huang and Wong's algorithm", we mean the algorithm as defined in pages 118-120 of their paper (independently of their statement of Lemma 4). Our refutation of that algorithm, after Theorem 2 below, gives an instance on which it fails.
Theorem 2 There exists a GBSPLIT instance (K, p) with the following property. In every optimal tree T * for (K, p), there is at least one node N such that the subtree T * N rooted at N in T * is not optimal for the HW-subproblem (I N , |H N |) arising at N. (The tree T * N has cost strictly larger than Proof Before we describe (K, p), we first describe an HW-subproblem for which using a minimumcost tree T ′ can be a bad choice globally. The HW-subproblem is (I 9 , 2), with h = 2 holes and interval I 9 consisting of nine keys I 9 = {A1, A2, A3, B0, B4, C0, D0, D1, E0}, ordered lexicographically, with weights as follows: weight  20  20  20  10  20  5 10 22 10 Figure 2 shows two possible subtrees T 2a and T 2b for (I 9 , 2), each with seven nodes. By calculation, subtree T 2b costs 1 more than subtree T 2a for (I 9 , 2). (Indeed, key C0 contributes 5 units more to T 2b than to T 2a , while key B4 contributes 4 units less to T 2b than key D1 contributes to T 2a .)  T 3b Fig. 3 Trees T 3a and T 3b for an instance of GBSPLIT with 31-key interval I 31 . Key order is lexicographic: A0 < A1 < A2 < A3 < B0 < ··· . As in Fig. 2, split keys are not shown. Huang and Wong's algorithm gives a tree of cost 1763, such as T 3a , but tree T 3b costs 1762.
Although T 2b costs 1 more than T 2a , choosing subtree T 2b instead of T 2a can decrease the cost of the overall tree! To see why, suppose that T 2a occurs as a subtree of some tree T * , in which T 2a has parent A3 and grandparent B4 as shown in the figure. (See also Fig. 3.) Consider replacing T 2a and its two hole keys A3 and B4 by T 2b and its two hole keys A3 and D1. This replacement decreases the cost of the entire tree by 1 unit, because the contribution of C0 increases by 5, swapping B4 and D1 decreases the cost by 6, and the contributions of other nodes do not change. But a different calculation gives better intuition why Huang and Wong's algorithm fails. The contribution of the subtree T 2a to the overall cost equals the cost of T 2a in isolation plus twice the weight of keys in T 2a (because T 2a has two ancestors). The modification increases the cost of the subtree by 1 (so it is no longer optimal for its subproblem) but decreases the total weight of its keys by 2. Thus, the subtree's contribution to the overall cost changes by +1 − 2 · 2 = −3. This decrease of 3 is more than the increase of 2 that comes from changing the key B4 at the overall root to D1, which is 2 units heavier.
Next we use this HW-subproblem to obtain the complete instance (K, p) for Theorem 2. The instance has a 31-key interval I 31 , which extends the previously considered interval I 9 by appending two "neutral" subintervals, with 7 and 15 keys. Figure 3 shows two trees T 3a and T 3b for (K, p). As shown there, the new keys are given weights so that each of the two added subintervals (without any holes) has a self-contained, optimal balanced subtree. To finish proving Theorem 2, we prove that (K, p) has the necessary properties: Lemma 5 Let T * be any optimal tree for this GBSPLIT instance (K, p). At some node N of T * the HW-subproblem (I 9 , 2) arises, but the subtree T * N rooted at N has cost at least 210 for (I 9 , 2), while opt * (I 9 , 2) ≤ 209.
To bound tree costs, define a key placement (for a tree T ) to be an assignment of the equalitytest keys in T to distinct nodes in the infinite rooted binary tree T ∞ . Define the cost of the placement to be the average weighted depth of the placed keys, weighted according to the key weightvector p. Each correct GBSPLIT tree T yields a placement of equal cost by placing each equalitytest key in the same place in T ∞ that it occupies in T . The converse does not hold, partly because placements can ignore the ordering of keys.
By an exchange argument, a placement has minimum cost if and only if it puts the weight-22 key D1 at depth 0, the fourteen weight-20 keys at depths 1-3, and the sixteen remaining (weight-10 and weight-5) keys at depth 4. By calculation, such a placement costs 1757. No placement costs less, so no tree costs less. Tree T 3b almost achieves a minimum-cost placement-it fails only in that it places the weight-5 key at depth 5, so costs 1762, just 5 units more than the minimum placement cost.
Claim 6 T * has the following structure: (i) It places the fifteen keys of weight 20 or more at depths 0-3. (ii) It places the fifteen weight-10 keys at depth 4.
Next we prove the claim. Since T * is optimal it costs at most 1762 (the cost of T 3b ), so its placement also costs at most 1762. Suppose for contradiction that (i) doesn't hold. Then T * places a key k of weight 20 or more at depth at least 4. Also, in depths 1-3, it either places at least one key k ′ of weight 10, or places fewer than fifteen keys. In either case, by exchanging k and k ′ , or just re-placing k in depth 1-3, we can obtain a key placement that costs at least 10 units less than 1762. But this is impossible, as the minimum placement cost is 1757. So (i) holds. Now suppose for contradiction that (ii) doesn't hold. Then there is a weight-10 key k ′ at depth 5 or more, and at most fifteen keys at depth 4, so k ′ can be re-placed in depth 4, yielding a key placement that costs 10 less, which is impossible. This proves the claim.
Key placements ignore the ordering of keys. The following order property captures the restrictions on key placements due to the ordering.
Let T be any correct GBSPLIT tree. Let P and P ′ be nodes in T with equality-test keys k and k ′ . Let Q be the least-common ancestor of P and P ′ . If P is in Q's left subtree, and P ′ is in Q's right subtree, then k < k ′ .
The property holds simply because k and k ′ are separated by M's split key. Fix any optimal tree T * for (K, p). Claim 6 imposes stringent constraints on the depth of all keys in T * , except for the weight-5 key C0. There are two cases: Case 1: T * places C0 at depth 4. With Claim 6, this implies that T * is a complete balanced binary tree of depth 4 (like T 3a ), whose sixteen depth-4 nodes hold the fifteen weight-10 keys and C0. By the order property, these depth-4 keys are ordered left to right, just as they are in T 3a , with the left-most four nodes at depth 4 having keys B0, C0, D0, and E0.
The left spine has only five nodes. By the order property, all five keys less than C0 cannot be elsewhere than on the spine. So D1 is not on the left spine.
Case 2: T * places C0 at depth 5. Let L 0 , L 1 , . . . , L ℓ be the left spine of T * , starting at the root. Take T ′ to be the subtree of T * rooted at L 2 . By Claim 6, T * has fifteen depth-4 nodes, holding the fifteen weight-10 keys. By the order property, these depth-4 keys are ordered left to right within their level and at most twelve of them are not in T ′ . This implies that the weight-10 keys B0, D0 and E0 must be in T ′ . The next larger weight-10 key, N0, cannot be in T ′ . Indeed, if it were, then by the order property, all keys less than or equal to N0 would be in T ′ ∪ {L 1 , L 0 }. But there are twelve keys less than or equal to N0 and at most eight keys in T ′ .
We now focus on the cost of T ′ . By the previous two paragraphs, T ′ has exactly three keys at depth 2, namely B0, D0, and E0. By the order property and the assumption for Case 2, C0 must be (the only key) at depth 3 in T ′ (as the child of either B0 or D0). By Lemma 8, the three keys at depths 0 and 1 in T ′ have weight 20 or 22. Therefore, by calculation, the cost of T ′ is at least 210 (see Fig. 2).
Since E0 is in T ′ , by the order property, all eight keys less than E0 are in T ′ ∪ {L 0 , L 1 }. That is, T ′ ∪ {L 0 , L 1 } contains at least the 9 keys in I 9 . But (as observed above) T ′ has seven nodes. So T ′ ∪ {L 0 , L 1 } contains exactly the 9 keys in I 9 , and the HW-subproblem solved by T ′ must be (I 9 , 2). As observed above, T ′ costs at least 210. But tree T 2a (Fig. 2) of cost 209 also solves (I 9 , 2), so opt * (I 9 , 2) ≤ 209. This proves Lemma 5 and Theorem 2.
⊓ ⊔ We prove one final utility lemma before we prove Theorem 1. Consider any execution of Huang and Wong's algorithm on the input (K, p) defined in the proof of Theorem 2. Let T = τ(I 31 , 0) be the algorithm's solution.
Proof Abusing notation, for 1 ≤ i < j ≤ 9, let [i, j] denote the ith through jth keys in interval I 9 , as shown in Fig. 3. (See also Fig. 2

for intuition.)
Consider Table 1. Each row of the table is for one HW-subproblem (I, h) (shown in the leftmost column), and demonstrates that the cost of the tree τ(I, h) computed by the algorithm for that subproblem is as shown in the fifth column ("cost for τ(I, h)"). The last column lists the keys that are holes in τ(I, h). The first four rows are singleton cases (key sets of size one), and their correctness and optimality can be verified by straightforward inspection. For each subsequent row, the second column gives one of the triples (s, h 1 , h 2 ) considered by the algorithm for the given HW-subproblem (I, h), where s is the split key, and h 1 and h 2 are the numbers of holes allocated to the left and right subtrees. Columns "left" and "right" show the left and right HWsubproblems that follow from that choice of (s, h 1 , h 2 ), and column "cost for τ(I, h)" gives the cost of tree T (s, h 1 , h 2 ) resulting from that choice. Likewise the final column "holes" describes the possible hole sets (in order to achieve the given cost, covering all ways to break ties). For the HW-subproblems in rows five and six, the choices of (s, h 1 , h 2 ) in the table are optimal. For the seventh subproblem, the cost of 209 is an upper bound (in fact it is optimal, but we don't need that here). Each row can be verified by manual computation assuming inductively that the previous rows are correct.
⊓ ⊔ By Lemmas 5 and 7, the tree T computed by Huang and Wang's algorithm for (K, p) cannot be optimal: Lemma 5 states that all optimal trees for (K, p) contain a node with a certain property, while Lemma 7 states that T does not contain such a node. This proves Theorem 1.
For empirical verification, note that executing the algorithm on (K, p), via the Python code in Appendix A, 3 returns a tree of cost 1763. This tree is not optimal, as T 3b costs 1762. The tree does have the HW-subproblem (I 9 , 2), and executing the algorithm directly on that subproblem does return a tree of cost 209.
Remark on Chen and Liu's algorithm for MULTIWAY GBSPLIT. Chen and Liu's algorithm [2] and analysis are patterned directly on Huang and Wong's, and the proofs they present also conflate (their equivalents of) opt * (I, h) and opt(I, H), leading to the same problems with optimal substructure. For example, Property 1 of [2] states "Any subtree of an optimal (m + 1)-way generalized split tree is optimal." They do not define "optimal", so their Property 1 has the same problem as Huang and Wong's Lemma 1: it is true if "optimal" means "with respect to their equivalent of opt(I, H)", but does not necessarily hold if "optimal" means "with respect to their equivalent of opt * (I, h)". Lemmas 2, 3 and 4 of [2], which state the recurrence relations for their dynamic program, are direct generalizations of Huang and Wong's Lemma 4. Their recurrence chooses equality keys by first finding optimal subtrees for the children, then taking the equality keys to be the least-likely keys that are not equality keys in the children's subtrees. As pointed out in the proof of Theorem 1, correctness of this approach requires the optimal-substructure property to hold with respect to opt * (I, h). But it does not. For these reasons, their proof of correctness is not valid. We believe that their algorithm for MULTIWAY GBSPLIT is also incorrect, but describing their algorithm and analysis in detail, and giving a complete counter-example, are out of the scope of this paper. Below each leaf is its weight. The cost of this tree is 0.6 · 1 + 0.1 · 3 + 0.1 · 3 + 0.05 · 4 + 0.05 · 4 + 0.1 · 3 = 1.9.

A 2WCST algorithm by Spuler fails on some subproblems
This section concerns 2WCST, the problem of computing an optimal two-way comparison search tree, given a set K of n keys and their weight distribution p. Such a tree T is a rooted binary tree, where each non-leaf node N has two children, as well as a key k N ∈ K and a binary comparison operator (equality or less-than). Denote such a node by v = k N or v < k N , depending on which comparison operator is used. The tree T has n leaves, each labeled with a unique key in K.
The search for a query v in T starts at the root. If the root is a leaf, the search halts. Otherwise, it compares v to the root's key using the root's comparison operator, then recurses left if the comparison succeeds, and right otherwise. For the tree to be correct, 4 the search for any query v ∈ K must end at the leaf that is labeled with v. Given an instance (K, p), the problem is to find a tree that minimizes the weighted average depth of the leaves (in the case that p is a probability distribution, this is the expected number of comparisons in a search for a query v drawn randomly according to p). Figure 4 shows an example.
Spuler's thesis proposed various algorithms for 2WCST and for GBSPLIT, for both the successfulqueries variant and the general variant [15]. 5 Here we discuss the (successful-queries) 2WCST algorithm that Spuler presented as a modification of Huang and Wong's GBSPLIT algorithm in Section 6.4.1 of his thesis [15, Section 6.4.1]. That section starts with the following remark: "The changes to the optimal generalized binary split tree algorithm of Huang and Wong [8] to produce optimal generalized two-way comparison trees are quite straight forward." ("Generalized two-way comparison trees" in the thesis are two-way comparison search trees as defined herein.) The remainder of his Section 6.4.1 sketches the code for the algorithm. His Appendix A.4.1. gives complete code. Spuler does not explicitly define the dynamic program or recurrence that he has in mind, however, it is implicitly defined by his algorithm as described below. In addition to lacking proofs of correctness, these algorithms have not appeared in any peer-reviewed publication, although Spuler did refer to them in his journal paper [14], and they have been cited in the literature as the first polynomial-time algorithms for 2WCST [1].
Following Huang and Wong, Spuler's algorithms are based on a dynamic program where each subproblem is specified by an interval of keys and a number of holes, and each subproblem is solved using a recurrence relation. In the remainder of the section, we prove that the dynamic program is flawed: Theorem 3 There is an instance (K, p) of 2WCST for which the dynamic program used by Spuler's 2WCST algorithm [15,Section 6.4

.1] has the following flaws: for some subproblems, the recurrence relation is incorrect and the algorithm computes non-optimal solutions.
Note that Theorem 3 does not imply that the algorithm is incorrect, in the sense that it gives an incorrect solution to some full instance (where the number h of holes is 0). Following Huang and Wong, the dynamic program implicit in Spuler's algorithm has a subproblem (I, h) for each query interval I and number of holes h. In what follows we call any such subproblem (I, h) an S-subproblem. The definition of a correct tree for an S-subproblem is a natural extension of the definition for full instances: a correct tree for (I, h) must have exactly |I| − h leaves, each labeled with a unique key from I; however, all keys in I can be used as inequality-comparison keys. We use opt * (I, h) to denote the minimum cost of any tree for Ssubproblem (I, h). The underlying flaw is the same as in Huang and Wong's dynamic program-S-subproblems do not have optimal substructure.
Given any S-subproblem (I, h), where I = [i, j] and the subproblem size |I| − h is more than one, Spuler's algorithm computes a tree τ(I, h) for S-subproblem (I, h) by combining trees τ(I ′ , h ′ ) that it has computed for smaller S-subproblems, as follows: 1. Construct one candidate tree with an equality test at the root, as follows: 1.1. Let e be a least-likely key in I that is not a leaf in τ(I, h + 1). 1.2. The candidate tree has root v = e and right subtree τ(I, h + 1). Remarks. The algorithm is not hard to implement. Appendix B gives Python code (42 lines). As noted earlier, Spuler does not explicitly define his dynamic program or recurrence relation for opt * (I, h), however, it is implicitly defined by his algorithm and his assumption that each tree τ(I, h) is optimal for S-subproblem (I, h) (so has cost opt * (I, h)).

For s ∈
Ties may arise in choosing the minimizers in Lines 1.1 and 3, but Spuler does not discuss ties. We'll show that his recurrence relation is incorrect no matter how ties are broken.
Given an S-subproblem (I, h), Spuler's algorithm constructs its tree τ(I, h) out of trees τ(I ′ , h ′ ) that it built for smaller S-subproblems. This only works if S-subproblems have optimal substructure. To complete the proof of Theorem 3, we show that they do not: Theorem 4 There exists a 2WCST S-subproblem (I, h) with the following property. In every optimal tree T * for (I, h), there is at least one node N such that, for the S-subproblem (I N , h ′ ) arising at N, the subtree T * N rooted at N in T * does not have minimum cost, opt * (I N , h ′ ), for that S-subproblem. 6 Proof Before we describe the full S-subproblem (I, h), we describe one smaller S-subproblem (I ′ , h ′ ) for which using a minimum-cost tree T ′ can be a bad choice globally. It is (I 8 , 1), with one hole and interval I 8 having keys {1, 2, . . . , 8} whose weights are as follows:  1). T 5a has cost 49 and weight 22. T 5b and T 5c have cost 50 but weight 20. T 5a is optimal for (I 8 ,1). Among trees that don't contain the weight-7 key 1, trees T 5b and T 5c have minimum cost. Subtrees marked with 0 (dark shaded) contain keys of weight 0. key 1 2 3 4 5 6 7 8 weight 7 5 0 5 0 5 0 5 Figure 5 shows three possible subtrees T 5a , T 5b , and T 5c for (I 8 , 1). By inspection, T 5a has cost 49 for S-subproblem (I 8 , 1), while T 5b and T 5c cost 50 but weigh 2 units less. Suppose, in a larger tree, that T 5a occurs as the left child of a node N, as shown in Fig. 5. Let T N be the subtree rooted at N. Suppose that the interval of the left child of T N (that is, the root of T 5a ) contains all keys 1, 2, . . . , 8. (For example, N might be v < 9 .) Then replacing T 5a by T 5b would reduce the overall cost by at least 1 unit. This is because the contribution of T 5a to the cost of T N is not the cost of T 5a ; rather, it is its cost plus its weight, and the cost plus weight of T 5b is 1 unit less.
Next we construct the full S-subproblem (I, h) = (I 15 , 2) for Theorem 4. It has two holes, and extends the above S-subproblem (I 8 , 1) to a larger interval I 15 = {1, 2, . . . , 15} with the following symmetric weights: key 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 weight 7 5 0 5 0 5 0 5 0 5 0 5 0 5 7 We use the following terminology to distinguish the different types of keys in a subtree. Given an S-subproblem (I ′ , h ′ ) of (I, h), and a tree T ′ for (I ′ , h ′ ), the keys of I ′ that appear in the leaves of T ′ are T ′ -queries. The other keys in interval I ′ , which are holes in T ′ , are T ′ -holes. (We don't introduce new terminology for the comparison keys in T ′ .) We drop the prefix T ′ from these terms when it is understood from context.
To analyze (I 15 , 2) we need some utility lemmas. We start with one that will help us characterize how weight-0 queries increase costs. This lemma (Lemma 8 below) is in fact general and it holds for S-subproblems of an arbitrary instance of 2WCST. Define two integer sequences Consider a tree T ′ for an S-subproblem of some arbitrary instance of 2WCST (not necessarily our specific instance (K, p)). A subset Q of T ′ -queries will be called T ′ -separated (or simply separated, if T ′ is understood from context) if for any two k, k ′ ∈ Q, with k < k ′ , there is a T ′query k ′′ that separates them, that is k < k ′′ < k ′ . Also, if Q \ { f } is T ′ -separated for some f ∈ Q, then we say that Q is nearly T ′ -separated. The proof of Lemma 8 is a straightforward induction-we postpone it to the end of this section, and proceed with our analysis. Now we focus our attention on our instance (K, p), and we characterize the weights and costs of optimal subtrees for certain subproblems. For 1 ≤ ℓ ≤ 14, let I ℓ = {1, 2, . . . , ℓ} denote the subinterval of I 15 containing its first ℓ keys. These keys have ℓ weights (in order) {7, 5, 0, 5, . . .}: one key of weight 7, then ⌊ℓ/2⌋ even keys of weight 5, separated by odd keys of weight 0. Let ℓ + = 1 + ⌊ℓ/2⌋ be the number of positive-weight keys in I ℓ . Note that each S-subproblem (I ℓ , h ′ ) can be solved by a tree with ℓ + −h ′ positive-weight queries, having h ′ (positive-weight) hole keys.
Proof As T ′ is fixed throughout the proof, the terms holes, queries, and separated, mean T ′ -holes, T ′ -queries, and T ′ -separated as defined earlier, unless otherwise specified.
Let h 0 be the number of weight-0 holes and q + the number of queries with positive weight. We use the following facts about T ′ .
To finish we show that T ′ costs at least 49. Along the way we show it has weight 22. Case 1: First consider the case that h 0 = 0. By (F2), there are 4 positive-weight queries in T ′ . Since h 0 = 0, all weight-0 keys are queries in T ′ , so the set of all weight-5 queries in T ′ is separated, and by (F3), there are at most three such queries. The fourth positive-weight query must be the weight-7 query, query 1. So the positive-weight queries in T ′ are the weight-7 query and three separated weight-5 queries.
So T ′ has total weight 22, as desired. Further, by Lemma 8, the four positive-weight queries in T ′ have total depth at least e 4 in T ′ . So T ′ costs at least 5 · e 4 + (7 − 5) · j = 45 + 2 j, where j is the depth of the weight-7 query. If j ≥ 2, by the previous bound, T ′ costs at least 49, and we are done. In the remaining case we have j = 1 (as j = 0 is impossible), so the weight-7 query is a child of the root. The three weight-5 queries are in the other child's subtree (and are a separated subset there), so by Lemma 8 have total depth at least d 3 = 6 in that subtree, and therefore total depth at least 9 in T ′ . So the total cost of T ′ is at least 7 + 5 · 9 > 49, contradicting (F1). This (with q + = 4 + h 0 and q 5 ≥ q + − 1) implies q 5 = h 0 + 3 = q + − 1. This implies that the weight-7 query is in T ′ , along with some q 5 − h 0 = 3 separated weight-5 queries. Reasoning as in Case 1, the cost of these four queries alone is at least 49. But T ′ contains at least one additional weight-5 query (as q 5 = 3 + h 0 > 3), so T ′ costs strictly more than 49, contradicting (F1). Thus Case 2 cannot actually occur.
Proof Again, throughout the proof, unless otherwise specified, the terms holes, queries, and separated, are all with respect to T ′ . Let h 0 be the number of weight-0 holes and q + the number of queries with positive weight. We use the following facts about T ′ .
To finish, we show that T ′ has cost at least 69. Along the way we show it has weight 27. Case 1: First consider the case that h 0 = 0. By (F5), there are 5 positive-weight queries in T ′ . Also, since h 0 = 0, all weight-0 keys are queries in T ′ , so the set of all weight-5 queries in T ′ is separated, and by (F6), there are at most four of them. The fifth positive-weight query must be the weight-7 query, query 1. So the positive-weight queries in T ′ are the weight-7 query and four separated weight-5 queries.
So T ′ has total weight 27. Further, by Lemma 8, the five positive-weight queries in T ′ have total depth at least e 5 in T ′ . So T ′ costs at least 5 · e 5 + (7 − 5) · j = 65 + 2 j, where j is the depth of the weight-7 query. If j ≥ 2 then, by the previous bound, T ′ costs at least 69, and we are done. In the remaining case we have j = 1 (as j = 0 is impossible), so the weight-7 query is a child of the root. The four weight-5 queries are in the other child's subtree (and form a separated set there), so by Lemma 8 have total depth at least d 4 = 10 in that subtree, and therefore total depth at least 14 in T ′ . So the total cost of T ′ is at least 7 + 5 · 14 > 69, contradicting (F4).

Case 2:
In the remaining case, h 0 ≥ 1. By (F5), there are q + = 5 + h 0 positive-weight queries in T ′ . Let q 5 ≥ q + − 1 be the number of weight-5 queries in T ′ . Since all but h 0 of the weight-0 queries are in T ′ , there is a separated set of q 5 − h 0 weight-5 queries in T ′ . By (F6), q 5 − h 0 ≤ 4. This (with q + = 5 + h 0 and q 5 ≥ q + − 1) implies q 5 = h 0 + 4 = q + − 1. This implies that the weight-7 query is in T ′ , along with some separated set of q 5 − h 0 = 4 weight-5 queries. Reasoning as in Case 1, the cost of these five queries alone is at least 69. But T ′ contains at least one additional weight-5 query (as q 5 = 4+h 0 > 4), so T ′ costs strictly more than 69, contradicting (F4). Thus Case 2 cannot actually occur.

⊓ ⊔
To conclude the proof of Theorem 4, we prove that (I 15 , 2) has the necessary properties: Lemma 11 Let T * be any optimal tree for S-subproblem (I 15 , 2). Then T * has at least one node N such that, for the S-subproblem (I N , |H N |) arising at N, the subtree T * N rooted at N in T * does not have minimum cost, opt * (I N , |H N |), for that S-subproblem.
Throughout the proof, unless otherwise specified, the terms holes, queries, and separated, are all with respect to T * . We use the following properties of T * : (P1) T * costs at most 115. Indeed, one way to solve (I 15 , 2) is to take the two weight-7 keys as holes, then use tree T 7b in Fig. 7, of cost 115. As T * is optimal, it costs at most 115. (P2) The root of T * does a less-than comparison. Indeed, by [1,Theorem 5], since T * is optimal for its queries, if T * does an equality-test at the root, then the total query weight in T * is at most four times the maximum query weight. But the total query weight in T * is at least 7 · 5 = 35, while the maximum query weight is at most 7. (P3) In T * there are seven positive-weight queries, and the set of weight-5 queries is separated (by weight-0 queries). To show this, we show that no weight-0 key is a hole. Suppose otherwise for contradiction. Let k ′ be a weight-0 hole. We can assume without loss of generality that k ′ is not used in any node of T * as an inequality key, for otherwise we can modify T * to not use it, without changing its cost, by replacing it with the weight-5 key k ′′ = k ′ + 1 (which could be a hole or a query). Since k ′ is a T * -hole, by definition, k ′ also cannot be used as an equality key. So we can assume that k ′ does not appear as a comparison key in T * . Let k ∈ {k ′ ± 1} be a weight-5 query in T * . (Query k exists in T * -otherwise {k ′ − 1, k ′ , k ′ + 1} would all be holes.) Replace k throughout T * by k ′ . As k ′ and k are adjacent keys and k ′ does not occur in T * , the resulting treeT still solves (I 15 , 2), andT costs less than T * (asT uses the weight-0 key k ′ instead of the weight-5 key k). This contradicts the optimality of T * .
By (P3), T * has seven positive-weight queries. Using condition (P2) and left-right symmetry of subproblem (I 15 , 2), we can assume that the left subtree of T * has at least four of the seven. (Note that "flipping" the tree, namely replacing each key k by 16 − k and swapping the yes and no-subtrees, would map each inequality comparison v < k to v ≤ 16 − k , while our model uses only strict inequalities. However, this latter comparison is equivalent to v < 17 − k .) Let T ′ be the left subtree. Denote the S-subproblem that T ′ solves by (I ℓ , h ′ ). To prove the lemma, assume for contradiction that T ′ is optimal for its S-subproblem, and proceed by cases: Case 1: T ′ has four positive-weight queries. That is, T ′ solves an S-subproblem (I ℓ , h ′ ) where ℓ + − h ′ = 4. By Lemma 9, T ′ has cost 49 and weight 22. The right subtree T ′′ of T * has the three remaining positive-weight queries, the leftmost two of which are separated in T ′′ by a zero-weight query (using (P3)). By Lemma 8 (ii), T ′′ has cost at least 5 · e 3 = 30 and weight at least 15. The cost of T * is its weight plus the costs of T ′ and T ′′ . By the above observations, this is at least (22 + 15) + 49 + 30 = 116, contradicting (P1).
Case 2: T ′ has five positive-weight queries. That is, T ′ solves an S-subproblem (I ℓ , h ′ ) where ℓ + − h ′ = 5. By Lemma 10, T ′ has cost 69 and weight 27. The right subtree T ′′ of T * has the two other positive-weight queries, which have total depth at least 1+1 = 2 in T ′′ , and each has weight at least 5. So T ′′ has cost, and weight, at least 5 · 2 = 10. The cost of T * is its weight plus the costs of T ′ and T ′′ . By the above observations, this is at least (27 + 10) + 69 + 10 = 116, contradicting (P1).
Case 3: T ′ has six or seven positive-weight queries. Let set S consist of just the first six of these queries. Since T ′ is the left subtree of T * (which has seven positive-weight queries) S does not contain the last key, 15. So (using (P3)) all queries in S, except possibly {1, 2}, are separated by weight-zero queries in T ′ . By Lemma 8 (ii), T ′ has cost at least 5 · e 6 = 90. The cost of T * is its weight (at least 7 · 5 = 35), plus the cost of its left and right subtrees (at least 90, counting T ′ alone). So T * costs at least 35 + 90 = 125, contradicting (P1).
This proves the lemma and Theorem 4.

Proof (Theorem 3)
Consider any execution of Spuler's algorithm on the S-subproblem (I, h) from Theorem 4, breaking ties arbitrarily. Let T be the tree it computes for that S-subproblem. By Theorem 4, either T is not optimal for (I, h), or some subtree T ′ of T is not optimal for its Ssubproblem (I ′ , h ′ ). So Spuler's algorithm must compute a non-optimal solution to at least one S-subproblem.

⊓ ⊔
In fact, for this instance (I, h), Spuler's algorithm (as implemented via the Python code in Appendix B) computes a non-optimal tree of cost 116, such as T 7a in Fig. 7. By inspection, tree T 7b in that figure costs 115, so T 7a is not optimal.
Discussion. As mentioned earlier, this counter-example is just for a subproblem. This subproblem has h = 2 holes, so it does not represent a complete instance of 2WCST for which Spuler's algorithm would give an incorrect final result. However, this counter-example does demonstrate that Spuler's algorithm solves some subproblems incorrectly, so that the recurrence relation underlying its dynamic program is incorrect. At a minimum, this suggests that any proof of correctness for Spuler's algorithm would require a more delicate approach. Anderson et al. [1] establish some conditions on the weights of equality-test keys in optimal trees. It may be possible to leverage the bounds from [1] to show that bad subproblems-those that are not solved correctly by the algorithm-never appear as subproblems of an optimal complete tree. For example, per Anderson et al's Theorem 5 for any equality-test node in any optimal tree, the weight of the node's key must be at least one quarter of the total weight of the keys that reach the node. Hence, if a subproblem (I ′ , h ′ ) is solved by some subtree T ′ of an optimal tree T * , then each hole key in T ′ must have weight at least one third of the total weight of the queries in T ′ . This implies that the subproblem (I 15 , 2) in the proof of Theorem 3 cannot actually occur in any optimal tree for (I 15 , 0).
While the question of correctness of Spuler's algorithm is somewhat intriguing, it should be noted that showing its correctness will not improve known complexity bounds for 2WCST, as there are faster 2WCST algorithms that are known to be correct [1,3].

Proof of Lemma 8.
Here is the promised proof of Lemma 8.

Proof (Lemma 8)
Recall that T is a tree for some S-subproblem and Q is a subset of the queries in T , with m = |Q|.
Part (i). Assume that Q is separated. Our goal is to show that the total depth in T of queries in Q is at least d m , as defined before Lemma 8. It is convenient to recast the problem as follows. Change the weight of each query in Q to 1. Change the weight of each query not in Q to 0. We will refer to the resulting cost of a tree as modified cost. Now we need to show that the modified cost of T is at least d m . The proof is by induction on m.
The base cases (when m = 1, 2) are easily verified, so consider the inductive step, for some given m ≥ 3. We assume that T and Q are chosen to minimize the modified cost of T , subject to |Q| = m. Call this the minimality assumption.
Suppose T does an inequality test at the root. Let T 1 and T 2 be the left and right subtrees of T , and for a ∈ {1, 2} let Q a ⊆ Q contain the queries in Q that fall in T a . Let i = |Q 1 |, so that |Q 2 | = m − i. For a ∈ {1, 2}, query set Q a is T a -separated. By the minimality assumption, 0 ∈ {i, m − i}. The modified cost of T is its weight (m), plus the modified costs of T 1 and T 2 . By the inductive assumption, this is at least m + d i + d m−i ≥ d m , as desired.
Suppose T does an equality test at the root. The minimality assumption implies that the equality-test key has non-zero (modified) weight. (This follows via the argument given for Property (P2) in the proof of Lemma 11, using Anderson et al's Theorem 5 or Corollary 3.) So the equality-test key is in Q. Let T 1 be the no-subtree of T and let Q 1 ⊆ Q contain the queries in Q that fall in T 1 ; so we have |Q 1 | = m − 1. Set Q 1 is T 1 -separated, so by the inductive assumption, T 1 has modified cost at least d m−1 . So the modified cost of T is at least m + d m−1 = m + d 1 + d m−1 ≥ d m , as desired.
Part (ii). The proof of Part (ii) follows the same inductive argument as above. The base cases for m = 1, 2 are trivial. The verification of the base case for m = 3 is by straightforward case analysis. In the inductive step, the only significant difference is in the case when T does an inequality test at the root. Since Q is now only nearly separated, Q 1 will be T 1 -separated while Q 2 will be nearly T 2 -separated (or vice versa), giving us that the modified cost of T is at least m + d i + e m−i ≥ e m . ⊓ ⊔ Note: We would like to use this opportunity to acknowledge yet another error in the literature on binary split trees, this one in our own paper [3]. In that paper we introduced a perturbation method that can be used to extend algorithms for binary search trees with keys of distinct weights to instances where key-weights need not be distinct, and we claimed that this method can be used to speed up the computation of optimal binary split trees to achieve running time O(n 4 ).