📖 Scenario: Imagine you have a collection of books and you want to organize them so you can find any book quickly. You can put them in any order (like a plain binary tree), or you can arrange them in a special order (like a Binary Search Tree) to find books faster.
🎯 Goal: Learn how to create a simple binary tree and a binary search tree (BST) with the same values, then see how searching is faster in BST because of its order.
📋 What You'll Learn
Create a plain binary tree with 5 nodes having values 10, 5, 15, 3, 7
Create a binary search tree (BST) with the same 5 values
Write a function to search for a value in both trees
Print the result of searching for value 7 in both trees
💡 Why This Matters
🌍 Real World
Organizing data so it can be found quickly is important in many apps like phone contacts, file systems, and databases.
💼 Career
Understanding BSTs helps in software development roles that involve data storage, search optimization, and algorithm design.
Progress0 / 4 steps
1
Create a plain binary tree with 5 nodes
Create a struct called Node with an int data and two pointers left and right. Then create 5 nodes with values 10, 5, 15, 3, and 7. Connect them to form a plain binary tree like this: root 10, left child 5, right child 15, left child of 5 is 3, right child of 5 is 7.
DSA C
Hint
Think of each node as a box holding a number and two arrows pointing to other boxes or nothing.
2
Create a binary search tree (BST) with the same values
Create a function insertBST that takes a Node* and an int value and inserts the value into the BST following BST rules (left child smaller, right child bigger). Use this function to insert values 10, 5, 15, 3, and 7 into a new BST root.
DSA C
Hint
Use recursion to find the right place to insert each value in the BST.
3
Write a function to search for a value in both trees
Write a function searchTree that takes a Node* and an int value and searches for the value in the plain binary tree using simple recursion (check current node, then left, then right). Also write a function searchBST that searches in the BST using BST rules (go left if value smaller, right if bigger).
DSA C
Hint
For plain tree, check all nodes. For BST, use the order to skip parts.
4
Print the result of searching for value 7 in both trees
Use printf to print "Found in plain tree" if searchTree(root, 7) returns 1, else "Not found in plain tree". Then print "Found in BST" if searchBST(bstRoot, 7) returns 1, else "Not found in BST".
DSA C
Hint
Use printf to show if the value 7 is found in each tree.
Practice
(1/5)
1. Why is a Binary Search Tree (BST) preferred over a plain Binary Tree for searching operations?
easy
A. Because BST does not require pointers to child nodes
B. Because BST uses less memory than a plain Binary Tree
C. Because BST nodes can have more than two children
D. Because BST keeps elements in a sorted order allowing faster search
Solution
Step 1: Understand the structure of BST
A BST stores elements so that left child nodes are smaller and right child nodes are larger than the parent node, maintaining sorted order.
Step 2: Compare search efficiency
This sorted structure allows searching by comparing values and moving left or right, reducing search time to O(log n) on average, unlike a plain Binary Tree which has no order and requires O(n) time.
Final Answer:
Because BST keeps elements in a sorted order allowing faster search -> Option D
Quick Check:
BST sorted order = faster search [OK]
Hint: BST is sorted, plain tree is not, so search is faster [OK]
Common Mistakes:
Thinking BST uses less memory than plain tree
Confusing number of children in BST
Assuming BST nodes don't have child pointers
2. Which of the following is the correct way to declare a node structure for a BST in C?
easy
A. typedef struct Node { int data; struct Node* left; struct Node* right; } Node;
B. typedef struct Node { int data; struct Node left; struct Node right; } Node;
C. typedef struct Node { int data; Node* left; Node* right; } Node;
D. typedef struct Node { int data; int* left; int* right; } Node;
Solution
Step 1: Understand C struct pointer usage
In C, to refer to the same struct type inside itself, pointers must be used with 'struct Node*'.
Step 2: Check each option
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; correctly uses 'struct Node*' for left and right pointers. typedef struct Node { int data; struct Node left; struct Node right; } Node; incorrectly uses struct Node (not pointer), causing infinite size. typedef struct Node { int data; Node* left; Node* right; } Node; uses 'Node*' without 'struct' keyword which is invalid in C without typedef. typedef struct Node { int data; int* left; int* right; } Node; uses int pointers which is incorrect for child nodes.
Final Answer:
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; -> Option A
Quick Check:
Use 'struct Node*' for child pointers in C [OK]
Hint: Use 'struct Node*' for child pointers in C structs [OK]
Common Mistakes:
Using struct Node instead of pointer causing infinite size
Omitting 'struct' keyword in C
Using int* instead of struct pointers
3. What will be the output of the following C code that inserts values into a BST and then performs an inorder traversal?
A. Not checking if root is NULL before accessing root->data
B. Using 'newNode' instead of 'newNode*' for pointer
C. Returning address of local variable 'newNode' which goes out of scope
D. Missing malloc to allocate memory for new node
Solution
Step 1: Analyze node creation in insert function
The code creates 'Node newNode;' as a local variable on stack inside the function.
Step 2: Understand scope and pointer return
Returning '&newNode' returns address of local variable which is destroyed after function returns, causing invalid memory access and segmentation fault.
Final Answer:
Returning address of local variable 'newNode' which goes out of scope -> Option C
Quick Check:
Local variable address returned = invalid pointer [OK]
Hint: Never return address of local variable; use malloc [OK]
Common Mistakes:
Ignoring local variable lifetime
Not using malloc for dynamic allocation
Confusing pointer and value types
5. You have a plain Binary Tree and a BST both containing the same 7 unique integer values. Which of the following statements about searching for a value is TRUE?
hard
A. Searching in BST is always faster because it is balanced
B. Searching in BST can be faster on average due to ordered structure, but worst-case can be as slow as plain Binary Tree
C. Searching in plain Binary Tree is faster because it has no order constraints
D. Searching speed is the same for both because they have same number of nodes
Solution
Step 1: Understand BST search complexity
BST search is O(log n) on average if balanced, but can degrade to O(n) if skewed.
Step 2: Compare with plain Binary Tree search
Plain Binary Tree has no order, so search is always O(n) as it may need to check all nodes.
Step 3: Analyze given statements
Searching in BST is always faster because it is balanced is false because BST is not always balanced. Searching in BST can be faster on average due to ordered structure, but worst-case can be as slow as plain Binary Tree correctly states average case advantage and worst case equality. Searching in plain Binary Tree is faster because it has no order constraints is false; plain tree is slower. Searching speed is the same for both because they have same number of nodes is false; structure affects search speed.
Final Answer:
Searching in BST can be faster on average due to ordered structure, but worst-case can be as slow as plain Binary Tree -> Option B
Quick Check:
BST average faster, worst-case equal [OK]
Hint: BST average faster, worst-case can be slow [OK]
Common Mistakes:
Assuming BST is always balanced
Thinking plain tree search is faster
Ignoring worst-case BST behavior
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.