📖 Scenario: You are working with a Binary Search Tree (BST) that stores integer values. You want to find the largest value stored in this tree. This is useful in many real-world cases, such as finding the highest score in a game leaderboard or the maximum price in a product catalog.
🎯 Goal: Build a simple BST with given nodes, then write code to find and print the maximum element in the BST.
📋 What You'll Learn
Create a BST node structure with integer data and left/right pointers
Manually create a BST with these exact nodes: 15 (root), 10 (left child), 20 (right child), 8 (left child of 10), 12 (right child of 10), 17 (left child of 20), 25 (right child of 20)
Write a function to find the maximum element in the BST by traversing to the rightmost node
Print the maximum element found
💡 Why This Matters
🌍 Real World
Finding maximum values in sorted data structures like BSTs is common in databases, search engines, and real-time analytics to quickly get highest scores, prices, or priorities.
💼 Career
Understanding BSTs and how to traverse them is fundamental for software engineers working with data storage, retrieval, and optimization algorithms.
Progress0 / 4 steps
1
Create BST Node Structure and Build Tree
Create a struct called Node with an int data field and two pointers left and right to Node. Then create nodes with these exact values and connections: root node with data = 15, left child 10, right child 20, left child of 10 is 8, right child of 10 is 12, left child of 20 is 17, right child of 20 is 25.
DSA C
Hint
Start by defining the Node struct with data, left, and right. Then create each node as a variable and link them by setting the left and right pointers.
2
Create a Pointer to the Root Node
Create a pointer variable called root and set it to point to the node with data = 15.
DSA C
Hint
Use Node* root = &n15; to create a pointer to the root node.
3
Write Function to Find Maximum Element in BST
Write a function called findMax that takes a pointer to Node called root and returns the maximum integer value in the BST. Traverse to the rightmost node and return its data.
DSA C
Hint
Use a while loop to move to the right child until it is NULL. Then return the data of that node.
4
Print the Maximum Element
Call the findMax function with root and print the returned maximum value using printf.
DSA C
Hint
Call findMax(root) and store the result in an int variable. Then use printf("%d\n", variable); to print the maximum value.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the maximum element always located?
easy
A. At any leaf node
B. At the leftmost node
C. At the rightmost node
D. At the root node
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values and right child nodes have larger values than their parent.
Step 2: Locate maximum element
The maximum element is the rightmost node because it has no right child and is greater than all other nodes.
Final Answer:
At the rightmost node -> Option C
Quick Check:
Maximum = rightmost node [OK]
Hint: Maximum is always the rightmost node in BST [OK]
Common Mistakes:
Confusing maximum with root node
Thinking maximum is at leftmost node
Assuming maximum can be any leaf
2. Which of the following C code snippets correctly finds the maximum element in a BST?
easy
A. while(root->left != NULL) root = root->left; return root->data;
B. while(root->right != NULL) root = root->right; return root->data;
C. if(root == NULL) return INT_MIN; return root->data;
D. return root->data;
Solution
Step 1: Analyze code logic
while(root->right != NULL) root = root->right; return root->data; moves right until no right child exists, which is correct for max in BST.
Step 2: Check other options
while(root->left != NULL) root = root->left; return root->data; moves left (minimum), C returns root data without traversal, D returns root data directly.
A. It returns -1 for empty tree instead of INT_MIN
B. It should use recursion instead of loop
C. It does not check if root is NULL before accessing data
D. It traverses left instead of right to find maximum
Solution
Step 1: Analyze traversal direction
The function moves left to find max, but max is at rightmost node in BST.
Step 2: Check other issues
Returning -1 is acceptable for empty tree; checking root NULL is done; recursion is optional.
Final Answer:
It traverses left instead of right to find maximum -> Option D
Quick Check:
Max is rightmost, not leftmost [OK]
Hint: Max element is at rightmost, not leftmost node [OK]
Common Mistakes:
Confusing left and right child traversal
Ignoring NULL checks
Thinking recursion is mandatory
5. Given a BST where some nodes have duplicate values, which approach correctly finds the maximum element considering duplicates are allowed on the right subtree?