0% found this document useful (0 votes)
2 views36 pages

Part B

The document contains multiple C++ programs demonstrating various algorithms and data structures, including GCD calculation, Tower of Hanoi, Fibonacci series, array operations (finding largest/smallest elements, sorting with selection/bubble/insertion sort), stack and queue operations, and linked list manipulations (inserting and deleting nodes). Each program includes user interaction for input and displays results accordingly. Additionally, it covers searching algorithms (linear and binary search) and the construction of a binary search tree.

Uploaded by

ameeanbee
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)
2 views36 pages

Part B

The document contains multiple C++ programs demonstrating various algorithms and data structures, including GCD calculation, Tower of Hanoi, Fibonacci series, array operations (finding largest/smallest elements, sorting with selection/bubble/insertion sort), stack and queue operations, and linked list manipulations (inserting and deleting nodes). Each program includes user interaction for input and displays results accordingly. Additionally, it covers searching algorithms (linear and binary search) and the construction of a binary search tree.

Uploaded by

ameeanbee
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/ 36

PART A

1. Program to find GCD of two numbers


#include<iostream.h>
#include<conio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void main()
{
clrscr();
int num1, num2;
cout<< "Enter two numbers: ";
cin>> num1 >> num2;
//cout<< "GCD of " << num1 << " and " << num2 << " is " <<gcd(num1,
num2);
cout<<"gcd = "<<gcd(num1,num2);
getch();
}
2. Program to implement Tower of Hanoi

#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();
}

3. Program to print Fibonacci series(using Recursion)


#include<iostream.h>
#include<conio.h>
int fibonacci(int n)
{
if (n <= 1)
{
return n;
}
else
{
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
}
int main()
{
int num,i;
clrscr();
cout<< "Enter the number of terms for Fibonacci series: ";
cin>>num;
cout<< "Fibonacci Series: ";
for(i=0;i<num;i++)
{
cout<<fibonacci(i) << " ";
}
getch();
}

4.Program to find largest and smallest element in an array.

#include<iostream.h>

#include<conio.h>

void main ()

int a[10],n,i,largest,smallest;

clrscr();

cout<<"Enter the size of the array : ";

cin>>n;

cout<<"Enter the elements of the array : "<<endl;

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

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];

cout<< "Largest element : " << largest<<endl;

cout<< "Smallest element : " << smallest<<endl;

getch();

4. Program to perform stack operation


#include<iostream.h>
#include<conio.h>
#define size 5 // Define the size of the stack
int stack[size],top=-1,i;
// Function to push an element into the stack
void push(int val)
{
if (top==size-1)
cout<< "Stack Overflow" <<endl;
else
{
top++;
stack[top]=val;
}
}

// Function to pop an element from the stack


void pop()
{
if (top<=-1)
cout<< "Stack Underflow" <<endl;
else
{
cout<< "The popped element is " << stack[top] <<endl;
top--;
}
}

// Function to display the elements of the stack


void display()
{
if (top>=0)
{
cout<< "Stack elements are: ";
for (i=top;i>=0;i--)
cout<<stack[i]<< " ";
cout<<endl;
}
else
{
cout<< "Stack is empty" <<endl;
}
}

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();
}

7.Program to insert a node at the beginning of a singly linked list.

#include<iostream.h>

#include<conio.h>

// Define a structure for a node

struct Node

int data;

Node* next;

};

// Function to insert a new node at the beginning

void insertAtBeginning(Node*& head, int val)

// Step 1: Create a new node

Node* newNode = new Node();

// Step 2: Assign value to new node

newNode->data = val;

// Step 3: Point new node to current head


newNode->next = head;

// Step 4: Update head to point to new node

head = newNode;

cout << "Node with value " << val << " inserted at the beginning.\n";

// Function to display the linked list

void display(Node* head)

if (head == NULL)

cout << "List is empty\n";

return;

Node* temp = head;

while (temp != NULL)

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL\n";


}

// Main function

void main()

Node* head = NULL; // Initialize an empty linked list

int ch,val;

clrscr();

// Display menu only once before the loop

cout << "* Linked List Operations *\n";

cout << "1. Insert a node at the beginning\n";

cout << "2. Display the linked list\n";

cout << "3. Exit\n";

do

cout << "\nEnter your choice: ";

cin >> ch;

switch (ch)

case 1:

cout << "Enter value to insert: ";

cin >> val;

insertAtBeginning(head, val);
break;

case 2:

display(head);

break;

case 3:

cout << "Exiting program...\n";

break;

default:

cout << "Invalid choice! Please try again.\n";

while (ch!=3);

getch();

8. Program to delete a node at the end of a singly linked list.

#include<iostream.h>

#include<conio.h>

// Define a structure for a node

struct Node

int data;

Node* next;

};
// Function to insert a new node at the beginning

void insertAtBeginning(Node*& head, int val)

// Step 1: Create a new node

Node* newNode=new Node();

// Step 2: Assign value to new node

newNode->data = val;

// Step 3: Point new node to current head

newNode->next = head;

// Step 4: Update head to point to new node

head = newNode;

// Function to delete a node from the end and return the deleted value

int deleteFromEnd(Node*& head)

if (head == NULL)

{ // Step 1: Check if list is empty

cout << "List is Underflow\n";

return -1;

if (head->next == NULL)

{ // Step 2: Check if only one node exists


int deletedValue = head->data;

delete head;

head = NULL;

return deletedValue;

Node* ptr = head;

Node* preptr = NULL;

while (ptr->next != NULL)

{ // Step 3: Traverse till the last node

preptr = ptr;

ptr = ptr->next;

preptr->next = NULL; // Step 6: Set second last node's next to NULL

int deletedValue = ptr->data;

delete ptr; // Step 7: Delete last node

return deletedValue;

// Function to display the linked list

void display(Node* head)

if (head == NULL)

cout << "List is empty\n";


return;

Node* temp = head;

while (temp != NULL)

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL\n";

// Main function

void main()

Node* head = NULL; // Initialize an empty linked list

int ch,val,i;

clrscr();

// Display menu only once before the loop

cout << "* Linked List Operations *\n";

cout << "1. Insert a node at the beginning\n";

cout << "2. Delete the last node\n";

cout << "3. Display the linked list\n";

cout << "4. Exit\n";

do
{

cout << "\nEnter your choice: ";

cin >> ch;

switch (ch)

case 1:

cout << "Enter value to insert: ";

cin >> val;

insertAtBeginning(head, val);

break;

case 2:

int deletedValue = deleteFromEnd(head);

if (deletedValue != -1)

cout << "Deleted element: " << deletedValue << endl;

break;

case 3:

display(head);

break;

case 4:
cout << "Exiting program...\n";

break;

default:

cout << "Invalid choice! Please try again.\n";

while (ch!= 4);

getch();

PART B

1. Program to sort an array (using selection sort)


//selection sort

#include<iostream.h>

#include<conio.h>

void main()

int n,i,j,temp,min,a[10];

clrscr();

cout<< "Enter the number of elements: ";

cin>>n;

cout<< "Enter the elements of the array: ";

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;

// Display the sorted array

cout<<"Sorted Array: ";

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

cout<<a[i]<<" ";

getch();

2. Program to sort an array (using bubble sort)


//bubble sort
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter "<<n<<" elements: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
// Bubble sort logic
for(i=0;i<n-1;i++)
{
for (j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1]) // Swap if the element is greater than the next element
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Sorted array: ";
for(i=0;i<n;i++)
{
cout<<a[i] <<" ";
}
getch();
}

3. Program to sort an array (using insertion sort)


//insertion sort
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,key;
clrscr();
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter "<<n<<" elements: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
//Insertion sort algorithm
for(i=1;i<n;i++)
{
key=a[i]; // Element to be inserted
j=i-1;
// Move elements of a[0..i-1] that are greater than key, one position ahead
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j=j-1;
}
// Insert the key in the correct position
a[j+1]=key;
}
// Output sorted array
cout<<"Sorted array: ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}
4. Program to perform linear search of an elements in an array.

#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();
}

5. Program to perform Binary Search of an element in an array.


#include <iostream.h>
#include <conio.h>
void main()
{

int a[10], n, i, key, low, high, mid;


clrscr();
cout << "Enter the number of elements: ";
cin >> n;
cout << "Enter " << n << " sorted elements:\n";
for (i = 0; i < n; i++)
{
cin >>a[i];
}
cout << "Enter the element to search: ";
cin >>key;
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (a[mid] == key)
{
cout << "Element " << key << " found at position " << (mid + 1) << endl;
break;
}
else if (a[mid] <key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if (low > high)
{
cout << "Element not found in the array.\n";
}
getch();
}

6. Program to construct a binary search tree


#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node* left;
Node* right;
Node(int new_data):data(new_data),left(NULL),right(NULL){}
};
Node* insert(Node* root,int key)
{
if(root==NULL)
{
return new Node(key);
}
if(key<root->data)
{
root->left=insert(root->left,key);
}
else
if(key<root->data)
{
root->left=insert(root->left,key);
}
else
if(key>root->data)
{
root->right=insert(root->right,key);
}
return root;
}
void inorder(Node* root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
void preorder(Node* root)
{
if(root!=NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node* root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
void freetree(Node* root)
{
if(root!=NULL)
{
freetree(root->left);
freetree(root->right);
delete root;
}
}
void main()
{
clrscr();
Node* root=NULL;
root=insert(root,50);
root=insert(root,30);
root=insert(root,70);
root=insert(root,20);
root=insert(root,40);
root=insert(root,60);
root=insert(root,80);
cout<<"inorder traversal ";
inorder(root);
cout<<endl;
cout<<"preorder traversal ";
preorder(root);
cout<<endl;
cout<<"postorder traversal ";
postorder(root);
cout<<endl;
freetree(root);
getch();
}

7. Program for Binary Tree traversal


#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node* left;
Node* right;
Node(int new_data) :data(new_data), left(NULL), right(NULL){}
};
void inorder(Node* root)
{
if (root != NULL)
{
inorder(root->left);
cout<< root->data <<" ";
inorder(root->right);
}
}
void preorder(Node* root)
{
if (root != NULL)
{
cout<< root->data <<" ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node* root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
cout<< root->data <<" ";
}
}
void freetree(Node* root)
{
if (root != NULL)
{
freetree(root->left);
freetree(root->right);
delete root;
}
}
void main()
{
clrscr();
Node* root=new Node(1);
root->left=new Node(2);
root->right=new Node(3);
root->left->left=new Node(4);
root->left->right=new Node(5);
root->right->right=new Node(6);
cout<<"inorder traversal ";
inorder(root);
cout<<endl;
cout<<"preorder traversal ";
preorder(root);
cout<<endl;
cout<<"postorder traversal ";
postorder(root);
cout<<endl;
freetree(root);
getch();
}
8. Program to implement DFS
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
void main()
{ clrscr();
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"DFS ORDER OF VISITED VERTICES:";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}
9. Program to implement BFS
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
void main()
{
clrscr();
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"Visitied vertices:";
cout <<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=1; j<=n; j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v <<" ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}

DATA STRUCTURES VIVA QUESTIONS


1Q) What is a Data Structure?
Ans) A
Data Structure
is a data object together with the relationships that exists among theinstances & among the
individual elements that compose an instance.
2Q) Types of Data Structures and give examples?
Ans) There are two types of Data Structures:1.

Linear Data Structures:


A data structure is said to be linear if the elements form asequence. It is sequential and continues
in nature i.e. access the data in sequentialmanner.In linear data structure we can not insert an
item in middle place and it maintains a linearrelationship between its elementsegs: Array, Linked
list, Stack, Queue, Dequeue etc.2.

Non Linear Data Structures:


A data structure is said to be non-linear if elements do notform a sequence. (Not sequential).
It does not maintain any linear relationship between their elements. Every data item is attached toseveral other data
items in a way that is specific for reflecting relationships. The data items arenot arranged in a sequential structure.
egs: Trees, Graphs.
[A data structure is linear if every item is related with next and previous item and it is nonlinear if it is attach
with many of the items in specific ways to reflect relationship.]3Q) What is a Singly Linked List?
Ans) Singly Linked List is a Sequence of dynamically allocated Storage elements, eachelement
of which contains a pointer to its successor. A pointer to the first element of thelist is called as
head and a pointer to the last element of the list is called as tail used tokeep track of the list
elements.
4Q) What is Doubly Linked List?
Ans) In Doubly Linked List each element contains two pointers: One Pointer points
to itssuccessor and another to its predecessor (previous element).It is also called as
two waylinked list (traversing can be done in both directions).

5Q) Differentiate Array and Linked List?


Ans)
STACKS: (LIFO DATA STRUCTURE)
6Q) What is a Stack? (LIFO Data Structure)
Ans) Stack is an ordered collection of items into which items can be inserted and deleted from
only one end called as “Top” of the
Stack. It is also called as LIFO list.(Last In FirstOut).
7Q) What is Stack Underflow?
Ans) Is Stack is empty and POP operation is performed it is not possible to delete the items.This
situation is called Stack Underflow.
8Q) What is Stack Overflow?
Ans) If Stack is full and PUSH operation is performed it is not possible to insert or Push thenew
items into the stack. This situation is called Stack Overflow.
9Q) What are the Applications of Stack?
Ans) i) Stacks are used to convert Infix expression into Postfix.ii) Stacks are used to Evaluate
Postfix Expression.iii) Stacks are used in recursion etc.
10Q) What is the use of Postfix expressions?
Ans) Postfix Expressions are easy to evaluate as postfix expressions does not make
use ofoperator precedence not does it require the use of parenthesis.
Array Linked List
1.Size of the array is fixed 1.Size of the linked list is not fixed2. Memory is allocated Statically
(or)Dynamically (at run time).(If the memory is allocated for an arrayStatically(at compile time)
it is called StaticArray and if memory is allocated at runtime (dynamically)using operator new it
iscalled Dynamic Array)2. Memory is allocated dynamically (atruntime).3.Memory wastage will
be there if all thearray positions are not utilized3.Memory is not wasted as only Requiredmemory
is allocated

QUEUES: (FIFO DATA STRUCTURE)


11Q) What is a Queue?
Ans) It is an ordered collection of items into which items can be inserted from one end calledas
REAR end and items are deleted from other end called as FRONT end of the Queue. Itis also
called as FIRST IN FIRST OUT (FIFO) LIST).
12Q) What are the applications of Queues?
Ans) i) Queues are used in Breadth First Traversal of a Tree.ii) Queues are used in
implementation of Scheduling algorithms of Operating Systems.
13Q) What is a Circular Queue?
Ans) In Circular Queue, the first position of the array is kept behind the last position of thearray.
14Q) Differentiate Linear Queue and Circular Queue?
Ans) In Linear Queue once the queue is full and the deletion is performed, even if first positionis
free(vacant) it is not possible to insert the item in that position whereas in CircularQueue it is
possible since the first position is kept behind the last position.
15Q) What is Dequeue? (Double Ended Queue)
Ans) In Double Ended Queue insertion and deletion are possible from both the ends.
TREES:

16Q) What is a Tree?


Ans) Tree is a finite non-empty set of nodes with the following properties:i)

A designated node of the set is called as root of the tree andii)

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.

[Note: In a general tree degree of a node is not fixed]


Nodes that have degree zero are called
Leaf or Terminal Nodes
. Consequently, the other nodesare referred to as
Non-Terminals.
The
Level of a node
is defined by letting the root be at level o or 1.The
height or depth of a tree
is defined to be the maximum level of any node in the tree.

17Q) What is a Binary Tree?


Ans) A Binary tree T is a finite set of nodes with the following properties:i)

Either the set is empty, T=Ø orii)

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)

Depth First Traversals


: PREOREDER (N L R) ,INORDER (L N R) &POSTORDER (L R N) Traversals.ii)

Breadth First Traversal


(or)
Level Order Traversal
(Visiting Level bylevel from left to right)
19Q) What is a Binary Search Tree? Give one example?
Ans) A Binary Search Tree T is a finite set of keys. Either the set is empty T=Ø ,or the set
consists of a root “r” and exactly two binary search trees TL and TR,T = {r,TL,TR} with
the following properties:i)

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).

21Q) What is an AVL Search Tree? What is AVL Balance Condition?


Ans) An AVL Search Tree is a balanced binary search tree. An empty binary tree is
AVL balanced. A non

empty binary tree, T={r,TL,TR} is AVL balanced if both TL & TR areAVL balanced and | h
L


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)

V = V' [The vertices in a graph and spanning tree are same]ii)

T is Acyclic.iii)

T is connected.
[Note: I f Graph has “n” vertices the
Spanning
Tree contains exactly “n
-
1” edges]

32Q) Name the methods to construct a Minimum cost Spanning Tree?


Ans)
Prim’s method and Kruskal’s method.

SORTING & SEARCHING:


33Q) What is Linear Search? What is the time complexity of searching an item in a listusing linear
search?
Ans) In linear search the item to be searched is compared with each item starting with the itemin
the first position of the array until the item is found or all the items of the array. Thetime
complexity of linear search in Worst case is
O(n).34Q) What is Binary Search method? What is the time complexity?
Ans) Binary Search method can be used only if the list is Sorted. In binary search method firstthe
array is divided in to two by finding the mi position of the array and the item to besearched is
compared with the mid value.
If the item to be searched is less than the mid value, the item is searched in the leftsubarray.

If the item is larger than the mid value it is searched in the right sub array recursively.

Otherwise it equals to the mid value and search is successful.


The time complexity of Binary Search method is


O(log n).

35Q) What is Sorting?


Ans) Arranging the elements in (ascending or descending) some particular order is calledSorting.
36Q) Study the procedure of following Sorting techniques?
Ans) i) MERGE SORT ii) QUICK SORT iii) INSERTION SORT iv) SELECTION SORTv)
HEAP SORT
37Q) Time complexities of Sorting Techniques:
SORTINGTECHNIQUEBEST CASE AVG CASE WORST CASE
INSERTION SORT O(n) O(n
2
) O(n
2
)SELECTION SORT O(n
2
) O(n
2
) O(n
2
)MERGE SORT O(n log n) O(n log n) O(n log n)QUICK SORT O(n log n) O(n log n) O(n
2
)HEAP SORT O(n log n) O(n log n) O(n log n)[NOTE: If the given array is already sorted then
Insertion Sort technique is best to sort thegiven sequence.]
Study the procedures of all the sorting methods.
38Q) Comparison between Quick Sort and Heap Sort?
Ans) If the size of the array to be sorted is very large the Heap sort is efficient than Quick
sortsince in worst case the time complexity of Heap sort remains same as O (n log n) butQuick
Sort time complexity changes to O (n
2
).But if the size of the Array is small thenQuick sort is efficient than Heap sort.

You might also like