Monday, 28 September 2015

B Trees in c/c++ source code, Trees source code in c/c++, source code,

B Trees in c/c++ source code, Trees source code in c/c++, source code,
// btrees.cpp : Defines the entry point for the console application.
//



B Trees in c/c++ source code, Trees source code in c/c++, source code, 

For More:>  Sorting......, selection sort..., merge sort....., insertion Sort....., General Tree....Binary Tree..... and many more.....



#include "stdafx.h"

/*A program to implement B-TREE.
   */

#include<iostream>
using namespace std;


class node;
struct pair
{
int key;
node *next;
}pair;
class node
{
public:
node *father;
int noofkeys;
    pair data[MAX];
node *first;
node();
void insert_in_node(pair x);
pair splitnode(pair x);
int leaf_node();
node *nextindex(int x);
void display();
};
void node::display()
{
int i;
cout<<"(";
for(i=0;i<noofkeys;i++)
cout<<data[i].key<<" ";
cout<<")";
}
node* node::nextindex(int x)
{
if(x<data[0].key)
return first;
for(int i=0;i<noofkeys;i++)
{
if(x<=data[i].key)
return (data[i-1].next);
}
return (data[i-1].next);
}
int node::leaf_node()
{
if(data[0].next==NULL)
return 1;
return 0;
}


 void node::insert_in_node(pair mypair)
   {
int i;
for(i=noofkeys-1;i>=0 && data[i].key>mypair.key;i--)
data[i+1]=data[i];
data[i+1]=mypair;
noofkeys++;
   }

node::node()
{
 for(int i=0;i<=MAX;i++)
  data[i].next=NULL;
 noofkeys=0;
 father=NULL;
 first=NULL;
}

class btree
 {
int mkeys;
node *root;
public:
btree(int n)
  {
mkeys=n;
root=NULL;
  }
void insert(int x);
void displaytree();
 };

void main()
   {
 int n,i,x,op;
 node *p;

 cout<<"\nmaximum number of keys in a node ? :";
 cin>>n;
 btree b(n);
 do
{
cout<<"\n\n1)Insert\n2)Print\n3)Quit";
cout<<"\nEnter your choice : ";
cin>>op;
switch(op)
  {
case 1: cout<<"\nEnter a data : ";
cin >> x;
b.insert(x);
break;

case 2:
b.displaytree();
  break;
}
 }while(op!=3);
}

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com 

Wednesday, 23 September 2015

Safe array in c/c++ source code, Array source code in c/c++, source code,

Safe array in c/c++ source code, Array source code in c/c++, source code,
// safe_arry.cpp : Defines the entry point for the console application.
//


Safe array in c/c++ source code, 

For More:>  Sorting......, selection sort..., merge sort....., insertion Sort....., General Tree....Binary Tree..... and many more......









#include<iostream.h>

int const size=10;


class safe_array
{
int array[size];
public:

safe_array()
{
for(int i=0;i<size;i++)
{
array[i]=0;
}
}



 void put(int f,int no)
 {
if(f>=0&&f<=size-1)
{

array[f]=no;
}
else{
cout<<"execed size:";
}
 }

 int get(int no)
 {
if(no>=0&&no<=size-1)
{
int a=array[no];
cout<<a<<endl;
return a;
}
else
{
return 0;
}
 }

 void input()
 {
for(int i=0;i<size;i++)
{
cout<<"enter no";
cin>>array[i];
}
 }

 void output()
 {
for(int i=0;i<size;i++)
{
cout<<array[i]<<endl;
}
 }

 safe_array adding(safe_array a)
 {
safe_array b;
for(int i=0;i<size;i++)
{
  b.array[i]=a.array[i]+array[i];
}
return b;
 }



 int search_array(int a)
 {

    for(int i=0;i<=size;i++)
{
if(array[i]==a)
{
return i;

}
}
 }
 void sort_array()
 {
for(int i=0;i<=size;i++)
{
for(int j=0;j<=size-1;j++)
{
if(array[j+1]<array[j])
{
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
 }
};




int main()
{
safe_array arr,b,c;

char a;
do
{

cout<<"------------------------------------------"<<endl;
cout<<"enter 'A' for put in array:"<<endl;
cout<<"enter 'B' for get from array:"<<endl;
cout<<"enter 'C' for whole input:"<<endl;
cout<<"enter 'D' for whole display:"<<endl;
cout<<"enter 'E' for adding two objects:"<<endl;
cout<<"enter 'F' for search:"<<endl;
cout<<"enter 'G' for sort:"<<endl;
cout<<"enter 'I' for Exit:"<<endl;
cout<<"-----------------------------------------------"<<endl;

cout<<"enter choice:";
cin>>a;
switch (a)
{
case 'A':
          int n,k;
  cout<<"enter index=";
  cin>>k;
  cout<<"enter value=";
  cin>>n;
  arr.put(k,n);

break;

case 'B':
          int m;
  cout<<"enter index=";
  cin>>m;
  arr.get(m);
  
break;
case 'C':
arr.input();

break;
case 'D':
  arr.output();

break;
case 'E':
     
 b.input();
c= arr.adding(b);
c.output();
break;

case 'F':
int l,s;
cout<<"enter number for search";
cin>>l;
s=arr.search_array(l);
cout<<"number is at index:"<<s;
break;
case 'G':
arr.sort_array();

break;

}
}while(a!='I');

return 0;
}


Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com

Binary Tree in c/c++ source code, Binary tree, source code,

Binary Tree in c/c++ source code, Binary tree, source code,


Binary Tree in c/c++ source code, Binary tree, 

For More:>  Sorting......, selection sort..., merge sort....., insertion Sort....., General Tree.... and many more......











#include <iostream>
using namespace std;

#include "BinaryTree.h"

// Constructor.
template <class Object>
BinaryNode<Object>::BinaryNode( const Object & theElement,
                                BinaryNode *lt, BinaryNode *rt )
  : element( theElement ), left( lt ), right( rt )
{ 
}

// Return size of tree rooted at t.
template <class Object>
int BinaryNode<Object>::size( BinaryNode<Object> * t )
{
    if( t == NULL )
        return 0;
    else
        return 1 + size( t->left ) + size( t->right );
}

#define max MAX

template <class Comparable>
Comparable max( const Comparable & a, const Comparable & b )
{
    return a > b ? a : b;
}

// Return height of tree rooted at t.
template <class Object>
int BinaryNode<Object>::height( BinaryNode<Object> * t )
{
    if( t == NULL )
        return -1;
    else
        return 1 + max( height( t->left ), height( t->right ) );
}

#undef max

// Print the tree rooted at current node using preorder traversal.
template <class Object>
void BinaryNode<Object>::printPreOrder( ) const
{
    cout << element << endl;                  // Node
    if( left != NULL )
        left->printPreOrder( );               // Left
    if( right != NULL )
        right->printPreOrder( );              // Right
}

// Print the tree rooted at current node using postorder traversal.
template <class Object>
void BinaryNode<Object>::printPostOrder( ) const
{
    if( left != NULL )                        // Left
        left->printPostOrder( );
    if( right != NULL )                       // Right
        right->printPostOrder( );
    cout << element << endl;                  // Node
}

// Print the tree rooted at current node using inorder traversal.
template <class Object>
void BinaryNode<Object>::printInOrder( ) const
{
    if( left != NULL )                        // Left
        left->printInOrder( );
    cout << element << endl;                  // Node
    if( right != NULL )
        right->printInOrder( );               // Right
}

// Return a pointer to a node that is the root of a
// duplicate of the tree rooted at the current node.
template <class Object>
BinaryNode<Object> * BinaryNode<Object>::duplicate( ) const
{
    BinaryNode<Object> *root =
                         new BinaryNode<Object>( element );

    if( left != NULL )            // If there's a left subtree
        root->left = left->duplicate( );   // Duplicate; attach
    if( right != NULL )           // If there's a right subtree
        root->right = right->duplicate( ); // Duplicate; attach

    return root;                     // Return resulting tree
}

// Deep copy.
template <class Object>
const BinaryTree<Object> &
BinaryTree<Object>::operator= ( const BinaryTree<Object> & rhs )
{
    if( this != &rhs )
    {
        makeEmpty( );
        if( rhs.root != NULL )
            root = rhs.root->duplicate( );
    }

    return *this;
}

// Merge routine for BinaryTree class.
// Forms a new tree from rootItem, t1 and t2.
// Does not allow t1 and t2 to be the same.
// Correctly handles other aliasing conditions.
template <class Object>
void BinaryTree<Object>::merge( const Object &  rootItem,
                         BinaryTree<Object> & t1, BinaryTree<Object> & t2 )
{
    if( t1.root == t2.root && t1.root != NULL )
    { 
        cerr << "Cannot merge a tree with itself; merge aborted." << endl;
        return;
    }

    Node *oldRoot = root;   // Save old root

      // Allocate new node
    root = new Node( rootItem, t1.root, t2.root );

      // Deallocate nodes in the original tree
    if( this != &t1 && this != &t2 )
        makeEmpty( oldRoot );

      // Ensure that every node is in one tree
    if( this != &t1 )
        t1.root = NULL;
    if( this != &t2 )
        t2.root = NULL;
}

// Make tree rooted at t empty, freeing nodes, and setting t to NULL.
template <class Object>
void BinaryTree<Object>::makeEmpty( BinaryNode<Object> * & t )
{
    if( t != NULL )
    {
        makeEmpty( t->left );
        makeEmpty( t->right );
        delete t;
        t = NULL;
    }
}

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com

Monday, 21 September 2015

Selection sort source code in c++, Insertion sort source code in c++, Merge sort source code in c++, Quick sort source code in c++, c++, source code,

Selection sort source code in c++, Insertion sort source code in c++, Merge sort source code in c++, Quick sort source code in c++, c++, source code,




// sorts.cpp : Defines the entry point for the console application.
//





#include "stdafx.h"
#include"string.h"
#include<time.h>
#include<iostream>

#include<complex>
using namespace std;

const int arrsize=100;
const int nrange = 320;

void display(int arr[], int n)
{
cout<<\n------------------------------------------\n";
    for(int i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}

void insertionsort(int a[], int n)
{
for(int i=0; i<n; i++)
{
int cur = a[i];
int j = i - 1;

while( j>=0 && a[j] > cur)
{
a[j+1] = a[j];
j--;
}
a[j+1]=curr;
}
}

void insertionsortrev(char a[], int n)
{
for(int i=0; i<n; i++)
{
char cur=a[i];
int j=i-1;

int k=0;
while(j>=0 && a[j]>curr)
{
a[j+1] = a[j];
j--;
}
a[j+1]=cur;
}
}

void selectsort(int arr[], int n)
{
int pos_min,temp;

for(int i=0; i<n-1; i++)
{
pos_min = 1;

for(int j=i+1; j<n; j++)
{
if (arr[j] < arr[pos_min])
pos_min=j;
}

if(pos_min != i)
{
temp=arr[i];
arr[i]=arr[pos_min];
arr[pos_min] = temp;
}
}
}

void mergsort(int s[], int n)
{
if(n <= 1) return;

int s1[arrsize/2+1];
int s2[arrsize/2+1];

for(int i=0; i<n/2; i++)
{
s1[i]=s[i];
}

for(i=n/2; i<n; i++)
{
s2[i-n/2] = s[i];
}

mergsort(s1, n/2);
mergsort(s2, n-n/2);

for(i=0; i<n; i++)
{
s[i]=0;
}

int n1=0;
int n2=0;
i=0;
while (n1 < n/2 && n2 < (n-n/2))
{
if(s1[n1] < s2[n2])
s[i++] = s1[n1++];
else
s[i++] = s2[n2++];
}

while (n1 < n/2)
{
s[i++] = s1[n1++];
}
while(n2 < n-n/2)
{
s[i++] = s2[n2++];
}
}

void quicksort(int arr[], int left, int right)
{
int i=left, j=right;
int tmp;
int pivot = arr[(left+right)/2];

while (i <= j)
{
while(arr[i] < pivot)
i++;
while(arr[j] > pivot)
j++;

if(i <= j)
{
tmp=arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
if(left<j)
quicksort(arr,left,j);
if(i<right)
quicksort(arr,i,right);
}

int main(int argc, char* argv[])
{
srand( (unsigned)time(NULL));

const int n=arrsize;
int b[n];
for(int i=0; i<n; i++)
{
b[i] = rand() % nrange;
}

display(b,n);

selectsort(b,n);
insertionsort(b,n);
mergsort(b,n);
quicksort(b,n);
}

}


Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com

LinkWithin