You are on page 1of 6

EXAMS OFFICE

3 hrs 05 / 11 / 2015 MSL


USE ONLY

University of the Witwatersrand, Johannesburg

Course or topic No(s) COMS1017

Course or topic name(s) Introduction to Data Structures and Algorithms


Paper Number & title Lab Exam

Examination to be held dur-


November 5, 2015
ing the month(s) of

Year of study

Degrees/Diplomas for which


BSc, BSc (Applied Computing), BEconSc
this course is prescribed

Faculties presenting candi-


Science
dates

Mr Richard Klein
Internal examiner(s)
x-76172

External examiner(s) Prof. Stefan Gruner (UP)

Special materials None

Time allowance 3 Hours

60 Marks available. 60 marks = 100%.


Answer all questions.
Instructions to candidates Presentation Errors (results that differ to the marker in
whitespace only) are considered correct and will receive
full marks.
Introduction to Data Structures and Algorithms
COMS1017 Lab Exam November 5, 2015

Question 1 Stacks [10 Marks]


Complete the code in stack.cpp. You may use any implementation as long as the output is cor-
rect. Main code is provided that reads commands from cin and calls the relevant functions. Do
not change this code. You can compile this code using make stack and run using ./stack

1. The code should implement push, pop, peek, and print. [10]

Sample Input Sample Output


push 4 Size: 3
push 5 Popped: 6
push 6 Size: 2
size Peeked: 5
pop Size: 2
size Print: 4 5
peek Popped: 5
size Print: 4
print
pop
print
quit

Page 2 of 6
Introduction to Data Structures and Algorithms
COMS1017 Lab Exam November 5, 2015

Question 2 LinkedLists [10 Marks]


You are given an incomplete Linked List class in linkedlist.cpp. The insert function
works correctly and uses a dummy head node. You can compile this code using make linkedlist
and run using ./linkedlist

1. Implement print() that prints out every item separated by spaces. [5]

2. Implement delete(int i) that deletes that item at index i from the list. [5]

Sample Input Sample Output


push_front 42 Print: 46 45 44 43 42
push_front 43 Deleting Link with value 46
push_front 44 Print: 45 44 43 42
push_front 45 Deleting Link with value 42
push_front 46 Print: 45 44 43
print Deleting Link with value 44
deleteAt 0 Print: 45 43
print Deleting Link with value 45
deleteAt 3 Print: 43
print Deleting Link with value 43
deleteAt 1 Print:
print
deleteAt 0
print
deleteAt 0
print
quit

Page 3 of 6
Introduction to Data Structures and Algorithms
COMS1017 Lab Exam November 5, 2015

Question 3 BST [30 Marks]


You are given an incomplete BST class in bst.cpp. The insert function works correctly and
sets up the left, right, and parent pointers of each node.

1. Implement the 3 traversal functions. Items should be printed out separated by spaces, a
space at the end of the line is acceptable. [10]

2. Implement the remove(int value) function that traverses to the correct link and
deletes that item from the BST. Your code must handle the following cases:

• Delete a TreeNode with no children. [5]


• Delete a TreeNode with only 1 left child. [5]
• Delete a TreeNode with only 1 right child. [5]
• Delete a TreeNode with 2 children. [5]
– Your code should handle this case by copying up the minimum value from the
right subtree, and then deleting that value from the right subtree.

You can compile this code using make bst and run using ./bst

Sample Input Traversals Sample Output


insert 10 10 5 0 7 15 12 20
insert 5 0 5 7 10 12 15 20
insert 0 0 7 5 12 20 15 10
insert 7
insert 15
insert 12
insert 20
pre
in
post
quit

Sample Input Remove (0 Children) Sample Output


insert 10 Deleting TreeNode with value 7
insert 5 10 5 0 15 12 20
insert 0
insert 7
insert 15
insert 12
insert 20
remove 7
quit

Page 4 of 6
Introduction to Data Structures and Algorithms
COMS1017 Lab Exam November 5, 2015

Sample Input Remove (1 Left Child) Sample Output


insert 10 Deleting TreeNode with value 5
insert 5 10 0 15 12 20
insert 0
insert 15
insert 12
insert 20
remove 5
quit

Sample Input Remove (1 Right Child) Sample Output


insert 10 Deleting TreeNode with value 5
insert 5 10 7 15 12 20
insert 7
insert 15
insert 12
insert 20
remove 5
quit

Sample Input Remove (2 Children) Sample Output


insert 10 Deleting TreeNode with value 17
insert 5 10 5 0 7 17 12 20
insert 0 Deleting TreeNode with value 12
insert 7 12 5 0 7 17 20
insert 15
insert 12
insert 20
insert 17
remove 15
remove 10
quit

Page 5 of 6
Introduction to Data Structures and Algorithms
COMS1017 Lab Exam November 5, 2015

Question 4 Binary Heaps [10 Marks]


There is a partially implemented Binary Heap in binaryheap.cpp.

1. Implement the popMax function. [10]

Sample Input Sample Output


push 20 Print: 20 15 10 7 0 9
push 15 Max: 20
push 10 Print: 15 9 10 7 0
push 7 Max: 15
push 0 Print: 10 9 0 7
push 9 Max: 10
print Print: 9 7 0
popMax Max: 9
print Print: 7 0
popMax Max: 7
print Print: 0
popMax Max: 0
print Print:
popMax
print
popMax
print
popMax
print
quit

Page 6 of 6

You might also like