Skip to content

Commit

Permalink
practical_file
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyajainn committed Oct 11, 2018
1 parent 09c4185 commit e75fc06
Show file tree
Hide file tree
Showing 56 changed files with 2,293 additions and 0 deletions.
123 changes: 123 additions & 0 deletions DELETION_IN _BST.cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using namespace std;
#include<iostream>
struct node
{
int data;
struct node*left=NULL;
struct node *right=NULL;
}*root=NULL;
struct node *insertnode(struct node *root,struct node *temp)
{
if(root==NULL)
{
root=new node;
root->data=temp->data;
root->left=NULL;
root->right=NULL;
}
else if(root->data>temp->data)
{
if(root->left==NULL)
{
root->left=temp;
root->left->left=NULL;
root->left->right=NULL;

}
else
{
insertnode(root->left,temp);
}
}
else if(root->data<temp->data)
{
if(root->right==NULL)
{
root->right=temp;
root->right->left=NULL;
root->right->right=NULL;
}
else
{
insertnode(root->right,temp);
}
}
return root;
}
struct node * findmin(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
current = current->left;

return current;
}
struct node *delete1(struct node *temp,int data)
{if(root==NULL)
return root;
else if(data<root->data)
{
root->left=delete1(root->left,data);
}
else if(root->data<data)
{
root->right=delete1(root->right,data);
}
else
{
if(root->left==NULL&&root->right==NULL)
{
delete(root);
root=NULL;
}
else if(root->left==NULL)
{
struct node *temp=root;
root=root->right;
delete(temp);
}
else if(root->right==NULL)
{
struct node *temp=root;
root=root->left;
delete(temp);
}
else
{
struct node*temp=findmin(root->right);
root->data=temp->data;
root->right=delete1(root->right,temp->data);
}
}
return root;
}
void traverse(struct node* node1)
{
if (node1 == NULL)
return;
traverse(node1->left);
cout << node1->data << " ";
traverse(node1->right);

}
int main()
{
int n;
cin>>n;
int data1;
struct node *p=NULL;
for(int i=0;i<n;i++)
{
cin>>data1;
struct node *temp=new struct node();
temp->data=data1;
temp->left=NULL;
temp->right=NULL;
p=insertnode(p,temp);
}
int p1;
cin>>p1;
traverse(root);
root=delete1(root,p1);
traverse(root);
}
Binary file added DELETION_IN _BST.cpp.exe
Binary file not shown.
72 changes: 72 additions & 0 deletions IN_ORDER_BINARY TREE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include<iostream>
using namespace std;
struct node
{
struct node *left;
int data;
struct node *right;
}*root=NULL;

struct node *insertnode(struct node *root,struct node *temp)
{

if(root==NULL)
{
root=new node;
root->data=temp->data;
root->left=NULL;
root->right=NULL;
}
else if(root->data<temp->data)
{
if(root->right==NULL)
{
root->right=temp;
root->right->left=NULL;
root->right->right=NULL;
}
else
{
insertnode(root->right,temp);
}
}
else if(root->data>temp->data)
{
if(root->left==NULL)
{
root->left=temp;
root->left->left=NULL;
root->left->right=NULL;
}
else
{
insertnode(root->left,temp);
}
}
return root;
}
void traverse(struct node* node1)
{
if (node1 == NULL)
return;
traverse(node1->left);
cout << node1->data << " ";
traverse(node1->right);


}
int main()
{
int data1;
struct node *temp,*p=NULL;
for(int i=0;i<5;i++)
{
cin>>data1;
struct node *temp=new struct node();
temp->data=data1;
temp->left=NULL;
temp->right=NULL;
p=insertnode(p,temp);
}
traverse(p);
}
Binary file added IN_ORDER_BINARY TREE.exe
Binary file not shown.
73 changes: 73 additions & 0 deletions POST_ORBER_BINARY_TREE.cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include<iostream>
using namespace std;
struct node
{
struct node *left;
int data;
struct node *right;
}*root=NULL;

struct node *insertnode(struct node *root,struct node *temp)
{

if(root==NULL)
{
root=new node;
root->data=temp->data;
root->left=NULL;
root->right=NULL;
}
else if(root->data<temp->data)
{
if(root->right==NULL)
{
root->right=temp;
root->right->left=NULL;
root->right->right=NULL;
}
else
{
insertnode(root->right,temp);
}
}
else if(root->data>temp->data)
{
if(root->left==NULL)
{
root->left=temp;
root->left->left=NULL;
root->left->right=NULL;
}
else
{
insertnode(root->left,temp);
}
}
return root;
}
void traverse(struct node* node1)
{
if (node1 == NULL)
return;
traverse(node1->left);
traverse(node1->right);
cout << node1->data << " ";

}
int main()
{
int data1;
struct node *temp,*p=NULL;
for(int i=0;i<5;i++)
{
cin>>data1;
struct node *temp=new struct node();
temp->data=data1;
temp->left=NULL;
temp->right=NULL;
p=insertnode(p,temp);
}
traverse(p);
}


Binary file added POST_ORBER_BINARY_TREE.cpp.exe
Binary file not shown.
72 changes: 72 additions & 0 deletions PRE_ORDER_BINARY_TREE,cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include<iostream>
using namespace std;
struct node
{
struct node *left;
int data;
struct node *right;
}*root=NULL;

struct node *insertnode(struct node *root,struct node *temp)
{

if(root==NULL)
{
root=new node;
root->data=temp->data;
root->left=NULL;
root->right=NULL;
}
else if(root->data<temp->data)
{
if(root->right==NULL)
{
root->right=temp;
root->right->left=NULL;
root->right->right=NULL;
}
else
{
insertnode(root->right,temp);
}
}
else if(root->data>temp->data)
{
if(root->left==NULL)
{
root->left=temp;
root->left->left=NULL;
root->left->right=NULL;
}
else
{
insertnode(root->left,temp);
}
}
return root;
}
void traverse(struct node* node1)
{
if (node1 == NULL)
return;
cout << node1->data << " ";
traverse(node1->left);
traverse(node1->right);


}
int main()
{
int data1;
struct node *temp,*p=NULL;
for(int i=0;i<5;i++)
{
cin>>data1;
struct node *temp=new struct node();
temp->data=data1;
temp->left=NULL;
temp->right=NULL;
p=insertnode(p,temp);
}
traverse(p);
}
Binary file added PRE_ORDER_BINARY_TREE,cpp.exe
Binary file not shown.
Loading

0 comments on commit e75fc06

Please sign in to comment.