In: Computer Science
IN C++
Given a struct Node { int value; Node *left, *right;}; , implement the functions below.
a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null.
b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child.
c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child.
For part c, if r->value happens to be the second smallest value, then overwrite r->value with some other value in the tree and delete the node containing that value. Don't ever delete r.
Be mindful of efficiency.
code for just the functions. Don't use helper functions.
I have attached the full code of binary search tree and also your fucntions a ,b,and c separately
below code is simple and efficient .
//a
int getSmallest(Node * r)
{
Node *temp=r;
while(temp->left!=NULL)
{
temp=temp->left;
}
return temp->value;
}
//b
void getSecondSmallest(Node * r,int &count)
{
if (r == NULL || count >= 2)
return;
getSecondSmallest(r->left, count);
count++;
if (count == 2)
{
secondsmall=r->value;
//cout << "2nd smallest element is "
// << r->value<< endl;
return;
}
// Recur for left subtree
getSecondSmallest(r->right, count);
}
//c
Node* removeSecondSmallest(Node * r)
{
// get the second smallest value by callig getSecondSmallest fucntion ()
// secondsmall stores the second smallest value
//if(secondsmall=-1)
// return root;
int key=secondsmall;
Node* curr = r;
Node* prev = NULL;
while (curr != NULL && curr->value != key) {
prev = curr;
if (key < curr->value)
curr = curr->left;
else
curr = curr->right;
}
if (curr == NULL) {
cout << "Key " << key
<< " not found in the"
<< " provided BST.\n";
return r;
}
if (curr->left == NULL
|| curr->right == NULL) {
Node* newCurr;
if (curr->left == NULL)
newCurr = curr->right;
else
newCurr = curr->left;
if (prev == NULL)
return newCurr;
if (curr == prev->left)
prev->left = newCurr;
else
prev->right = newCurr;
free(curr);
}
else {
Node* p = NULL;
Node* temp;
temp = curr->right;
while (temp->left != NULL) {
p = temp;
temp = temp->left;
}
if (p != NULL)
p->left = temp->right;
else
curr->right = temp->right;
curr->value = temp->value;
free(temp);
}
return r;
}
#include<bits/stdc++.h>
using namespace std;
struct Node {
int value;
Node *left, *right;
};
int secondsmall;
Node* newNode(int key)
{
Node* node = new Node;
node->value = key;
node->left = node->right = nullptr;
return node;
}
Node* insert(Node* root, int key)
{
if (root == nullptr)
return newNode(key);
if (key < root->value)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
}
void inorder(Node* root)
{
if (root == nullptr)
return;
inorder(root->left);
cout << root->value << " ";
inorder(root->right);
}
int getSmallest(Node * r)
{
Node *temp=r;
while(temp->left!=NULL)
{
temp=temp->left;
}
return temp->value;
}
void getSecondSmallest(Node * r,int &count)
{
if (r == NULL || count >= 2)
return;
getSecondSmallest(r->left, count);
count++;
if (count == 2)
{
secondsmall=r->value;
//cout << "2nd smallest element is "
// << r->value<< endl;
return;
}
// Recur for left subtree
getSecondSmallest(r->right, count);
}
Node* removeSecondSmallest(Node * r)
{
// get the second smallest value by callig getSecondSmallest fucntion ()
// secondsmall stores the second smallest value
//if(secondsmall=-1)
// return root;
int key=secondsmall;
Node* curr = r;
Node* prev = NULL;
while (curr != NULL && curr->value != key) {
prev = curr;
if (key < curr->value)
curr = curr->left;
else
curr = curr->right;
}
if (curr == NULL) {
cout << "Key " << key
<< " not found in the"
<< " provided BST.\n";
return r;
}
if (curr->left == NULL
|| curr->right == NULL) {
Node* newCurr;
if (curr->left == NULL)
newCurr = curr->right;
else
newCurr = curr->left;
if (prev == NULL)
return newCurr;
if (curr == prev->left)
prev->left = newCurr;
else
prev->right = newCurr;
free(curr);
}
else {
Node* p = NULL;
Node* temp;
temp = curr->right;
while (temp->left != NULL) {
p = temp;
temp = temp->left;
}
if (p != NULL)
p->left = temp->right;
else
curr->right = temp->right;
curr->value = temp->value;
free(temp);
}
return r;
}
int main()
{
Node* root = nullptr;
int keys[] = { 15, 10, 20, 18, 12, 16, 5 };
for (int key : keys)
root = insert(root, key);
cout<<" smallest element "<<getSmallest(root)<<endl;
secondsmall=-1;
int count=0;
getSecondSmallest(root,count);
cout<<" second smallest element "<<secondsmall<<endl;
cout<<endl;
cout<<" ------------------- Inorder Traversal ------------- "<<endl;
inorder(root);
root=removeSecondSmallest(root);
cout<<endl;
cout<<" ------------------- After removing second smallest element --- Inorder Traversal ------------- "<<endl;
inorder(root);
return 0;
}