You are on page 1of 13

SET 1

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE


MID SEMESTER - 2 EXAMINATION
18AIC203J - DATA STRUCTURES AND ALGORITHMS

Year/Semester & Branch: II/III & AI&D Date: 15.11.2022


Faculty Name: Ms.A.Nithyasri Max. Marks: 100 Time: 1:30PM – 4:30PM

PART-A Answer ALL Questions (10x2=20 Marks)


1. How do you find depth and height of the tree? Give example.
For any node n, the depth of n is the length of the unique path from the root to node n.
Thus, for a root the depth is always zero. CO3 K1
The height of n is the length of the longest path from root to a leaf. Thus, all leaves have
height zero. The height of a tree is equal to a height of a root.
Why it is said that searching a node in a binary search tree is efficient than that of a simple
binary tree?
2. CO3 K2
Binary Search Tree allows for fast retrieval of elements stored in the tree as each node key
is thoroughly compared with the root node, which discards half of the tree.
Define AVL Tree.
AVL stands for Adelson-Velskii and Landis. An AVL tree is a binary search tree which has
the following properties:
3. CO3 K1
1.The sub-trees of every node differ in height by at most one.
2.Every sub-tree is an AVL tree.
Search time is O(logn). Addition and deletion operations also take O(logn) time.
What do you mean by structure property in heap?
A binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last
one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of
that level are filled from left to right.

4. CO3 K1

Show the result of inorder traversal of the binary search tree given in figure.

5. CO3 K3

INORDER TRAVERSAL : 1 2 3 4 5 6 7 9
SET 1

Compare linear and binary search algorithm techniques.

Basis of Linear search Binary search


comparison

Definition The linear search starts searching from the It finds the position of the searched
first element and compares each element element by finding the middle element
with a searched element till the element is of the array.
not found.

Sorted data In a linear search, the elements don't need to The pre-condition for the binary search
be arranged in sorted order. is that the elements must be arranged
in a sorted order.
6. CO4 K2
Implementation The linear search can be implemented on The implementation of binary search is
any linear data structure such as an array, limited as it can be implemented only
linked list, etc. on those data structures that have two-
way traversal.

Approach It is based on the sequential approach. It is based on the divide and conquer
approach.

Size It is preferrable for the small-sized data sets. It is preferrable for the large-size data
sets.

Efficiency It is less efficient in the case of large-size It is more efficient in the case of large-
data sets. size data sets.

What do mean by heap?


A heap is a complete binary tree, and the binary tree is a tree in which the node can have
atmost two children. It is a specialized tree-based data structure which is essentially an
7. almost complete tree that satisfies the heap property: in a max heap, for any given node C, CO4 K1
if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of
C. In a min heap, the key of P is less than or equal to the key of C. The node at the "top" of
the heap (with no parents) is called the root node.
Recall internal and external sorting?
Internal sorting: If the input data is such that it can be adjusted in the main memory at once,
8. it is called internal sorting. CO4 K1
External sorting: If the input data is such that it cannot be adjusted in the memory entirely at
once, it needs to be stored in a hard disk, floppy disk, or any other storage device.
Mention the collision handling techniques.
The Process of finding another Position for The Collide Record Is said to be collision
Resolution Strategy.
• Two categories of Hashing .
9. 1. Open Hashing CO4 K1
eg: Separate Chaining.
2. Closed Hashing .
eg : Open Addressing ,Rehashing and
Extendable hashing.
10. Showing the result of sorting the following numbers using radix sort. (step by step o/p). 4, CO4 K3
7, 3, 2, 5, 1, 6
SET 1

PART-B Answer ALL Questions (5x16=80 Marks)


Write the routines for tree traversal. Give the preorder, postorder and inorder
traversals for the following.
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
// Preorder traversal
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
cout << node->data << "->";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
// Postorder traversal
void postorderTraversal(struct Node* node) {
if (node == NULL)
return;
postorderTraversal(node->left);
11.
postorderTraversal(node->right); CO3 K3
(a)
cout << node->data << "->";
}
// Inorder traversal
void inorderTraversal(struct Node* node) {
if (node == NULL)
return;
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
Problem Ans.

Inorder : 1 2 3 4 12 13 14 18
Preorder : 13 3 1 2 4 12 14 18
Post order : 2 1 12 4 3 18 14 13
SET 1

(or)
11. Write the following routines to implement the basic binary search tree operations. CO3 K1
(b) (i) Perform Insertion and Deletion operation in binary search tree. (10)
Procedure for Insertion into a binary search tree
Searchtree insert(elementtype X, Searchtree T)
{
if(T== NULL) {
T=malloc(sizeof(structtreenode));
if(T==NULL)
fatalerror(􀇲Out of Space􀇲);
else {
T-->element=X; T-->left=T-->right=NULL;
}
}
else if(x<T-->element) T-->left=insert(X,T-->left);
else if(X>=T-->left) T-->right=insert(X,T-->right);
return T;}
Deletion routine for binary search trees
Searchtree delete(elementtype X, searchtree T) {
positiontmpcell;
if(T==NULL) error(“element not found”);
else if(X<T-->element) T-->left=delete(X,T-->left);
else if(X>T-->element) T-->right=delete(X,T-->right);
else if(T-->left != NULL && T-->right!=NULL) {
tmpcell=findmin(T-->right);
T-->element=tmpcell-->element;
T-->right=delete(T-->element,T-->right);
} else
{
tmpcell=T; if(T-->left==NULL)
T=T-->right;
else if(T-->right==NULL) T=T-->left;
Free(tmpcell);
}
return T; }
(ii) Find_min and Fin_max. (6)
// Finding Minimum Position
findmin(searchtree T) {
if(T==NULL)
return NULL;
else if(T-->left==NULL) return T;
else
return findmin(T-->left);
}
SET 1

// Finding Maximum Position


findmax(searchtree T) {
if(T==NULL)
return NULL;
else if(T-->right==NULL) return T; else
return findmax(T-->right);
}
i) Derive expression tree and write the procedure for constructing an expression tree.
(8)
Expression tree is a binary tree in which Leaf nodes are operands and internal nodes
are operators.
For constructing an expression tree by performing the following steps :
1. Convert the given expression into postfix form
2. Read one symbol at a time from the postfix expression.
3. Check whether the symbol is an operand or operator.
i. If the symbol is an operand, create a one node tree and push a pointer on to the
stack.
ii. If the symbol is an operator, pop two pointers from the stack namely, T1 and T2
and form a new tree with root as the operator, and T2 as the left child and T1 as the
right child.
iii. A pointer to this new tree is then pushed on to the stack.
12. ii) Construct an expression tree for the expression (a+b*c) + ((d*e+f)*g). Give the
CO3 K2
(a) outputs when you apply inorder, preorder and postorder traversals. (8)

INORDER : (a+b*c) + ((d*e+f)*g)


PERORDER : ++a*bc*+*defg
POSTORDER : abc*+de*f+g*+
(or)
12. Write a routine for AVL tree insertion and insert the following elements in the empty CO3 K3
(b) tree and how do you balance the tree after each element insertion?
14, 8, 12, 46, 23, 5, 77, 88, 20.
Insertion Routine:
avltree insert(elementtype X, avltree T)
{
if(T==NULL)
{ / * Create and return a one nodetree*/
T= malloc(sizeof(structavlnode));
if(T==NULL)
fatalerror(“Out of Space”);
else
SET 1

{
T-->element=X;
T-->height=0;
T-->left=T-->right=NULL;
}
}
else if(X<T-->element)
{
T-->left=Insert(X,T-->left);
if(height(T-->left) -height(T-->right)==2)
if(X<T-->left-->element)
T=singlerotatewithleft(T);
else
T=doublerotatewithleft(T);
}
else if(X>T-->element)
{
T-->right=insert(X,T-->right);
if(height(T-->left) - height(T-->right)==2)
if(X>T-->right-->element)
T= singlerotatewithright(T);
else
T= doublerotatewithright(T);
}
T-->height=max(height(T-->left),height(T-->right)) + 1; Return T;
}
SET 1

Create a B Tree of order 5 by inserting the following elements: 3, 14, 7, 1, 8, 5, 11,


17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25 and 19

13.
CO3 K3
(a)

(or)
SET 1

Discuss in detail about Min heap and Max heap and build the max heap for the
following sequence 80, 150, 70, 40, 100, 20, 30, 10, 110, 90, 60, 50, 120. Show the
result of sorting using delete max.

13.
CO3 K3
(b)
SET 1

Write a routine for Insertion sort. Sort the following sequences using Insertion sort. 3,
10, 4, 2, 8, 6, 5, 1. (8)

Sort the following list of numbers using bubble sort technique 52, 1, 27, 85, 66, 23,
14. 13, 57. (8)
CO4 K3
(a)
SET 1

(or)
14. i) Distinguish between linear search and binary search. State and explain the routines CO4 K2
(b) for both the search with example. (8)
SET 1

Linear Search:
int search(int array[], int n, int x)
{
for (int i = 0; i < n; i++)
if (array[i] == x)
return i;
return -1;
}
Binary Search:
int binarySearch(int array[], int x, int low, int high)
{
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
ii) State and explain the shell sort. Write the routine for shell sort and the following
elements to be sort using shell sort. 25, 57, 48, 37, 12, 92, 86, 33. (8)
int shellSort(int arr[], int n)
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
SET 1

Explain in detail about selection sort and radix sort. sort the following using it.
70,30,20,50,60,10,40.
Selection Sort:
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if(min_idx!=i)
swap(&arr[min_idx], &arr[i]);
}

15.
CO4 K3
(a)

Radix Sort:
void radixsort(int arr[], int n)
{
int exp, m;
m = getMax(arr, n);
// Calling countSort() for digit at (exp)th place in every input.
for (exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}

(or)
SET 1

Consider a hash table with 9 slots. The hash function is h(k) = K mod 9.
The following keys are inserted in the order 5, 28, 19, 15, 20, 33, 12, 17, 10. Draw the
contents of the hash table when the collision is resolved by
i) Chaining
ii) Linear probing
iii) Double hashing. The second hash function h2(x) = 7 – (x mod 7)

15 Linear probing
CO4 K3
(b)

Knowledge Remember (K1), Understand (K2), Apply (K3), Analysis (K4), Evaluate (K5) & Design
Level (K) (K6)

*****

Verified by Approved By

You might also like