📖 Scenario: Imagine you are organizing a collection of books by their unique ID numbers. You want to store them so that searching for a book by its ID is very fast. A Binary Search Tree (BST) helps you do this by keeping books in a special order.
🎯 Goal: You will build a simple Binary Search Tree (BST) with three nodes and then print the tree structure to see how the BST property keeps the data organized.
📋 What You'll Learn
Create three nodes with values 10, 5, and 15
Link the nodes to form a BST where 5 is left child of 10 and 15 is right child of 10
Print the tree nodes in order: left child, root, right child
💡 Why This Matters
🌍 Real World
BSTs are used in databases and file systems to quickly find data by keeping it sorted.
💼 Career
Understanding BSTs helps in software development roles that involve data storage, search optimization, and algorithm design.
Progress0 / 4 steps
1
Create BST nodes
Create three variables called root, leftChild, and rightChild with values 10, 5, and 15 respectively.
DSA Javascript
Hint
Use const to create variables and assign the exact numbers.
2
Link nodes to form BST
Create an object called bst with properties value, left, and right. Set value to root, left to leftChild, and right to rightChild.
DSA Javascript
Hint
Create an object with keys value, left, and right using the variables from Step 1.
3
Write function to print BST in order
Write a function called inOrderPrint that takes a node object and prints its left child, then its value, then its right child. Use console.log to print values.
DSA Javascript
Hint
Print left child first, then value, then right child using console.log.
4
Print the BST nodes in order
Call the function inOrderPrint with the bst object to print the nodes in order.
DSA Javascript
Hint
Call inOrderPrint(bst); to see the nodes printed in order.
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. Each node has exactly two children.
B. For each node, all values in the left subtree are smaller, and all values in the right subtree are larger.
C. All nodes have the same value.
D. Nodes are arranged in a circular linked list.
Solution
Step 1: Understand BST node value rules
In a BST, the left subtree contains values smaller than the node, and the right subtree contains values larger than the node.
Step 2: Compare options with BST definition
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. matches the BST property exactly, while others describe different structures.
Final Answer:
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option B
Quick Check:
BST property = left < node < right [OK]
Hint: Remember: left < node < right defines BST [OK]
Common Mistakes:
Confusing BST with binary tree (not ordered)
Thinking all nodes must have two children
Assuming all values are equal
2. Which of the following is the correct way to check if a value should go to the left subtree in a BST insertion function in JavaScript?
easy
A. if (value < node.value) { insertLeft(); }
B. if (value > node.value) { insertLeft(); }
C. if (value == node.value) { insertLeft(); }
D. if (value >= node.value) { insertLeft(); }
Solution
Step 1: Recall BST insertion rule
Values smaller than the current node go to the left subtree.
Step 2: Match condition to insertion side
Only the condition 'value < node.value' correctly directs insertion to the left.
Final Answer:
if (value < node.value) { insertLeft(); } -> Option A
Quick Check:
Smaller values go left [OK]
Hint: Use < for left subtree insertion [OK]
Common Mistakes:
Using > instead of < for left subtree
Using == which is not for left or right
Using >= which includes equal values incorrectly
3. What will be the in-order traversal output of the following BST?
10
/ \
5 15
/ \ \
3 7 20
medium
A. [20, 15, 10, 7, 5, 3]
B. [10, 5, 3, 7, 15, 20]
C. [3, 5, 7, 10, 15, 20]
D. [5, 3, 7, 10, 15, 20]
Solution
Step 1: Understand in-order traversal
In-order traversal visits left subtree, then node, then right subtree recursively.
Step 2: Apply in-order to given tree
Left subtree of 10: 3, 5, 7; then 10; then right subtree: 15, 20. Combined: [3, 5, 7, 10, 15, 20]
Final Answer:
[3, 5, 7, 10, 15, 20] -> Option C
Quick Check:
In-order = sorted BST values [OK]
Hint: In-order traversal of BST gives sorted values [OK]
Common Mistakes:
Confusing pre-order or post-order with in-order
Listing nodes in insertion order
Reversing the traversal order
4. Consider this BST insertion code snippet in JavaScript:
What is the main issue with this code regarding BST property?
medium
A. It inserts larger values on the left subtree incorrectly.
B. It does not handle the case when node is null.
C. It returns undefined instead of the node.
D. It allows duplicate values on the left subtree, which may break strict BST rules.
Solution
Step 1: Analyze duplicate handling in insertion
The condition 'value <= node.value' inserts duplicates to the left subtree.
Step 2: Understand BST strictness
Strict BSTs require left subtree values strictly less than node value; duplicates on left can break this.
Final Answer:
It allows duplicate values on the left subtree, which may break strict BST rules. -> Option D
Quick Check:
Duplicates on left break strict BST [OK]
Hint: Use < for left to avoid duplicates on left [OK]
Common Mistakes:
Ignoring duplicate value placement
Thinking null check is missing
Confusing left and right subtree insertion
5. You have a BST where some nodes violate the BST property because their left child has a value greater than the node. Which approach best fixes the tree without rebuilding it completely?
hard
A. Swap the values of the violating nodes with their children to restore BST property locally.
B. Ignore the violations as they do not affect search operations.
C. Delete all nodes and insert them again in correct order.
D. Perform an in-order traversal, collect values, then rebuild the BST from sorted values.
Solution
Step 1: Understand the violation type
Left child having greater value than node breaks BST property locally.
Step 2: Choose minimal fix without full rebuild
Swapping values of violating nodes with their children can restore local BST property without full rebuild.
Step 3: Evaluate other options
Perform an in-order traversal, collect values, then rebuild the BST from sorted values. rebuilds fully, Delete all nodes and insert them again in correct order. deletes all nodes (costly), Ignore the violations as they do not affect search operations. ignores problem (wrong).
Final Answer:
Swap the values of the violating nodes with their children to restore BST property locally. -> Option A
Quick Check:
Local swaps fix BST violations efficiently [OK]
Hint: Fix BST by swapping violating node values locally [OK]
Common Mistakes:
Rebuilding entire tree unnecessarily
Ignoring violations thinking no impact
Deleting all nodes instead of fixing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.