You are on page 1of 6

UNIVERSITY OF THE WEST INDIES CAVE HILL CAMPUS Department of Computer Science, Mathematics & Physics COMP2115 – Information 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. Identify the fundamental characteristic(s) which an ADT must possess. 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 collection 4. Be able add to find an item matching some criterion in the collection 5. Be able add to destroy the collection [2 marks]

(b)

(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 program’s 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. Identify the difference(s) between a Constructor function and a Destructor function 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. Briefly describe accessor, mutator and utility functions in classes. [3 marks] Accessor: Member functions which access or get the data from within an object’s 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.

(d)

(e)

All sub-trees are BST’s.e.Question 2 (a) Briefly differentiate between the following Abstract Data Types: (i) Queue AND Priority Queue [2 marks] Queue: Lists which employ a first-in. Insertion and deletion are only permitted at the two ends. Deque: A double ended queue which permits insertion and deletion at both the head and tail of the list. first-out (FIFO) concept. This sorting is done whenever a new element is added to the list. All elements stored in the right subtree of a node whose value is K have values greater than or equal to K. the node with the highest priority. Completeness The tree is complete. Priority Queue: A lists which imposes a sorted priority on the elements so that the element with the highest priority is always at the head of the list. without leaving any spaces. . Heapness The item in the tree with the highest priority is at the top of the tree. identify the difference(s) between the following pairs of terms: (i) Height and Depth [2 marks]  The depth of a node M in a tree is the length of the path from the root of the tree to M. left to right. Binary Heap: A special kind of binary tree which has two properties that are not generally true for other trees: 1. and has 2h+1-1 nodes. (iii) (b) With reference to a Tree ADT. first-out (LIFO) concept. Nodes are added and removed only from the top of the structure. Binary Search Tree AND Binary 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. (ii) Stack AND Deque [2 marks] Stack: Lists which employ a last-in.  The height of a tree is one more than the depth of the deepest node in the tree. 2. Nodes are removed only from the head and nodes are inserted only at the tail. h. which means that nodes are added from top to bottom. and the same is true for every subtree. A binary tree is completely full if it is of height. Nodes are removed only from the head – i.

16. 7. 70 (iii) Postorder traversal 3. usually for the purpose of carrying out some operation on the node. give the results of: (i) Inorder traversal 2. 61. then it has all nodes to the left side. 24. 60. 30. 16. 2. (a) Insert the values into a Binary Search Tree. apart from the root) which has no children is called a leaf node or simply a leaf. 3. Visiting and Traversing [2 marks]  A node is visited when program control arrives at the node. 53 [2 marks] 24 62 23 30 60 70 2 28 31 61 7 44 3 16 (b) From the tree structure created in (a) above. 28. Otherwise.  A node (apart from the root) that has children is an internal node. 60. 31. 53 [3 marks] . 70. 44. 24. 2. 23. (iii) (iv) (c) Describe “balance” with respect to trees. If the bottom level is incomplete. 70 (ii) Preorder traversal 53. 3. 16. 61. If the heights are equal. 44. 24. then the node is said to be balance. 30. 28. 30. 61. 61. 31. 62. 30. and all levels. 60. 7. 7. 23. 3.  To traverse a tree means to visit all of the nodes in some specified order. 60.  A complete binary tree is a binary tree where if the height is d. 44. 16. 62. 53. 44. 23. 2. Question 3 Consider the following data set: 53. 23. it may be said to be left-heavy or right-heavy. Complete binary trees and Full binary trees [2 marks]  A full binary tree is a binary tree where each node is either a leaf or is an internal node with exactly two non-empty children. [1 mark] Balance is described as the comparison between the heights of a node’s left and right sub-trees. 62. 24. are completely full. 31. 28. 7. 28. 62. 70.(ii) Leaf Node and Internal Node [2 marks]  A node. 31. except possibly level d.

. } // Returns the value of node Node* getNext(void) { return Next. } Node(int Val) { data = Val. //Displays the contents of the priority queue bool isEmpty(void). } //Assigns a data value void setNext(Node* Nx) { Next = Nx. Using the classes’ definitions for the nodes and Priority Queue ADT above.} void addNode(int). } //Define the accessor member functions. //dequeues + returns the value of the node removed bool find(int).Question 4 class Node { private: int data Node* Next.. //Determines if a specified value is in the P/ queue void showNodes(void). //Checks to see if the priority queue is empty }. int getData(void) { return data. } //Sets the next pointer }. public: PriorityQueue (void) { Head = NULL. class PriorityQueue { private: Node* Head. provide the C++ code for the following PriorityQueue member functions: . //Adds new node (with given data) to the P/queue int dequeue(void). } // Address of next node //Define the mutators member functions void setValue(int Vx) { data = Vx.. public: Node(void) { Next = NULL. Next = NULL.

(a) addNode void PriorityQueue::addNode(int val) { Node *newnode= new Node(val). if (Head == NULL) return -1. if (Head == NULL) Head = newnode. } } } (b) dequeue int PriorityQueue::dequeue() { Node* temp = Head. return val. prevPtr->setNext(newnode). } if ( curPtr == NULL) precPtr->setNext(newnode). else { if ( val > Head->getData()) { Newnode->setNext(Head). [6 marks] while(curPtr!=NULL && curPtr->getData()> val) { prevPtr = curPtr. curPtr = curPtr->getNext(). } } [4 marks] . delete temp. Val = temp->getData(). else { Newnode->setNext(curPtr). } else { Node *prevPtr = Head. Head = newnode. Node *curPtr = Head->getNext(). else { Temp = Head. int val. Head = Head->getNext().

else return True. else return false. [1 mark] . for (temp = Head. } (d) isEmpty bool PriorityQueue::isEmpty() { if (Head == NULL) return true. temp != NULL && temp->getData() != val. } Note: These are class member functions and NOT regular functions. temp = temp->getNext(). } if (temp == NULL) return False. else { Node* temp.(c) find bool PriorityQueue::find(int val) { [4 marks] if (Head == NULL) return False.