Tutorals Exercises
Tutorals Exercises
Tutorials
Exercise 1 Manipulate List
Given a singly linked list, complete a function
insert(i, d) which inserts a new node with data d at position i of a Singly Linked List.
For example, ”7 5 3 1” with i=1, d=2 will become -> ”7 2 5 3 1 " )
If i is larger than the current list size, we do nothing.
if (curr != nullptr)
prev->next = new_node;
new_node->next = curr;
}
Exercise 2 Manipulate List
Given a singly linked list, complete a function
reverse(i, j) which reverses elements from i-th element to j-th element(i,j inclusive).
For example, ”0 1 2 3 4 5” with i=1, j=3 will become -> ”0 3 2 1 4 5" )
void List::reverse(int i, int j)
{
...
} class List
{
class ListNode public:
{
List( String );
public:
ListNode( int ); List();
ListNode( int, ListNode *); int size();
ListNode *get_Next() ... //various member functions
…
private: private:
int data; ListNode *first;
ListNode *next; string name;
};
}
void List::reverse(int i, int j)
{
if (i >= j || first == nullptr || first->get_Next() == nullptr)
return;
while (!IsEmpty())
{
int top = pop();
if (top != d)
tempStack.push(top);
}
while (!tempStack.empty())
{
int top = tempStack.top();
tempStack.pop();
push(top);
}
}
Exercise 1
Determine whether each of the following characteristics apply to
a stack, a queue, both, or none.
a. An element is inserted at a special place called the top.
b. An element is inserted at a special place called the rear.
c. The structure can hold only one type of data element.
d. An element is deleted at the front.
e. The i-th position may be deleted.
f. An element is deleted at the top. A is stack
g. The structure is a LIFO structure. B is queue
C is for both
h. The structure is a FIFO structure. D is for queue
E is for none
F is for stack
G is for stack
H is for queue
Exercise 2
Given an integer k and a queue of integers, the task is to reverse
the order of the first k elements of the queue, leaving the other
elements in the same relative order.
Only following standard operations are allowed on queue.
• enqueue(x) : Add an item x to rear of queue
• dequeue() : Remove an item from front of queue
• size() : Returns number of elements in queue.
• front() : Finds front item but not remove it.
Example:
Input: k = 5, queue = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100} (10 is the front)
Output: {50, 40, 30, 20, 10, 60, 70, 80, 90, 100}
elements from the front for (int i = 0; i < Queue.size() - k; i++) { // step 4
and enqueue them one by Queue.push(Queue.front());
one to the queue. Queue.pop();
}
} Complexity?
Exercise 3
According to lecture, a hash table consists of several components.
Which of these was NOT one of those components?
1. An array
2. Collision handling
3. Encryption
4. A hash function
Exercise 4
Given the following input
(4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199)
and the hash function x mod 10,
which of the following statement(s) are true?
1. 9679, 1989, 4199 hash to the same value
2. 1471, 6171 hash to the same value
3. All elements hash to the same value
4. Each element hashes to a different value
Exercise 5
The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially
empty hash table of length 10 using open addressing with hash
function h(k) = k mod 10 and linear probing.
What is the resultant hash table? 0
1
2 12
3 13
4 2
5 3
6 23
7 5
8 18
9 15
Exercise 1 for Tree
15
6 18
3 7 17 20
2 4 13
struct Node {
int data;
struct Node *left, *right;
};
// base cases
if (root == NULL)
return 0;
if (root == ptr)
return lev;
Example:
Example:
Example:
Output: 1 2 3 4 5
struct Node {
int data;
struct Node *left, *right;
};
MAX
MIN
MAX
Exercise 2
• Given the following game tree and it will be searched from left
to right. Describe the best move for player 1 in the first step
(assume using the minimax algorithm). And list all each leaf
nodes that will be pruned by alpha-beta pruning.
Player 1
Player 2
Player 1
2 1 6 1 8 3 5 2 3 4 2 6
Exercise 3
• Given the following AVL tree, show how it will change after
inserting 45.
30
5 35
32 40
Exercise 3
Exercise 4
• Insert the following numbers into an empty AVL tree.
{45, 70, 35, 3, 74, 25, 81, 60} and show the results.
45
25 74
3 35 70 81
60
Exercise 1
Showing each step of find(27) in the following splay tree.
22
8 38
15 27 45
Exercise 2
Given the disjoint set as below (array-based tree implementation).
Show the newly generated array after we perform Find(8) with
path compression.
1 3 3 -1 5 3 5 5 5 3 -1
Exercise 3
Given the disjoint set array as below, what will the array look like
after the operation Union (1, 4) and Union(7, 9)?
Assume we perform the operations with Union-by-Height, and
when the heights are the same, we connect the first tree to the
second tree.
1 3 3 -1 5 -1 5 5 5 -1 -1
Exercise 4
Please show the adjacency list representation for the following graph. And do the DFS and
BFS (starting from node 1). Choosing the smaller one when there are multiple choices.
Exercise 1
What is the adjacency matrix of the following graph?
How would you do BFS and DFS search (starting from node 1)?
When there are multiple choices, visit the vertices in alphabetical
order.
Exercise 1
What is the adjacency matrix of the following graph?
How would you do BFS and DFS search (starting from node 1)?
When there are multiple choices, visit the vertices in alphabetical
order.
DFS: 1, 3, 2, 4, 6, 5, 7
BFS: 1, 3, 4, 5, 7, 2, 6
Exercise 2
Do Heap sort with using only one slot of extra storage on the
following numbers with their order as: 2, 1, 4, 3, 5, 6.
Exercise 2
Do Heap sort with using only one slot of extra storage on the
following numbers with their order as: 2, 1, 4, 3, 5, 6.