Information Science 2 — 2015 — Mid

advertisement
Name:
ID:
Information Science 2 — 2015 — Mid-Semester Test
1. [4 points] Circle all the keys that are compared during a binary search for the target
‘Z’ in the following sorted array:
index:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
key:
A
C
D
E
F
I
L
M
O
P
T
U
X
Y
Z
↑
2. Two stacks can be used to evaluate an arithmetic expression. One stack stores the operands
(values), and the other stack stores the operators. For example, the expression ‘4+5*6-7’
would be evaluated as follows:
4 + 5 * 6 - 7
45
6
5
4
4
operands operators
+
operands operators
4
+
operands operators
5
*
5
*
4
+
4
+
operands operators
operands operators
7
30
-
4
+
operands operators
30
-
4
+
operands operators
23
4
+
operands operators
27
operands operators
operands operators
[4 points] In the empty stacks below, draw the steps needed to evalute the expression ‘2+3*4*5’:
2 + 3 * 4 * 5
4
3
2
2
operands operators
+
operands operators
2
+
operands operators
3
*
2
+
operands operators
3
*
2
+
operands operators
5
12
*
2
+
operands operators
12
*
2
+
operands operators
60
2
+
operands operators
62
operands operators
operands operators
2
Page 1 of 4
8
Information Science 2 — 2015 — Mid-Semester Test
3. [2 points] Write the adjacency matrix for the following graph:
Adjacency Matrix =
A
A
B
A
C
E
D
B
1
C
1
B
C
1
1
D
E
1
1
1
1
D
1
E
1
2
2
[4 points] Calculate the order, size, maximum size, and density of the graph:
Order
=
5
Size
=
7
Maximum size
=
10
Density
=
7/10
[4 points] Draw a box around T (true) or F (false) to indicate whether each statement is correct:
The above graph is complete?
T
The above graph is connected?
The above graph is simple?
F
T
F
T
The above graph is traversable?
F
T
F
4. [4 points] Write a sequence of numbers to indicate the order in which the nodes of the tree would
be processed during each kind of traversal:
4
2
1
7
3
6
5
8
Breadth-first:
4
2
7
1
3
6
8
5
Depth-first pre-order :
4
2
1
3
5
7
6
8
Depth-first in-order :
1
2
3
5
4
6
7
8
Depth-first post-order :
1
5
3
2
6
8
7
4
[3 points] Write the answers to the following questions about the above tree:
The above tree is a binary tree (write YES or NO):
YES
The above tree is an ordered tree (write YES or NO):
NO
The path length from the root to node 3 is:
2
17
Page 2 of 4
2
Name:
ID:
5. [4 points] Write the remaining steps when sorting the following array using selection sort:
5
1
2
7
4
6
3
1
5
2
7
4
6
3
1
2
5
7
4
6
3
1
2
3
7
4
6
5
1
2
3
4
7
6
5
1
2
3
4
5
6
7
.
&
6. [8 points] Draw a box around T (true) or F (false) to indicate whether each of the following
statements is true or false:
1)
a queue is a last-in first-out (LIFO) data structure
T
2)
enqueue and dequeue are operations on a FIFO data structure
T
F
3)
linear search over n elements requires, on average, n/2 comparisons
T
F
4)
{ a, b, e, d } is a permutation of { a, b, c, d }
T
F
5)
an ordered linked list can be searched using a binary search
T
F
6)
a parity bit is used for error detection
7)
a doubly-linked list can be traversed in only one direction
8)
manipulating a doubly-linked list is more complicated than for a singly-linked list
T
T
T
F
F
F
F
2
Page 3 of 4
12
Information Science 2 — 2015 — Mid-Semester Test
7. [2 points] Draw the ordered binary tree for the following string: ‘ P U S H D O W N ’
P
H
U
D
O
S
W
N
8. [4 points] In the space below, draw the binary tree which represents the Huffman encoding for
the following string (treat each letter as a separate symbol): ‘ P E P P E R P I Z Z A ’
symbols =
binary tree for Huffman encoding =
symbol frequency encoding
P
4
11
E
2
101
R
1
010
I
1
011
Z
2
00
11
0
4
1
A
1
1
7
0
0
2
100
0
1
R
1
3
1
0
1
I
1
A
1
2
E
2
Z
4
P
[2 points] Using the binary tree that you created above, write the Huffman binary encoding for
the string: ‘ P E P P E R P I Z Z A ’
Huffman binary encoding for ‘ P E P P E R P I Z Z A ’:
P E P P E R P I Z Z A
11 101 11 11 101 010 11 011 00 00 100
[1 bonus point] Assume the original string is represented with 8-bit ASCII characters. What is the
compression ratio for the encoded string?
88/27 = 3.2593
2
8
Page 4 of 4
Name:
ID:
Information Science 2 — 2015 — Mid-Semester Test
1. [4 points] Circle all the keys that are compared during a binary search for the target
‘A’ in the following sorted array:
index:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
key:
A
↑
C
D
E
F
I
L
M
O
P
T
U
X
Y
Z
2. Two stacks can be used to evaluate an arithmetic expression. One stack stores the operands
(values), and the other stack stores the operators. For example, the expression ‘4+5*6-7’
would be evaluated as follows:
4 + 5 * 6 - 7
45
6
5
4
4
operands operators
+
operands operators
4
+
operands operators
5
*
5
*
4
+
4
+
operands operators
operands operators
7
30
-
4
+
operands operators
30
-
4
+
operands operators
23
4
+
operands operators
27
operands operators
operands operators
[4 points] In the empty stacks below, draw the steps needed to evalute the expression ‘2+3+4*5’:
2 + 3 + 4 * 5
3
2
2
operands operators
+
operands operators
2
4
+
operands operators
5
+
operands operators
5
+
operands operators
5
4
*
5
+
operands operators
4
*
5
+
operands operators
20
5
+
operands operators
25
operands operators
operands operators
3
Page 1 of 4
8
Information Science 2 — 2015 — Mid-Semester Test
3. [2 points] Write the adjacency matrix for the following graph:
Adjacency Matrix =
A
A
B
A
B
C
B
C
D
E
2
1
2
1
C
D
E
D
E
1
2
1
2
[4 points] Calculate the order, size, maximum size, and density of the graph:
Order
=
5
Size
=
6
Maximum size
=
10
Density
=
6/10
[4 points] Draw a box around T (true) or F (false) to indicate whether each statement is correct:
The above graph is a multigraph?
The above graph is traversable?
The above graph is unconnected?
The above graph is complete?
T
F
T
F
T
F
F
T
4. [4 points] Write a sequence of numbers to indicate the order in which the nodes of the tree would
be processed during each kind of traversal:
5
3
1
7
4
2
6
8
Breadth-first:
5
3
7
1
4
6
8
2
Depth-first pre-order :
5
3
1
2
4
7
6
8
Depth-first in-order :
1
2
3
4
5
6
7
8
Depth-first post-order :
2
1
4
3
6
8
7
5
[3 points] Write the answers to the following questions about the above tree:
The above tree is a binary tree (write YES or NO):
YES
The above tree is an ordered tree (write YES or NO):
YES
The path length from the root to node 5 is:
3
17
Page 2 of 4
0
Name:
ID:
5. [4 points] Write the remaining steps when sorting the following array using bubble sort:
5
1
2
7
3
4
6
1
5
2
7
3
4
6
1
2
5
7
3
4
6
1
2
5
3
7
4
6
1
2
5
3
4
7
6
1
2
5
3
4
6
7
1
2
3
5
4
6
7
1
2
3
4
5
6
7
.
&
6. [8 points] Draw a box around T (true) or F (false) to indicate whether each of the following
statements is true or false:
1)
a queue is a first-in first-out (FIFO) data structure
T
2)
push and pop are operations on a FIFO data structure
T
F
3)
binary search over n elements requires, on average, n/2 comparisons
T
F
4)
{ a, b, c, d } is a permutation of { a, b, c, d }
T
F
5)
an unordered linked list can be searched using a linear search
T
F
6)
a parity bit is used for data compression
7)
a singly-linked list can be traversed in only one direction
8)
manipulating a singly-linked list is more complicated than for a doubly-linked list
T
T
T
F
F
F
F
3
Page 3 of 4
12
Information Science 2 — 2015 — Mid-Semester Test
7. [2 points] Draw the ordered binary tree for the following string: ‘ X W N G R D F V ’
X
W
N
G
R
D
V
F
8. [4 points] In the space below, draw the binary tree which represents the Huffman encoding for
the following string (treat each letter as a separate symbol): ‘ C O C A C O L A B A D ’
symbols =
binary tree for Huffman encoding =
symbol frequency encoding
C
3
01
O
2
111
A
3
10
L
1
000
11
0
B
1
001
D
1
110
1
5
6
1
0
1
2
0
1
L
0
3
1
0
1
B
1
D
1
2
O
3
C
3
A
[2 points] Using the binary tree that you created above, write the Huffman binary encoding for
the string: ‘ C O C A C O L A B A D ’
Huffman binary encoding for ‘ C O C A C O L A B A D ’:
C O C A C O L A B A D
01 111 01 10 01 111 000 10 001 10 110
[1 bonus point] Assume the original string is represented with 8-bit ASCII characters. What is the
compression ratio for the encoded string?
88/27 = 3.2593
3
8
Page 4 of 4
Download