Midterm 1


CS2604 : Data Structures and File Processing

Summer Session II 1999 : Midterm 1

Question One [20]

  1. Why do we study advanced data structures ? [5]

    Efficiency and generalization

  2. What does it mean when we say that "f(n) is in O(g(n))" ? [5]

    There exists a c and n0 such that f(n) <= c g(n) for all n > n0

  3. Why do we use a dummy head node in a linked list ? [5]

    To avoid having to write special coding cases to handle trivial (eg. empty) lists

  4. In a singly linked list, why is it more efficient to use a "current" pointer that points to the node before the current node ? [5]

    So that operations that need relinking to be performed on the previous node can find it without an O(n) search from the head.

Question Two [20]

Match the following terms with the most appropriate descriptions :
queue, parent, doubly linked list, stack, average case, worst case, data structure, right child, singly linked list, freelist
data structureimplementation
single linked listone pointer per node
stackelements are added/removed at one end of the data structure
queueFIFO (first-in-first-out)
right child2r+2
doubly linked listability to always find previous node in O(1) time
parent(r-1)/2
worst casecrucial for real-time applications
freelistavoid unnecessary allocation/deallocation of storage
average caseconsider all input combinations

Question Three [40]

  1. What is a stack ? [4]
  2. An abstract data type where the first element added is the last removed (FILO).

  3. What are the specific names of the 3 primitive abstract operations associated with a stack ? [6]
  4. (a) Push, (b) Pop, (c) Top

  5. What is the asymptotic time complexity (in the average case) for each of these operations, when the stack is implemented efficiently using a linked dynamic data structure, as compared to when the stack is implemented efficiently using an array ? [6]
  6. Operation

    T(n) for array

    T(n) for linked list

    (a) Push

    O(1) O(1)

    (b) Pop

    O(1) O(1)

    (c) Top

    O(1) O(1)

  7. Fill in the code to REMOVE an element from the top of the stack, and return it to the calling routine, in a linked list implementation. [14]
  8. Node *Stack::Remove ()
    {
       if (TopOfStack == NULL) return NULL;
       Node *Temp = TopOfStack;
       TopOfStack = TopOfStack->Next;
       return Temp;
    }
    

    with the following definitions :

    class Node {
       char aValue;
       Node *Next;
    };
    
    class Stack {
       Node *TopOfStack;
       Stack () { TopOfStack = NULL; }
       Node *Remove ();
       .
       .
    };
    

    Assume that TopOfStack points to the topmost element and that there is no dummy head node.

  9. Explain how you would use a stack to check if the parentheses in a string or expression are properly balanced. In a balanced string, when scanning from left to right, you will never have encountered more right parentheses than left parentheses. For example, "(())()" is balanced while "()))" is not. Two errors are possible – either there are too many left parentheses (e.g. "(()" ) or too many right parentheses (e.g. "())" ) – indicate how each of these can be checked for. [10]
  10. Scan string L->R

    After scanning, if stack is not empty then "too many (s"

Question Four [20]

Consider the following tree:

  1. Write out the order in which elements are visited if a PostOrder traversal is performed on the tree. [5] 4, 6, 2, 3, 7, 1, 8, 5
  2. Write out the order in which elements are visited if an InOrder traversal is performed on the tree. [5] 4, 7, 6, 3, 2, 5, 1, 8
  3. Approximately how many nodes in a full tree (where the tree has n nodes) are leaf nodes ? [2] n/2
  4. Answer the following questions [8] :

  5. what are the parents of "6" : 3
    what are the children of "4" : none
    what are the ancestors of "3" : 5, 7
    what are the descendents of "7" : 4, 3, 6, 2

20 July
Last updated : 21 July 1999 11:45pm