Department of Computer Science

Department of Computer Science,
Data Structures (CSC212),
Final Exam
st
1 Semester 1431-32H
16/2/1432 (20/1/2011G)
Instructors: Dr. Gamal Shorbagy, Dr. Inayatullah Shah, Dr. Muhammad Hussain,
Dr. Khalil el Hindi,
Time: 3 hours
Marks: 100
Question 1. (10 + 8 + 8 + 4 = 30 marks)
(a) Select the correct choice for each of the following questions.
(i)
The data structure that is appropriate to use when a LIFO behavior is
required is: (A) Stack (B) Queue (C) Tree (D) List
(ii)
In the circular queues: (A) size = tail + 1 (B) size = tail (C) size = tail –
head (D) None of the above
(iii) A heap can be used to represent: (A) Stacks (B) Queues (C) Trees (D)
Priority queues.
(iv)
If we want to store 20 student records in a hash table with hashing
function 𝐻(π‘˜π‘’π‘¦) = π‘˜π‘’π‘¦ π‘šπ‘œπ‘‘ π‘‡π‘Žπ‘π‘™π‘’π‘†π‘–π‘§π‘’, the best value for TableSize is:
(A) 20 (B) 23 (C) 1l9 (D) 28
(v)
π‘˜π‘’π‘¦
The hash function 𝐻(π‘˜π‘’π‘¦) = (10000) π‘šπ‘œπ‘‘ 100 will select the digits of
key: (A) 5th , 6th and 7th digit from the right. (B) 5th and 6th digit from the
right. (C) first four digits from the right (D) None of the above.
(vi)
Collisions make a hash table inefficient because of: (A) the additional
search needed during an insert operation. (B) the additional search needed
during both insert and findkey operations. (C) the increase in space
complexity (D) None of the above.
(vii) A min-heap: (A) is a complete binary tree (B) has the smallest the key
value at the root (C) is a height balanced tree (D) all of the above
(viii) If an array is used to represent a heap and a key value is stored at location
11 then the parent of this value is stored at location: (A) 5 (B) 6 (C) 10 (D)
A and B are correct
(ix)
We do not need to perform rotations during an insert operation in an AVL
tree: (A) if all nodes have a balance factor of 0 (B) if all nodes have a
balance factor of 1 (C) if all nodes have a balance factor of -1 (D) None of
the above
(x)
Using an adjacency matrix to represent a graph is not the best method in
terms of the memory usage, if the graph has: (A) too many vertices
(nodes) a very few edges (arcs) (B) too many arcs and very few vertices
(C) we have many vertices and many arc (D) we have too many vertices
(nodes) a very few edges (arcs)
(b) Use the hash function 𝐻(π‘˜π‘’π‘¦) = π‘˜π‘’π‘¦ π‘šπ‘œπ‘‘ 11 to insert the integer keys: 11, 22,
33, 31, 28, 4, 45, 27, 59, 79, 35 in a hash table. Use linear re-hashing as the
collision resolution strategy. What is
the average number of probes required
to insert the keys?
(c) Insert the following values in the B+
tree of order 3 shown. Draw the tree
after each insert operation. (i) insert 30
and (ii) insert 50.
(d) Represent the graph shown below as an
(i) adjacency matrix (ii) adjacency list.
Question 1 (b)
Index
0
Value
11
#probes 1
1
22
2
2
33
3
3
45
3
4
4
1
5
27
1
6
28
1
7
59
4
8
79
7
9
31
1
10
35
9
Average probes = 33/11
Question 1 (c)
After inserting 30 the tree should be as follows:
22:_
11:16
1, 8
11, 15
After inserting 50:
31:58
16, 17,18
22, 23, 30
31,41,52
58, 51, 61
22:50
11:16
1, 8
11, 15
31:_
16, 17,18
58:_
22, 23, 30
31, 41
50, 52
58, 51, 61
Question 1 (d)
(i)
adjacency matrix
A
0
0
0
0
0
0
A
B
C
D
E
F
(ii)
B
1
0
1
1
0
0
C
1
0
0
0
0
0
D
0
1
1
0
0
0
E
0
0
1
1
0
0
F
0
0
0
1
1
0
adjacency list: can be induced from the adjacency matrix
Question 2. (4 + 6 = 10 marks)
(a) For the following method, calculate the time function T(n) i.e. the total number of
steps. For the for-loops count the number of comparisons only.
int square(int n) {
int sqr = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sqr++;
return sqr;
}
Number of iterations (as long as β€œi < n;” remains true) of the first for loop is n
Number iterations (as long as β€œj < n;” remains true) of the inner loop for i = 0, 1,
…, n-1 is n, so the total number of iterations of the this loop is nxn = n2.
The statement β€œsqr++;” is executed as many times as the inner loop, so this
statement will be executed n2 times.
In view of this
T(n) = 1 + n + n2 + n2 +1 = 2 n2 + n +2
The comparison statement will also be executed when it is false. If we also count
this, then
T(n) = 1 + (n+1) + (n2+n) + n2 +1 = 2 n2 +2 n +3
(b) Find the big-O of the following functions, giving C and n0. Show the complete
working.
(i) 3𝑛2 + 7𝑛 + 4 (ii) f (n) ο€½ 2n  6n7 (iii) (2n2  3) / (n  1)
(i) In this the dominant term is 𝑛2
𝑓(𝑛) = 3𝑛2 + 7𝑛 + 4
𝑓(𝑛) ≀ 3𝑛2 + 7𝑛2 + 4𝑛2 = 14𝑛2
i.e.
𝑓(𝑛) ≀ 𝑐𝑛2 π‘“π‘œπ‘Ÿ π‘Žπ‘™π‘™ 𝑛 β‰₯ 𝑛0
where
𝑐 = 14 π‘Žπ‘›π‘‘ 𝑛0 = 1
=> 𝑓(𝑛) = 𝑂(𝑛2 )
(ii) In this case the dominant term is 2n.
f (n) ο€½ 2n  6n7 ο‚£ 2n  62n
f (n) ο‚£ 72n
i.e.
𝑓(𝑛) ≀ 𝑐2𝑛 π‘“π‘œπ‘Ÿ π‘Žπ‘™π‘™ 𝑛 β‰₯ 𝑛0
where
𝑐 = 7 π‘Žπ‘›π‘‘ 𝑛0 = 1
=> 𝑓(𝑛) = 𝑂(2𝑛 )
(iii) 𝑓(𝑛) ≀ 𝑐𝑛 π‘“π‘œπ‘Ÿ π‘Žπ‘™π‘™ 𝑛 β‰₯ 𝑛0
where
𝑐 = 5 π‘Žπ‘›π‘‘ 𝑛0 = 1
=> 𝑓(𝑛) = 𝑂(𝑛)
Question 3. (20 marks)
A library wants to automate its record keeping system. The automated record keeping
system must be able to maintain information about libraries’ books and its members. At
any time, a librarian must be able to find the location of a book. If it has been borrowed,
librarian must be able to determine who has borrowed the book and when it is due back.
For a member, a librarian must be able to determine the books the member has borrowed
and when the books are due back. A member cannot borrow more than 15 books. Assume
each book is identified by a unique bookId and each member is identified by a unique
memberId. Assume that the books are stored in a book list and the members are stored in
a member list.
(a) List the important attributes of the data elements: Book and Member.
Book: {name String; bookId int; location Location; boolean borrowed; int
whoBorrowed; Date dateBorrowed; Date dateDueBack}
Member: {name String; memberId int; books List<Book>}
(b) Write the header (or signature) of the operations: Book_Borrowed and
Book_Returned.
Method Book_Borrowed (int bookId, int memberId)
Method Book_Returned (int bookId)
(c) Specify the processing that will be done by the operations Book_Borrowed and
Book_Returned.
Method Book_Borrowed (int bookId, int memberId)
Results/Processing: The element in BookList corresponding to bookId is
accessed and its borrowed field is set to true, its whoBorrowed field is set to
memberId, its dateBorrowed field is set to the date the book was borrowed, its
dateDueBack is also set. Furthermore, element in MemberList corresponding to
memberId is accessed and bookId is inserted in the list of books (i.e. books) the
member has borrowed.
Method Book_Returned (int bookId)
Results/Processing: The element corresponding to bookId in the BookList is
accessed and its borrowed field is set of false, its dateBorrowed and dateDueBack
are reset. Furthermore, memberId of the member who had borrowed the book is
retrieved and element corresponding to this memberId is accessed in the
MemberList and bookId is removed from the list of borrowed books of this
member.
Question 4. (8 + 4 + 4 + 4 = 20 marks)
Answer the following questions for the library record keeping system of Question 3:
(a) Assuming that the book list is implemented as an AVL tree, write a method in the
book list class to find a book i.e. void findBook (int bookId).
boolean findbook (int bookId){
AVLNode<Book> p, q;
p = root;
q = root;
if (empty())
return false;
while (p!=null){
q = p;
if (p.key == bookId){
current = p; return true;
}
else if (bookId < p.key)
P = p.left;
else
p = p.right;
}
current = q;
return false;
}
(b) If the number of books in the library is about a million, write two problems that
may arise if an AVL is used to implement the book list in the automated recordkeeping system.
1. The tree may not fit in the systems main memory.
2. The tree’s height may be large, making finding a book inefficient.
(c) Assuming that it is known that retrieve and update are the most frequently
performed operations in the record keeping system, which of the following
storage structures would be appropriate to implement the book list? [doublylinked list, doubly-linked list with sentinel nodes, array-based list, circular list]
Array-based list
(d) If an array list is used to implement the booklist name the best search algorithm
for finding a book in the list.
Binary-search algorithm
Question 5. (3 + 4 + 3 = 10 marks)
(a) What is the worst case time complexity of findkey operation in a BST and in
AVL tree?
O(n)
(b) Draw a graphical representation of a doubly-linked list with header and trailer sentinel
nodes containing three data.
header
trailer
NULL
current
t
(c) Write one advantage of using lists with sentinel nodes?
Using sentinel nodes simplifies code.
Question 6. (5 + 5 = 10 marks)
(a) Write a recursive method in the binary tree class that returns the number of nonleaf nodes in a binary tree.
public int countNonLeaves(){
return countNleaves(root);
}
private int countNLeaves( BSTNode<T> p){
if (p==null) return 0;
int l = countNLeaves(p.left);
int r = countNLeaves(p.right);
if (p.left != null || p.right!=null)
return l+r+1;
else
return l+r;
}
(b) Write a recursive method in the binary tree class that displays the key values in
the binary tree in a format similar to that used by the Windows explorer; that is
the tree should be displayed as if it is lying on its left side. You must use
appropriate indentation to make it easy for the reader to distinguish the child
nodes from their parent nodes.
public void displayTree(){
dispTree(root, 1);
}
private void dispTree(BSTNode<T> p, int
if (p!=null){
dispTree(p.right, nSpaces+3);
for(int i=0;i<nSpaces;i++)
System.out.print(β€œ β€œ);
System.out.println(β€œ β€œ+p.key);
dispTree(p.left, nSpaces+3);
} //if
}
nSpaces){