0% found this document useful (0 votes)
58 views54 pages

Tutorals Exercises

Uploaded by

xbaby y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views54 pages

Tutorals Exercises

Uploaded by

xbaby y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

CS3334 - Data Structures

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.

void List::insert(int i, int d)


class List
{
{
...
class ListNode public:
}
{ List( String );
public:
List();
ListNode( int );
ListNode( int, ListNode *); int size();
ListNode *get_Next() ... //various member functions

private:
private:
int data; ListNode *first;
ListNode *next; string name;
}; }
void List::insert(int i, int d)
{
if (i < 0)
return;

ListNode* new_node = new ListNode(d);


if (i == 0) {
new_node->next = first;
first = new_node;
return;
}

ListNode* prev = nullptr;


ListNode* curr = first;
int pos = 0;

// Traverse to the (i-1)-th element


while (curr != nullptr && pos < i) {
prev = curr;
curr = curr->get_Next();
pos++;
}

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;

ListNode* prev = nullptr; ListNode* curr = first;


int pos = 0;

while (curr != nullptr && pos < i) {


prev = curr;
curr = curr->get_Next();
pos++;
}
ListNode* prev_i = prev; ListNode* curr_i = curr;

// Reverse the elements from i-th to j-th


ListNode* next_node = nullptr;
while (curr != nullptr && pos <= j) {
next_node = curr->get_Next();
curr->next = prev;
prev = curr;
curr = next_node;
pos++;
}

if (prev_i != nullptr) prev_i->next = prev;


else first = prev;
curr_i->next = curr;
}
Exercise 3 Stack
Given a stack, complete a function
delete(d) which removes all occurrences of an item d in a stack.
For example, ”5 2 3 5 4” (4 is on top) with d = 5 will become -> ”2 3 4" ) (4 is on
top)
// Stack.h
#include “stdlib.h”
{ void Stack::delete(int d)
public class Stack
{
{
public: ...
Stack(); }
bool IsEmpty();
bool IsFull();
void push(int);
int pop();
int top();
private:

// maybe array or linked based implementation
};
}
Exercise 3 Stack
Given a stack, complete a function
delete(d) which removes all occurrences of an item d in a stack.
For example, ”5 2 3 5 4” (4 is on top) with d = 5 will become -> ”2 3 4" ) (4 is on
top)
void Stack::delete(int d)
{
std::stack<int> tempStack;

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}

void reverseFirstK(int k, queue<int>& Queue) { . . . }


1. Create an empty stack. void reverseFirstK(int k, queue<int>& Queue)
{
if (Queue.empty() == true || k > Queue.size())
2. One by one dequeue return;
if (k <= 0)
first K items from queue return;
and push the dequeued
items to stack. stack<int> Stack; // step 1
for (int i = 0; i < k; i++) { // step 2
Stack.push(Queue.front());
Queue.pop();
3. Enqueue the contents }
of stack at the back of the
queue while (!Stack.empty()) { // step 3
Queue.push(Stack.top());
Stack.pop();
4. Dequeue (size-k) }

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

1. What’s the height of node 6?


2. What’s the height of node 3?
3. What is the depth of node 6?
4. What’s the depth of node 3?
5. What’s the preorder traversal of the tree?
Exercise 2 for Tree
• Create a binary search tree according to the input insertion
sequence:
{3, 5, 0, 2, 7, 6, 8, 9, 4, 1}.
Exercise 2 for Tree
• Create a binary search tree according to the input sequence:
{3, 5, 0, 2, 7, 6, 8, 9, 4, 1}.
• Then delete 3 from the constructed binary search tree.
Exercise 3 for Tree
• What’s the running time for the function “countleaf”? O(n).
Exercise 4 for Tree
• Reconstruct the binary tree whose postorder and inorder are
as follows.
– Postorder: BDFECA
– Inorder: BACDFE
Exercise 4 for Tree
• Reconstruct the binary tree whose postorder and inorder are
as follows.
– Postorder: BDFECA
– Inorder: BACDFE
Exercise 5 for Tree
• Showing the postorder traversal of the binary tree with
preorder and inorder shown as follows.
– Preorder: ABCDE
– Inorder: BDACE
Exercise 6 for Tree
• find the level of Node “ptr” in a binary tree with root node
“root”. Using the recursive function

struct Node {
int data;
struct Node *left, *right;
};

int level (Node* root, Node* ptr, int lev) {


… …
}
Exercise 6 for Tree
• find the level of Node “ptr” in a binary tree with root node
“root”. Using the recursive function.
struct Node {
int data;
struct Node *left, *right;
};

int level (Node* root, Node* ptr, int lev) {

// base cases
if (root == NULL)
return 0;
if (root == ptr)
return lev;

// Return level if Node is present in left subtree


int l = level(root->left, ptr, lev + 1);
if (l != 0)
return l;

// Else search in right subtree


return level(root->right, ptr, lev + 1);
}
Exercise 1 Check for Symmetric
• Given a binary tree, check whether it is a mirror of itself
(i.e., symmetric).

Example:

(this binary tree is symmetric) (this binary tree is not symmetric)


struct Node {
int key;
struct Node *left, *right;
};

bool isMirror(Node* root1, Node* root2) {


...
}
Exercise 2 Check for Children Sum
• Given a binary tree, the task is to check for every node, and see
if its value is equal to the sum of values of its immediate left
and right child. For NULL values, consider the value to be 0.

Example:

The given tree satisfies the children sum property


Exercise 3 Level Order Traversal
• Ttraverse a Tree such that all nodes present in the same level
are traversed completely before traversing the next level.

Example:

Output: 1 2 3 4 5
struct Node {
int data;
struct Node *left, *right;
};

// Iterative method to find height of Binary Tree


void printLevelOrder(Node* root)
{
. . .
}
Exercise 1
• Two player are playing a game where player 1 takes the first move and then
the second player. Based on outcome, the total amount of 10 dollars will be
divided. The outcome value is the amount of dollars player 1 can get if the
game process follows the path from root to that leaf in the game tree below.
• This game tree illustrates this game and 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.

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.

First Step: Build a max-heap.


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.

Second Step: Delete root (max).


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.

Second Step: Delete root (max).


Exercise 3
How many swaps do you need to do when doing quicksort on {2,
1, 4, 3, 5, 6}?
Exercise 3
How many swaps do you need to do when doing quicksort on {2,
1, 4, 3, 5, 6}?
Exercise 4
Explain how mergesort works on 2, 1, 4, 3, 5, 6.
Exercise 4
Explain how mergesort works on 2, 1, 4, 3, 5, 6.
Exercise 5
Explain how radix sort works on 003, 044, 286, 360, 202, 011, 023.
Exercise 5
Explain how radix sort works on 003, 044, 286, 360, 202, 011, 023.
Exercise 5
Explain how radix sort works on 003, 044, 286, 360, 202, 011, 023.
Exercise 5
Explain how radix sort works on 003, 044, 286, 360, 202, 011, 023.

You might also like