Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Bcsl 033 solve assignment

663

Published on

Bcsl 033 solve assignment …

Bcsl 033 solve assignment

IGNOU University

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
663
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Course Code : BCSL-033 Course Title : Data and File Structures Lab Assignment Number : BCA(III)-033/Assign/13 Maximum Marks : 50 Weightage : 25% Last Dates for Submission : 15th October,2013 (For July 2013 Session) 15th April, 2014 (For January 2014 Session) This assignment has two questions, each of 20 marks.10 marks are for viva-voce. Attach input and output of the program to the assignment. Write programs in ‘C’ language. Question 1: (20 marks) Write algorithm and program for the conversion of a Tree to a Binary Tree. Ans :Following is a 3 step solution for converting Binary tree to Binary Search Tree. 1) Create a temp array arr[] that stores inorder traversal of the tree. This step takes O(n) time. 2) Sort the temp array arr[]. Time complexity of this step depends upon the sorting algorithm. In the following implementation, Quick Sort is used which takes (n^2) time. This can be done in O(nLogn) time using Heap Sort or Merge Sort. 3) Again do inorder traversal of tree and copy array elements to tree nodes one by one. This step takes O(n) time. Following is C implementation of the above approach. The main function to convert is highlighted in the following code.
• 2. /* A program to convert Binary Tree to Binary Search Tree */ #include<stdio.h> #include<stdlib.h> /* A binary tree node structure */ struct node { int data; struct node *left; struct node *right; }; /* A helper function that stores inorder traversal of a tree rooted with node */ void storeInorder (struct node* node, int inorder[], int *index_ptr) { // Base Case if (node == NULL) return; /* first store the left subtree */ storeInorder (node->left, inorder, index_ptr); /* Copy the root’s data */ inorder[*index_ptr] = node->data; (*index_ptr)++; // increase index for next entry /* finally store the right subtree */ storeInorder (node->right, inorder, index_ptr); } /* A helper function to count nodes in a Binary Tree */ int countNodes (struct node* root) { if (root == NULL) return 0;
• 3. return countNodes (root->left) + countNodes (root->right) + 1; } // Following function is needed for library function qsort() int compare (const void * a, const void * b) { return ( *(int*)a – *(int*)b ); } /* A helper function that copies contents of arr[] to Binary Tree. This functon basically does Inorder traversal of Binary Tree and one by one copy arr[] elements to Binary Tree nodes */ void arrayToBST (int *arr, struct node* root, int *index_ptr) { // Base Case if (root == NULL) return; /* first update the left subtree */ arrayToBST (arr, root->left, index_ptr); /* Now update root’s data and increment index */ root->data = arr[*index_ptr]; (*index_ptr)++; /* finally update the right subtree */ arrayToBST (arr, root->right, index_ptr); } // This function converts a given Binary Tree to BST void binaryTreeToBST (struct node *root) { // base case: tree is empty if(root == NULL) return;
• 4. /* Count the number of nodes in Binary Tree so that we know the size of temporary array to be created */ int n = countNodes (root); // Create a temp array arr[] and store inorder traversal of tree in arr[] int *arr = new int[n]; int i = 0; storeInorder (root, arr, &i); // Sort the array using library function for quick sort qsort (arr, n, sizeof(arr[0]), compare); // Copy array elements back to Binary Tree i = 0; arrayToBST (arr, root, &i); // delete dynamically allocated memory to avoid meory leak delete [] arr; } /* Utility function to create a new Binary Tree node */ struct node* newNode (int data) { struct node *temp = new struct node; temp->data = data; temp->left = NULL; temp->right = NULL; return temp; } /* Utility function to print inorder traversal of Binary Tree */ void printInorder (struct node* node) { if (node == NULL) return;
• 5. /* first recur on left child */ printInorder (node->left); /* then print the data of node */ printf(“%d “, node->data); /* now recur on right child */ printInorder (node->right); } /* Driver function to test above functions */ int main() { struct node *root = NULL; /* Constructing tree given in the above figure 10 / 30 15 / 20 5 */ root = newNode(10); root->left = newNode(30); root->right = newNode(15); root->left->left = newNode(20); root->right->right = newNode(5); // convert Binary Tree to BST binaryTreeToBST (root); printf(“Following is Inorder Traversal of the converted BST: n”);
• 6. printInorder (root); return 0; } Output: Following is Inorder Traversal of the converted BST: 5 10 15 20 30 Question 2: (20 marks) Write algorithm and program for multiplication of two Sparse Matrices using Pointers. Ans :- algorithen A useful application of linear list is the representation of matrices that contain a preponderance of zero elements. These matrices are called sparse matrices. Consider the matrix 0 0 6 0 9 0 0 2 0 0 7 8 0 4 10 0 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 5 Of the 42 elements in the 6*7 matrix, only 10 are nonzero. One of the basic methods of storing is to store nonzero elements in a one-dimensional array and to identify each array element with row and column indices as shown in figure (a). The ith element of vector A is the matrix element with row and column indices ROW[i] and COLUMN[i]. Matrix elements are stored in row major order with zero elements removed. In a more efficient representation shown in figure (b), the ROW vector is changed so that its ith element is the index to the first of column indices for the elements in row i of the matrix. ROW COLUMN A 1 1 3 6 2 1 5 9 3 2 1 2
• 7. 4 2 5 2 6 2 7 3 8 4 9 6 10 6 figure (a) COLUMN 1 3 ROW 2 1 1 2 3 3 7 4 8 5 0 6 9 row number column number 4 5 7 1 3 4 7 7 8 4 10 12 3 5 6 5 9 A 3 4 5 6 7 8 1 4 5 7 1 3 9 10 4 7 2 7 8 4 10 12 3 5 figure (b) An algorithm for adding 2 matrices that are represented in figure(b) is given below: Algorithm MATRIX_ADDITION. Given sparse matrices A and B represented by vectors A and B with row and column indices AROW and ACOL, BROW and BCOL, respectively, it is required to form the matrix sum C=A+B. C must be represented in the same manner A and B, so CROW and CCOL are formed. A and B have the same dimensions, M*N, and contain R and S nonzero elements, respectively. The number of nonzero elements in C on completion of the algorithm is T. Auxiliary variable l is used to index the rows of the matrices and J and K index the matrix elements in vectors A and B, respectively. Variables SUM and COLUMN are used to contain each new element for matrix C and column position. 1. Initialize l=1
• 8. T=0 2. Scan each row Repeat thru step 9 while l<=M 3. Obtain row indices and starting positions of next rows J=AROW[l] K=BROW[l] CROW[l]=T+1 AMAX=BMAX=0 If l<M then Repeat for P=l+1, l+2, ……M while AMAX=0 If AROW[P]/=0 then AMAX=AROW[P] Repeat for P=l+1, l+2,…….M while BMAX=0 If BROW[P]/=0 then BMAX=BROW[P] If AMAX=0 then AMAX=R+1 If BMAX=0 then BMAX=S+1 4. Scan columns of this row Repeat thru step 7 while J/=0 and K/=0 5. Elements in same column? If ACOL[J]=BCOL[K] then SUM=A[J]+B[K] COLUMN=ACOL[J] J=J+1 K=K+1 else If ACOL[J]<BCOL[K] then SUM=A[J] COLUMN=ACOL[J] J=J+1 else SUM=B[K] COLUMN=BCOL[K] K=K+1 6. Add new elements to sum of matrices If SUM/=0
• 9. then T=T+1 C[T]=SUM CCOL[T]=COLUMN 7. End of either row? If J=AMAX then J=0 If K=BMAX then K=0 8. Add remaining elements of a row If J=0 and K/=0 then repeat while K<BMAX T=T+1 C[T]=B[K] CCOL[T]=BCOL[K] K=K+1 else if K=0 and J/=0 then repeat while J<AMAX T=T+1 C[T]=A[J] CCOL[T]=ACOL[J] J=J+1 9. Adjust index to matrix C and increment row index If T<CROW[l] then CROW[l]=0 l=l+1 10. Finished Exit For each pair of correspondin rows in matrices A and B, steps 3 to 9 are executed to add matrix elements from those rows. When J or K is zero, the nonzero elements in the row of matrix A or B, respectively, have all been accounted for. When both J and K are zero, the algorithm can proceed to add the next rows of the matrices. If J or K are nor initially zero, they are set to zero when they reach the values AMAX or BMAX, respectively. AMAX and BMAX are the positions in the ACOL and A and BCOL and B vectors where the next row starts. However, if there is no next row, AMAX and BMAX have values R+1 and S+1, respectively.