In a BST, for every node, all values in its left subtree are smaller, and all values in its right subtree are larger than the node's value.
Click to reveal answer
beginner
Why does the BST property matter for searching?
Because the BST property keeps values ordered, it allows searching for a value by comparing and moving left or right, making search faster than checking every node.
Click to reveal answer
beginner
How does the BST property help with insertion?
Insertion uses the BST property to find the correct spot by comparing values and moving left or right until an empty spot is found, keeping the tree ordered.
Click to reveal answer
intermediate
What happens if the BST property is not maintained?
If the BST property breaks, searching and insertion become inefficient because the tree loses its order, possibly becoming like a linked list with slow operations.
Click to reveal answer
beginner
Explain the BST property using a real-life example.
Think of a phone book sorted by names: all names before 'John' come before it alphabetically (left), and all after come after (right). This order helps find a name quickly, like the BST property does for numbers.
Click to reveal answer
What must be true about the left subtree of a BST node?
AAll values are smaller than the node's value
BAll values are larger than the node's value
CValues can be any number
DValues are equal to the node's value
✗ Incorrect
By definition, the left subtree contains only values smaller than the node's value.
Why is the BST property important for efficient searching?
AIt stores values randomly
BIt makes the tree balanced automatically
CIt allows skipping half of the tree at each step
DIt stores duplicate values
✗ Incorrect
The BST property lets us decide to go left or right, skipping unnecessary parts and speeding up search.
If a BST property is broken, what is a likely consequence?
AThe tree becomes balanced
BSearch operations become slower
CInsertion becomes faster
DThe tree deletes nodes automatically
✗ Incorrect
Without the BST property, the tree loses order, making search slower, often like a linked list.
In a BST, where do values larger than a node go?
AIn the right subtree
BIn the left subtree
CAt the root
DAnywhere in the tree
✗ Incorrect
Values larger than the node must be placed in the right subtree to maintain the BST property.
Which real-life example best illustrates the BST property?
AA queue of people
BA random pile of books
CA stack of plates
DA sorted phone book
✗ Incorrect
A sorted phone book orders names so you can quickly find one, similar to how BST orders values.
Describe the BST property and explain why it is important for searching and insertion.
Think about how ordering helps find or add values quickly.
You got /4 concepts.
What problems arise if the BST property is not maintained? Give an example.
Consider what happens if values are not in the right place.
You got /4 concepts.
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. For each node, all values in the left subtree are smaller, and all values in the right subtree are larger.
B. Each node has exactly two children.
C. All nodes have the same value.
D. Nodes are arranged in a complete binary tree structure.
Solution
Step 1: Understand BST definition
A BST requires that for every node, values in the left subtree are smaller and values in the right subtree are larger.
Step 2: Compare options with BST property
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 tree types or incorrect properties.
Final Answer:
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option A
Quick Check:
BST property = For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. [OK]
Hint: Remember: left < node < right in BST [OK]
Common Mistakes:
Thinking BST means every node has two children
Confusing BST with balanced or complete trees
Assuming all nodes have the same value
2. Which of the following is the correct C++ syntax to declare a node structure for a BST?
easy
A. class Node { int data; Node left; Node right; };
B. struct Node { int data; Node left; Node right; };
C. struct Node { int data; Node* left; Node* right; };
D. class Node { int data; Node* left; Node* right; };
Solution
Step 1: Check pointer usage in node declaration
BST nodes must use pointers for left and right children to allow dynamic linking.
Step 2: Compare struct and class syntax
struct Node { int data; Node* left; Node* right; }; correctly uses struct with pointers. class Node { int data; Node* left; Node* right; }; uses class but misses access specifiers (defaults private). Declarations using objects instead of pointers for children are incorrect.
Final Answer:
struct Node { int data; Node* left; Node* right; }; -> Option C
Quick Check:
Use pointers for children in BST node [OK]
Hint: Use pointers for child nodes in BST struct [OK]
Common Mistakes:
Using objects instead of pointers for children
Forgetting semicolon after struct
Using class without public access for members
3. Consider the following BST insertion code snippet in C++:
Traverse left subtree: 5, root: 10, left of right subtree: 12, root of right subtree: 15, right of right subtree: 18. So output is [5, 10, 12, 15, 18].
Final Answer:
[5, 10, 12, 15, 18] -> Option D
Quick Check:
In-order traversal of BST = sorted values [OK]
Hint: In-order traversal of BST gives sorted values [OK]
Common Mistakes:
Confusing pre-order or post-order with in-order
Ignoring BST insertion rules
Mixing up left and right subtree traversal
4. The following code attempts to insert a value into a BST but has a bug:
Node* insert(Node* root, int val) {
if (root == nullptr) {
Node* newNode = new Node{val, nullptr, nullptr};
return newNode;
}
if (val <= root->data) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
}
What is the main issue with this code regarding BST property?
medium
A. It does not allocate memory for new nodes.
B. Using <= allows duplicate values on the left, which may violate strict BST property.
C. It uses recursion incorrectly causing infinite loops.
D. It does not handle the case when root is null.
Solution
Step 1: Analyze the condition for left subtree insertion
The code uses val <= root->data to decide left subtree insertion, allowing duplicates on the left.
Step 2: Understand BST strict property
Strict BST property requires all left subtree values to be strictly less than the node's value. Allowing duplicates on left can break this.
Final Answer:
Using <= allows duplicate values on the left, which may violate strict BST property. -> Option B
Quick Check:
Strict BST: left < node < right [OK]
Hint: Use < for left subtree to avoid duplicates breaking BST [OK]
Common Mistakes:
Ignoring duplicate handling in BST
Thinking recursion causes infinite loops here
Assuming null root case is missing
5. You have a BST where each node stores an integer. You want to find the kth smallest element efficiently. Which approach best uses the BST property to solve this?
hard
A. Perform an in-order traversal and stop when the kth element is reached.
B. Perform a level-order traversal and count nodes until kth is found.
C. Traverse only the right subtree k times to find the kth smallest.
D. Use pre-order traversal and store all nodes in a list, then sort it.
Solution
Step 1: Recall BST in-order traversal property
In-order traversal visits nodes in ascending order for BST.
Step 2: Use in-order traversal to find kth smallest
By traversing in-order and counting nodes, we can stop at kth node, efficiently finding kth smallest.
Final Answer:
Perform an in-order traversal and stop when the kth element is reached. -> Option A
Quick Check:
In-order traversal = sorted order [OK]
Hint: In-order traversal visits nodes in ascending order [OK]
Common Mistakes:
Using level-order which is not sorted
Traversing only right subtree misses smaller values
Sorting after pre-order wastes time
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.