Monday 21 April 2014

BST Program

Write a BST Program that insert,inorder traversal, count external node,count internal node of tree as well as display external node, internal node and also  display height of tree:-

#include<conio.h>
#include<iostream.h>
#include<process.h>
struct node
{
node *left;
node *right;
int info;
};
node *root=NULL;
int count_e, count_i;
void insert()
{
node *temp,*save,*ptr;
int x;
temp=new node;
temp->left=NULL;
temp->right=NULL;
cout<<"enter value for node:"  ;
cin>>x;
temp->info=x;
if(root==NULL)
root=temp;
else
{
save=NULL;ptr=root;
while(ptr!=NULL)
{
if(x<ptr->info)
{
save=ptr;
ptr=ptr->left;
}
else
{
save=ptr;
ptr=ptr->right;
}


}
if(x<save->info)
save->left=temp;
else
save->right=temp;
}
}

void determineHeight(node *tree, int *height)
{
int left_height, right_height;
if( tree == NULL)
*height = 0;
else
{
determineHeight(tree->left, &left_height);
determineHeight(tree->right, &right_height);
if( left_height > right_height)
*height = left_height + 1;
else
*height = right_height + 1;
}
}



void inorder(node *root)
{
if(root==NULL)
return;
else
{
inorder(root->left);
cout<<"  " <<root->info;
inorder (root->right);
}
}
void inorder_e(node *root)
{
if(root==NULL)
return;
else
{
inorder_e(root->left);
if((root->left==NULL)&&(root->right==NULL))
{
 count_e++;
 cout<<" "<<root->info; 
 }                                    
inorder_e(root->right);
}

}
void inorder_i(node *root)
{
if(root==NULL)
return;
else
{
inorder_i(root->left);
if((root->left!=NULL)||(root->right!=NULL))
{
     count_i++;                                      
      cout<<" "<<root->info;  
      }                                   
inorder_i(root->right);
}

}
int main()
{
int ch;
int height=0;
do
{
cout<<"\n press 1 for insertion :"   ;
cout<<"\n press 2 for traversal :"  ;
cout<<"\n press 3 for printing external nodes and internal nodes";
cout<<"\n press 4 for display height of tree:" 
cout<<"\n any key for exit:" ;
cin>>ch;
if(ch==1)
insert();
else if(ch==2)
inorder(root) ;
else
if(ch==3)
     {
     count_e=0;count_i=0;
     cout<<"\n ";
     inorder_e(root);
     cout<<" \n number of external nodes are"<<count_e;
      cout<<"\n ";
     inorder_i(root);
     cout<<" \n number of internal nodes are"<<count_i;

     }
else if(ch==4)
{
determineHeight(root,&height);
cout<<"\n\nHeight of Tree : "<<height<<endl;
}
else
exit(0);
}while((ch==1)||(ch==2)||(ch==3)||(ch==4));
getch();
return 0;
}

Saturday 15 March 2014

Quick Sort

Quick Sort


#include<conio.h>
#include<iostream.h>
quick(int a[],int beg,int end)
{
int loc,left,right,temp;
left=beg;
right=end;
loc=beg;
step2: while((a[loc]<=a[right])&&(right!=loc))
{
right=right-1;
}
if(loc==right)
{
return loc;
}
if(a[loc]>a[right])
{
temp=a[loc] ;
a[loc]=a[right];
a[right]=temp;
loc=right;
}

while((a[loc]>=a[left])&&(left!=loc))
{
left=left+1;
}
if(loc==left)
{
return loc;
}
if(a[loc]<a[left])
{
temp=a[loc] ;
a[loc]=a[left];
a[left]=temp;
loc=left;
}

goto step2;
}
int main()
{
int a[100],n,l[20],u[20];
int top=-1,beg,end,loc;
cout<<"Enter no. element in the list";
cin>>n;
cout<<"enter element in the list:" ;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
top=top+1 ;
l[top]=0;
u[top]=n-1;
if(n<2)
{
cout<<"list is already sorted";
}
else
{
while(top!=-1)
{
beg=l[top];
end=u[top];
top=top-1;
loc=quick(a,beg,end);
if(loc-1>beg)
{
top=top+1;
l[top]=beg;
u[top]=loc-1;
}
if(loc+1<end)
{
top=top+1;
l[top]=loc+1;
u[top]=end;
}
}
}
cout<<"sorted element are:";
for(i=0;i<n;i++)
{
cout <<a[i];
}
getch();
return 0;
}




Tuesday 11 March 2014

TRANSACTION MANAGEMENT

TRANSACTION MANAGEMENT

Transaction
A transaction is a unit of a program execution that accesses and possibly modifies various data
objects (tuples, relations).
A transaction is a Logical unit of database processing that includes one or more access
operations (read -retrieval, write - insert or update, delete).
A transaction (set of operations) may be stand-alone specified in a high level language like SQL
submitted interactively, or may be embedded within a program.
A transaction (collection of actions) makes transformations of system states while preserving the
database consistency.
A user’s program may carry out many operations on the data retrieved from the database, but the
DBMS is only concerned about what data is read/written from/to the database.
A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
PROPOERTIES OF TRANSACTION
The DBMS need to ensure the following properties of transactions:
1. Atomicity
– Transactions are either done or not done
– They are never left partially executed
An executing transaction completes in its entirety or it is aborted altogether.
–e.g., Transfer_Money (Amount, X, Y) means i) DEBIT (Amount, X);
ii) CREDIT (Amount, Y). Either both take place or none
2. Consistency
– Transactions should leave the database in a consistent state
If each Transaction is consistent, and the DB starts consistent, then the Database ends up
consistent.
–If a transaction violates the database’s consistency rules, the entire transaction will be
rolled back and the database will be restored to a state consistent with those rules.

3. Isolation
– Transactions must behave as if they were executed in isolation.
An executing transaction cannot reveal its (incomplete) results before it commits.
–Consequently, the net effect is identical to executing all transactions, the one after the
other in some serial order.
4. Durability
– Effects of completed transactions are resilient against failures
Once a transaction commits, the system must guarantee that the results of its operations
will never be lost, in spite of subsequent failures.
SIMPLE MODEL OF A DATABASE
A database is a collection of named data items.
Granularity of data - a field, a record, or a whole disk block (Concepts are independent of
granularity).
Basic operations are read and write:
read_item(X): Reads a database item named X into a program variable. To simplify our
notation, we assume that the program variable is also named X.
write_item(X): Writes the value of program variable X into the database item named X.
READ AND WRITE OPERATIONS:
Basic unit of data transfer from the disk to the computer main memory is one block. In
general, a data item (what is read or written) will be the field of some record in the database,
although it may be a larger unit such as a record or even a whole block.
read_item(X) command includes the following steps:
1. Find the address of the disk block that contains item X.
2. Copy that disk block into a buffer in main memory (if that disk block is not already in
some main memory buffer).

3. Copy item X from the buffer to the program variable named X.
write_item(X) command includes the following steps:
1. Find the address of the disk block that contains item X.
2. Copy that disk block into a buffer in main memory (if that disk block is not already in
some main memory buffer).
3. Copy item X from the program variable named X into its correct location in the buffer.
4. Store the updated block from the buffer back to disk (either immediately or at some later
point in time).

TRANSACTION STATES
1. Active state
2. Partially committed state
3. Committed state
4. Failed state
5. Terminated State