DATA STRUCTURES Write code to find Maximum and Minimum value in the binary searc
ID: 3761642 • Letter: D
Question
DATA STRUCTURES
Write code to find Maximum and Minimum value in the binary search tree. The code should work with the written code below.
#include
#include
using namespace std;
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
cout<data< print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
cout<data< print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
cout<data< }
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
int main()
{
cout< node *root;
// node *tmp;
int nodevalue,i=0;
root = NULL;
/* Inserting nodes into tree */
do{
cout<<"Enter the value of the node"< cin>>nodevalue;
insert(&root, nodevalue);
cout<<"Would you like to insert another node, 1 for yes"< cin>>i;
}while(i==1);
/* Printing nodes of tree */
cout<<"Pre Order Display"< print_preorder(root);
cout<<"In Order Display"< print_inorder(root);
cout<<"Post Order Display"< print_postorder(root);
deltree(root);
return 0;
}
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
cout<<tree->data<<" ";
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_preorder(tree->left);
cout<<tree->data<<" ";
print_preorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
cout<<tree->data<<" ";
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
int max(node *root)
{
int max=-100;
while(root)
{
max = root->data;
root=root->right;
}
return max;
}
int min(node *root)
{
int min=-100;
while(root)
{
min = root->data;
root=root->left;
}
return min;
}
int main()
{
node *root;
// node *tmp;
int nodevalue,i=0;
root = NULL;
/* Inserting nodes into tree */
do{
cout<<"Enter the value of the node "< cin>>nodevalue;
insert(&root, nodevalue);
cout<<"Would you like to insert another node, 1 for yes "< cin>>i;
}while(i==1);
/* Printing nodes of tree */
cout<<"Pre Order Display ";
print_preorder(root);
cout<<' ';
cout<<"In Order Display ";
print_inorder(root);
cout<<' ';
cout<<"Post Order Display ";
print_postorder(root);
cout<<' ';
cout<<"Maximum value "<<max(root);
cout<<' ';
cout<<"Minimum value "<<min(root);
cout<<' ';
deltree(root);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.