📖 Scenario: Imagine you have a collection of numbers and you want to find, add, or check if a number exists quickly. You can store these numbers in a tree structure. A plain binary tree stores numbers without any order, while a Binary Search Tree (BST) keeps numbers in order to speed up searching.
🎯 Goal: Build two simple tree structures: a plain binary tree and a binary search tree (BST). Then, compare how searching for a number works in both. This will show why BST is better for quick searches.
📋 What You'll Learn
Create a plain binary tree with specific nodes
Create a binary search tree (BST) with the same nodes in order
Write a search function for both trees
Search for a number in both trees and print the results
💡 Why This Matters
🌍 Real World
Binary search trees are used in databases and file systems to quickly find data without checking every item.
💼 Career
Understanding BSTs helps in software engineering roles that involve data storage, search optimization, and algorithm design.
Progress0 / 4 steps
1
Create a plain binary tree
Create a plain binary tree using the TreeNode class with these exact nodes: root node with value 10, left child 5, right child 15, left child of 5 is 3, right child of 5 is 7.
DSA Typescript
Hint
Use the TreeNode class to create nodes and link them by setting left and right properties.
2
Create a binary search tree (BST)
Create a binary search tree (BST) by inserting these values in order: 10, 5, 15, 3, 7. Use the insertBST function to place nodes correctly.
DSA Typescript
Hint
Use the insertBST function to add each value to the BST starting from null for the root.
3
Write search functions for both trees
Write two search functions: searchPlain to search the plain binary tree and searchBST to search the BST. Both take a TreeNode | null and a number value, and return true if found, false otherwise.
DSA Typescript
Hint
Use recursion to check nodes. For searchPlain, check both left and right children. For searchBST, decide direction based on value comparison.
4
Search for a number and print results
Use searchPlain and searchBST to search for the number 7 in both trees. Print the results exactly as: "Plain Tree: true" and "BST: true".
DSA Typescript
Hint
Call both search functions with 7 and print results exactly as shown.
Practice
(1/5)
1. Why is a Binary Search Tree (BST) preferred over a plain Binary Tree for searching operations?
easy
A. Because BST keeps elements in sorted order allowing faster search
B. Because BST uses less memory than a plain Binary Tree
C. Because BST nodes can have more than two children
D. Because BST does not require pointers to child nodes
Solution
Step 1: Understand the structure of BST vs plain Binary Tree
A BST stores nodes so that left child is smaller and right child is larger, keeping data sorted. A plain Binary Tree has no order.
Step 2: Analyze search efficiency
Because BST is sorted, searching can skip half the tree at each step, making it faster than searching a plain Binary Tree which may require checking all nodes.
Final Answer:
Because BST keeps elements in sorted order allowing faster search -> Option A
Quick Check:
Sorted structure = faster search [OK]
Hint: BST is sorted, plain tree is not, so search is faster [OK]
Common Mistakes:
Thinking BST uses less memory
Confusing number of children in BST
Assuming BST has no pointers
2. Which of the following is the correct TypeScript syntax to declare a node in a Binary Search Tree?
easy
A. class Node { value: number; left: Node | null; right: Node | null; }
B. function Node(value) { this.value = value; this.left = null; this.right = null; }
D. type Node = { value: number; left?: Node; right?: Node }
Solution
Step 1: Identify correct TypeScript class syntax for BST node
class Node { value: number; left: Node | null; right: Node | null; } correctly declares a class with value and nullable left and right nodes using TypeScript types.
Step 2: Check other options for correctness
function Node(value) { this.value = value; this.left = null; this.right = null; } is JavaScript function style, not TypeScript class. interface Node { value: string; left: Node; right: Node; } uses string value and non-nullable children which is incorrect. type Node = { value: number; left?: Node; right?: Node } uses optional children but is a type alias, not a class.
Final Answer:
class Node { value: number; left: Node | null; right: Node | null; } -> Option A
Quick Check:
TypeScript class with nullable children = class Node { value: number; left: Node | null; right: Node | null; } [OK]
Hint: Look for TypeScript class with nullable left/right nodes [OK]
Common Mistakes:
Confusing JavaScript function with TypeScript class
Using non-nullable children causing errors
Using wrong value types
3. Given the following BST insertion code snippet, what will be the in-order traversal output after inserting values 10, 5, 15, 3, 7?
medium
A. [5, 3, 7, 10, 15]
B. [10, 5, 15, 3, 7]
C. [15, 10, 7, 5, 3]
D. [3, 5, 7, 10, 15]
Solution
Step 1: Insert values into BST in given order
Insert 10 as root, 5 to left, 15 to right, 3 to left of 5, 7 to right of 5.
Traverse left subtree: 3, 5, 7; then root 10; then right subtree 15. Combined: [3, 5, 7, 10, 15]
Final Answer:
[3, 5, 7, 10, 15] -> Option D
Quick Check:
In-order traversal of BST = sorted values [OK]
Hint: In-order traversal of BST always gives sorted list [OK]
Common Mistakes:
Confusing traversal order
Inserting nodes incorrectly
Mixing up left and right children
4. Identify the bug in this BST insertion function snippet in TypeScript:
function insert(root: Node | null, val: number): Node {
if (!root) return new Node(val);
if (val < root.value) insert(root.left, val);
else insert(root.right, val);
return root;
}
medium
A. Node constructor is called without new keyword
B. Missing return statements for recursive calls
C. Incorrect comparison operator for BST insertion
D. Function does not handle duplicate values
Solution
Step 1: Analyze recursive calls in insertion
The recursive calls insert(root.left, val) and insert(root.right, val) do not assign the returned node back to root.left or root.right.
Step 2: Identify missing return assignments
Without assigning, the tree structure does not update. Correct code should be root.left = insert(root.left, val) and root.right = insert(root.right, val).
Final Answer:
Missing return statements for recursive calls -> Option B
Quick Check:
Recursive insert must update child links [OK]
Hint: Always assign recursive insert results to child nodes [OK]
Common Mistakes:
Not assigning recursive call results
Using wrong comparison operators
Ignoring duplicates silently
5. You have a plain Binary Tree and a BST both containing the same values. Which operation benefits the most from using the BST instead of the plain Binary Tree, and why?
hard
A. Traversing all nodes, because BST traversal is faster than plain tree traversal
B. Inserting a new value, because BST insertion is always O(1)
C. Searching for a value, because BST allows O(log n) average time search
D. Deleting a node, because BST deletion never requires rebalancing
Solution
Step 1: Compare searching in BST vs plain Binary Tree
BST keeps nodes sorted, enabling binary search-like behavior with average O(log n) time. Plain Binary Tree has no order, so search is O(n).
Step 2: Evaluate other operations
Insertion in BST is not always O(1), deletion may require rebalancing, and traversal visits all nodes in both trees equally.
Final Answer:
Searching for a value, because BST allows O(log n) average time search -> Option C
Quick Check:
BST speeds up search, not insertion or traversal [OK]
Hint: BST speeds up search, not insertion or traversal [OK]
Common Mistakes:
Thinking insertion is always O(1)
Assuming deletion never needs rebalancing
Believing traversal is faster in BST
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.