Dsa Lab 12 064
Dsa Lab 12 064
ALGORITHMS
LAB NO 12
TASK 1
INPUT
#include<iostream>
using namespace std;
class node{
public:
int data;
node* left = NULL;
node* right = NULL;
};
class bt
{ public:
// Insert data.
if(value > root->data)
{
root->right = Insert(root->right, value);
}
else
{
root->left = Insert(root->left, value);
}
return root;
}
preorder(root->right);
}
postorder(root->right);
cout << root->data<<" ";
}
};
// Driver code
int main()
{
node *root = NULL;
bt b;
int num,value;
cout<<"\n Enter the number of nodes: ";
cin>>num;
cout<<"Enter the value of nodes : ";
cin>>value;
root = b.Insert(root,value);
for(int i =0;i<num-1;i++){
cout<<"\n Enter the value of nodes : ";
cin>>value;
b.Insert(root,value);
}
cout<<"\n Display by the preorder : ";
b.preorder(root);
cout<<"\n Display by the inorder : ";
b.Inorder(root);
cout<<endl;
cout<<"\n Displaying by the post order: ";
b.postorder(root);
return 0;
}
OUTPUT
TASK 2
INPUT
#include<iostream>
using namespace std;
class node{
public:
int data;
node* left = NULL;
node* right = NULL;
};
class bt
{ public:
// Insert data.
if(value > root->data)
{
root->right = Insert(root->right, value);
}
else
{
root->left = Insert(root->left, value);
}
return root;
}
preorder(root->right);
}
return current;
}
node* deleteNode(node* root, int key)
{
// base case
if (root == NULL)
return root;
else {
// node has no child
if (root->left == NULL and root->right == NULL)
{
delete root;
return NULL;
}
};
int main()
{
node *root = NULL;
bt b;
int num,value;
cout<<"\n Enter the number of nodes: ";
cin>>num;
cout<<"Enter the value of nodes : ";
cin>>value;
root = b.Insert(root,value);
for(int i =0;i<num-1;i++){
cout<<"\n Enter the value of nodes : ";
cin>>value;
b.Insert(root,value);
}
cout<<"\n Display by the preorder : ";
b.preorder(root);
cout<<"\n Display by the inorder : ";
b.Inorder(root);
cout<<endl;
cout<<"\n Displaying by the post order: ";
b.postorder(root);
cout<<"\n Enter number you want to delete : ";
int del;
cin>>del;
b.deleteNode(root,del);
cout<<"\n Displaying data after deletion : ";
b.preorder(root);
return 0;
}
OUTPUT