Searching in a Binary Search Tree (Implementation)
Explore how to implement both iterative and recursive search methods in a binary search tree using C#. Understand the algorithm to traverse the tree for matching values, and gain insight into time complexities for average and worst cases. This lesson builds skills to efficiently retrieve nodes in tree data structures.
Introduction
In this lesson, you will implement a search function for binary search trees, which will return a node from the tree if the value to be searched matches it. And again, you will 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 is less than the
currentNode's value, then move on to the left-subtree. Otherwise, move on to the right-subtree. -
Repeat until the value at the
currentNodeis equal to the value searched, or it becomesNULL. -
Return the
currentNode. ...