Sie sind auf Seite 1von 6

UNIVERSITY OF THE WEST INDIES CAVE HILL CAMPUS Department of Computer Science, Mathematics & Physics COMP2115 Information

n 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 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. 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 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.

(d)

(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, 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 element with 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 head i.e. the node with the highest priority. (ii) Stack AND Deque [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 the two ends. 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. 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.

(iii)

(b)

With reference to a Tree ADT, 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. The height of a tree is one more than the depth of the deepest node in the tree.

(ii)

Leaf Node and Internal Node [2 marks] A node, apart from the root) which has no children is called a leaf node or simply a leaf. A node (apart from the root) that has children is an internal node. Visiting and Traversing [2 marks] A node is visited when program control arrives at the node, usually for the purpose of carrying out some operation on the node. To traverse a tree means to visit all of the nodes in some specified order. 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. A complete binary tree is a binary tree where if the height is d, and all levels, except possibly level d, are completely full. If the bottom level is incomplete, then it has all nodes to the left side.

(iii)

(iv)

(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.
53

[2 marks]

24

62

23

30

60

70

28

31

61

44

16

(b)

From the tree structure created in (a) above, give the results of: (i) Inorder traversal 2, 3, 7, 16, 23, 24, 28, 30, 31, 44, 53, 60, 61, 62, 70 (ii) Preorder traversal 53, 24, 23, 2, 7, 3, 16, 30, 28, 31, 44, 62, 60, 61, 70 (iii) Postorder traversal 3, 16, 7, 2, 23, 28, 44, 31, 30, 24, 61, 60, 70, 62, 53 [3 marks]

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

(a)

addNode 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();

[6 marks]

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 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; } } [4 marks]

(c) find bool PriorityQueue::find(int val) {

[4 marks]

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 bool PriorityQueue::isEmpty() { if (Head == NULL) return true; else return false; } Note: These are class member functions and NOT regular functions.

[1 mark]

Das könnte Ihnen auch gefallen