Part B
Part B
#include<iostream.h>
#include<conio.h>
void towerOfHanoi(int n, char S, char D, char T)
{
if (n == 1)
{
cout<< "Move disk 1 from " << S << " to " << D <<endl;
return;
}
towerOfHanoi(n - 1, S, T, D);
cout<< "Move disk " << n << " from " << S << " to " << D <<endl;
towerOfHanoi(n - 1, T, D, S);
}
void main()
{
clrscr();
int n;
cout<< "Enter number of disks: ";
cin>> n;
towerOfHanoi(n, 'A', 'C', 'B');
getch();
}
#include<iostream.h>
#include<conio.h>
void main ()
int a[10],n,i,largest,smallest;
clrscr();
cin>>n;
cin>>a[i];
largest= a[0];
for(i=0;i<n;i++)
if(largest<a[i])
largest=a[i];
}
smallest=a[0];
for(i=0;i<n;i++)
if(smallest>a[i])
smallest=a[i];
getch();
void main()
{
int ch, val;
clrscr();
cout<< "Stack Operations: \n1) Push in stack \n2) Pop from stack \n3)
Display stack \n4) Exit" <<endl;
do
{
cout<< "Enter choice: ";
cin>>ch;
switch (ch)
{
case 1:
cout<< "Enter value to be pushed: ";
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);
getch();
}
5. Program to perform operations on linear Queue.
#include<iostream.h>
#include<conio.h>
#define size 5
int queue[size],front=-1,rear=-1,i;
void enqueue(int val)
{
if (rear==size-1)
{
cout<<"Queue Overflow"<<endl;
}
else
{
if(front==-1)
front=0;
rear++;
queue[rear]=val;
}
}
void dequeue()
{
if(front==-1|| front>rear)
{
cout<<"Queue Underflow"<<endl;
}
else
{
cout<<"The dequeued element is "<< queue[front]<<endl;
front++;
if(front>rear)
{
front=rear=-1;
}
}
}
void display()
{
if(front==-1)
{
cout<<"Queue is emty"<<endl;
}
else
{
cout<<"Queue elements are:";
for(i=front;i<=rear;i++)
{
cout<<queue[i]<<" ";
}
cout<<endl;
}
}
void main()
{
int ch,val;
clrscr();
cout<<"Queue operations:\n1) Enqueue\n2) Dequeue \n3) Display Queue\
n4) Exit"<<endl;
do
{
cout<<"Enter choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter value to be enqueued:";
cin>>val;
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
default:
cout<<"Invalid choice"<<endl;
}
}
while(ch!=4);
getch();
}
#include<iostream.h>
#include<conio.h>
struct Node
int data;
Node* next;
};
newNode->data = val;
head = newNode;
cout << "Node with value " << val << " inserted at the beginning.\n";
if (head == NULL)
return;
temp = temp->next;
// Main function
void main()
int ch,val;
clrscr();
do
switch (ch)
case 1:
insertAtBeginning(head, val);
break;
case 2:
display(head);
break;
case 3:
break;
default:
while (ch!=3);
getch();
#include<iostream.h>
#include<conio.h>
struct Node
int data;
Node* next;
};
// Function to insert a new node at the beginning
newNode->data = val;
newNode->next = head;
head = newNode;
// Function to delete a node from the end and return the deleted value
if (head == NULL)
return -1;
if (head->next == NULL)
delete head;
head = NULL;
return deletedValue;
preptr = ptr;
ptr = ptr->next;
return deletedValue;
if (head == NULL)
temp = temp->next;
// Main function
void main()
int ch,val,i;
clrscr();
do
{
switch (ch)
case 1:
insertAtBeginning(head, val);
break;
case 2:
if (deletedValue != -1)
break;
case 3:
display(head);
break;
case 4:
cout << "Exiting program...\n";
break;
default:
getch();
PART B
#include<iostream.h>
#include<conio.h>
void main()
int n,i,j,temp,min,a[10];
clrscr();
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
// Selection sort
for(i=0;i<n-1;i++)
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
}
// Swap the found minimum element with the first element
temp=a[i];
a[i]=a[min];
a[min]=temp;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
#include <iostream.h>
#include <conio.h>
void main()
{
int a[10], n,i,key;
clrscr();
cout <<"Enter the number of elements: ";
cin >> n;
cout << "Enter " << n << " elements:\n";
for (i = 0; i < n; i++)
{
cin >>a[i];
}
cout << "Enter the element to search: ";
cin >>key;
for (i = 0; i < n; i++)
{
if (a[i] == key)
{
cout << "Element " << search << " found at position " << i << endl;
break;
}
}
if (i == n)
{
cout << "Element not found in the array.\n";
}
getch();
}
The remaining nodes are partitioned into n>=0 subsets, each of which is a tree.
Degree of a node:
The number of sub trees attached to a node is called degree of that node andthe maximum degree of any node in a
tree is called
degree of that tree.
The set consists of a root and exactly two distinct binary trees TL andTR,T={r,TL,TR}.TL is the
left subtree and TR is the right subtree of T.
[Note: Maximum degree of any node in a binary tree is 2.Degree of a node in aBinary Tree be either 0 or 1 or
2]18Q) What is Tree Traversal? List different Tree Traversal Techniques?
Ans) Visiting all nodes of a tree exactly once in a systematic way is called
Tree Traversal.Different types of tree traversals arei)
All the keys contained in the left subtree are less than the root key.ii)
All the keys contained in the right subtree are larger than the root key.[Note: Duplicates are not
allowed in a Binary Search Tree]
20Q) What is the best, average and worst case time complexity of insertion, deletion and
Search operations in a Binary Search Tree?Ans) In Best and Avg case
O(log n)
and in Worst case
O(n).
–
h
R
| <=1,Where : h
L
is the Height of the left subtree and h
R
is the Height of the right subtree.
[
Note
: Allowable balance factors of any node is an AVL tree are 0 or 1 or -1 and the use of balancing
a binary search tree is even in worst case the time complexity of insert, delete andsearch
operations is reduced to
O(log n)
from
O(n)
]
22Q) What is a Full (perfect) Binary Tree?
Ans)
If a binary tree of height ‘h’ has exactly
[2
h+1
-1]
nodes then that binary tree is called asFull Binary Tree.
23Q) What is a Complete Binary Tree?
Ans) Complete Binary Tree is a binary tree T = {r,TL,TR} with the following properties:
If “i” is the index of any node in a complete binary tree then:
i)
The parent of “i” is at position “i/2” [if i=1 it is the root node and has no parent]
ii)
The left child of node “i” is at position “2i” [if 2i>n then no left child exists]
iii)
The right child of node “i” is at position “2i+1” [if 2i+1>n then no right child
exists]
[Note: In a complete binary tree nodes are filled level by level from left to
right]24Q) List one application of trees (Search trees)?
Ans) Search trees are used to implement dictionaries.
PRIORITY QUEUES:
25Q) What is Priority Queue and differentiate Priority Queue and Queue?
Ans) In Priority Queue each item is associated with a priority. In Priority Queue items can be
inserted arbitrary order but the items are deleted based upon the priority that is the item with
highest priority is deleted first. Whereas in Queue the items are inserted from rear end and
deleted from front end and the item which is inserted first is deleted first (FIFO).
26Q) What is a Min Heap?
Ans) Min Heap is a Complete Binary Tree in which the key value at parent is always less thanor
equal to its child values.
27Q) What is a Max Heap?
Ans) In Max Heap the key value at parent is always larger or equal to its child values.
28Q) What is a (Binary) Heap?
Ans) A (Binary) Heap is a Complete Binary Tree with Heap Ordered Property (Min Heap orMax
Heap)
[Note: Duplicates are allowed in a Heap]
GRAPHS:
29Q) What is a Graph? Name various Graph Representation Techniques?
Ans) A Graph G = (V,E) is Set of Vertices and Edges.A Graph can be represented as an
Adjacency Matrix (or) as an Adjacency List.
30Q) What is difference between a Graph and a Tree?
Ans) A Graph contains Cycles (loops) but a Tree does not contain any cycles.[A Tree contains a
root node but Graph does not contain the root node.]
31Q) What is a Spanning Tree?
Ans) A Spanning Tree T = (V', E') is a Sub Graph of G = (V,E) with following properties:i)
T is Acyclic.iii)
T is connected.
[Note: I f Graph has “n” vertices the
Spanning
Tree contains exactly “n
-
1” edges]
If the item is larger than the mid value it is searched in the right sub array recursively.