📖 Scenario: You are building a simple phone book app that stores phone numbers in a Binary Search Tree (BST) to keep them sorted for quick search and insertion.
🎯 Goal: Build a BST by inserting phone numbers one by one and then print the tree in sorted order.
📋 What You'll Learn
Create a class called Node with attributes value, left, and right
Create a class called BST with a root attribute initialized to None
Add an insert method in BST to insert a new number correctly
Add an inorder method in BST to print the tree values in sorted order
💡 Why This Matters
🌍 Real World
Binary Search Trees are used in databases and file systems to keep data sorted and allow fast search, insert, and delete operations.
💼 Career
Understanding BST insertions is important for software engineers working with data structures, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create Node and BST classes
Create a class called Node with an __init__ method that takes value and sets left and right to None. Then create a class called BST with an __init__ method that sets root to None.
DSA Python
Hint
Think of Node as a box holding a number and two empty spaces for smaller and bigger numbers.
2
Add insert method to BST
Add a method called insert in BST that takes a value. If root is None, set root to a new Node with value. Otherwise, call a helper method _insert_recursive with root and value.
DSA Python
Hint
Start by checking if the tree is empty. If yes, the new number becomes the root.
3
Implement _insert_recursive helper method
Add a private method called _insert_recursive in BST that takes current_node and value. If value is less than current_node.value, insert to the left subtree; if left child is None, create new Node, else recurse left. If value is greater or equal, do the same on the right subtree.
DSA Python
Hint
Compare the new number with current node. Go left if smaller, right if bigger or equal. Insert when you find an empty spot.
4
Add inorder traversal and print the BST
Add a method called inorder in BST that prints all values in sorted order by traversing left subtree, printing current node value, then right subtree. Use a helper method _inorder_recursive that takes a node. Then create a BST instance, insert values 50, 30, 70, 20, 40, 60, 80, and call inorder to print the sorted numbers separated by spaces.
DSA Python
Hint
Inorder traversal visits left child, then current node, then right child. This prints numbers sorted.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Binary Search Tree (BST)?
easy
A. To balance the BST automatically
B. To add a new value while keeping the BST property intact
C. To find the maximum value in the BST
D. To delete a node from the BST
Solution
Step 1: Understand the BST property
A BST keeps values in order: left child smaller, right child larger.
Step 2: Purpose of insert operation
Insert adds a new value so the tree remains ordered by BST rules.
Final Answer:
To add a new value while keeping the BST property intact -> Option B
Quick Check:
Insert = Add value maintaining order [OK]
Hint: Insert means add value keeping order [OK]
Common Mistakes:
Confusing insert with delete
Thinking insert balances the tree automatically
Believing insert finds max value
2. Which of the following is the correct Python function signature for inserting a value into a BST node?
easy
A. def insert(root, value):
B. def insert(value, root):
C. def insert(value):
D. def insert(root):
Solution
Step 1: Identify parameters needed for insertion
We need the root node to start and the value to insert.
Step 2: Check function signature correctness
Signature with root first, then value is common and clear: def insert(root, value):.
Final Answer:
def insert(root, value): -> Option A
Quick Check:
Insert(root, value) signature [OK]
Hint: Insert needs root node and value [OK]
Common Mistakes:
Swapping parameter order
Missing root parameter
Using only one parameter
3. What will be the output of the BST after inserting 7 into the BST shown below?