📖 Scenario: You are building a simple phone book application that stores phone numbers in a Binary Search Tree (BST) to keep them sorted for quick search and insertion.
🎯 Goal: Learn how to insert new phone numbers into a BST while maintaining its sorted property.
📋 What You'll Learn
Create a BST node class with integer data and left/right child references
Create a BST class with an insert method to add new nodes
Insert multiple phone numbers into the BST
Print the BST in-order to show sorted phone numbers
💡 Why This Matters
🌍 Real World
Phone books, contact lists, and databases use BSTs to keep data sorted for fast insertion and search.
💼 Career
Understanding BST insertions is key for software engineers working on efficient data storage and retrieval systems.
Progress0 / 4 steps
1
Create BST Node Class
Create a class called Node with an integer variable data, and two Node references called left and right. Add a constructor that sets data and initializes left and right to null.
DSA Java
Hint
Think of Node as a box holding a number and links to two other boxes.
2
Create BST Class with Root
Create a class called BST with a Node variable called root. Initialize root to null in the constructor.
DSA Java
Hint
The BST class holds the starting point of your tree called root.
3
Add Insert Method to BST
Inside the BST class, add a method called insert that takes an integer data and inserts it into the BST. Use a helper method insertRec that takes a Node and data, and returns the updated Node. Insert smaller values to the left and larger or equal values to the right.
DSA Java
Hint
Use recursion to find the right place to add the new number.
4
Insert Numbers and Print In-Order
In the BST class, add a method called inOrder that prints the BST nodes in ascending order separated by spaces. In the main method, create a BST object, insert these numbers in order: 50, 30, 70, 20, 40, 60, 80. Then call inOrder to print the sorted numbers.
DSA Java
Hint
In-order traversal prints the numbers in sorted order.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Binary Search Tree (BST)?
easy
A. To add a new node while maintaining BST properties
B. To delete a node from the BST
C. To find the maximum value in the BST
D. To balance the BST after insertion
Solution
Step 1: Understand BST insert operation
The insert operation adds a new node to the BST without breaking the BST property where left child nodes are smaller and right child nodes are larger.
Step 2: Identify the purpose
Insertion is specifically for adding new nodes, not deleting or balancing the tree.
Final Answer:
To add a new node while maintaining BST properties -> Option A
Quick Check:
Insert = Add node [OK]
Hint: Insert means add node keeping order [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert balances the tree automatically
Assuming insert finds max value
2. Which of the following is the correct Java method signature for inserting a value into a BST node?
easy
A. public void insert(int value)
B. public boolean insert(int value, Node root)
C. public int insert(Node root, int value)
D. public Node insert(Node root, int value)
Solution
Step 1: Analyze method parameters and return type
Insertion usually requires the root node and the value to insert. It returns the updated root node after insertion.
Step 2: Match signature with common BST insert pattern
The method returns a Node and takes Node root and int value as parameters, matching public Node insert(Node root, int value).
Final Answer:
public Node insert(Node root, int value) -> Option D
Quick Check:
Insert method returns Node with root and value [OK]
3. What will be the inorder traversal output after inserting 15 into the BST with nodes 10, 5, 20?
medium
A. [5, 15, 10, 20]
B. [10, 5, 15, 20]
C. [5, 10, 15, 20]
D. [20, 15, 10, 5]
Solution
Step 1: Insert 15 into BST
Start at root 10: 15 > 10, go right to 20. 15 < 20, insert as left child of 20.
Step 2: Perform inorder traversal
Inorder visits left, root, right: left subtree [5], root 10, left child of 20 is 15, then 20. Result: [5, 10, 15, 20]
Final Answer:
[5, 10, 15, 20] -> Option C
Quick Check:
Inorder after insert = sorted list [OK]
Hint: Inorder traversal of BST is sorted [OK]
Common Mistakes:
Inserting 15 as right child of 10 incorrectly
Confusing preorder with inorder traversal
Ignoring BST property during insertion
4. Identify the error in this BST insert method snippet:
public Node insert(Node root, int val) {
if (root == null) {
root = new Node(val);
} else if (val < root.val) {
insert(root.left, val);
} else {
insert(root.right, val);
}
return root;
}
medium
A. Incorrect base case for null root
B. Missing return when recursively inserting left or right
C. Using assignment instead of comparison in if condition
D. Returning void instead of Node
Solution
Step 1: Check recursive calls
Recursive calls to 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 fix
Must assign: root.left = insert(root.left, val); and root.right = insert(root.right, val); to update tree links.
Final Answer:
Missing return when recursively inserting left or right -> Option B
Quick Check:
Assign recursive insert result to child [OK]
Hint: Assign recursive insert result to child node [OK]
Common Mistakes:
Not assigning recursive insert result to left/right child
Confusing base case logic
Returning wrong type
5. Given a BST with nodes inserted in this order: 30, 20, 40, 10, 25, 35, 50, what will be the structure after inserting 22? Choose the correct inorder traversal output.
hard
A. [10, 20, 22, 25, 30, 35, 40, 50]
B. [10, 20, 25, 22, 30, 35, 40, 50]
C. [10, 22, 20, 25, 30, 35, 40, 50]
D. [10, 20, 25, 30, 22, 35, 40, 50]
Solution
Step 1: Insert 22 into BST
Start at root 30: 22 < 30 go left to 20; 22 > 20 go right to 25; 22 < 25 insert as left child of 25.