(19.1-1)
If x is not a root:
degree[sibling[x]] < degree[x].
If x is a root:
degree[sibling[x]] > degree[x].
(19.1-2)
degree[x] < degree[p[x]]
(19.1-3)
Chapter 11
11.1
4.
Chapter 13
(13-3) AVL tree
Some resource on the internet:
http://www.eli.sdsu.edu/courses/fall96/cs660/notes/avl/avl.html
http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch10.html
http://www/cs.purdue.edu/homes/ayg/CS251/slides/chap7b.pdf
(a) First, let me prove this lemma: in an AVL tree of height h, there are at most Fh+3-1 nodes,
where Fn is the nth Fibonacci number.
Step(1) get the generation function of Fibonacci number
Fibonacci number: F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2 ( n>=2 )
Let G ( z ) Fn z n
n0
z Fn zn Fn 1 z n Fn 2
n
z
n2
n
Fn z n Fn 1 z n Fn 2
n2
n2
G ( z ) F0 zF1 z (G ( z ) F0 ) z 2G ( z )
G ( z ) 1 zz z 2
By using Tayor series, we can get the closed form of Fibonacci number.
Step(2) get the equation of the AVL tree.
When height of an AVL tree is h, let Ah denotes number of nodes at least.
Obviously, A0 = 1, A1 = 2 and Ah = Ah-1 + Ah-2 + 1 ( h >= 2 )
Step(3) get the generation function about Ah and get closed form of Ah
Let G ( z ) An z n
n0
z An zn An 1 z n An 2 z n
n
z
n 2
n
An z n An 1 z n An 2 z n
n 2
n 2
n 2
G ( z ) A0 zA1 z (G ( z ) A0 ) z 2G ( z ) 1z z
2
G( z)
Note
z
1 z z 2
1
(1 z z 2 )(1 z )
1 zz z 2 1 z2 z 2 11z 1 zz z 2 2z 1 zz z 2 11z
is generation function of Fibonacci number.
let f ( z ) 11z , According to Tylor series : f ( z )
k 0
let a 0, we get f ( z )
k 0
f k (0)
k!
z z
k
f k (a)
k!
( z a) k
k
k 0
So An = Fn+2Fn+1-1 = Fn+3-1
Step(4)
Let Nh denotes number of nodes when an AVL tree is of height h.
Then Nh >= Ah => Nh >= Fh+3-1 > Fh+4
Fn
1
5
n
( n )
n
5
and (1 5 / 2), (1 5) / 2
Obviously we can get h is O(log(Nh))
(b)
I just consider the situation that height of left subtree is more than the height of right subtree by 2.
The other situation is symmetric.
Case 1:
x
h-1
h
h-1
h-2
h
h-1
h+1
h+1
right
rotate
The height of subtree does not change. Height is still h+1.
Case 2
h-1
h-2
x
h
h+1
h-1
h-1
h-1
h-2
h
right
rotate
h-2
h-2
h-2
Height of subtree decreases by 1. Height changes from h+1 to h.
Case 3
x
x
h+1
h-2
h
h-2
h-2
h-2
h-2
h-1
h-2
h-2
This
is
case 2.
Case 4
x
x
h+1
h-2
h
h-2
Case 5
h-2
h+1
h-2
h
left
rotate
h-1
h-3
h-2
h
left
rotate
h-1
h+1
h-2
h-1
h-2
h-3
This
is
case 2.
x
x
h+1
h-2
h
h-2
h-2
h-2
h
left
rotate
h-1
h+1
h-3
h-3
h-1
h-2
h-2
h
h-1
h-1
right
rotate
h-2
h-2
h-3
h-2
(c) AVL-INSERT(root[T], z).
Assume that the new node is added to node p.
(1) After insertion, if sibling of the new node is not empty, the height of all subtrees in the tree
does not change.
p
new node
p
node
or
node
new node
(2) If node p does not have children before insertion, the insertion increases height of some
subtrees.
First, we move from node p along the path to root to check whether some subtrees violate the
properties of AVL tree. If we find a subtree rooted at x whose left and right children are height
balanced and have heights that differ by 2, we can use the function/routine in question (b) to
correct it. After the correction, whether we need to move further up depends on the situation:
1) If it is case 2 and 5, then we don’t need to move further.
Reason: after rotation, the height of the whole subtree is the original height before
insertion.
2) In other cases, we need to move further.
Actually, some cases listed in answer to question (b) are impossible in this question. Case 1 and 3
are impossible.
In case 1, after insertion height of two subtrees of node a is h-1 each. The new node just can
be inserted into one of the two subtrees, so before insertion height of the two subtrees should be
h-1 and h-2. As a result, before insertion height of subtree rooted at node a is h. That means,
before insertion the tree is not balanced!!! It contradicts to our assumption.
h+1
h+1
a
h-1
h-1
h
h-1
h-2
h
right
rotate
h-1
h-2
By the same method, we can get case 3 is impossible too.
So the possible cases are case 2, 4 and 5. In case 2 and 5, one rotation is enough to balance the
whole tree. In case 4, one rotation can transform the tree into a form where case 2 can be applied.
So, two rotations are enough in case 4.
In summary, at most two rotations are needed to balance the tree.
More:
In fact, case 4 and 5 can be treated as the same case because they rotate the same nodes in the
same order.
(d)
Rotations take constant time(one rotation or two rotations are enough). We need to move along the
path up to root to find the unbalanced subtree. It is possible that we move up to the root. In this
case, the time needed is O(lg n). In summary, the total time is O(lg n)+O(1) = O(lg n).
How about deletion?
The first step is a regular deletion in binary search tree.
Deletion is kind of similar to insertion. Deletion can decrease height of some subtrees by 1. We
need to move path along up to root to check whether properties of AVL tree are violated. All the
five cases in answer to question (b) are possible. What is different from insertion is that more than
two rotations may be needed. In case 2, 4 and 5, height of the subtree is decreased by 1 so that we
need to go further up. In case 1 and 3, height of the subtree does not change, so deletion is
completed.
So number of rotations is up to O(lg n).
(13-4) Treaps
Splay tree
unsolved:
Exercises 15.2-3
Problems 15-1: Bitonic euclidean traveling-salesman problem
Problems 15-2: Printing neatly
Consider the problem of neatly printing a paragraph on a printer. The input text is a sequence of n
words of lengths l1, l2, ..., ln, measured in characters. We want to print this paragraph neatly on a
number of lines that hold a maximum of M characters each. Our criterion of "neatness" is as
follows. If a given line contains words i through j, where i ≤ j, and we leave exactly one space
between words, the number of extra space characters at the end of the line is
,
which must be nonnegative so that the words fit on the line. We wish to minimize the sum, over all
lines except the last, of the cubes of the numbers of extra space characters at the ends of lines.
Give a dynamic-programming algorithm to print a paragraph of n words neatly on a printer.
Analyze the running time and space requirements of your algorithm.
Answer:
We use W(i, j) denotes the minimum value of criteria of the words i through j.
If a line contains words i through j (j>=i), let S(i, j) denotes the number of extra space characters at
the end of the line:
S(i, j) = M j i
j
l
k i k
Assume A is an array of size n. Ai ( 1<= i <= n) is equal to W(i, n).
A(n+1) = 0
A(n) = M - ln
A(i) = min {S (i, j ) A( j 1) | n j i S (i, j ) 0}
3
where 1<= i < n
Assume I is an array of size n, I(i) is the value of j which is corresponding to A(i).
I(n) = n;
I(i) = { j | min{S (i, j ) A( j 1)} when n j i S (i, j ) 0}
3
Finally, by tracking array I, we can get the layout of the input:
j = 0;
while j < n+1
j = I(i)
print words i through j
print a new line character
i = j+1
end
Space requirements:
Two arrays, about 2*n elements totally.
Time requirements:
O (n2)
Problems 15-6: Moving on a checkerboard
Suppose that you are given an n × n checkerboard and a checker. You must move the checker from
the bottom edge of the board to the top edge of the board according to the following rule. At each
step you may move the checker to one of three squares:
the square immediately above,
the square that is one up and one to the left (but only if the checker is not already in the
leftmost column),
the square that is one up and one to the right (but only if the checker is not already in the
rightmost column).
Each time you move from square x to square y, you receive p(x, y) dollars. You are given p(x, y)
for all pairs (x, y) for which a move from x to y is legal. Do not assume that p(x, y) is positive.
Give an algorithm that figures out the set of moves that will move the checker from somewhere
along the bottom edge to somewhere along the top edge while gathering as many dollars as
possible. Your algorithm is free to pick any square along the bottom edge as a starting point and
any square along the top edge as a destination in order to maximize the number of dollars gathered
along the way. What is the running time of your algorithm?
Answer:
A51
A52
A53
A54
A55
A41
A42
A43
A44
A45
A31
A32
A33
A34
A35
A21
A22
A23
A24
A25
A11
A12
A13
A14
A15
In above checkerboard, assume square A53 is the destination square along the top edge so that the
number of dollars gathered is maximized.
The possible squares in the second top row are painted in green.
Given a node a, the node right below it is denoted by B(a), the node one down and one to the left
is denoted by BL(a) and the node one down and to the right is denoted by BR(a).
Let D denote a matrix (n x n) of which elements record the maximum number of dollars gathered
to that square.
Obviously, we can get following conclusion:
(1) If a node a is the leftmost square
D(a) = max( D(B(a)) + p(B(a), a), D(BR(a))+p(BR(a), a) )
(2) If a node a is the rightmost square
D(a) = max( D(B(a)) + p(B(a), a), D(BL(a)+p(BL(a), a) )
(3) If a node a is neither leftmost nor rightmost square
D(a) = max( D(B(a)) + p(B(a), a), D(BL(a)+p(BL(a), a), D(BR(a))+p(BR(a), a) )
We use i, j to index the coordinate of node.
The three conclusions above can be represented as:
(1) If node at coordinate (i, j) is the leftmost square
D(i,j) = max( D(i-1,j)+p(node(i-1,j), node(i,j)),
(2) If node at coordinate (i, j) is the rightmost square
(3) If node at coordinate (i, j) is neither the leftmost nor the rightmost square
Pseudo code:
D is a n x n matrix and R is a n x n matrix too. Matrix R is used to record the path.
D( 1, 1…n ) = 0; //record dollars gathered
while 1 < row < =n
//First handle leftmost square
dollar1 = D(row-1, 1)+p((row-1, 1), (row, 1))
dollar2 = D( row-1, 2)+p((row-1,2), (row, 1))
if dollar1 > dollar2 then
D(row,1) = dollar1;
R(row,1) = 0
else
D(row,1) = dollar2;
R(row,1) = 1
end
while 1 < col < n
D(row,col)=max(D(row-1, col)+p((row-1, col), (row, col)),
D( row-1, col-1)+p((row-1,col-1), (row, col),
D( row-1, col+1)+p((row-1,col+1), (row, col))
set R(row, col)
end
//At last handle rightmost square
dollar1 = D(row-1, n)+p((row-1, n), (row, n))
dollar2 = D( row-1, n-1)+p((row-1,n-1), (row, n))
if dollar1 > dollar2 then
D(row,1) = dollar1;
R(row,1) = 0
else
D(row,1) = dollar2;
R(row,1) = -1
end
end
In pseudo code above, we need to test whether a square is the leftmost or the rightmost. Actually,
we can make expand matrix D to n x n+2. And elements in the first column and last column are all
negative infinity. So the destination path must not contain any square in the first column or the
last column. We also need to expand p to include the newly added squares. The values can be set
to 0.
while 1 <= col <= n
D(row,col)=max(D(row-1, col)+p((row-1, col), (row, col)),
D( row-1, col-1)+p((row-1,col-1), (row, col),
D( row-1, col+1)+p((row-1,col+1), (row, col))
set R(row, col)
end
Chapter 16
(16.3-1)
Assume binary tree T is not full and height of T is h. Because T is not full, there exists an internal
node n which has one child.
If h is 1, tree T must look like this:
root
root
leaf
leaf
We can remove the root and make the unique leaf node as new root.
If h is larger than 1:
(1) if depth of n in T is h-1
We can remove node n and make child of node n as child of parent node of n.
node
node
node
node
node
leaf
leaf
Obviously, cost of tree T is reduced after the transformation.
(2) If depth of n in T is between 0 and h-2
We can remove a leaf node depth of which is larger than that of n and make it as a child of node n.
Note: leaf node depth of which is larger than that of node n definitely exists because height of
node n is less than h-1.
node
node
node
node
node
node
node
leaf
node
(16.3-2)
character
a
b
c
d
e
f
g
h
frequencies
1
1
2
3
5
8
13
21
codeword
1111111
1111110
111110
11110
1110
110
10
0
(16.3-3)
Chapter 32
(32.1-1)
(32.1-2)
Because all characters in pattern P are different, we can skip those matched characters to the
unmatched character.
As a result all characters in the text are examined once more or less.
For example, the pattern P is “abcde”, and the text is “abcdabcde”.
T: abcdabcde
P: abcde
The first four characters are matched and the fifth characters don’t match.
Now we can skip the matched characters.
T: abcdabcde
P:
abcde
Pseudo code for accelerated string match:
MODIFIED-NAIVE-STRING-MATCHER(T, P)
n ← length[T]
m ← length[P]
for s ← 0 to n - m
do P[1 ‥ k] = T[s + 1 ‥ s + k] (k is the maximum integer that makes the equation
satisfied.)
if k = m
then print "Pattern occurs with shift" s
s += k-1
(32.1-3)
For a character in T and a character in P, the possibility that these two characters are the same is:
p = d/d2=1/d
possibility of one comparison (the first characters don’t match) is:
p1 = (d-1)/d
possibility of two comparisons (the first characters match and the second characters don’t match)
is:
p2 = 1/d * (d-1)/d = (d-1)/d2
…
possibility of m comparisons is:
pm = (d-1)/dm + 1/dm
So average expected number of comparisons for some shift is:
E = 1*p1 + 2*p2 + … + m*pm = (d-1)*(d-1+2d-2+…+md-m) + m/dm = (1-d-m)/1-d-1 + m/dm
Number of total comparisons:
(n-m+1)*E = (n-m+1)* ((1-d-m)/1-d-1 + m/dm)
(32.1-4)
© Copyright 2026 Paperzz