Optimal Binary Search Trees

Learn about the optimal binary search trees problem and its solution using backtracking.

Introduction to optimal binary search trees

Our final example combines recursive backtracking with the divide-and-conquer strategy. Recall that the running time for a successful search in a binary search tree is proportional to the number of ancestors of the target node. As a result, the worst-case search time is proportional to the depth of the tree. Thus, to minimize the worst-case search time, the height of the tree should be as small as possible; by this metric, the ideal tree is perfectly balanced.

Press + to interact
A simple binary search tree
A simple binary search tree

In many applications of binary search trees, however, it is more important to minimize the total cost of several searches rather than the worst-case cost of a single search. If xx is a more frequent search target than yy, we can save time by building a tree where the depth of xx is smaller than the depth of yy, even if that means increasing the overall depth of the tree. A perfectly balanced tree is not the best choice if some items are significantly more popular than others. In fact, a totally unbalanced tree with depth Ω(n)Ω(n) might actually be the best choice!

Problem statement

This situation suggests the following problem. Suppose we are given a sorted array of keys A[1..n]A[1 .. n] and an array ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy