Exam2 Cheat Sheet PDF
Exam2 Cheat Sheet PDF
Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 128
words, the active memory set size is 256 (i.e., at any time no more than 256 pages may be in main memory), and the replacement
strategy is LRU (the Least Recently Used page is always replaced); also assume that all two-dimensional arrays are of size (1:512,
1:512), with each array element occupying one word, provided the arrays are mapped into the main memory space in column-major
order:
for I := to 512 do
for J := to 512 do
{ A[I,J] := A[I,J] * B[512-I+1,J] }
Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 64
words, the active memory set size is 256 (i.e., at any time no more than 256 pages may be in main memory), and the replacement
strategy is LRU (the Least Recently Used page is always replaced); also assume that all two-dimensional arrays are of size (1:512,
1:512), with each array element occupying one word, provided the arrays are mapped into the main memory space in column-major
order:
for I := to 512 do
for J := to 512 do
{ A[I,J] := A[I,J] * B[512-I+1,J] }
Consider the following function CheckProds(A,B,C) where A is a 2D array of size (1:n, 1:n) and B and C are vectors of size (1:n),
n a positive integer. The body of CheckProds implements the following computations:
B[i] = A[i,1] * A[i,2] * A[i,3] * … * A[i,n]
C[i] = A[1,i] * A[2,i] * A[3,i] * … * A[n,i]
(a) Give the complete function CheckProds(A,B,C), including in particular the code of the function body and the method of
passing the three parameters.
(b) Determine for your function its time and space complexity (space complexity is the amount of space of memory required by
the function, in addition to the space for the actual parameters).
Warning: Your function must work correctly for calls such as CheckProds(D[1:n,1:n],D[1:n,1],D[1,1:n])! Here D[1:n,1] is the first column of the 2D array D, D[1,1:n] the first row.
(A)
(B)
(A) The following questions apply to a (not necessarily balanced) binary tree.
(a) Insert the following values into an initially empty search tree; show the tree after each insertion:
5 1 2 8 6 3 4 7 10 9
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
1 2 8 5
(B) The following questions apply to a balanced binary search tree (AVL tree).
(a) Insert the following values into an initially empty AVL tree; show the AVL tree after each insertion, indicating
precisely the rotations used:
5 1 2 8 6 3 4 7 10 9
(b) Delete the following elements from the AVL tree you constructed in (a); make sure you rebalance as needed and
show your AVL tree after each deletion, indicating precisely the rotations used:
1 2 8 5
(A)
(B)
(A) The following questions apply to a (not necessarily balanced) binary tree.
(a) Insert the following values into an initially empty search tree; show the tree after each insertion:
8 1 2 6 5 3 4 7 10 9
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
1 2 8 5
(B) The following questions apply to a balanced binary search tree (AVL tree).
(a) Insert the following values into an initially empty AVL tree; show the AVL tree after each insertion, indicating
precisely the rotations used:
8 1 2 6 5 3 4 7 10 9
(b) Delete the following elements from the AVL tree you constructed in (a); make sure you rebalance as needed and
show your AVL tree after each deletion, indicating precisely the rotations used:
1 2 8 5
(A)
(B)
1 2 3 4 5 6 8 11 110
5∙ +5∙ +4∙ +3∙ +3∙ +3∙ +2∙ +2∙ = = 2.75
40 40 40 40 40 40 40 40 40
When building the tree, the values are stored in a priority queue, so the smaller two values are always branched together first.
Example:
[1 2 3 4 5 6 8 11] 1 + 2 = 3 Add 3 to PQ [8 9 11 12] 8 + 9 = 17 Add 17 to PQ
[3 3 4 5 6 8 11] 3+3=6 Add 6 to PQ [11 12 17] 11 + 12 = 23 Add 23 to PQ
[4 5 6 6 8 11] 4+5=9 Add 9 to PQ [17 23] 17 + 23 = 40 Add 40 to PQ
[6 6 8 9 11] 6 + 6 = 12 Add 12 to PQ [40]
Construct a Huffman code for the symbols 𝑎 through 𝑔, listed below together with their probabilities. Then determine the expected
length of your resulting code!
(a) 1/28 (b) 2/28 (c) 3/28 (d) 4/28 (e) 5/28 (f) 6/28 (g) 7/28
1 2 3 4 5 6 7 74
4∙ +4∙ +3∙ +3∙ +3∙ +2∙ +2∙ = = 2.64
28 28 28 28 28 28 28 28
Related to HeapSort
(a) Construct a heap for the following array of numbers: 1 8 2 5 6 3 4 7 9 10
Show the array after the insertion of each element into the heap.
(b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.
(a) When inserting, check that the inserted value is not greater than its parent.
If it is, swap that node with its parent and continue moving upwards until the value is no longer greater than its parent.
(b) Remove the max value and replace it with the last value. Place the removed value into the max array.
Swap values until the replacement value is no longer less than its children.
If both children are larger, replace with the largest of the two.
1
1 8
8 1 2
8 1 2 5
8 5 2 1 6
8 6 2 1 5 3
8 6 3 1 5 2 4
8 6 4 1 5 2 3 7
8 7 4 6 5 2 3 1 9
9 8 4 7 5 2 3 1 6 10
10 9 4 7 8 2 3 1 6 5
10 9 4 7 8 2 3 1 6 5
5 9 4 7 8 2 3 1 6 10
9 8 4 7 5 2 3 1 6 10
6 8 4 7 5 2 3 1 9 10
8 7 4 6 5 2 3 1 9 10
1 7 4 6 5 2 3 8 9 10
7 6 4 1 5 2 3 8 9 10
3 6 4 1 5 2 7 8 9 10
6 5 4 1 3 2 7 8 9 10
2 5 4 1 3 6 7 8 9 10
5 3 4 1 2 6 7 8 9 10
2 3 4 1 5 6 7 8 9 10
4 3 2 1 5 6 7 8 9 10
1 3 2 4 5 6 7 8 9 10
3 1 2 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
(a) Construct a heap for the following array of numbers: 5 1 2 8 6 3 4 7 10 9
Show the array after the insertion of each element into the heap.
(b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.
5 Part b of this one is the same as the one on the previous page.
5 1
5 1 2
5 1 2 8
8 5 2 1 6
8 6 2 1 5 3
8 6 3 1 5 2 4
8 6 4 1 5 2 3 7
8 7 4 6 5 2 3 1 10
10 8 4 7 5 2 3 1 6 9
10 9 4 7 8 2 3 1 6 5
8 Part b of this one is the same as the one on the previous page.
8 1
8 1 2
8 1 2 6
8 6 2 1 5
8 6 2 1 5 3
8 6 3 1 5 2 4
8 6 4 1 5 2 3 7
8 7 4 6 5 2 3 1 10
10 8 4 7 5 2 3 1 6 9
10 9 4 7 8 2 3 1 6 5
Related to AVL Trees
For a balanced search tree (AVL tree) containing 𝑛 elements, determine (in terms of 𝑛)
(a) the length of a longest path from the root to a leaf.
(b) the length of a shortest path from the root to a leaf.
Hint: Consider an AVL tree with 𝑛 nodes that is of maximal height.
(a) The length of a longest path from the root to a leaf is the equal to the height 𝑛 of the tree. Therefore, it is log & 𝑛 .
(b) The length of a shortest path from the root to a leaf can be found due to AVL height restrictions. Either side of a node can
only have a height difference of at most 1. Because of the height restriction causing both sides to be within one child of
each other, the length of a shortest path from root to a leaf is log & 𝑛 − 1.