Homework-4: Write a C program that demonstrate use of Binary Search Tree, as fol
ID: 3728636 • Letter: H
Question
Homework-4: Write a C program that demonstrate use of Binary Search Tree, as follow.Create two function 1) findLargestBST and 2) findSmallestBST and include in above program. Function “findLargestBST” should have code that find the largest integer number from the BST and function “findSmallestBST” should have code that find smallest integer number from the BST. Please have a logic explanation of the code.
Homework-4: Write a C program that demonstrate use of Binary Search Tree, as follow.
Create two function 1) findLargestBST and 2) findSmallestBST and include in above program. Function “findLargestBST” should have code that find the largest integer number from the BST and function “findSmallestBST” should have code that find smallest integer number from the BST. Please have a logic explanation of the code.
Create two function 1) findLargestBST and 2) findSmallestBST and include in above program. Function “findLargestBST” should have code that find the largest integer number from the BST and function “findSmallestBST” should have code that find smallest integer number from the BST. Please have a logic explanation of the code.
Explanation / Answer
struct Node
{
int key;
Node *left, *right;
};
// A function to find largest element in a given tree.
int findLargestBST(Node *root)
{
// Base cases
if (root == NULL ||)
return -9999;
//right most element is the largest element
while(root->right != NULL)
root = root->right;
return root->key;
}
// A function to find smalest element in a given tree.
int findSmallestBST(Node *root)
{
// Base cases
if (root == NULL ||)
return -9999;
//left most element is the smalest element
while(root->left != NULL)
root = root->left;
return root->key;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.