Exam Paper Fa1 2022s1 Memo
Exam Paper Fa1 2022s1 Memo
Exam Paper Fa1 2022s1 Memo
YEAR: 2022
SEMESTER: 1
ASSESSMENT: FA 1 MEMO
SPECIAL REQUIREMENTS
NONE
NON-PROGRAMMABLE POCKET CALCULATOR
SCIENTIFIC CALCULATOR
COMPUTER ANSWER SHEET
GRAPH PAPER
DRAWING INSTRUMENTS
OTHER: COMPUTER
ANSWER BOOK
Theory
a. Use the Word to answer the questions for section A and convert it to PDF before submission.
b. Show all steps where applicable.
c. Write legibly
Practical
a. Use Java to create the programs required in section B
b. Write your code legibly and provide comments
c. To evidence your output, capture the output of all the various functionality requirements into a word
document (correctly numbered) and save it as PDF at the end.
d. You are then required to upload the PDF output file and the associated code (.java files) into Britespace.
Question 1 [20]
1.1 Insert the values 5, 9, 7, 3, 8, 12, 6, 4, and 20 into a binary search three drawing a different tree for each
stage of the insertion. Only show that final tree. You may scribble on paper but need to draw the final tree
digitally. (10)
Answer:
[allocate 10 marks for the correct answer (i). No partial marks applicable]
Deleting any node requires one to recalculate the balance factors to determine if the tree is still balanced
after the deletion. If not balanced, one needs to balance it out. When deleting a node with children,
replace that node with its inorder predecessor (largest element from the left subtree) or inorder successor
(smallest element from the right subtree).
1.3 What is the formula to calculate the balance factor for AVL trees? (2)
1.4 What is the acceptable balance factor for AVL trees (2)
3 DTD117V – FA1 MEMO (2022S1)
Answer: between -1 and 1, i.e. -1, 0 or 1.
1.5 What colour should the root node of a Red-black tree be? (1)
Answer: Black.
Question 2 [30]
2.1 Distinguish between linear and non-linear data structures and provide at least one example of each.
(6)
Answer:
Linear data structures: When all of elements are arranged in the linear order, with each element has the
successors and predecessors except the first and last element. Queue.
Non-linear data structures: A non-sequential data structure where each item or element is connected with
two or more other items in a non-linear arrangement. AVL tree.
Answer:
Time Complexity: Running time of the program as a function of the size of input
Space Complexity: Amount of computer memory required
2.3 List and distinguish between three types of linked list data structures. (9)
Answer:
Singly Linked List - made up of two field node with the last element pointer field being NULL and a pointer
to first element (start). Navigation/traversal is one way.
Doubly Linked List - made up of three field node with pointers in both directions. The last element pointer
field is NULL, it has a pointer to first element (start) and the pointer to prev for first element is NULL.
Navigation/traversal is bi-directional
Circularly Linked List - made up of two field node similar to a singly linked list. It has a pointer to first
element (start) and simulates circular traversal. The last element pointer field is points to first element.
2.4 Discuss a double ended queue and its variations. You may also utilize diagrams to clarify certain points.
(6)
Answer:
In a double ended queue, insertion and deletion done at both ends. It can be used as a palindrome checker
if one is removing elements from both ends at the same time. It can be used both as Stack and Queue but
does not follow FIFO principle.
There are two types of the double ended queue data structure. The first is "input restricted" in which
insertion happens only on one end while deletions can happen on both ends.
The second type is "output restricted" in which insertion happens on both ends but deletion happens only
on one end.
2.5 Distinguish between depth first search and breadth first search. (5)
Answer:
Depth-First Search (DFS) Algorithm: It starts with the root node and first visits all nodes of one branch as
deep as possible of the chosen Node and before backtracking, it visits all other branches in a similar
fashion. There are three sub-types under Depth-First Search, namely, inoder, preorder and postorder
traversals.
Breadth-First Search (BFS) Algorithm: It also starts from the root node and visits all nodes of current depth
before moving to the next depth in the tree.
Question 3 [30]
3.1 Develop a simple java program to demonstrate the use of the min heap. Construct a min heap using the
following elements or insert the following elements into a min heap: the elements: 10, 20, 30, 1, 2, 3, 4, 11,
21, 41. Display the tree or elements of the tree after its construction. (20)
Answer
2 3
11 10 30 4
20 21 41
OR
The values should be in the following order: 1, 2, 3, 11, 10, 30, 4, 20, 21 and 41.
3.2 Display the array index of the node with value 11? (2)
Answer: index 3
3.3 Delete a value from the min heap and display the elements of the heap. (8)
Develop a program that uses a queue data structure to perform the functions below. You should use the Java
built-in package to develop the program. Only if you do not know how to use the built-in package should you
develop the logic behind the different functions. Make sure that your program is intuitive for a user friendly. You
might need more than one queue or data structure to perform some of the operations.
4.1 Create and initialise a queue data structure named TeamsQueue with the following values: Sundowns,
RoyalAM, City, Pirates, Chiefs, Stellenbosch, Amazulu, Supersport, Arrows and Sekhukhune using the
appropriate function. Each initialisation operation should display a message such as “value XX added into
queue TeamsQueue”. (7)
[allocate up to 7 marks for correctly creating, initializing the queue and display appropriate message]
Answer: Sundowns, RoyalAM, City, Pirates, Chiefs, Stellenbosch, Amazulu, Supersport, Arrows,
Sekhukhune
4.3 Add the value Galants to the queue and display the resultant output (values) of the queue. (2)
Answer: Sundowns, RoyalAM, City, Pirates, Chiefs, Stellenbosch, Amazulu, Supersport, Arrows,
Sekhukhune, Galants
4.4 Remove the first element in the queue and display the resultant output (values) of the queue. (2)
Answer: RoyalAM, City, Pirates, Chiefs, Stellenbosch, Amazulu, Supersport, Arrows, Sekhukhune, Galants
Answer: Size = 10
4.6 Update the values City to Cape Town City and display the resultant output (values) of the queue. (3)
Answer: RoyalAM, Cape Town City, Pirates, Chiefs, Stellenbosch, Amazulu, Supersport, Arrows,
Sekhukhune, Galants
[allocate 2 marks for the update and 1 mark the correct display]
4.7 Update the values RoyalAM to Royal AM and display the resultant output (values) of the queue. (3)
Answer: Royal AM, Cape Town City, Pirates, Chiefs, Stellenbosch, Amazulu, Supersport, Arrows,
Sekhukhune, Galants
[allocate 2 marks for the update and 1 mark the correct display]
The End