mc0068-2 august 2010 q & ans

Upload: abhish9k

Post on 08-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    1/16

    Master of Computer Application (MCA) Semester 2

    MC0068 Data Structures using C 4 Credits

    (Book ID: B0701 & B0702)

    Assignment Set 1 (40 Marks)

    1. Describe the theory of circular singly linked lists?

    Ans: There are four cases, which can occur while removing the node. These cases are similarto the cases in add operation. We have the same four situations, but the order of algorithmactions is opposite. Notice, that removal algorithm includes the disposal of the deleted node,which may be unnecessary in languages with automatic garbage collection (i.e., Java).

    List has only one node

    When list has only one node, which is indicated by the condition, that the head points to thesame node as the tail, the removal is quite simple. Algorithm disposes the node, pointed byhead (or tail) and sets both head and tail to NULL.

    Remove first

    In this case, first node (current head node) is removed from the list.

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    2/16

    It can be done in two steps:

    1. Update head link to point to the node, next to the head.

    2. Dispose removed node.

    Remove last

    In this case, last node (current tail node) is removed from the list. This operation is a bit moretricky, than removing the first node, because algorithm should find a node, which is previousto the tail first.

    It can be done in three steps:

    1. Update tail link to point to the node, before the tail. In order to find it, list should betraversed first, beginning from the head.

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    3/16

    2. Set next link of the new tail to NULL.

    3. Dispose removed node.

    General case

    In general case, node to be removed is always located between two list nodes. Head and taillinks are not updated in this case.

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    4/16

    Such a removal can be done in two steps:

    1. Update next link of the previous node, to point to the next node, relative to the removednode.

    2. Dispose removed node.

    Code snippets

    All cases, shown above, can be implemented in one function with a single argument, which isnode previous to the node to be removed. Forremove firstoperation, the argument is NULL.Forremove lastoperation, the argument is the node, previous to tail. Though, it's better toimplement this special cases (remove first and remove last) in separate functions. Notice, thatremoving first and last node have different complexity, because remove lastneeds to traverse

    through the whole list.

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    5/16

    Java implementation

    publicclass SinglyLinkedList {

    publicvoid removeFirst() {

    if(head == null)

    return;

    else {

    if(head == tail) {

    head = null;

    tail = null;

    } else {

    head = head.next;

    }

    }

    }

    publicvoid removeLast() {

    if(tail == null)

    return;

    else {

    if(head == tail) {

    head = null;

    tail = null;

    } else {

    SinglyLinkedListNode previousToTail = head;

    while (previousToTail.next != tail)

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    6/16

    previousToTail = previousToTail.next;

    tail = previousToTail;

    tail.next = null;

    }

    }

    }

    publicvoid removeNext(SinglyLinkedListNode previous) {

    if(previous == null)

    removeFirst();

    elseif(previous.next == tail) {

    tail = previous;

    tail.next = null;

    } elseif(previous == tail)

    return;

    else {

    previous.next = previous.next.next;

    }

    }

    }

    C++ implementation

    void SinglyLinkedList::removeFirst() {

    if(head == NULL)

    return;

    else {

    SinglyLinkedListNode *removedNode;

    removedNode = head;

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    7/16

    if(head == tail) {

    head = NULL;

    tail = NULL;

    } else {

    head = head->next;

    }

    delete removedNode;

    }

    }

    void SinglyLinkedList::removeLast() {

    if(tail == NULL)

    return;

    else {

    SinglyLinkedListNode *removedNode;

    removedNode = tail;

    if(head == tail) {

    head = NULL;

    tail = NULL;

    } else {

    SinglyLinkedListNode *previousToTail = head;

    while (previousToTail->next != tail)

    previousToTail = previousToTail->next;

    tail = previousToTail;

    tail->next = NULL;

    }

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    8/16

    delete removedNode;

    }

    }

    void SinglyLinkedList::removeNext(SinglyLinkedListNode *previous) {

    if(previous == NULL)

    removeFirst();

    elseif(previous->next == tail) {

    SinglyLinkedListNode *removedNode = previous->next;

    tail = previous;

    tail->next = NULL;

    delete removedNode;

    } elseif(previous == tail)

    return;

    else {

    SinglyLinkedListNode *removedNode = previous->next;

    previous->next = removedNode->next;

    delete removedNode;

    }

    }

    2. 2. Describe following Binary Trees:

    A) Strictly Binary trees

    B) Complete Binary trees

    C) Almost Complete Binary Trees.

    Ans

    A) Strictly binary tree

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    9/16

    If the outdegree of every node in a tree is either 0 or 2, then the tree is said to be strictlybinary 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 andright 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 2i-1, then the tree is said tobe a complete binary tree. The tree shown in figure below is a strictly binary tree and at thesame 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

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    10/16

    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 totalnumber of leaf nodes is 2d. In a complete binary tree, we can easily find the number of non-leafnodes. Now, let us find the number of non-leaf nodes in a complete binary tree. Total number ofnodes 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 n t = 2d+1- 1

    Nodes at level d are all leaf nodes. So, number of non-leaf nodes is given by 2d+1 1 2d whichis equal to 2d 1.

    3. With the help of a program and an example, explain Breadth First Tree Traversal

    Ans

    Breadth First Traversal

    In the breadth-first traversal of a graph, we process all adjacent vertices of a vertexbefore going to the next level. Looking at the tree in, Figure 7.7, we see that its breadth-firsttraversal starts at level 1 and then processes all the vertices in level 2 before going on toprocess the vertices in level 3.

    Breadth-first traversal of a tree

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    11/16

    The breadth-first traversal of a graph follows the same concept we begin by picking astarting vertex; after processing it, we process all of its adjacent vertices. After we process all ofthe 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 beprocessed, we delete a vertex from the queue and process it. Lets trace this logic through thegraph 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 2in Figure 7.8(b]), we dequeue vertex X, process it, and then place vertices G and H in thequeue. We are then ready for Step 3, in which we process vertex G.

    3. When the queue is empty, the traversal is complete.

    4. Write a program to perform a binary search on an unsorted list of n integers?

    Ans

    #include

    #include

    void main()

    { clrscr();

    int a[50],n,p,s,bsearch(int [],int,int);

    void bubsort(int a[],int n)

    coutn;

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    12/16

    cout

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    13/16

    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, aninsertion sort visits each element of the array, in turn. As it visits a particular element, it scansthe array from the beginning up to the determines where in that segment of the array the currentvalue belongs. It then inserts the current value in that location and shifts every element of thearray to the right, up to the present location. It then goes on to the next location (value) in thearray. Notice that one index is going from 0 to n and for each such value and another index isscanning the array from 0 to the value of the first index. The result of this is that this type ofsort is O(n2).

    This is a naturally occurring sorting method exemplified by a card player arranging the cardsdealt 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:

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    14/16

    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. Describe Depth First search algorithm and analyze its complexity?

    Ans

    Depth First Traversal

    In the depth-first traversal, we process all of a vertexs descendents before we move toan 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 firstvertex of the graph. After processing the first vertex, we select any vertex adjacent to the firstvertex and process it. As we process each vertex, we select an adjacent vertex until we reach avertex with no adjacent entries. This is similar to reaching a leaf in a tree. We then back out ofthe structure, processing adjacent vertices as we go. It should be obvious that this logic requiresa 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

    7. Explain the following theorems of Splay trees:

    A) Working Set Theorem

    Let t(i) be the number of accesses of different items that occurred between access j and the

    previous access of the same item. The cost of the sequence is bounded by:

    O(m + n log(n) + Sum log (t(i) + 1))

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    15/16

    j

    NOTE: this is proven by using a weight assignment very similar to the one used in the proof of

    the static finger theorem.

    C) Sequential Access TheoremThe cost of the access sequence that accesses each of the n items in the tree in symmetric

    (left-to-right)order is O(n).

    This is generalized by the following theorem, the proof of which is very difficult.

    D) Dynamic Finger Theorem

    Using the notation of the static finger theorem, a bound on the splaying cost is:

    O( m + n + Sum log (Ii[i-1] i[i]I + 1))

    The two theorems above are special cases of the following conjecture.

    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 linkingpointers. 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 therecords. Sometimes when the attribute value is constant for a large number of records a secondkey 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.

  • 8/7/2019 MC0068-2 August 2010 Q & Ans

    16/16

    Its disadvantages:

    (1) It is difficult to update inserting a new record may require moving a large proportion of thefile;

    (2) Random access is extremely slow.

    Sometimes a file is considered to be sequentially organised despite the fact that it is notordered 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 thata list is defined with respect to a keyword K, so every K-list contains only one record. Thisimplies that the directory will be such that ni = hi for all i, that is, the number of recordscontaining Kiwill 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 canimmediately locate the addresses of all the documents containing that keyword. For theprevious example let us assume that a non-black entry in the field corresponding to an attributeindicates the presence of a keyword and a black entry its absence. Then the directory will pointto the file in the way shown in Figure 6.3. The definition of an inverted files does notrequire thatthe addresses in the directory are in any order. However, to facilitate operations such asconjunction (and) and disjunction (or) on any two inverted lists, the addresses are normallykept in record number order. This means that and and or operations can be performed withone pass through both lists. The penalty we pay is of course that the inverted file becomesslower to update.