mid-term exam - sample solution

Upload: derwin-layne

Post on 04-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Mid-Term Exam - Sample Solution

    1/6

    UNIVERSITY OF THE WEST INDIES

    CAVE HILL CAMPUS

    Department of Computer Science, Mathematics & Physics

    COMP2115Information Structures

    Mid-Term Examination

    SAMPLE SOLUTION

    12 November, 2010 Time: 1 hrs

    INSTRUCTIONS: This paper contains TWO (2) pages and FOUR (4) questions

    DO ALL FOUR QUESTIONS

    Question 1

    (a) Define an Abstract Data Type (ADT). [2 marks]

    A data structure and a collection of functions or procedures which operate on the

    data structure.

    (b) Identify the fundamental characteristic(s) which an ADT must possess. [2 marks]

    1. Be able to create a new collection

    2. Be able add an item to a collection

    3. Be able add to delete an item from a collection4. Be able add to find an item matching some criterion in the collection5. Be able add to destroy the collection

    (c) How does an ADT differ from other data containers such as arrays? [1 mark]

    Non-ADT data containers such as arrays, are static structure which require

    contiguous memory locations to be allocated to them as soon as they are declared.

    The size of such a structure does not change throughout the programs execution.The ADT data containers are dynamic data structures and are the exact opposite.

    Memory locations allocated to ADTs need not be contiguous and the structure is

    allowed to expand and shrink as the data needs change.

    (d) Identify the difference(s) between a Constructorfunction and a Destructorfunction in a

    class definition. [2 marks]

    Constructor : Initialize objects of a class during instantiation.

    Destructor : Performs termination house-keeping just before the object is destroyed

    or goes out of scope.

    (e) Briefly describe accessor, mutatorand utilityfunctions in classes. [3 marks]Accessor: Member functions which access or get the data from within an objects

    attribute, but do not change the data.

    Mutator: Member functions which access the attributes within an in order to

    change the data value.

    Utility : Private class member fuctions which are called by other member

    functions to perform a particular task within the object.

  • 8/13/2019 Mid-Term Exam - Sample Solution

    2/6

    Question 2

    (a) Briefly differentiate between the following Abstract Data Types:

    (i) Queue ANDPriority Queue [2 marks]

    Queue:

    Lists which employ a first-in, first-out (FIFO) concept. Nodes are removed

    only from the head and nodes are inserted only at the tail.

    Priority Queue:

    A lists which imposes a sorted priority on the elements so that the elementwith the highest priority is always at the head of the list. This sorting is done

    whenever a new element is added to the list. Nodes are removed only from the

    headi.e. the node with the highest priority.

    (ii) Stack ANDDeque [2 marks]

    Stack:

    Lists which employ a last-in, first-out (LIFO) concept. Nodes are added

    and removed only from the top of the structure.

    Deque:

    A double ended queue which permits insertion and deletion at both the

    head and tail of the list. Insertion and deletion are only permitted at thetwo ends.

    (iii) Binary Search Tree ANDBinary Heap [2 marks]

    Binary Search Tree:

    A binary tree such that all elements stored in the left subtree of a node

    whose value is K have values less than K. All elements stored in the

    right subtree of a node whose value is K have values greater than or

    equal to K. All sub-trees are BSTs.

    Binary Heap:

    A special kind of binary tree which has two properties that are not

    generally true for other trees:

    1. Completeness

    The tree is complete, which means that nodes are added from

    top to bottom, left to right, without leaving any spaces. A binary

    tree is completely full if it is of height, h, and has 2h+1-1 nodes.

    2. Heapness

    The item in the tree with the highest priority is at the top of the

    tree, and the same is true for every subtree.

    (b) With reference to a Tree ADT, identify the difference(s) between the following pairs of

    terms:(i) Heightand Depth [2 marks]

    The depth of a node M in a tree is the length of the path from the root ofthe tree to M.

    The height of a tree is one more than the depth of the deepest node in thetree.

  • 8/13/2019 Mid-Term Exam - Sample Solution

    3/6

    (ii) Leaf Nodeand Internal Node [2 marks]

    A node, apart from the root) which has no children is called a leaf node orsimply a leaf.

    A node (apart from the root) that has children is an internal node.(iii) Visitingand Traversing [2 marks]

    A node is visited when program control arrives at the node, usually for thepurpose of carrying out some operation on the node.

    To traverse a tree means to visit all of the nodes in some specified order.(iv) Completebinary trees and Fullbinary trees [2 marks]

    A full binary tree is a binary tree where each node is either a leaf or is aninternal node with exactly two non-empty children.

    A complete binary tree is a binary tree where if the height is d, and alllevels, except possibly level d, are completely full. If the bottom level is

    incomplete, then it has all nodes to the left side.

    (c) Describe balance with respect to trees. [1 mark]

    Balance is described as the comparison between the heights of a nodes left and right

    sub-trees. If the heights are equal, then the node is said to be balance. Otherwise, it

    may be said to be left-heavy or right-heavy.

    Question 3

    Consider the following data set: 53, 24, 30, 62, 28, 60, 23, 61, 70, 2, 7, 31, 44, 3, 16.

    (a) Insert the values into a Binary Search Tree. [2 marks]

    (b) From the tree structure created in (a) above, give the results of:

    (i) Inordertraversal 2, 3, 7, 16, 23, 24, 28, 30, 31, 44, 53, 60, 61, 62, 70

    (ii) Preordertraversal 53, 24, 23, 2, 7, 3, 16, 30, 28, 31, 44, 62, 60, 61, 70(iii) Postordertraversal 3, 16, 7, 2, 23, 28, 44, 31, 30, 24, 61, 60, 70, 62, 53

    [3 marks]

    53

    62

    7060

    24

    30

    28 31

    23

    2

    7

    3 16

    44

    61

  • 8/13/2019 Mid-Term Exam - Sample Solution

    4/6

  • 8/13/2019 Mid-Term Exam - Sample Solution

    5/6

    (a) addNode [6 marks]

    void PriorityQueue::addNode(int val)

    {

    Node *newnode= new Node(val);

    if (Head == NULL) Head = newnode;

    else

    {if ( val > Head->getData())

    {

    Newnode->setNext(Head);

    Head = newnode;

    }

    else

    {

    Node *prevPtr = Head;

    Node *curPtr = Head->getNext();

    while(curPtr!=NULL && curPtr->getData()> val){

    prevPtr = curPtr;

    curPtr = curPtr->getNext();

    }

    if ( curPtr == NULL) precPtr->setNext(newnode);

    else

    {

    Newnode->setNext(curPtr);

    prevPtr->setNext(newnode);

    }}

    }

    (b) dequeue [4 marks]

    int PriorityQueue::dequeue()

    {

    Node* temp = Head;

    int val;

    if (Head == NULL) return -1;

    else{

    Temp = Head;

    Head = Head->getNext();

    Val = temp->getData();

    delete temp;

    return val;

    }

    }

  • 8/13/2019 Mid-Term Exam - Sample Solution

    6/6

    (c) find [4 marks]

    bool PriorityQueue::find(int val)

    {

    if (Head == NULL)

    return False;else

    {

    Node* temp;

    for (temp = Head; temp != NULL && temp->getData() != val; temp =

    temp->getNext();

    }

    if (temp == NULL)

    return False;

    else

    return True;

    }

    (d) isEmpty [1 mark]

    bool PriorityQueue::isEmpty()

    {

    if (Head == NULL)

    return true;

    else

    return false;

    }

    Note:These are class member functions and NOT regular functions.