BST Property and Why It Matters in DSA C - Time & Space Complexity
We want to understand how the special rule of a Binary Search Tree (BST) affects how fast we can find or add items.
The question is: How does this rule help us do tasks faster as the tree grows?
Analyze the time complexity of the following code snippet.
// Check if a value exists in a BST
int searchBST(Node* root, int val) {
if (root == NULL) return 0;
if (root->data == val) return 1;
if (val < root->data) return searchBST(root->left, val);
else return searchBST(root->right, val);
}
This code looks for a value in a BST by following the BST rule to decide which side to search next.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls moving down one level of the tree.
- How many times: At most once per tree level until the value is found or tree ends.
Each step moves down one level, so the number of steps grows with the tree's height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps (tree height) |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Steps grow slowly as tree size grows, roughly like the height of a balanced tree.
Time Complexity: O(h) where h is tree height
This means the time depends on how tall the tree is, not just how many items it has.
[X] Wrong: "Searching a BST always takes the same time as searching a sorted list."
[OK] Correct: A BST search depends on tree height, which can be much less than list length if the tree is balanced.
Understanding how the BST rule affects search speed shows you know why data structures matter for fast programs.
"What if the BST becomes a linked list (all nodes only have one child)? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST definition
A BST is defined by the order property where left subtree nodes are smaller and right subtree nodes are larger than the current node.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. Other options describe different tree types or properties.Final Answer:
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option DQuick Check:
BST property = left < node < right [OK]
- Confusing BST with complete binary tree
- Thinking all nodes must have two children
- Assuming leaf nodes must be at same depth
Solution
Step 1: Understand C struct pointer access
In C, to access a member of a struct pointer, use '->'. So node->left accesses left child pointer.Step 2: Check each option for correct pointer syntax
if (node->left->value < node->value) {} uses node->left->value correctly. Others mix '.' and '->' incorrectly.Final Answer:
if (node->left->value < node->value) {} -> Option AQuick Check:
Use '->' for pointer members in C [OK]
- Using '.' instead of '->' for pointers
- Mixing '.' and '->' incorrectly
- Forgetting to check if left child is NULL before access
Solution
Step 1: Insert values into BST following property
Insert 10 as root, 5 left of 10, 15 right of 10, 3 left of 5, 7 right of 5.Step 2: Perform inorder traversal (left, node, right)
Inorder visits nodes in ascending order: 3, 5, 7, 10, 15.Final Answer:
[3, 5, 7, 10, 15] -> Option CQuick Check:
Inorder traversal of BST = sorted values [OK]
- Confusing preorder or postorder with inorder
- Inserting nodes incorrectly violating BST property
- Reading traversal output in insertion order
void insert(Node** root, int val) {
if (*root == NULL) {
*root = malloc(sizeof(Node));
(*root)->value = val;
(*root)->left = NULL;
(*root)->right = NULL;
} else if (val < (*root)->value) {
insert(&(*root)->left, val);
} else {
insert(&(*root)->right, val);
}
}Solution
Step 1: Analyze insertion logic for duplicates
The code inserts values less than current node to left, else to right, so duplicates go right without check.Step 2: Identify missing duplicate check
BST usually avoids duplicates or handles them explicitly; this code lacks that check.Final Answer:
Missing check for duplicate values before insertion. -> Option AQuick Check:
Duplicates need explicit handling in BST insert [OK]
- Assuming duplicates are allowed without check
- Confusing pointer usage in recursive calls
- Incorrect malloc size for struct allocation
Solution
Step 1: Understand BST property importance in search
BST search relies on left < node < right property to decide which subtree to explore.Step 2: Analyze effect of violation on search
If right child is smaller, search for larger values may wrongly go left or miss nodes, causing failure or wrong results.Final Answer:
Search may fail or return incorrect results because the BST property is broken. -> Option BQuick Check:
BST property violation breaks search correctness [OK]
- Assuming one violation doesn't affect search
- Thinking search speed improves with violation
- Believing search always returns parent node
Ready to prove it?
Pass the test and this pattern moves unknown
