0
0
DSA Typescriptprogramming~30 mins

BST Search Operation in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
BST Search Operation
📖 Scenario: You are building a simple phone book app that stores contacts in a Binary Search Tree (BST). Each contact has a unique phone number. You want to find if a contact exists by searching the BST.
🎯 Goal: Build a TypeScript program that creates a BST with given phone numbers, then searches for a specific phone number in the BST.
📋 What You'll Learn
Create a BST node class with value, left, and right properties
Insert given phone numbers into the BST in the given order
Create a function to search for a phone number in the BST
Print true if the phone number is found, otherwise false
💡 Why This Matters
🌍 Real World
Binary Search Trees are used in phone books, contact lists, and databases to quickly find information by key.
💼 Career
Understanding BST search is important for software engineers working with data storage, search engines, and performance optimization.
Progress0 / 4 steps
1
Create BST Node Class and Insert Nodes
Create a class called BSTNode with a constructor that takes a value of type number. It should have public properties value, left, and right initialized to null. Then create a variable called root and insert these phone numbers in this order: 50, 30, 70, 20, 40, 60, 80 using a helper function insertNode.
DSA Typescript
Hint

Think of each node as a contact with a phone number. Insert smaller numbers to the left, bigger to the right.

2
Set the Phone Number to Search
Create a variable called searchValue and set it to 40. This is the phone number you want to find in the BST.
DSA Typescript
Hint

This variable holds the phone number you want to find in the tree.

3
Implement BST Search Function
Create a function called searchBST that takes two parameters: root of type BSTNode | null and value of type number. It should return true if the value is found in the BST, otherwise false. Use the BST property to decide whether to search left or right subtree or return true if found.
DSA Typescript
Hint

Use recursion to go left if value is smaller, right if bigger, or return true if found.

4
Print Search Result
Use console.log to print the result of calling searchBST with root and searchValue. It should print true if the value is found, otherwise false.
DSA Typescript
Hint

Call searchBST(root, searchValue) and print the result using console.log.