Data Structure and Algorithm I Homework #6
Due: 17pm, Wednesday, January 12, 2011
Submit the answers for problem 2-5 through the CEIBA system (electronic copy) or to the TA in R432 (hard copy). You also need to submit the answers of problem 1 through the CEIBA system. If you choose to submit your writing problems through CEIBA,
1. please combine the answers of all writing problems into only one file in the doc/docx or pdf format, e.g., hw4 writing.pdf; otherwise, you will only get the score of one problem (the one that the TA chooses);
2. please submit your programming report together with your source codes (in the zip archive); if the programming report is submitted with the answers of the writing problems, you will NOT get the score for the report; and
3. please write down your name and school ID in the header of both of the above two documents, e.g., b98902xxx Your Name
Problem 1. (40%)
In section 10.3.4 of the textbook, it was shown how to insert value into a Red-Black tree. In this problem, you are required to implement the insertion operation of Red-Black Tree. Your program will need to output the preorder traversal of Red-Black tree after each insertion. Your program should take the input from the standard input device (stdin) and please abide by the following input/output format:
Input format:
There are n different positive values that are inserted sequentially and seperated by space.
Example:
7 3 89 4 9 15 35 8 24
Output format:
insertion.
Notice that you should put a minus(”-”) before the black node value output.
Example:
-7 -7 3 -7 3 89 -7 -3 4 -89 -7 -3 4 -89 9 -7 -3 4 -15 9 89 -7 -3 4 15 -9 -89 35 -7 -3 4 15 -9 8 -89 35 -7 -3 4 15 -9 8 -35 24 89
You must upload your homework in the format of a compressed zip file to the CEIBA, and the zip file should include the following three files:
1. The source code (.c file),
2. A shell script to compile the source (.sh), and
3. A document in PDF format to describe how your program/algorithm works.
Your score of 40% is divided into two parts: correctness (with 4 test cases)(32%) and explanations in the document(8%).
Problem 2. (10%)
1. Refer to Figure 8.7 of the textbook, into an empty dynamic hash tables with di- rectories. The hash table uses buckets that have 2 slots each. Insert elements A0, A1, B0, B1 ,C1, C2, C3 ,C5 (in this order). Show your steps.(5%) - you can refer Figure 8.8 of the textbook -
Ans: See Figure1 and Figure2.
2. Refer to Figure 8.7 of the textbook, into an empty directoryless dynamic hash ta- bles. The hash table uses buckets that have 2 slots each. Insert elements A0, A1, B0, B1 ,C1, C2, C3 ,C5 (in this order). Show your steps. (5%) - you can refer Figure 8.9 of the textbook -
Ans: See Figure3.
Problem 3. (25%)
1. Into an empty min leftist tree, insert elements with priorities 20, 10, 5, 18, 6, 12, 14, 4, and 22(in this order). Show the min leftist tree following each insert. (5%) Ans: See Figure4 and Figure5.
2. Delete the min element from the final min leftist tree of part (1). Show the resulting min leftist tree. (5%)
Ans: See Figure6.
3. Into an empty min B-heap, insert elements with priorities 20, 10, 5, 18, 6, 12, 14, 4, and 22(in this order). Show the resulting B-heap. Show how you arrived at this final B-heap. (5%)
Ans: See Figure7.
4. Delete the min element from the final B-heap of part (3). Show the resulting B- heap. Show how you arrived at this final B-heap. (5%)
Ans: See Figure8.
5. Prove that the binomial tree Bk has 2k nodes, k ≥ 0. (5%) Ans:
nodes.
Then we can obtain that when k = n+1, Bn+1= Bn+Bn−1+Bn−2+..+1 = Bn+Bn
has 2∗ 2n = 2n+1 nodes.
So, we can get that Bk has 2k nodes, k ≥ 0 by induction.
Problem 4. (10%) For the Fibonacci numbers Fk and the numbers Ni of Lemma 9.4, prove the following:
1. Fh = Σhk=0−2Fk+ 1, h > 1 (5%) Ans:
F2 = F0+ F1 = 0 + 1 = 1 = F0+ 1 = Σ2k=0−2Fk+ 1.
⇒ Fh = Σhk=0−2Fk+ 1 holds when h = 2.
Assume that Fh = Σhk=0−2Fk+ 1 holds when h = n.
Then we can get that when h = n + 1, Fn+1 = Fn+ Fn−1 = Σnk=0−2Fk+ 1 + Fn−1 = Σnk=0−1Fk+ 1.
So, we can get that Fh = Σhk=0−2Fk+ 1, h > 1 by induction
2. Use (a) to show that Ni = Fi+2, i≥ 0. (5%) Ans:
When i = 0, N0 = 1 = F2
When i = 1, N1 = 2 = F3
Assume that Ni = Fi+2 when i = n− 1 and i = n.
Then we can get that when i = n + 1, Nn+1= Nn+ Nn−1 = Fn+2+ Fn+1= Fn+3. So, we can get that Ni = Fi+2, i≥ 0 by induction.
Problem 5. (15%)
1. Start with an empty red-black tree and insert the following keys in the given order:
20, 10, 5 ,30 , 40, 57, 3, 2, 4, 35, 25, 18, 22, 21. Draw figures similar to Figure 10.18
of the textbook depicting your tree immediately after each insertion and following the rebalancing rotation or color change (if any). Label all nodes with their color and identify the rotation type. (10%)
Ans: See Figure9 ∼ Figure13.
2. What is the largest possible number of internal nodes in a red-black tree with black- height k? What is the smallest possible number? (5%)
Ans:
largest : 22k− 1 nodes. Full binary tree, and its height is 2k.
smallest : 2k− 1 nodes.
0
1
A0 d=1
0
1
A0 d=1
A1
0
1
A0 B0 d=1
A1
0
1
A0 B0 d=1
A1 B1
A0 -> A1 -> B0 -> B1 -> C1 -> C2 -> C3 -> C5
0000
0001
A0 B0 d=4
A1 C1
. . .
1001 B1
. . .
0010 0000
0001
A0 B0 d=4
A1 C1
. . .
1001 B1
. . .
C2
Figure 1:
0010 0000
0001
A0 B0 d=4
A1 C1
. . .
1001 B1
. . .
C2 0011 C3
0000
0001
A0 B0 d=4
A1 C1
. . .
1001 B1
. . .
0010 C2
0011 C3 0100
0101 C5
Figure 2:
0 A0 1
r=1, q=0
A0 -> A1 -> B0 -> B1 -> C1 -> C2 -> C3 -> C5
A0 A1 0 1
r=1, q=0
A0 B0 A1 0 1
r=1, q=0
A0 B0 A1 B1 0
1
r=1, q=0
A0 B0 A1 B1 00
01
r=1, q=1 10
C1
A0 B0 A1 B1 00
01
r=1, q=1 10 C2
C1
11
A0 B0 A1 B1 00
01
r=2 q=0 10 C2
C1
C3 011
A0 B0 A1 B1 000 001
r=2 q=1 010 C2
C1 C5
C3 100
Figure 3:
20 -> 10 -> 5 -> 18 -> 6 -> 12 -> 14 -> 4 -> 22
20
20 10
5
20 10
20 10
5
18
20 10
5
6
18 18
6
5
10
12 20
Figure 4:
18 6
5
12 20
10
14
18 6
5
12 20
10
14 4
18 6
5
12 20
10
14 4
12
Figure 5:
18 6
5
12 20
10
14 22
Figure 6:
20 10
20
10
20 5
10 20
5
18 6
6 12
6 12
10 20
5
18 14
10 20
5
18 10
20 5
18
10
20 5
18
14 4
6
12 10
20 5
18
14 4
6
12 22
Figure 7:
10 20
5
18
14 6
12 22
10 20
5
18
14 6
12 22
10
20 5
18 14
22 6
12
10
20 5
18
14 6
12
20 -> 10 -> 5 -> 30 -> 40 -> 57 -> 3 -> 2 -> 4 -> 35 -> 25 -> 18 -> 22 -> 21
20
10 20
5 10
10 20 20
5
LLb
RRr and change root color 30
5 10
20
30 5
10
20
30 5
10
20
30 5
10
20
40
RRb 40 5
10
30
20
10 5
30
40
Figure 9:
57 and RRr
40 5
10
30
20
57
40 5
10
30
20
57
3 3
40 5
10
30
20
57
10
3
5
2 40
30
20
57 40
5 10
30
20
57 3
2 2 and LLb
Figure 10:
10
3
5
2 40
30
20 4 57
4 and RLr
10
3
5
2 40
30
20 4 57
35
10
3
5
2 40
30
20
4 35 57
25
10
3
5
2 40
30
4 35 57
20
25
Figure 11:
18
10
3
5
2 40
30
4 57
35 25 18
20
10
3
5
2 40
30
4 57
35 25 18
20
22
10
3
5
2 40
30
4 57
35 25 18
20
22
10
3
5
2 40
30
4 57
35 25 18
20
22 22 and RLr and
root color change
10
3
5
2 40
30
4 57
35 25 18
20
22
21
10
3
5
2 40
30
4 57
22 35 18
20
21 25
21 and LLb
Figure 13: