Showing posts with label tree. Show all posts
Showing posts with label tree. Show all posts

Friday, May 1, 2015

Traverse Tree According To User Value :Get Value From User ☻ :~C++ Code Tree

Traverse Tree According To User Value :Get Value From User ☻

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void inorder(Node *root){
if(root == NULL) return;
else{
inorder(root->left);
cout <<root->data <<"  ";
inorder(root->right);
}    }


void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
while(getch()!=30){

cout<<"\n\n\n\tSelect A Mode: \n\n1 for Inorder Treverse\n2 for PostOrder Treverse\n3 for PreOrder Treverse\n";
cin>>c;
switch(c){
case 1:{
cout<<"\n\nInorder: ";
b.inorder(b.root);
break;
}
case 2:
{
cout<<"\n\nPost Order: ";
b.postorder(b.root);
break;
}
case 3:
{
cout<<"\n\nPre Order: ";
b.preorder(b.root);
break;
}
default:{
}
}
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";
cout<<"\n\nPress Space Bar To Exit || Enter To Continue \n\n";
}
getch();

}



Traverse Left Subtree with Post Order and Right Sub tree with PreOrder ~ C++ Codes Tree

Traverse root left sub tree with post order ☻
and then print root ☻
and then root right sub tree with pre order ☻

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void mixorder(Node *root){
cout<<"\n\nPost Order: ";
postorder(root->left);
cout <<"\n\nRoot: "<<root->data <<"  ";
cout<<"\n\nPre Order: ";
preorder(root->right);
}


void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\n Traverse Left Subtree with Post Order and Right Sub tree with PreOrder\n\n";
b.mixorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";

getch();

}


Insert A Value on Left Side which is greater Then Root and Small Value on Right side ~ C++ Tree codes

Insert A Value on Left Side which is greater Then Root and Small Value on Right side

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data< q->data)
q = q->right;
else q = q->left;
}
if (newnode->data< p->data)
p->right = newnode;
else p->left = newnode;
}




void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
}
}

void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\nGreater then root on left side and smaller values on Right side\n\n";
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreorder triversal: ";
b.preorder(b.root);
cout<<"\n\nPostorder triversal: ";
b.postorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";

getch();

}





Convert Left subtree into right subtree and right into left in tree C++ code: ~ BST

Convert Left Sub Tree of a Root into Right Sub Tree and Right sub Tree into Left Sub 
Tree ☺ ☻


#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
}
}

void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}
//Function is used to join left sub tree on right side of root
//and right sub tree on left side of root
void exchange(){
Node *temp=root->left;
root->left=root->right;
root->right=temp;
}


};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreorder triversal: ";
b.preorder(b.root);
cout<<"\n\nPostorder triversal: ";
b.postorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";
cout<<"\n\nPress Enter to see result after exchanging right subtree with left subtree\n\n";
getch();
b.exchange();
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreorder triversal: ";
b.preorder(b.root);
cout<<"\n\nPostorder triversal: ";
b.postorder(b.root);
getch();


}



Tuesday, April 21, 2015

BST (Binary Search Tree) in C++

Read Also:  


Creation of BST(binary Search Tree),Insertion at any point,Deletion At any point,Maximum Value,Minimum Value,Searching in tree 

[[Important Data Structure Codes in C++]]
Tree Non Liner Data Structure


#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;

void insert(int data){

Node* newnode = new Node();

newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}

Node* remove(Node *root, int value){
Node *t;
int cmp = value - root->data;

if (cmp < 0){
t = remove(root->left, value);
root->left = t;
}
else if (cmp > 0){
t = remove(root->right, value);
root->right = t;
}
else if (root->left != NULL && root->right != NULL){
Node* minNode;
minNode = findMin(root->right);
root->data = minNode->data;
t = remove(root->right, minNode->data);
root->right = t;
}
else {

Node* nodeToDelete = root;
if (root->left == NULL) //will handle 0 children
root = root->right;
else if (root->right == NULL)
root = root->left;
else root = NULL;
delete nodeToDelete;
}

return root;
}

Node* findMin(Node* tree)
{
if (tree == NULL)
return NULL;
if (tree->left == NULL)
return tree;
return findMin(tree->left);
}

void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
}
}



void search(Node *root,int v)
{
Node *q=root;
while(v != q->data)
{
if(v < q->data)
{
q=q->left;
}
else
{
q=q->right;
}
}
cout<<"\nNode is  found= "<<q->data<<endl;

}

Node *min_value(Node *r)
{
Node *q=r;
Node *p;
if(q!=NULL)
{
p=q;
q=q->left;
min_value(q);
 }
else
cout<<"\n Minimum Value = "<<p->data<<endl;
return q;

}

Node  *max_value(Node *r)
{
Node *q=r;
Node *p;
if(q!=NULL)
{
p=q;
q=q->right;
max_value( q);
}
else
cout<<"\n Maximum Value= "<<p->data<<endl;
return q;
}
};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
b.inorder(b.root);
getch();cout<<"\n\nEnter Value For Search ";cin>>c;
b.search(b.root,c); getch();
b.min_value(b.root);getch();
b.max_value(b.root);getch();
cout<<"\n\nEnter Value For Delete: ";cin>>c;
b.remove(b.root, c);
b.inorder(b.root);getch();
cout<<"\n\nInsert a New Value ";cin>>c;
b.insert(c);
b.inorder(b.root);
getch();

}



Tuesday, December 2, 2014

Removing in Bineary Tree


Removing In Bineary Tree All 3 cases: 
1st case having no child (leaf node)
2nd case having one child (non leaf node)
3rd case having two childs :)
==========================================

#include<iostream.h>
#include<conio.h>
class treenode{
public:
int info;
treenode *left,*right;
treenode()
{ this->info=NULL;
  this->left=NULL;
  this->right=NULL;
}
treenode(int info)
{ this->info=info;
  this->left=NULL;
  this->right=NULL;
}
void setinfo(int info)
{ this->info=info; }
int getinfo()
{ return info; }
void setleft(treenode *left)
{  this->left=left; }
void setright(treenode *right)
{   this->right=right; }
treenode *getleft()
{ return left; }
treenode *getright()
{  return right; }

void insert(treenode *root,int info)
{
treenode *node=new treenode(info);
treenode *p,*q;
p=q=root;
while(info!=p->getinfo() && q!=NULL)
{
p=q;
if(info<p->getinfo())
{ q=p->getleft(); }
else
{  q=p->getright(); }
}
if(info==p->getinfo())
{ cout<<" \n Attempt to insert duplicate "<<info<<endl;
delete node;
}
else if(info<p->getinfo())
{ p->setleft(node); }
else
{p->setright(node); }
}

void preorder(treenode *root)
{
if(root!=NULL)
{ cout<<"\t"<<root->getinfo();
preorder(root->getleft());
preorder(root->getright());
}}
void postorder(treenode *root)
{
if(root!=NULL)
{
postorder(root->getleft());
postorder(root->getright());
cout<<"\t"<<root->getinfo();
}}
void inorder(treenode *root)
{
if(root!=NULL)
{
inorder(root->getleft());
cout<<"\t"<<root->getinfo();
inorder(root->getright());
}}

treenode *remove(treenode *tree,int info)
{
treenode *t;
int cmp=info-tree->getinfo();
if(cmp<0){
t=remove(tree->getleft(),info);
tree->setleft(t);
}
else if(cmp>0){
t=remove(tree->getright(),info);
tree->setright(t);
}
else if(tree->getleft()!=NULL && tree->getright()!=NULL)
{
treenode *minnode;
minnode=findmin(tree->getright());
tree->setinfo(minnode->getinfo());
t=remove(tree->getright(),minnode->getinfo());
tree->setright(t);
}
else
{ treenode *nodetodelete=tree;
  if(tree->getleft()==NULL)
  tree=tree->getright();
  else if(tree->getright()==NULL)
  tree=tree->getright();
  else{
  tree=NULL;
  delete nodetodelete;}
}
return tree;
}
treenode *findmin(treenode *tree)
{ if(tree==NULL)
 return NULL;
 if(tree->getleft()==NULL)
 return tree;
 return findmin(tree->getleft());
}

};
void main()
{
clrscr();
int x[]={14,15,4,8,7,3,5,2,1,9,6,-1};
treenode *root=new treenode();
root->setinfo(x[0]);
for(int i=1;x[i]>0;i++)
{ root->insert(root,x[i]);}
cout<<"\n\nDispaly ::Pre Order\n\n";
root->preorder(root);
cout<<"\n\n Display ::In Order\n\n";
root->inorder(root);
cout<<"\n\n Display ::Post Order\n\n";
root->postorder(root);
getch();
clrscr();
root->preorder(root);
cout<<"\n\n Removing leaf node     :: 1st case\n\n ";
root->remove(root,x[10]);
root->preorder(root);
cout<<"\n\n Removing Non leaf node :: 2nd case\n\n";
root->remove(root,x[6]);
root->preorder(root);
cout<<"\n\n Removing parent node [having two child] ::case 3\n\n";
root->remove(root,x[2]);
root->preorder(root);
getch();
}


Sunday, November 30, 2014

Heap Sort (min heap)

Sorting Using min heap and arrange in decending order 


 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
#define max 7
class array{
int arr[max];
int count;
public:
array()
{
count=0;
for(int i=0;i<max;i++)
{ arr[i]=0; }
}
void add(int num){
if(count<max){
arr[count]=num; count++;}
else
{cout<<"\n Array is full"<<endl;}
}
void heapsort()
{
for(int i=count-1;i>0;i--)
{int ivalue=arr[i];
arr[i]=arr[0];
arr[0]=ivalue;
makeheap(i);}
}
void display()
{
for(int i=0;i<count;i++)
{ cout<<arr[i]<<"\t";}
}
void makeheap(int c)
{
for(int i=0;i<c;i++)
{
 int val=arr[i];
int s=i;
int f=(s-1)/2;
while(s>0 && arr[f]>val)
{ arr[s]=arr[f];
s=f;
f=(s-1)/2;}
arr[s]=val;
}
}
};
void main()
{
clrscr();
array a;
a.add(15);
a.add(19);
a.add(18);
a.add(7);
a.add(17);
a.add(16);
a.add(8);
cout<<"\n\n Values you insert in min order heap\n \n";
a.display();
a.makeheap(7);
cout<<"\n\n Values inserted in Heap tree\n\n";
a.display();
a.heapsort();
cout<<"\n\n Values after heap sort (min order)( desecnding )\n\n";
a.display();
getch();
}



Tuesday, November 25, 2014

Trees in C++

Implementation of Binary Tree .... 
In-Order Traversal
Pre-Order Traversal
Post-Order Traversal



#include<iostream.h>
#include<conio.h>
class treenode{
public:
int info;
treenode *left,*right;
treenode()
{ this->info=NULL;
  this->left=NULL;
  this->right=NULL;
}
treenode(int info)
{ this->info=info;
  this->left=NULL;
  this->right=NULL;
}
void setinfo(int info)
{ this->info=info; }
int getinfo()
{ return info; }
void setleft(treenode *left)
{  this->left=left; }
void setright(treenode *right)
{   this->right=right; }
treenode *getleft()
{ return left; }
treenode *getright()
{  return right; }

void insert(treenode *root,int info)
{
treenode *node=new treenode(info);
treenode *p,*q;
p=q=root;
while(info!=p->getinfo() && q!=NULL)
{
p=q;
if(info<p->getinfo())
{ q=p->getleft(); }
else
{  q=p->getright(); }
}
if(info==p->getinfo())
{ cout<<" \n Attempt to insert duplicate "<<info<<endl;
delete node;
}
else if(info<p->getinfo())
{ p->setleft(node); }
else
{p->setright(node); }
}

void preorder(treenode *root)
{
if(root!=NULL)
{ cout<<"\t"<<root->getinfo();
preorder(root->getleft());
preorder(root->getright());
}}
void postorder(treenode *root)
{
if(root!=NULL)
{
postorder(root->getleft());
postorder(root->getright());
cout<<"\t"<<root->getinfo();
}}
void inorder(treenode *root)
{
if(root!=NULL)
{
inorder(root->getleft());
cout<<"\t"<<root->getinfo();
inorder(root->getright());
}}
};
void main()
{
clrscr();
int x[]={14,15,4,8,7,3,5,2,1,9,6,-1};
treenode *root=new treenode();
root->setinfo(x[0]);
for(int i=1;x[i]>0;i++)
{ root->insert(root,x[i]);}
cout<<"\n\nDispaly ::Pre Order\n\n";
root->preorder(root);
cout<<"\n\n Display ::In Order\n\n";
root->inorder(root);
cout<<"\n\n Display ::Post Order\n\n";
root->postorder(root);
getch();

}


Saturday, September 20, 2014

Data Structure Programs

Read Also:  


"All Data Structure Programs up-till now"

To get code Click on that program heading :) ☺ ☻ 

 ALL Type Of Sorting Click here :: most important :: 

-------------------

`Queues 

----------------

`Single Link-List:

*  How to create single Link-list in C++

*  Insertion at any point in link-list in C++

*  Concatenate or Merge two Link-lists in C++

Deletion At any point in Circular Link List in C++
------------------------------------

`Double Link-list:

=================

Stacks Data Structure:


Decimal to binary conversion using stack in c++
============ 

How to add two matrix size is define by use






Program to print counting 10 to 1 using Recursion

--------------------------------------------------