0% found this document useful (0 votes)
23 views

Programs

The document discusses different algorithms including tower of Hanoi, binary tree traversals, stack operations using linked lists, quick sort, and heap sort. Code implementations are provided for each algorithm.

Uploaded by

Suja Mary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Programs

The document discusses different algorithms including tower of Hanoi, binary tree traversals, stack operations using linked lists, quick sort, and heap sort. Code implementations are provided for each algorithm.

Uploaded by

Suja Mary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <iostream.

h>
#include<conio.h>
//tower of HANOI function implementation
void TOH(int n, char Sour, char Aux, char Des)
{
if (n == 1) {
cout << "Move Disk " << n << " from " << Sour << " to " << Des << endl;
return;
}

TOH(n - 1, Sour, Des, Aux);


cout << "Move Disk " << n << " from " << Sour << " to " << Des << endl;
TOH(n - 1, Aux, Sour, Des);
}

//main program
int main()
{
int n;

cout << "Enter no. of disks:";


cin >> n;
//calling the TOH
TOH(n, 'A', 'B', 'C');
getch();
return 0;
}

Binary Tree Traversals

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
class treetrav
{
public:
struct tree
{
char no;
struct tree *left,*right;
}
*start,*root,*temp,*st[30];
int top;
treetrav()
{
start=root=NULL;
top=0;
}
void create();
void view();
void preorder(struct tree*);
void postorder(struct tree*);
void inorder(struct tree*);
};

void treetrav::create()
{
char ch;
root=start=new(struct tree);
cout<<"\n \t Enter the Root node:";
cin>>root->no;
root->left=NULL;
root->right=NULL;
while(root)
{
cout<<"\n \t Enter the Left child:";
cout<<root->no<<"Exist?(Y/N):";
cin>>ch;
if(tolower (ch)=='y')
{
cout<<"\n \t Enter the Left child of:";
cout<<root->no<<":";
temp=new(struct tree);
cin>>temp->no;
root->left=temp;
temp->left=NULL;
temp->right=NULL;
}
else
root->left=NULL;
cout<<"\n \t \t Is Right child of:";
cout<<root->no<<"Exist?(Y/N):";
cin>>ch;
if(tolower (ch)=='y')
{
cout<<"\n \t Enter the Right child of:";
cout<<root->no<<":";
temp=new(struct tree);
cin>>temp->no;
root->right=temp;
temp->left=NULL;
temp->right=NULL;
st[++top]=temp;
}
else
root->right=NULL;
if(root->left!=NULL)
root=root->left;
else
{
if(top==0)
root=NULL;
else
root=st[top--];
}
}
cout<<"\n";
}

void treetrav::preorder(struct tree *temp)


{
if(temp)
{
cout<<" "<<temp->no;
preorder(temp->left);
preorder(temp->right);
}
}

void treetrav::inorder(struct tree *temp)


{
if(temp)
{
inorder(temp->left);
cout<<" "<<temp->no;
inorder(temp->right);
}
}

void treetrav::postorder(struct tree *temp)


{
if(temp)
{
postorder(temp->left);
postorder(temp->right);
cout<<" "<<temp->no;
}
}

void main()
{
treetrav t;
clrscr();
t.create();
cout<<"\n \t Preorder \t:\t";
t.preorder(t.start);
cout<<"\n \t Inorder \t:\t";
t.inorder(t.start);
cout<<"\n \t Postorder \t:\t";
t.postorder(t.start);
getch();
}
Stack Operations Using linked list
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;
void push(int val) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = val;
newnode->next = top;
top = newnode;
}
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< top->data <<endl;
top = top->next;
}
}
void display() {
struct Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Quick Sort
#include<iostream.h>
#include<conio.h>
void swap(int arr[] , int pos1, int pos2){
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}

int partition(int arr[], int low, int high, int pivot){


int i = low;
int j = low;
while( i <= high){
if(arr[i] > pivot){
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}

void quickSort(int arr[], int low, int high){


if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high, pivot);

quickSort(arr, low, pos-1);


quickSort(arr, pos+1, high);
}
}

int main()
{
int n,arr[25];;
cout << " enter the number of elements in array\n";
cin>>n;
cout<<”Enter the array Elements one by one\n”

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


cin>> arr[i];
}
quickSort(arr, 0 , n-1);
cout<<"The sorted array is: ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}

Heap Sort
#include<iostream.h>
#include<conio.h>
void heapify(int arr[],int n,int i)
{
int t;
int la=i;
int l=2*i+1;
int r=2*i+2;
if(l<n&&arr[l]>arr[la])
la=l;
if(r<n && arr[r]>arr[la])
la=r;
if(la!=i)
{
t=arr[i];
arr[i]=arr[la];
arr[la]=t;
heapify(arr,n,la);
}
}
void heapsort(int arr[],int n)
{
int i,t;
for(i=n/2-1;i>=0;i--)
heapify(arr,n,i);
for(i=n-1;i>=0;i--)
{
t=arr[0];
arr[0]=arr[i];
arr[i]=t;
heapify(arr,i,0);
}
}
int main()
{
int arr[50],n,i;
cout<<"Enter the value of n\n";
cin>>n;
cout<<"Enter the elements of array:\n";
for(i=0;i<n;i++)
cin>>arr[i];
heapsort(arr,n);
cout<<"Sorted elements are:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
getch();
return 0;
}

You might also like