Searching in a Binary Search Tree (Implementation)
Explore the implementation of search functions in binary search trees using JavaScript. Understand both iterative and recursive methods to efficiently find nodes, preparing you for coding interview challenges on tree-based data structures.
Introduction #
In this lesson, we’ll implement a search function for binary search trees which will return a node from the tree if the value to be searched matches it. We’ll again implement both an iterative and a recursive solution.
Here is a high-level description of the algorithm:
-
Set the
currentNodeequal to root. -
If the value to be searched is less than the
currentNode's value, then move on to the left subtree, otherwise move on to the right subtree. -
Repeat ...