Solution Set Data Structures
Solution Set Data Structures
Q1. (a) Perform the insertion sort on the array {8,2,1,9,3}, show the steps after each iteration. Also, report the number of
comparisons. (4)
8,2,1,9,3 1 comparison
2,8,1,9,3 2 comparisons
3 marks
1,2,8,9,3 1 comparison
1,2,8,9,3 3 comparisons
1,2,3,8,9
Total comparisons= 7 1 mark
(b) Explain the properties of a binary heap. How is it different from a binary search tree. (4)
A Binary Heap is either Min Heap or Max Heap. In a Min Binary Heap, the key at the root must be minimum among all
keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree. In a Similarly, in
a Max Binary Heap, the key at the root must be maximum among all keys present in Binary Heap.
It is different from a Binary Search Tree as a BST has the following properties:
• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
Properties of binary heap- 2 marks
Difference from BST- 2 marks
(c) Differentiate between the following: (4)
i. Arrays and Linked list: (1 mark for each difference) - 2Marks
An array is a collection of elements of a similar data type. A linked list is a collection of objects known as a node where node consists of two
parts, i.e., data and address.
Array elements store in a contiguous memory location. Linked list elements can be stored anywhere in the memory or randomly stored.
Array works with a static memory. Here static memory means that The Linked list works with dynamic memory. Here, dynamic memory means that
the memory size is fixed and cannot be changed at the run time. the memory size can be changed at the run time according to our requirements.
Array takes more time while performing any operation like insertion, Linked list takes less time while performing any operation like insertion, deletion,
deletion, etc. etc.
(e) Draw a binary search tree using the following values; 16, 7, 23, 22, 14, 15 (4)
(f) What are the different operations that can be performed on a Dequeue. Explain using an example. (4)
2 marks- example
(g) What do you understand by a height-balanced tree? Explain using example. (3)
A height-balanced binary tree is defined as a binary tree in which the height of the left and the right subtree of any
node differ by not more than 1.
For example:
Consider node v1 in the tree given below. Height of left subtree of node v1 is 2 and height of right subtree of node v1 is
1. Therefore, the difference in the heights of left subtree and right subtree of node v1 is 1.
Similarly, difference in the heights of left and right subtree of node v2 is 0.
Hence, the given tree is a height- balanced tree.
Definition – 1 mark
Example- 2 marks
(h) ‘Stacks play a role in the implementation of recursion’. Justify the statement using suitable example. (3)
“Recursion" is technique of solving any problem by calling same function again and again until some breaking (base)
condition where recursion stops and it starts calculating the solution from there on. For eg. calculating factorial of a given
number. Thus, in recursion last function called needs to be completed first. Now Stack is a LIFO data structure i.e. (Last In
First Out) and hence it is used to implement recursion.
Q2. Each part of 3 marks each
Q3. (a) What is Binary Recursion? Write a program in C++ for computing Fibonacci numbers via Binary Recursion. (6)
When an algorithm makes two recursive calls, we say that it uses binary recursion.
#include <iostream>
using namespace std;
int fib(int x) {
if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
}
int main() {
int x , i=0;
cout << "Enter the number of terms of series : ";
cin >> x;
cout << "\nFibonnaci Series : ";
while(i < x) {
cout << " " << fib(i);
i++;
}
return 0;
}
Definition – 2 marks
Program code- 4 marks
(b) Write a program in C++ for performing a push operation on a stack implemented using linked list. (5)
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* link;
Node(int n)
{
this->data = n;
this->link = NULL;
}
};
class Stack {
Node* top;
public:
Stack() {top = NULL; }
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* prev;
};
void deleteNode(Node** head_ref, Node* del)
{
if (*head_ref == NULL || del == NULL)
return;
if (*head_ref == del)
*head_ref = del->next;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free(del);
return;
}
Q4. (a) Consider the following sequence of operations performed on an initially empty doubly linked list: (6)
InsertBeginning (10),InsertBeginning (5),InsertEnd(7),InsertEnd(2),DeleteBeginning(),Deletenode(2)
Show the contents of the list, links between the nodes, head and tail after each operation.
(b) What is an abstract data type? Differentiate between Stack and Queue with the help of a suitable example. (4)
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of values and a set of
operations. The definition of ADT only mentions what operations are to be performed but not how these operations will
be implemented. It is called “abstract” because it gives an implementation-independent view.
A stack follows a LIFO (Last In First Out) order, whereas a queue follows a FIFO (First In First Out) order for storing the
elements. A stack uses one end known as a top for insertion and deletion whereas a queue uses two ends front and rear for
insertion and deletion.
ADT definition- 1 mark
Difference between stack and queue- 3 marks
4 marks
(b) Give the asymptotic analysis for the Big-O notation using suitable example. (5)
(c) Write any two real-life applications each of stack and queue. (4)
Real-life applications of stack
• Back and forward buttons in a web browser.
• UNDO/REDO functionality in text editors and image editing software
• Implementing recursion in programming
Real-life applications of queue
• Managing requests on a single shared resource such as CPU scheduling and disk scheduling.
• Handling hardware or real-time systems interrupts.
• Handling website traffic.
Q6. (a) For each of the following trees, specify whether it is a binary search tree or not. Give reasons for your
answers. (6)
i) Not a BST (Value of left child of node 11 is 12 which is more than 11)
ii) It is a BST (Value of left child of each node is less than the value of the node and value of right child
of the node is more than the value of the node)
iii) Not a BST (Node with value 23 has three children which is not possible)
(b) Consider the following sequence of operations performed on a stack of size 5. Show the contents of the stack
after each operation. (5)
push (10), push (5), pop (), push (2), push (16), push (12), push (22), push (6), pop ()
(c) Write a C++ program to sum ‘n’ number of elements of an array using a recursive function. (4)
int findSum(int A[], int N)
{
if (N <= 0)
return 0;
return (findSum(A, N - 1) + A[N - 1]);
}
int main()
{
int A[] = { 1, 2, 3, 4, 5 };
int N = sizeof(A) / sizeof(A[0]);
printf("%d", findSum(A, N));
return 0;
}
Q7. (a) What do you understand by the Recursion-tree method for solving recurrences. Draw a Recursion tree for
𝑛 2𝑛
the recurrence 𝑇(𝑛) = 𝑇 ( ) + 𝑇 ( ) + 𝑐𝑛 (6)
3 3
Recursion Tree Method is a pictorial representation of an iteration method which is in the form of a tree where at each level
nodes are expanded. In general, we consider the second term in recurrence as root.
It is useful when the divide & Conquer algorithm is used. In Recursion tree, each root and child represents the cost of a
single subproblem. We sum the costs within each of the levels of the tree to obtain a set of pre-level costs and then sum all
pre-level costs to determine the total cost of all levels of the recursion.
A Recursion Tree is best used to generate a good guess, which can be verified by the Substitution Method.
Recursion-tree method- 3marks
Recursion-tree construction- 3 marks
(b) Explain Master’s theorem for solving recurrences giving suitable example. (5)
(c) Write a C++ program to insert an element at the front of a singly linked list. (4)
#include<iostream>
using namespace std;
class node{
public:
int data;
node*next;
node(int d);
data=d;
next= NULL;
}
};
void insertAthead(node*&head, int data){
node*n= new node(data);
n->next= head;
head= n;
}
void print(node*head){
while(head!=NULL){
cout<<head->data<<"->";
head= head->next;
}
}
int main(){
node*head= NULL;
insertAthead(head,5);
insertAthead(head,2);
insertAthead(head,8);
insertAthead(head,3);
print(head);
}