February 2010 Master of Computer Application (MCA) – Semester 2 MC0068 – Data Structures using C Assignment Set

1. Describe the theory of: A) Strictly Binary Tree B) Complete Binary Tree
Ans – A) Strictly binary tree If the outdegree of every node in a tree is either 0 or 2, then the tree is said to be strictly binary tree i.e., each node can have maximum two children or empty left and empty right child. Example : A binary tree is said to be strictly binary if every non-leaf node has non-empty left and right subtrees. Fig shows a strictly binary tree.

A Strictly binary tree

B) Complete binary tree A strictly binary tree in which the number of nodes at any level i is 2 i-1, then the tree is said to be a complete binary tree. The tree shown in figure below is a strictly binary tree and at the same time it is a complete binary tree. Example:

A Complete Binary tree In a complete binary tree, the total number of nodes at level 0 is 1 i.e., 2° Number of nodes at level 1 is 2 i.e., 21 Number of nodes at level 2 is 4 i.e., 22 Number of nodes at level 3 is 8 i.e., 23 …………………………………… …………………………………… …………………………………… Number of nodes at the last level d is 2d. It is clear from the figure that all the nodes in the final level d are leaf nodes and the total number of leaf nodes is 2d. In a complete binary tree, we can easily find the number of non-leaf nodes. Now, let us find the number of non-leaf nodes in a complete binary tree. Total number of nodes in the complete binary tree = 2° + 21 + 22 + ………….2d.

Summation of this series is given by S = a( rn- 1) 1( r- 1) where a = 1, n = d+ 1 and r = 2 So, total number of nodes nt = 2d+1- 1 Nodes at level d are all leaf nodes. So, number of non-leaf nodes is given by 2d+1 – 1 –2d which is equal to 2d – 1.

2. Describe with sample code snippets the following operations on Binary Search Trees (BST): A) Insertion B) Searching
Ans – A) Insertion Creating a binary search tree is nothing but repeated insertion of an item into the existing tree. So, we concentrate on how to insert an item into the tree. In a BST (Binary Search Tree), items towards left subtree of a node temp will be less than info(temp) and the items in the right subtree are greater or equal to info(temp).

Consider the BST shown in above figure. Suppose the node pointed to by temp (with item 140) has to be inserted. The item 140 is compared with root node i.e., 100. Since it is greater, the right subtree of root node has to be considered. Now compare 140 with 200 and since it is less, consider the left subtree of the node 200. Now, compare 140 with 150.

Since it is less, consider the left subtree of node 150. Since, left subtree of node 150 empty, the node containing the Item 140 has to be inserted towards left of a node 150. Thus, to find the appropriate place and insert an item, the search should start from the root node. This is achieved by using two pointer variables prev and cur. The pointer variable prev always points to parent of cur node. Initially cur points to root node and prev points to NULL i.e., prev = NULL cur = root Now, as long as the item is less than info (cur), keep updating the node pointed to by the pointer variable cur towards left. Otherwise, update towards right. The pointer variable prev always points to the parent node and cur points to the child node. Once cur points to NULL, insert the node temp towards left (prev) if item is less than info (prev), otherwise insert towards right. The code corresponding to this can be prev = NULL; cur = root; /* find the position where to insert */ while ( cur != NULL ) { prev = cur; cur = ( item < cur->info ) ? cur->llink : cur->rlink; } if ( item < prev->info) prev->llink = temp; else prev->rlink = temp; The above steps can be executed provided the tree exists. If the tree is empty initially, then make the node pointed to by temp itself as root node. The complete C function to insert an item into a binary search tree is shown in below example B) Searching Start searching from the root node and move downward towards left or right based on whether the item lies towards left subtree or right subtree. This is achieved by comparing this item with root node of an appropriate subtree (left subtree or right subtree). If two items are same

then search is successful and address of that node is returned. If the item is less than info (root), search in the left subtree, otherwise search in the right subtree. If item is not found in the tree, finally root points to NULL and return root indicating search is unsuccessful. The iterative function to search for this item is shown in below example. Example Function to search for an item in BST using iteration NODE iterative_ search (int item, NODE root) { /* search for the item */ while ( root != NULL && item != root->info ) { root = ( item < root->info ) ? root->llink : root->rlink; } eturn root; } The recursive function to search for an item is shown in below example. Example : Function to search for an item in BST using recursion NODE recursive_ search (int item, NODE root) { if ( root = = NULL ÷÷ item = = root->info ) return root; if ( item < root->info ) return recursive_ search(item, root->llink); return recursive_ search(item, root->rlink); }

3. With the help of a program and a numerical example explain the Depth First Traversal of a tree.
Ans – 3 Depth – First Traversal In the depth-first traversal, we process all of a vertex’s descendents before we move to an adjacent vertex. This concept is most easily seen when the graph is a tree. In Figure below, we show the preorder traversal, one of the standard depth-first traversals. In a similar manner, the depth-first traversal of a graph starts by processing the first vertex of the graph. After processing the first vertex, we select any vertex adjacent to the first vertex and process it. As we process each vertex, we select an adjacent vertex until we reach a vertex with no adjacent entries. This is similar to reaching a leaf in a

tree. We then back out of the structure, processing adjacent vertices as we go. It should be obvious that this logic requires a stack (or recursion) to complete the traversal. The order in which the adjacent vertices are processed depends on how the graph is physically stored.

Depth first traversal of a tree

4. Write a program to perform a binary search on an unsorted list of n integers.
Ans – #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[50],n,p,s,bsearch(int [],int,int); void bubsort(int a[],int n) cout<<"Enter the no. of elements: ";cin>>n; cout<<"Enter the array...\n"; for (int i=0;i<n;i++) cin>>a[i]; cout<<"Enter the no. to be searched: ";cin>>s; bubsort(a,n); p=bsearch(a,n,s); if (p==-1) cout<<"Sorry, element not found"; else cout<<s<<" was found at position "<<p+1; getch(); }

void sort(int a[],int n) { int t; for (int i=0;i<n;i++) { for (int j=0;j<(n-1)-i;j++) { if (a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } } int bsearch(int a[],int n,int s) { int b=0,m,l=n-1; while(b<l) { m=(b+l)/2; if (s==a[m]) return(m); else if(s>a[m]) b=m+1; else l=m-1; } return(-1); }

5. With the help of a numerical example, explain the working of Insertion Sort.
Ans – An insertion sort has the advantage that it is simple to understand and simple to implement. Unfortunately, it is rather slow. Given an unsorted array of integer values, an insertion sort visits each element of the array, in turn. As it visits a particular element, it scans the array from the beginning up to the determines where in that segment of the array the current value belongs. It then inserts the current value in that

location and shifts every element of the array to the right, up to the present location. It then goes on to the next location (value) in the array. Notice that one index is going from 0 to n and for each such value and another index is scanning the array from 0 to the value of the first index. The result of this is – that this type of sort is O(n2). This is a naturally occurring sorting method exemplified by a card player arranging the cards dealt to him. He picks up the cards as they are dealt and inserts them into the required position. Thus at every step, we insert an item into its proper place in an already ordered list. We will illustrate insertion sort with an example below given and presenting the formal algorithm. Example : Sort the following list using the insertion sort method:

Step 1 1 < 4, Therefore insert before 4 Step 2 3 > 1, 3 Insert between 1 & 4 Step 3 2 > I, 2, Insert between I & 3 Step 4 5 > I, 2,3,4, Insert after 4 (5) Thus to find the correct position search the list till an item just greater than the target is found. Shift all the items from this point one, down the list. Insert the target in the vacated slot.

6.

With

your

own

example

explain

breadth

first

traversal technique, and analyze its complexity.
Ans – Breadth – First Traversal In the breadth-first traversal of a graph, we process all adjacent vertices of a vertex before going to the next level. Looking at the tree in, Figure 7.7, we see that its breadth-first traversal starts at level 1 and then processes all the vertices in level 2 before going on to process the vertices in level 3.

Breadth-first traversal of a tree The breadth-first traversal of a graph follows the same concept we begin by picking a starting vertex; after processing it, we process all of its adjacent vertices. After we process all of the first vertex adjacent vertices, we pick the first adjacent vertex and process all of its vertices, then the second adjacent vertex and process all of its vertices and so forth until we are finished. The breadth-first traversal uses a queue rather than a stack, As we process each vertex, we place all of its adjacent vertices in the queue. Then, to select the next vertex to be processed, we delete a vertex from the queue and process it. Let’s trace this logic through the graph in Figure below.

Breadth-first traversal of a graph 1. We begin by enqueuing vertex A in the queue. 2. We then loop, dequeuing the queue and processing the vertex from the front of the queue. After processing the vertex, we place all of its adjacent vertices into the queue. Thus, at Step 2 in Figure 7.8(b]), we dequeue vertex X, process it, and then place vertices G and H in the queue. We are then ready for Step 3, in which we process vertex G. 3. When the queue is empty, the traversal is complete.

7. Explain the theory regarding the following splay trees: A) Access Lemma B) Static Optimality Theorem
Ans – : A red-black tree with n internal nodes has height at most 2lg(n+1) Proof: The tree rooted at any node x contains at least 2 bh(x) – 1 internal nodes. Base case: If the height of x is zero, then x must be a leaf and the tree rooted at x contains at least 20 – 1 = 0 internal nodes. Inductive hypothesis: Consider a node x that has a positive height and is an internal node with two children. Each child has a black-height of either bh(x) or bh(x) – 1 depending upon whether the child is red or black, respectively. Since the height of a tree rooted at a child of x is less than the height of the tree rooted at x itself, we can apply the inductive hypothesis to conclude that each subtree rooted at a child has at least 2bh(x)-1 –1 internal nodes. Thus the tree rooted at x has at least (2 bh(x)-1 –1) + (2bh(x)-1 –1) + 1 = 2bh(x) – 1 internal nodes. Let h be the height of the tree. According to properties 4 and 5 at least half the node on any path from the root to a leaf, not including the root, must be black. Consequently the black height of the root must be at least h/2. Thus n ³ 2h/2 – 1 and (n+1) ³ 2h/2

Take the lg of both sides of this inequality and find that lg(n+1) ³ h/2 or h £ 2lg(n+1)

8. Explain the following in the context of files: A) Sequential Files B) Inverted Files Ans –
A) Sequential files A sequential file is the most primitive of all file structures. It has no directory and no linking pointers. The records are generally organised in lexicographic order on the value of some key. In other words, a particular attribute is chosen whose value will determine the order of the records. Sometimes when the attribute value is constant for a large number of records a second key is chosen to give an order when the first key fails to discriminate. The implementation of this file structure requires the use of a sorting routine. Its main advantages are: (1) it is easy to implement; (2) it provides fast access to the next record using lexicographic order. Its disadvantages: (1) it is difficult to update – inserting a new record may require moving a large proportion of the file; (2) random access is extremely slow. Sometimes a file is considered to be sequentially organised despite the fact that it is not ordered according to any key. Perhaps the date of acquisition is considered to be the key value, the newest entries are added to the end of the file and therefore pose no difficulty to updating.

B) Inverted files An inverted file is a file structure in which every list contains only one record. Remember that a list is defined with respect to a keyword K, so every K-list contains only one record. This implies that the directory will be such that ni = hi for all i, that is, the number of records containing Ki will equal the number of Ki-lists. So the directory will have an address for each record containing Ki . For document retrieval this means that given a keyword we can immediately locate the addresses of all the documents containing that keyword. For the previous example let us assume that a non-black entry in the field corresponding to an attribute indicates the presence of a keyword and a black entry its absence. Then the directory will point to the file in the way shown in Figure 6.3. The definition of an inverted files does not require that the addresses in the directory are in any order. However, to facilitate operations such as conjunction (’and’) and disjunction (’or’) on any two inverted lists, the addresses are normally kept in record number order. This means that ‘and’ and ‘or’ operations can be performed with one pass through both lists. The penalty we pay is of course that the inverted file becomes slower to update.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.