Search⌘ K
AI Features

Solution: Random Binary Search Trees

Explore the implementation of randomized binary search trees through Treaps by constructing them from sorted arrays. Understand how recursive methods build the tree by dividing the array and adding nodes efficiently, ensuring optimal O(n) performance. This lesson helps you master the detailed algorithm and recursion involved in creating Treaps that simulate sequential inserts.

We'll cover the following...

Task

Here is the code that implements an algorithm that constructs a Treap from a sorted array, a, of n elements. This method runs in O(n)O(n) worst-case time and should construct a Treap that is indistinguishable from one in which the elements of a were added one at a time using the add(x) method.

Solution

This code widget shows the implementation of the addAll() and the addAllRecursive() methods that construct a Treap from a sorted array a of n ...