Search⌘ K

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:

  1. Set the currentNode equal to root.

  2. If the value is less than the currentNode's value, then move on to the left-subtree. Otherwise, move on to the right-subtree.

  3. Repeat until the value at the currentNode is equal to the value searched, or it becomes NULL.

  4. Return the currentNode. ...