0% found this document useful (0 votes)
7 views

Final - Fall 2020 (Answered)

Uploaded by

DizzyDragon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Final - Fall 2020 (Answered)

Uploaded by

DizzyDragon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Qatar University

Dept. of Computer Science & Engineering Data Structure (CMPS303)


Fall 2020

Final Exam
ID:__________________ Name:_______________
Q1) Circle T for correct statement and F for false one (1 pt. for each)
1. [ T / F] Heap sort is in-place sorting algorithm.
2. [ T / F] The complexity of worst case when searching in BST is O(n).
3. [ T / F] In heap, the root is always the first element inserted.
4. [ T / F] In hash table that is based on separate chaining, load factor can be greater than one.
5. [ T / F] The adjacency matrix that represents directed graph cannot be symmetric around the main
diagonal.
6. [ T / F] The best case of searching for an element in hash table based on linear propping is O(1).
7. [ T / F] All nodes in a heap has two children except the leaves.
8. [ T / F] We use a queue when implementing bridth first search in a graph.
9. [ T / F] Heap sort is faster than insertion sort.
10. [ T / F] In singly-linked list implementation of stack, the complexity of pop operation is O(1).
11. [ T / F] Separate chaining hash table uses more space than open addressing.

Q2) a) Show in detail steps (draw trees) how the following numbers will be inserted into heap one after
another :(2 pts.)
3,6,1,7,8,9

b) Show (in steps) what will happen when removing the maximum from the following heap (2 pts).
Q3) Using quadratic probing, and double hashing, h(k)= k % 7 and h2(k)=5-(k%5), show how the
following elements will be inserted (one after another) in the below hash table

13,6,20,10,15 0
9,5,33,26,19 0 26 (5pts.)
1 10
1
2 15
2 9
3 6
3
4 20
4 19
5
5 5
6 13
6 33

Quadratic
Double hasing

Q4) Given the following graph:


a) Starting by node F , shows the order of graph vertices when
using DFS (1pts)
FADBEC

b) Starting by A, shows the order of graph vertices when using


BFS (1pts)
FACDBE

c) Show the adjacency matrix that represents this graph (1pts)

A B C D E F
A 0 1 1 0 1
B 1 0 0 1 1
C 0 0 0 0 1 1
D 1 1 0 0 1 0
E 0 1 1 1 0 0
F 1 0 1 0 0 0
d) Show the steps of using Dijkstra’s algorithm to calculate shortest path from vertex F to other vertices.
(3pts)

A B C D E F
9 ∞ 4 ∞ ∞ 0
9 ∞ 4 ∞ 6 0
9 7 4 ∞ 6 0
9 7 4 11 6 0
9 7 4 11 6 0

Q5) Using only one extra data structure if necessary, write method swapTopBottom that receives a stack of
elements and swap the element on the top with the element in the bottom, the order of the remaining
elements should not be changed.

public void swapTopBottom(Stack<E> s) (5pts.


{
Stack<E> tmp= new LinkedStack<E>();
E top=s.pop();
while (!s.isEmpty())
tmp.push(s.pop());
E bottom=tmp.pop();

s.push(top);
while(!tmp.isEmpty())
s.push(tmp.pop());

s.push(bottom);

Q6) To class Tree, add method oneChild (int n ) that returns how many nodes with one child in level n (This method
should call another recursive method, assume root in level 1).
(5pts.
public class Node <E> {
int key;
E data;
public int oneChild(int n) Node<E> leftChild;
{ Node<E> rightChild;
return oneChild(root,n); ….
}
} public class Tree<E> {
public int onChild(Node<E> n, int level ) private Node<E> root;
{ …..
if (n==null)
return 0;
if (level==0)
if (n.lefChild!=null && n.rightChild==null || n.leftChild==null and n.rightChild!=null)
return 1;
else
return 0;
else
return oneChild(n.leftChild)+oneChild(n.rightChild)
}

Q7) Using recursion, and without any extra data structure, write method totalOdd(Stack<Integer> s) that
receives a stack of integers and returns the total of odd elements in the stack. The order of the elements in the
stack should not be changed after calling this method.

public int totalOdd(Stack<Integer> s) (5pts.


{
int sum=0;
if (s.isEmpty())
return 0;
int n=s.pop();
if (n%2==1)
sum=n+totalOdd(s);
else
sum=totalOdd(s);
s.push(n);
return sum;

Q8) Given the following array: (3 pts).


0 1 2 3 4 5 6 7
12 9 8 5 1 6 2

a) Does it represent a heap? Yes [ ] No[X ]


If no, why?

Because it is not complete

b) Does it represent a BST? Yes [ ] No[ x ]


If no, why?

Because it violates the condition of BST where 8 is right child of the root and it is less than the parent
(root).
Q9) if you have the following binary tree (3 pts).
Write the order of the elements when traversing this tree using
a) Inorder:DNBEALCHGK
b) Post-order: NDEBLHKGCA
c) Pre-order:ABDNECLGHK

Q10) Why do we use addFirst/removeFirst to implement push/pop when implementing stack using singly
linked list instead of using addLast/removeLast. (1pt.)

Because removeLast operation has complexity O(n), while remveFirst has complexity O(1), therefore using
addFirst/removeFirst means both operation will be O(1).

Q11) Explain the advantage(s) of using heap to implement priority queue instead of sorted and unsorted
array? (1.5 pt).

If we use heap, the complexity of the operations insert, removeMax will be O(log n), however if we use
sorted array the operation of inserting and removeMax will be O(n).
With unsorted array the insertion will be O(1), however removeMax will be o(n).

B) Show with an example how would the worst case happen in BST (1.5 pt.)
The worst case will happen if the elements always are inserted in one side (left or right )

In this case, all operations will be O(n).

You might also like