Professional Documents
Culture Documents
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
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.
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
{
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
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