📖 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: Build a program that inserts phone numbers into a BST and prints the tree in sorted order.
📋 What You'll Learn
Create a BST node structure with integer phone number data
Write a function to insert a new phone number into the BST
Write a function to print the BST in ascending order (inorder traversal)
Insert given phone numbers into the BST and print the sorted phone numbers
💡 Why This Matters
🌍 Real World
Phone books, contact lists, and databases use BSTs to keep data sorted for fast searching and insertion.
💼 Career
Understanding BST insertions is fundamental for software engineers working with data structures, databases, and performance optimization.
Progress0 / 4 steps
1
Create BST Node Structure
Create a struct Node with an int data field, and two pointers left and right to struct Node.
DSA C
Hint
Think of each node as a box holding a number and two arrows pointing to left and right child nodes.
2
Create Insert Function
Write a function struct Node* insert(struct Node* root, int data) that inserts data into the BST rooted at root and returns the new root. Use recursion and create a new node when root is NULL.
DSA C
Hint
Insert smaller numbers to the left, bigger or equal to the right. Create a new node when you reach a NULL spot.
3
Create Inorder Print Function
Write a function void inorder(struct Node* root) that prints the BST nodes in ascending order using inorder traversal. Print each data followed by a space.
DSA C
Hint
Visit left child, print current node, then visit right child recursively.
4
Insert Data and Print BST
In main(), create a struct Node* root initialized to NULL. Insert these phone numbers in order: 50, 30, 70, 20, 40, 60, 80 using the insert function. Then call inorder(root) to print the sorted phone numbers.
DSA C
Hint
Insert each number by calling insert and update root. Then call inorder(root) to print.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Binary Search Tree (BST)?
easy
A. To find the height of the BST
B. To delete a node from the BST
C. To add a new node while keeping the BST property intact
D. To traverse the BST in order
Solution
Step 1: Understand BST property
A BST keeps nodes ordered so that left child is smaller and right child is larger than the parent.
Step 2: Purpose of insert operation
Insert adds a new node in the correct position to maintain this order.
Final Answer:
To add a new node while keeping the BST property intact -> Option C
Quick Check:
Insert = Add node correctly [OK]
Hint: Insert means add node keeping order [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert changes tree traversal
Assuming insert changes tree height always
2. Which of the following is the correct way to declare a BST node structure in C for insertion?
easy
A. typedef struct Node { int data; struct Node* left; struct Node* right; } Node;
B. struct Node { int data; int left; int right; };
C. typedef struct Node { int data; Node left; Node right; } Node;
D. struct Node { int data; struct Node left; struct Node right; };
Solution
Step 1: Check pointer usage in struct
BST nodes must have pointers to left and right child nodes, not direct structs or ints.
Step 2: Validate correct syntax
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; uses pointers to struct Node correctly, others use wrong types or missing pointers.
Final Answer:
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; -> Option A
Quick Check:
Node pointers needed for children [OK]
Hint: Use pointers for left and right child nodes [OK]
Common Mistakes:
Using int instead of pointers for children
Missing struct keyword before Node*
Declaring children as struct instead of pointers
3. Given the following BST and insertion code snippet, what will be the inorder traversal output after inserting 25?
20
/ \
10 30
// Insert function called with root and 25
medium
A. [10, 20, 25, 30]
B. [10, 20, 30, 25]
C. [10, 25, 20, 30]
D. [20, 10, 25, 30]
Solution
Step 1: Insert 25 in BST
25 is greater than 20, so move right to 30. 25 is less than 30, so insert as left child of 30.
Step 2: Inorder traversal after insertion
Inorder visits left, root, right: 10, 20, 25, 30.
Final Answer:
[10, 20, 25, 30] -> Option A
Quick Check:
Inorder sorted = [10, 20, 25, 30] [OK]
Hint: Inorder traversal of BST is sorted list [OK]
Common Mistakes:
Inserting 25 as right child of 30
Misordering inorder traversal
Confusing preorder with inorder
4. Identify the error in the following BST insert function snippet: