/* These are c++ code on Trees All codes are commented to view the output of code uncomment it and comment all other codes to see it's output. */
#include<bits/stdc++.h>
using namespace std;
//code 1
/*int main()
{
int n,i;
cin>>n;
int a[n+1];
for(i=0;i<n;i++)
{
cin>>a[i];
}
bool flag=true;
for(i=0;i<(n/2)-1;i++)
{
if(a[i]<a[(2*i)+1] || a[i]<a[2*i+2])
{
flag=false;
break;
}
}
if(flag==true)
{
cout<<"it is max heap"<<endl;
}
else
{
cout<<"not a max heap"<<endl;
}
return 0;
}*/
//code 2
/*
int main()
{
priority_queue<int,vector<int>, greater<int> > pq;
pq.push(10);
pq.push(100);
pq.push(1);
pq.push(1000);
pq.push(10000);
pq.push(11);
priority_queue<int, vector<int>, greater<int> > nq=pq;
while(!nq.empty())
{
cout<<nq.top()<<endl;
nq.pop();
}
return 0;
}
*/
//code 3
/* A binary tree node has key, pointer to left child
and a pointer to right child */
/*
struct node {
int data;
node* left, *right;
};
node* newNode(int data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
};
void inorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
void insert(node* root, int data)
{
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
root->left = newNode(data);
break;
} else
q.push(root->left);
if (!root->right) {
root->right = newNode(data);
break;
} else
q.push(root->right);
}
}
int main()
{
node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right=newNode(50);
inorder(root);
cout << endl;
int data = 60;
insert(root, data);
inorder(root);
return 0;
}
*/
//code 4
/*struct node {
int data;
node* left, *right;
};
node* newNode(int data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
};
void inorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// preorder
void preorder(node* root)
{
if (root==NULL)
return;
cout << root->data << " ";
inorder(root->left);
inorder(root->right);
}
//postoder
void postorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
inorder(root->right);
cout << root->data << " ";
}
//levelorder
void levelorder(node *root)
{
queue<node *>q;
q.push(root);
while(!q.empty())
{
node *da=q.front();
q.pop();
cout<<da->data<<" ";
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
}
void insert(node* root, int data)
{
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
root->left = newNode(data);
break;
} else
q.push(root->left);
if (!root->right) {
root->right = newNode(data);
break;
} else
q.push(root->right);
}
}
int main()
{
node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right=newNode(50);
inorder(root);
cout << endl;
int data = 60;
insert(root, data);
cout<<endl<<"INORDER"<<endl;
inorder(root);
cout<<endl<<"PREORDER"<<endl;
preorder(root);
cout<<endl<<"POSTORDER"<<endl;
postorder(root);
cout<<endl<<"LEVELORDER"<<endl;
levelorder(root);
return 0;
}
*/
// code 6
struct node {
int data;
node* left, *right;
};
node* newNode(int data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
};
void inorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// preorder
void preorder(node* root)
{
if (root==NULL)
return;
cout << root->data << " ";
inorder(root->left);
inorder(root->right);
}
//postoder
void postorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
inorder(root->right);
cout << root->data << " ";
}
//levelorder
void levelorder(node *root)
{
queue<node *>q;
q.push(root);
while(!q.empty())
{
node *da=q.front();
q.pop();
cout<<da->data<<" ";
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
}
// delete from tree
void deltree(node *root,int number)
{
queue<node *>q;
q.push(root);
// finding last element
node *da;
while(!q.empty())
{
da=q.front();
q.pop();
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
int element=da->data;
// replaced
q.push(root);
while(!q.empty())
{
da=q.front();
q.pop();
if(da->data==number)
{
da->data=element;
break;
}
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
cout<<endl<<"TRAVESAL OF ELEMENT"<<endl;
queue<node *>qe;
qe.push(root);
while(!qe.empty())
{
node *da=qe.front();
qe.pop();
if(da->data!=element && da->left!=NULL && da->right!=NULL)
{
cout<<da->data<<" ";
}
if(da->left!=NULL)
{
qe.push(da->left);
}
if(da->right!=NULL)
{
qe.push(da->right);
}
}
}
int height(node *root)
{
if(root==NULL)
{
return 0;
}
return max(height(root->left),height(root->right))+1;
}
void insert(node* root, int data)
{
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
root->left = newNode(data);
break;
} else
q.push(root->left);
if (!root->right) {
root->right = newNode(data);
break;
} else
q.push(root->right);
}
}
int main()
{
node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right=newNode(50);
inorder(root);
cout << endl;
int data = 60;
insert(root, data);
cout<<endl<<"INORDER"<<endl;
inorder(root);
cout<<endl<<"PREORDER"<<endl;
preorder(root);
cout<<endl<<"POSTORDER"<<endl;
postorder(root);
cout<<endl<<"LEVELORDER"<<endl;
levelorder(root);
data=30;
deltree(root,30);
/*cout<<endl<<"LEVELORDER AFTER DELETION"<<endl;
levelorder(root);*/
cout<<endl<<"HEIGHT OF TREE "<<endl;
cout<<height(root)<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
//code 1
/*int main()
{
int n,i;
cin>>n;
int a[n+1];
for(i=0;i<n;i++)
{
cin>>a[i];
}
bool flag=true;
for(i=0;i<(n/2)-1;i++)
{
if(a[i]<a[(2*i)+1] || a[i]<a[2*i+2])
{
flag=false;
break;
}
}
if(flag==true)
{
cout<<"it is max heap"<<endl;
}
else
{
cout<<"not a max heap"<<endl;
}
return 0;
}*/
//code 2
/*
int main()
{
priority_queue<int,vector<int>, greater<int> > pq;
pq.push(10);
pq.push(100);
pq.push(1);
pq.push(1000);
pq.push(10000);
pq.push(11);
priority_queue<int, vector<int>, greater<int> > nq=pq;
while(!nq.empty())
{
cout<<nq.top()<<endl;
nq.pop();
}
return 0;
}
*/
//code 3
/* A binary tree node has key, pointer to left child
and a pointer to right child */
/*
struct node {
int data;
node* left, *right;
};
node* newNode(int data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
};
void inorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
void insert(node* root, int data)
{
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
root->left = newNode(data);
break;
} else
q.push(root->left);
if (!root->right) {
root->right = newNode(data);
break;
} else
q.push(root->right);
}
}
int main()
{
node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right=newNode(50);
inorder(root);
cout << endl;
int data = 60;
insert(root, data);
inorder(root);
return 0;
}
*/
//code 4
/*struct node {
int data;
node* left, *right;
};
node* newNode(int data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
};
void inorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// preorder
void preorder(node* root)
{
if (root==NULL)
return;
cout << root->data << " ";
inorder(root->left);
inorder(root->right);
}
//postoder
void postorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
inorder(root->right);
cout << root->data << " ";
}
//levelorder
void levelorder(node *root)
{
queue<node *>q;
q.push(root);
while(!q.empty())
{
node *da=q.front();
q.pop();
cout<<da->data<<" ";
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
}
void insert(node* root, int data)
{
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
root->left = newNode(data);
break;
} else
q.push(root->left);
if (!root->right) {
root->right = newNode(data);
break;
} else
q.push(root->right);
}
}
int main()
{
node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right=newNode(50);
inorder(root);
cout << endl;
int data = 60;
insert(root, data);
cout<<endl<<"INORDER"<<endl;
inorder(root);
cout<<endl<<"PREORDER"<<endl;
preorder(root);
cout<<endl<<"POSTORDER"<<endl;
postorder(root);
cout<<endl<<"LEVELORDER"<<endl;
levelorder(root);
return 0;
}
*/
// code 6
struct node {
int data;
node* left, *right;
};
node* newNode(int data)
{
node* newnode = new node;
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
};
void inorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// preorder
void preorder(node* root)
{
if (root==NULL)
return;
cout << root->data << " ";
inorder(root->left);
inorder(root->right);
}
//postoder
void postorder(node* root)
{
if (root==NULL)
return;
inorder(root->left);
inorder(root->right);
cout << root->data << " ";
}
//levelorder
void levelorder(node *root)
{
queue<node *>q;
q.push(root);
while(!q.empty())
{
node *da=q.front();
q.pop();
cout<<da->data<<" ";
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
}
// delete from tree
void deltree(node *root,int number)
{
queue<node *>q;
q.push(root);
// finding last element
node *da;
while(!q.empty())
{
da=q.front();
q.pop();
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
int element=da->data;
// replaced
q.push(root);
while(!q.empty())
{
da=q.front();
q.pop();
if(da->data==number)
{
da->data=element;
break;
}
if(da->left!=NULL)
{
q.push(da->left);
}
if(da->right!=NULL)
{
q.push(da->right);
}
}
cout<<endl<<"TRAVESAL OF ELEMENT"<<endl;
queue<node *>qe;
qe.push(root);
while(!qe.empty())
{
node *da=qe.front();
qe.pop();
if(da->data!=element && da->left!=NULL && da->right!=NULL)
{
cout<<da->data<<" ";
}
if(da->left!=NULL)
{
qe.push(da->left);
}
if(da->right!=NULL)
{
qe.push(da->right);
}
}
}
int height(node *root)
{
if(root==NULL)
{
return 0;
}
return max(height(root->left),height(root->right))+1;
}
void insert(node* root, int data)
{
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
root->left = newNode(data);
break;
} else
q.push(root->left);
if (!root->right) {
root->right = newNode(data);
break;
} else
q.push(root->right);
}
}
int main()
{
node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right=newNode(50);
inorder(root);
cout << endl;
int data = 60;
insert(root, data);
cout<<endl<<"INORDER"<<endl;
inorder(root);
cout<<endl<<"PREORDER"<<endl;
preorder(root);
cout<<endl<<"POSTORDER"<<endl;
postorder(root);
cout<<endl<<"LEVELORDER"<<endl;
levelorder(root);
data=30;
deltree(root,30);
/*cout<<endl<<"LEVELORDER AFTER DELETION"<<endl;
levelorder(root);*/
cout<<endl<<"HEIGHT OF TREE "<<endl;
cout<<height(root)<<endl;
return 0;
}
Comments
Post a Comment