Professional Documents
Culture Documents
20MDBCS065
Lab task:
Code:
#include<iostream>
#include<conio.h>
// function that returns a pointer to the node containing the value to be found
tree_node* find( int x, tree_node* T ) {
if( T == NULL )
return NULL;
if( x < T->element )
return( find( x, T->left ) );
else{
// function that returns pointer to the minimum value in the tree. it is recursive
tree_node* find_min(tree_node* T ) {
if( T == NULL )
return NULL;
else{
//functin that reutrns pointer to the max value in the tree. it is non recrusive
tree_node* find_max(tree_node* T ) {
if( T != NULL )
while( T->right != NULL )
T = T->right;
return T;
}
else
if(T->left && T->right){
temp=find_min(T->right);
int xmin=temp->element;
T->element=xmin;
T->right= Delete(xmin, T->right);
}
else{
temp=T;
if( T->left==NULL)
child=T->right;
if(T->right==NULL)
child=T->left;
delete(temp);
return child;
}
return T;
}
inorder(T->left);
cout<<"\t"<<T->element;
inorder(T->right);
}
//preorder traversal
void preorder(tree_node *T){
if(T== NULL)
return;
cout<<"\t"<<T->element;
preorder(T->left);
preorder(T->right);
}
//postorder traversal
void postorder(tree_node *T){
if(T== NULL)
return;
postorder(T->left);
postorder(T->right);
cout<<"\t"<<T->element;
}
int main(){
tree_node* root = NULL;
root = insert(15,root);
root = insert(17,root);
root = insert(35,root);
root = insert(1,root);
root = insert(14,root);
cout<<endl<<endl;
cout<<"In-order traversal :"<<endl;
inorder(root);
cout<<endl<<endl;
cout<<"Pre-order traversal :"<<endl;
preorder(root);
cout<<endl<<endl;
cout<<"Post-order traversal :"<<endl;
postorder(root);
cout<<endl<<endl;
cout<<"Deleting :"<<5<<endl;
Delete(5,root);
cout<<endl<<endl;
cout<<"Pre-order traversal :"<<endl;
preorder(root);
return 0;
}