📖 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 numbers
Insert multiple phone numbers into the BST
Print the BST in-order to show sorted phone numbers
💡 Why This Matters
🌍 Real World
BSTs are used in phone books, contact lists, and databases to keep data sorted for fast search and insertion.
💼 Career
Understanding BST insertions is fundamental for software engineers working with data structures, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create BST Node Class
Create a class called Node with a public integer field Data, and two public Node fields called Left and Right. Add a constructor that takes an integer parameter data and sets the Data field to it, and initializes Left and Right to null.
DSA C#
Hint
Think of each node as a box holding a number and links to two smaller boxes on the left and right.
2
Create BST Class with Root
Create a class called BST with a public Node field called Root. Initialize Root to null in the constructor.
DSA C#
Hint
The BST class holds the top node called Root, which starts empty.
3
Add Insert Method to BST
Inside the BST class, add a public method called Insert that takes an integer parameter data. If Root is null, set Root to a new Node with data. Otherwise, call a private helper method InsertNode with Root and data. Implement InsertNode as a private method that takes a Node and an integer data. If data is less than the node's Data, recursively insert into the left subtree; if the left child is null, create a new Node there. If data is greater or equal, do the same on the right subtree.
DSA C#
Hint
Insert by comparing the new number with current node and going left or right recursively.
4
Print BST In-Order
Add a public method PrintInOrder in the BST class that prints all node Data values in ascending order. Use a private helper method PrintInOrderNode that takes a Node. It should recursively print the left subtree, then the node's Data, then the right subtree. In the Main method, create a BST instance, insert the numbers 50, 30, 70, 20, 40, 60, and 80 in that order, then call PrintInOrder. Print each number followed by a space on the same line.
DSA C#
Hint
Print left subtree, then current node, then right subtree to get sorted order.
Practice
(1/5)
1. What is the main purpose of the Insert operation in a Binary Search Tree (BST)?
easy
A. To delete a node from the BST
B. To add a new node while keeping the BST property intact
C. To find the maximum value in the BST
D. To balance the BST automatically
Solution
Step 1: Understand the Insert operation
The Insert operation adds a new value to the BST while maintaining the order property: left child < parent < right child.
Step 2: Identify the purpose of Insert
Insert does not delete nodes, find max, or balance the tree automatically. Its main job is to add nodes correctly.
Final Answer:
To add a new node while keeping the BST property intact -> Option B
Quick Check:
Insert = Add node correctly [OK]
Hint: Insert adds nodes preserving BST order [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert balances the tree automatically
Assuming insert finds max or min values
2. Which of the following is the correct C# method signature for inserting a value into a BST node class?
A. Missing assignment when inserting into left or right subtree
B. Incorrect base case for null root
C. Method should return void instead of Node
D. Using else instead of else if for right subtree insertion
Solution
Step 1: Analyze recursive calls
Recursive calls to Insert on left or right subtree do not assign the returned node back to root.Left or root.Right.
Step 2: Identify fix
Must assign: root.Left = Insert(value, root.Left); and similarly for right to update tree links.
Final Answer:
Missing assignment when inserting into left or right subtree -> Option A
Quick Check:
Assign recursive result to subtree [OK]
Hint: Always assign recursive insert result to child node [OK]
Common Mistakes:
Not assigning recursive insert result
Changing base case incorrectly
Confusing return type with void
5. You have a BST with nodes 10, 5, 15. You want to insert 12 and 18. After insertion, which of the following represents the correct pre-order traversal?
hard
A. 10 -> 15 -> 18 -> 12 -> 5 -> null
B. 10 -> 5 -> 12 -> 15 -> 18 -> null
C. 10 -> 15 -> 5 -> 12 -> 18 -> null
D. 10 -> 5 -> 15 -> 12 -> 18 -> null
Solution
Step 1: Insert 12 and 18 into BST
Start with root 10, left child 5, right child 15. Insert 12: 12 < 15, goes left of 15. Insert 18: 18 > 15, goes right of 15.