Create Balanced Binary Search Tree From Sorted List: Last Modi Ed: August 25, 2021
Create Balanced Binary Search Tree From Sorted List: Last Modi Ed: August 25, 2021
com/cs/)
Algorithms (https://www.baeldung.com/cs/category/algorithms)
Trees (https://www.baeldung.com/cs/category/graph-
theory/trees)
Binary Tree (https://www.baeldung.com/cs/tag/binary-tree)
4. Top-Down Approach
The top-down approach uses a sorted array to create a balanced BST.
Therefore, we can access any index inside the array in constant time.
Let’s have a look at the top-down approach to creating a balanced BST:
The initial call for this function passes the array , the value of is zero
and is , where is the size of the array.
In the beginning, we check to see if we reached an empty range. In this
case, we simply return , indicating an empty object.
Next, we get the middle index and initiate the root node with .
After that, we make two recursive calls. The first call is for the range
, which represents the elements before the index . On the
other hand, the second call is for the range , which
corresponds to the elements after the index .
The first call returns the root of the left subtree. Therefore, we assign its
value to the left pointer of the root node. Similarly, the second call
returns the root of the right subtree. Hence, we assign its value to the
right pointer of the root node.
The complexity of the top-down approach is , where is the
number of elements inside the array.
5. Bottom-Up Approach
The bottom-up approach uses a linked list to build a balanced BST. As a
result, we’ll have a pointer to the head of the list and we can only move
this pointer forward in constant time.
Take a look at the bottom-up approach to creating a balanced BST:
The initial call for this function is similar to the top-down approach. We’ll
pass a pointer the beginning of the list , the value of is zero and
is , where is the number of elements.
First, we check whether we have reached an empty range. If so, we
return indicating an empty tree.
Then, We build the left subtree recursively. Next, since the left subtree is
completely built, it means the pointer is now pointing to the
element with index . Therefore, we store its value inside the root.
After that, we move the pointer one step forward because when
the recursive call for the left subtree finished, we assumed that the
pointer was on the mid element. Hence, we need to move the pointer
forward so that the next recursive call can use it from there.
Finally, we perform a recursive call to build the right subtree and then
return the root node.
The reason for calling this approach bottom-up is that we keep going
into recursive calls until we reach the leftmost node. We create this
node and then move to other recursive calls to build their nodes as well.
The complexity of the top-down approach is , where is the
number of elements inside the linked list.
6. Comparison
Usually, the top-down approach is considered easier to understand and
implement, because it’s straight forward. On the other hand, the bottom-
up approach needs a little more understanding of the recursive calls,
and how exactly is the tree built from the leftmost to the rightmost node.
Both approaches have the same time complexity. However, each
approach is based on a different data structure.
If the data is inside a sorted array, then using the top-down approach is
usually easier than the bottom-up approach. On the other hand, if the
data is inside a sorted linked list, then we need to use the bottom-up
approach.
7. Conclusion
In this tutorial, we presented two approaches to building a balanced
binary search tree from a sorted list.
Firstly, we explained the general concept of balanced binary search
trees. Secondly, we presented the top-down approach and the bottom-
up approach.
In the end, we compared both approaches and showed when to use
each one.
2 COMMENTS Oldest
Ada
7 months ago
Typo in the code for the top down approach: should be root.right = build(A,
mid + 1, R);
Loredana Crusoveanu
5 months ago
Reply to
Ada
Thanks for catching this; fixed.
SERIES
DRAWING CHARTS IN LATEX (/CS/CATEGORY/SERIES)
ABOUT
ABOUT BAELDUNG (HTTPS://WWW.BAELDUNG.COM/ABOUT)
THE FULL ARCHIVE (/CS/FULL_ARCHIVE)
WRITE FOR BAELDUNG (/CONTRIBUTION-GUIDELINES)
EDITORS (HTTPS://WWW.BAELDUNG.COM/EDITORS)