0% found this document useful (0 votes)
48 views39 pages

Aaditya DS PRACTICAL FILE

The document contains a data structure practical file submitted by a student named Aaditya Tyagi to their professors Dr. Pardeep Kumar and Dr. R.K. Chauhan. The file contains 16 programs implementing various data structures and algorithms using classes in C++, including programs to find the sum of natural numbers, check for palindromes, perform linear and binary search, and implement sorting algorithms like selection, insertion and bubble sort. Each program is presented with the source code and sample output.

Uploaded by

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

Aaditya DS PRACTICAL FILE

The document contains a data structure practical file submitted by a student named Aaditya Tyagi to their professors Dr. Pardeep Kumar and Dr. R.K. Chauhan. The file contains 16 programs implementing various data structures and algorithms using classes in C++, including programs to find the sum of natural numbers, check for palindromes, perform linear and binary search, and implement sorting algorithms like selection, insertion and bubble sort. Each program is presented with the source code and sample output.

Uploaded by

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

KURUKSHETRA UNIVERSITY, KURUKSHETRA

DEPARTMENT OF COMPUTER SCIENCE AND


APPLICATION

DATA STRUCTURE
PRACTICAL FILE
(MCA-20-17)

Submitted To:
Dr. Pardeep Kumar
Dr. R.K. Chauhan

Submitted By : Aaditya Tyagi


Class : M.C.A.
Section : A
Class Roll No : 22
Exam Roll No : 2021027001
INDEX
Sr. No. Program Name Signature

1 Write a program to find Sum of n natural numbers using class.

2 Write a program to check a string is palindrome or not.

3 Write a program to convert decimal number into their binary equivalent.

4 Write a program to implement Linear search using class.

5 Write a program to implement Binary search using class.

6 Write a program to implement selection sort using class.

7 Write a program to implement insertion sort using class.

8 Write a program to implement Bubble sort using class.

9 Write a program to implement searching in linked list using class.

10 Write a program to implement searching in doubly linked list using class.

11 Write a program to implement push and pop operation in stack using class.

12 Write a program to implement insertion and deletion in a queue using class.

13 Write a program to implement BST.

14 Write a program to implement AVL tree.

Write a program to implement heap sort.


15

16 Write a program to implement Dijkstra’s algorithm.


Q1 Write a program to find Sum of n natural numbers using class.

#include <iostream>
using namespace std;

class Sum_N_Num
{
private:
int n, sum = 0, i;
public:
//Define getdata() function for getiing value of N.
void getdata()
{
cout<<"Enter the value for N (Value should be natural number): ";
cin>> n;
}

//Defining calculate() function for calculate the sum of first n natural


number
void calculate()
{
for (i=1; i<=n; i++)
{
sum += i;
}
}

//Defining the display() function for display the calculated sum.


void display()
{
cout<<"The Sum of First "<<n<<" number is: " <<sum;
}
};

int main()
{
Sum_N_Num s; // Create object of Class Sum_N_Num.

// Calling Member function using object.


s.getdata();
s.calculate();
s.display();
return 0;
}

Output:
Q2 Write a program to check a string is palindrome or not.

#include<iostream>
#include<string.h>
using namespace std;

class String_Pallin
{
private:
char string[20];
int len, i, flag = 0;
public:
//Define getdata() function for getiing String.
void getdata()
{
cout<<"Enter a String: ";
cin>> string;
}
//Defining calculate() function for checking string pallindrome or not.
void calculate()
{
len = strlen(string);
for(i=0;i < len ;i++)
{
if(string[i] != string[len-i-1])
{
flag = 1;
break;
}
}
}
//Defining the display() function for display the calculated sum.
void display()
{
if (flag)
{
cout << string << " is not a palindrome" << endl;
}
else
{
cout << string << " is a palindrome" << endl;
}
}
};

int main()
{
//Creating Object od Class String_Pallin.
String_Pallin p;
// Calling Member function using object.
p.getdata();
p.calculate();
p.display();
return 0;
}
Output:

Q3 Write a program to convert decimal number into their binary equivalent.

#include <iostream>
using namespace std;

class Dec_Bin
{
private:
int a[10], n, i;

public:
//Defining the getdata() function for taking input.
void getdata()
{
cout<<"Enter the number to convert: ";
cin>>n;
}
// Defining the calculate() function for convert decimal to binary.
void calculate()
{
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
}
//Defining the display() function for display result.
void display()
{
cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)
{
cout<<a[i];
}
}
};
int main()
{
//Creating Object od Class Dec_Bin.
Dec_Bin b;
// Calling Member function using object.
b.getdata();
b.calculate();
b.display();
return 0;
}

Output:

Q4 Write a program to implement Linear search using class.

#include <iostream>
using namespace std;

class LinearSearch
{
private:
int Data[20];
int n, LOC, ITEM, L,i;

public:
//Defining the getdata() function for taking input.
void getdata()
{
cout<<"Enter the Number of Elements: ";
cin>>n;
cout<<"Enter the Elements of Array: ";
for(i=1; i<=n; i++)
{
cin>>Data[i];
}
cout<<"Enter the Element to be searched: ";
cin>> ITEM;
}
// Defining the calculate() function for Linear Search in an Array.
int calculate()
{
Data[n] = ITEM;
L=0;

while(Data[L] != ITEM)
{
L= L+1;
}
if(L==n)
{
L = -1;
}
return (L);
}
//Defining the display() function for display result.
void display()
{
LOC = calculate();
if(LOC == -1)
cout<<"\n\n ITEM is not present in the Array!";
else
cout<<"'"<<ITEM <<"' is present in array at location: '"
<<LOC<<"'";
}
};
int main()
{
//Creating Object od Class LinearSearch.
LinearSearch l;
// Calling Member function using object.
l.getdata();
l.display();
return 0;
}

Output:
Q5 Write a program to implement Binary Search using Class.

#include <iostream>
#define MAX 50
using namespace std;
class BS
{
private:
int i, arr[MAX], num, first, last, middle,n;
public:
//Defining function getdata() for takink input from user.
void getdata()
{
cout<<"Enter number of Element to be Entered: ";
cin>>n;
cout<<"Enter " << n <<" Elements (in ascending order): ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\nEnter Element to be Search: ";
cin>>num;
}
//Defining calculate() function to perform binary Search.
int calculate()
{
first = 0;
last = n;
middle = (first+last)/2;
while(first <= last)
{
if(arr[middle]<num)
first = middle+1;
else if(arr[middle]==num)
{
cout<<"\nThe number, "<<num<<" found at Position "<<middle+1;
break;
}
else
last = middle-1;
middle = (first+last)/2;
}
if(first>last)
cout<<"\nThe number, "<<num<<" is not found in given Array";
cout<<endl;
return 0;
}
};
int main(void)
{
//Creating object of class BS.
BS b;
b.getdata(); // Calling member functions of class using object.
b.calculate();
return 0;
}
Output:
Q6 Write a program to implement selection sort using class.

#include <iostream>
#define MAX 50
using namespace std;
class Selection_Sort
{
private:
int i, arr[MAX], pos,temp,pass=0,n;
public:
//defining function getdata() for taking input from user.
void getdata()
{
cout<<"Enter number of Element to be Entered: ";
cin>>n;
cout<<"Enter the " << n <<" Elements that are to be Sorted one by one: \n";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
}
//defining function findSmallest for sorting the Array.
int findSmallest(int arr[],int i)
{
int ele_small,position,j;
ele_small = arr[i];
position = i;
for(j=i+1;j<10;j++)
{
if(arr[j]<ele_small)
{
ele_small = arr[j];
position=j;
}
}
return position;
}
//defining function calculate for shift the smallest element at the earliest.
void calculate()
{
for(int i=0;i<n;i++)
{
pos = findSmallest (arr,i);
temp = arr[i];
arr[i]=arr[pos];
arr[pos] = temp;
pass++;
}
}
//Defining the Display() function for showing Result.
void Display()
{
cout<<"\t\t\t\tSorted list of elements is: ";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<"\t";
} cout<<endl;
cout<<"\nNumber of passes required to sort the array: "<<pass<<endl;
}
};
int main(void)
{
//Creating object of class Selection_Sort.
Selection_Sort s;
s.getdata();
s.calculate();
s.Display(); //Calling Member function using object.
return 0;
}

Output:

Q7 Write a program to implement insertion sort using class.

#include <iostream>
#define MAX 50
using namespace std;
class Insertion_Sort
{
private:
int i, array[MAX], n;
public:
//Defining function getdata() for taking input from user.
void getdata()
{
cout<<"Enter number of Element: ";
cin>>n;
cout<<"Enter the " << n <<" Elements that are to be Sorted one by one:
\n";
for(i=0; i<n; i++)
{
cin>>array[i];
}
}
//Defining function insertionSort() for sorting element using Insertion Sort
Method.
void insertionSort()
{
for(int i=0;i<n;i++)
{
int key, j;
for(int i = 1; i<n; i++)
{
key = array[i];
j = i;
while(j > 0 && array[j-1]>key)
{
array[j] = array[j-1];
j--;
}
array[j] = key;
}
}
}
//Defining Function Display() for showing result.
void Display()
{
for(int i = 0; i<n; i++)
{
cout << array[i] << "\t";
}
cout << endl;

}
};

int main(void)
{
Insertion_Sort o; // Creating Object of Class Insertion_Sort.
o.getdata(); // Calling getdata() function using object.
//Calling Member Function Dsiplay before sorting.
cout << "\nArray before Sorting: ";
o.Display();
o.insertionSort(); //Calling Member Function insertionSort for sorting array.
cout << "\nArray after Sorting: "; // Caling Display() Member Function after
sorting.
o.Display();
return 0;
}
Output:
Q8 Write a program to implement Bubble sort using class.

#include <iostream>
#define MAX 50
using namespace std;
class Bubble_Sort
{
private:
int i, array[MAX], n;
public:
//Defining function getdata() for taking input from user.
void getdata()
{
cout<<"Enter number of Element: ";
cin>>n;
cout<<"Enter the " << n <<" Elements that are to be Sorted one by one:
\n";
for(i=0; i<n; i++)
{
cin>>array[i];
}
}
//Defining swap() Function for swaping small Element at the earliest.
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
//Defining the bubbleSort() functio for find small element.
void bubbleSort()
{
int i, j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (array[j] > array[j+1])
{
swap(&array[j], &array[j+1]);
}
}
}
}
//Defining the Display() function for showing result.
void Display()
{
int i;
for (i = 0; i < n; i++)
cout << array[i] << "\t";
cout << endl;
}
};
int main()
{
Bubble_Sort b; // Creating Object of Class Bubble Sort.
b.getdata(); // Calling getdata() function using object.
//Calling Member Function Dsiplay before sorting.
cout << "\nArray before Sorting: ";
b.Display();
b.bubbleSort(); //Calling Member Function bubbleSort for sorting array.
cout << "\nArray after Sorting: "; // Caling Display() Member Function after
sorting.
b.Display();
return 0;
}

Output:
Q9 Write a program to implement searching in linked list using class.

#include<iostream>
using namespace std;

class SearchInLL
{
private:
//Declaration of Process Function and Variables.
int List[10];
int Link[10];
int Start;
int Count = 0;
int PTR, x, LOC;

public:
//Defining ListData() Function
void ListData()
{
List[0] = 57; List[1] = 37; List[2] = 29; List[3] = 47; List[4] = 82;
List[5] = 77; List[6] = 28; List[7] = 7; List[8] = 22; List[9] = 88;
Link[0] = 9; Link[1] = 8; Link[2] = 7; Link[3] = 6; Link[4] = -1;
Link[5] = 0 ; Link[6] = 4; Link[7] = 3; Link[8] = 2; Link[9] = 1;
Start = 5;
}

// Defining the SearchProcess() funtion


int SearchProcess()
{
// Take element to be searched.
cout<<"\nEnter the Element to be searched: ";
cin>> x;
int P = Start;
int L= -1;
while(P!= -1)
{
if(x == List[P])
{
L = P;
break;
}
else
{
P = Link[P];
}
}
return(L);
}
int P = Start;
int L= -1;
while(P!= -1)
{
if(x == List[P])
{
L = P;
break;
}
else
{
P = Link[P];
}
}
return(L);
}

//Defining DisplayData() Function


void DisplayData()
{
// Calling ListData(), SearchProcess() Member Functions.
ListData();
SearchProcess();

// Traversing the Linked List Before Applying Process to it.


PTR = Start;
cout<<"\t -------Initial Linked List-------"<<endl;
while (PTR != -1)
{
cout<<"\t\t "<< List[PTR] <<" is at Index Location "<< PTR
<<"\n";
PTR = Link[PTR];
}

// Calling Search function.


LOC = SearchProcess();

if(LOC == -1)
cout<<"\n Item is not present in the list";
else
cout<<"\n'"<<x <<"' is Present at Index location '" << LOC <<"' in
the list";
}
};

int main()
{
SearchInLL s;
s.DisplayData();
}
Output:

Q10 Write a program to implement searching in doubly linked list using class.

#include<iostream>
using namespace std;

class SearchInLL
{
private:
//Declaration of Process Function and Variables.
int Info[10];
int Forw[10];
int Back[10];
int Start, Avail;
int Count = 0;
int PTR, x, LOC, c;

public:
//Defining InfoData() Function
void ListData()
{
Info[0] = 57; Info[1] = 37; Info[2] = 29; Info[3] = 47; Info[4] = 82;
Info[5] = 77; Info[6] = 28; Info[7] = 7; Info[8] = 22; Info[9] = 88;
Forw[0] = 9; Forw[1] = 8; Forw[2] = 7; Forw[3] = 6; Forw[4] = -1;
Forw[5] = 0 ; Forw[6] = 4; Forw[7] = 3; Forw[8] = 2; Forw[9] = 1;
Back[0] = 1; Back[1] = 2; Back[2] = 3; Back[3] = 6; Back[4] = 0;
Back[5] = -1; Back[6] = 7; Back[7] = 8; Back[8] = 9; Back[9] = 5;
}
// Defining the SearchProcess() funtion
int SearchProcess()
{
// Take element to be searched.
cout<<"\nEnter the Element to be searched: ";
cin>> x;
int L= -1;
cout<<"\n1: Using Forward PTR.";
cout<<"\n2: Using Backward PTR.";
cout<<"\nSelect Your Choice: ";
cin>> c;
switch(c)
{
case 1:
{
int P = 5;
while(P!= -1)
{
if(x == Info[P])
{
L = P;
break;
}
else
{
P = Forw[P];
}
}
return(L);
}
break;
case 2:
{
int P = 4;
while(P!= -1)
{
if(x == Info[P])
{
L = P;
break;
}
else
{
P = Back[P];
}
}
return(L);
break;
}
default:
cout<<"You Enter Wrong Choise!";
break;
}
}
//Defining DisplayData() Function
void DisplayData()
{
// Traversing the Linked Info Before Applying Process to it.
cout<<"\n1: Display Using Forward PTR.";
cout<<"\n2: Display Using Backward PTR.";
cout<<"\nSelect Your Choice: ";
cin>> c;
switch(c)
{
case 2:
// Calling ListData() Member Functions in DisplayData()
Function.
ListData();
Start = 4;
PTR = Start;
cout<<"\t -------Elements using Backward of Linked
List-------"<<endl;
while (PTR != -1)
{
cout<<"\t\t "<< Info[PTR] <<" is at Index Location
"<< PTR <<"\n";
PTR = Back[PTR];
}
break;
case 1:
// Calling ListData() Member Functions in DisplayData()
Function.
ListData();
Start = 5;
PTR = Start;
cout<<"\t -------Elements using Forward of Linked List-----
--"<<endl;
while (PTR != -1)
{
cout<<"\t\t "<< Info[PTR] <<" is at Index Location "<<
PTR <<"\n";
PTR = Forw[PTR];
}
break;
default:
cout<<"You Enter Wrong Choise!";
break;
}

// Calling Search function.


LOC = SearchProcess();

if(LOC == -1)
cout<<"\n Item is not present in the Info";
else
cout<<"\n'"<<x <<"' is Present at Index location '" << LOC <<"' in
the Info";
}
};

int main()
int main()
{
SearchInLL s;
s.DisplayData();
}

Output:
Q11 Write a program to implement push and pop operation in stack using class.

#include<iostream>
#define Size 4
using namespace std;
class Stack
{
private:
int Top=-1, inp_array[Size];
public:
//Defining the Puch() Function for Puch Operation.
void Push()
{
int x;

if(Top==Size-1)
{
cout<<"\nOverflow!!";
}
else
{
cout<<"\nEnter element to be inserted to the stack:";
cin>>x;
Top=Top+1;
inp_array[Top]=x;
}
}
//Defining the Pop() Function for POP Operation.
void Pop()
{
if(Top==-1)
{
cout<<"\nUnderflow!!";
}
else
{
cout<<"\nPopped element: "<<inp_array[Top];
Top=Top-1;
}
}
//Defining The Show() Function to show result.
void show()
{
if(Top==-1)
{
cout<<"\nUnderflow!!";
}
else
{
cout<<"\nElements present in the stack: \n";
for(int i=Top;i>=0;--i)
cout<<"\n"<<inp_array[i];
}
}
};
int main()
{
Stack s; //Creating object of Class Stack.
int choice;
while(1) // Ask to the user for Operation Choice.
{
cout<<"\nOperations performed by Stack";
cout<<"\n1.Push the element\n2.Pop the element\n3.Show\n4.End";
cout<<"\n\nEnter the choice:";
cin>>choice;

switch(choice)
{
case 1:
s.Push();
break;
case 2:
s.Pop();
break;
case 3:
s.show();
break;
case 4:
exit(0);

default:
cout<<"\nInvalid choice!!";
}
}
return 0;
}

Output
Output:

Q12 Write a program to implement insertion and deletion in a queue using class.

#include <iostream>
# define SIZE 100
using namespace std;
class Queue
{
private:
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
public:
//Defining the enqueue() function for insert element in the queue.
void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
cout<<"\nOverflow\n";
else
{
if (Front == - 1)

Front = 0;
cout<<"\nElement to be inserted in the Queue : ";
cin>>insert_item;
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}
//Defining the dequeue() function for deleting the element from the quque.
void dequeue()
{
if (Front == - 1 || Front > Rear)
{
cout<<"\nUnderflow\n";
return ;
}
else
{
cout<<"\nElement deleted from the Queue: "<< inp_arr[Front]<<"\n";
Front = Front + 1;
}
}
//Defining the show() function for showing result.
void show()
{
if (Front == - 1)
cout<<"\nEmpty Queue \n";
else
{
cout<<"\nQueue: \n";
for (int i = Front; i <= Rear; i++)
{
cout<<inp_arr[i];
cout<<"\n";
}
}
}
};
int main()
{
Queue q;// Creating Object of Class Queue.
int ch;
while (1)
{
cout<<"\n1.Enqueue Operation\n";
cout<<"2.Dequeue Operation\n";
cout<<"3.Display the Queue\n";
cout<<"4.Exit\n";
cout<<"Enter your choice of operations : ";
cin>>ch;
switch (ch) // Using Switch Case.
{
case 1:
q.enqueue(); //Calling enqueue() Memeber function using object.
break;
case 2:
q.dequeue(); //Calling dequeue() Memeber function using object.
break;
case 3:
q.show(); //Calling show Memeber function using object.
break;
case 4:
exit(0);
default:
cout<<"Incorrect choice \n";
}
}
return 0;
}

Output:
Q13 Write a program to implement BST.

#include <iostream>
mport java.awt.*;
using
importnamespace std;
java.awt.event.*;
class AwtSMA extends Frame implements ActionListener
struct
{ nod
{ Button sum, mul, avg;
nod *l, *r;t1,
TextField // l and
t2, t3,r are
t4; the left and right pointer of Tree.
int d; // d is used for data item.
AwtSMA()
}*r =
{ NULL, *p = NULL, *np = NULL, *q;
class BSTt1 = new TextField();
{ t1.setBounds(50, 50, 200, 30);
private:
t2 = new TextField();
int v,c = 0;
t2.setBounds(50, 100, 200, 30);
public:
t3 = new TextField();
//Defining the create
t3.setBounds(50, 150, function
200, 30);for creating Tree.
void
t4 = create()
new TextField();
{t4.setBounds(50, 200, 200, 30);
while (c < 5)
t4.setEditable(false);
sum{= new Button("Add");
if (r == NULL)
sum.setBounds(50, 250, 50, 30);
{
sum.addActionListener(this);
r = new nod;
cout<<"Enter value of root node\n";
cin>>r->d;
r->r = NULL;
r->l = NULL;
} else
{
p = r;
cout<<"\nEnter value of node\n";
cin>>v;
while(true)
{
if (v< p->d)
{
if (p->l == NULL)
{
p->l = new nod;
p = p->l;
p->d = v;
p->l = NULL;
p->r = NULL;
cout<<"\nValue entered in left\n";
break;
}
else if (p->l != NULL)
{
p = p->l;
}
}
else if (v >p->d)
{
if (p->r == NULL)
{
p->r = new nod;
p = p->r;
p->d = v;
p->l = NULL;
p->r = NULL;
cout<<"\nValue entered in right\n";
break;
}
else if (p->r != NULL)
{
p = p->r;
}
}
}
}
c++;
}
}
// Defining inorder() function for Inoreder Traversal.
void inorder(nod *p)
{
if (p != NULL)
{
inorder(p->l);
cout<<p->d<<endl;
inorder(p->r);
}
}
//Defining preorder(0 function for Pre-Order Traversal.
void preorder(nod *p)
{
if (p != NULL)
{
cout<<p->d<<endl;
preorder(p->l);
preorder(p->r);
}
}
//Defining postorder() functin for Post-Oreder Traversal.
void postorder(nod *p)
{
if (p != NULL)
{
postorder(p->l);
postorder(p->r);
cout<<p->d<<endl;
}
}
};
int main()
{
BST b;
b.create();// Creating Object of Class BST.
cout<<"\n Traversal in Inorder\n";
b.inorder(r); // Calling inorder() Member Function using Object.
cout<<"\n Traversal in Preorder\n";
b.preorder(r); // Caling preorder() Member Function using Object.
cout<<"\n Traversal in Postorder\n";
b.postorder(r); // Caling postorder() Member Function using Object.
return 0;
}

Output:
Q14 Write a program to implement AVL tree.

#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;
struct avl
{
int d;
struct avl *l;
struct avl *r;
}*r;
class avl_tree
{
public:
int height(avl *);
int difference(avl *);
avl *rr_rotat(avl *);
avl *ll_rotat(avl *);
avl *lr_rotat(avl*);
avl *rl_rotat(avl *);
avl * balance(avl *);
avl * insert(avl*, int);
void show(avl*, int);
void inorder(avl *);
void preorder(avl *);
void postorder(avl*);
avl_tree()
{
r = NULL;
}
};
int avl_tree::height(avl *t)
{
int h = 0;
if (t != NULL)
{
int l_height = height(t->l);
int r_height = height(t->r);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
int avl_tree::difference(avl *t)
{
int l_height = height(t->l);
int r_height = height(t->r);
int b_factor = l_height - r_height;
return b_factor;
}
avl *avl_tree::rr_rotat(avl *parent)
{
avl *t;
t = parent->r;
parent->r = t->l;
t->l = parent;
cout<<"\nRight-Right Rotation\n";
return t;
}
avl *avl_tree::ll_rotat(avl *parent)
{
avl *t;
t = parent->l;
parent->l = t->r;
t->r = parent;
cout<<"\nLeft-Left Rotation\n";
return t;
}
avl *avl_tree::lr_rotat(avl *parent)
{
avl *t;
t = parent->l;
parent->l = rr_rotat(t);
cout<<"\nLeft-Right Rotation\n";
return ll_rotat(parent);
}
avl *avl_tree::rl_rotat(avl *parent)
{
avl *t;
t = parent->r;
parent->r = ll_rotat(t);
cout<<"\nRight-Left Rotation\n";
return rr_rotat(parent);
}
avl *avl_tree::balance(avl *t)
{
int bal_factor = difference(t);
if (bal_factor > 1)
{
if (difference(t->l) > 0)
t = ll_rotat(t);
else
t = lr_rotat(t);
} else if (bal_factor < -1)
{
if (difference(t->r) > 0)
t = rl_rotat(t);
else
t = rr_rotat(t);
}
return t;
}
avl *avl_tree::insert(avl *r, int v)
{
if (r == NULL)
{
r = new avl;
r->d = v;
r->l = NULL;
r->r = NULL;
return r;
} else if (v< r->d)
{
r->l = insert(r->l, v);
r = balance(r);
} else if (v >= r->d)
{
r->r = insert(r->r, v);
r = balance(r);
} return r;
}
void avl_tree::show(avl *p, int l)
{
int i;
if (p != NULL)
{
show(p->r, l+ 1);
cout<<" ";
if (p == r)
cout << "\nRoot -> ";
for (i = 0; i < l&& p != r; i++)
cout << " ";
cout << p->d;
show(p->l, l + 1);
}
}
void avl_tree::inorder(avl *t)
{
if (t == NULL)
return;
inorder(t->l);
cout << t->d << " ";
inorder(t->r);
}
void avl_tree::preorder(avl *t)
{
if (t == NULL)
return;
cout << t->d << " ";
preorder(t->l);
preorder(t->r);
}
void avl_tree::postorder(avl *t)
{
if (t == NULL)
return;
postorder(t ->l);
postorder(t ->r);
cout << t->d << " ";
}
int main()
{
int c, i;
avl_tree avl;
while (1)
{
cout << "\n1.Insert Element into the tree" << endl;
cout << "2.show Balanced AVL Tree" << endl;
cout << "3.InOrder traversal" << endl;
cout << "4.PreOrder traversal" << endl;
cout << "5.PostOrder traversal" << endl;
cout << "6.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch (c)
{
case 1:
cout << "\nEnter value to be inserted: ";
cin >> i;
r = avl.insert(r, i);
break;
case 2:
if (r == NULL)
{
cout << "\nTree is Empty" << endl;
continue;
}
cout << "\nBalanced AVL Tree:";
avl.show(r, 1);
cout<<endl;
break;
case 3:
cout << "\nInorder Traversal:" << endl;
avl.inorder(r);
cout << endl;
break;
case 4:
cout << "\nPreorder Traversal:" << endl;
avl.preorder(r);
cout << endl;
break;
case 5:
cout << "\nPostorder Traversal:" << endl;
avl.postorder(r);
cout << endl;
break;
case 6:
exit(1);
break;
default:
cout << "\nWrong Choice" << endl;
}
}
return 0;
}

Output:
Q15 Write a program to implement heap sort.

include <iostream>
using namespace std;
class Heap
{
public:
void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;

while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
MaxHeapify(a, 1, i - 1);
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeapify(a, i, n);
}
};
int main()
{
Heap h;
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
h.Build_MaxHeap(arr, n-1);
h.HeapSort(arr, n-1);
cout<<"\nSorted Data ";

for (i = 1; i < n; i++)


cout<<"->"<<arr[i];

return 0;
}

Output:
Q16 Write a program to implement Dijkstra’s algorithm.

#include <iostream>
#include <climits>
#include <set>
using namespace std;
#define N 5
int minDist(int dist[], bool Set[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < N; v++)
if (Set[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSol(int dist[], int n)
{
cout<<"Vertex Distance from Source\n";
for (int i = 0; i < N; i++)
cout<<" \t\t \n"<< i<<" \t\t "<<dist[i];
}
void dijkstra(int g[N][N], int src)
{
int dist[N];
bool Set[N];
for (int i = 0; i < N; i++)
dist[i] = INT_MAX, Set[i] = false;
dist[src] = 0;
for (int c = 0; c < N- 1; c++)
{
int u = minDist(dist, Set);
Set[u] = true;
for (int v = 0; v < N; v++)
if (!Set[v] && g[u][v] && dist[u] != INT_MAX && dist[u]
+ g[u][v] < dist[v])
dist[v] = dist[u] + g[u][v];
}
printSol(dist, N);
}
int main()
{
int g[N][N] = { { 0, 4, 0, 0, 0 },
{ 4, 0, 7, 0, 0 },
{ 0, 8, 0, 9, 0 },
{ 0, 0, 7, 0, 6 },
{ 0, 2, 0, 9, 0 }};
dijkstra(g, 0);
return 0;
}
Output:

You might also like