02 2002 Su Sol
02 2002 Su Sol
Exam02 Solution
=================================================
1.
F-heap (ChildCut: T(true), F(false))
4 degree 1 4
| ===> / | degree 2
5(F) 5(F) 15(F) ======>
|
22(T)
min
3 7
/ | \ / \
(T)11 (T)12 4(F) (T)8 9(T)
| \ |
5(F) 15(F) 10(T)
|
22(T)
(b)
min
3 7 2
/ | \ / \ |
(T)11 (F)12 4(T) (T)8 9(T) 22(T)
| |
5(F) 10(T)
2.
(a)
5 5 5 15 15 15
=> | => / | => | => / | => / / |
3 1 3 5 7 5 8 7 5
/ | / | / |
1 3 1 3 1 3
15 15 15
=> / / / | ==> / / / / | ==> / / / / / |
12 8 7 5 2 12 8 7 5 2 12 4 8 7 5
/ | /| / |
1 3 1 3 1 3
3.
a) 2, 1, 3, and 5 are inserted normally.
Then a single rotation inserts 4, yielding
2
/ \
1 4
/ \
3 5
The insertion of 8 requires a single rotation at the root,
resulting in
4
/ \
2 5
/ \ \
1 3 8
Next 7 is inserted, which requires a double rotation
4
/ \
2 7
/ \ / \
1 3 5 8
Finally, 6 is inserted yielding the result:
4
/ \
2 7
/ \ / \
1 3 5 8
\
6
part b:
30
/ \
20 55
/ \ / \
15 25 45 60
/ \ / \ / ==
5 17 23 27 43
/
21
30
/ \
20 55
/ \ /
15 25 45
/ \ / \ /
5 17 23 27 43
/
21
R1 Rotation
30 (2)
/ \
20 (-1) 45 (0)
/ \ / \
15 25 43 55
/ \ / \
5 17 23 27
/
21
R-1 Rotation
25 (0)
/ \
20 (0) 30 (-1)
/ \ / \
15 23 27 45
/ \ / / \
1 7 21 43 55
4.
(a)
The solution can be represented in more than one way.
25
/ \
21 42
// \ // \\
3 24 29 60
/ \ / \ / \
1 13 27 36 45 55
// \\ // // \\
4 19 34 44 50
(b)
24
/ \
/ \
( 3 , 21 ) (29 , 42 , 60 )
/ | \ / | | \
/ | \ / | | \
1 (4,13,19) 27 (34,36) (44,45,50) 650
24
/ \
/ \
( 3 , 19 ) (29 , 42 , 60 )
/ | \ / | | \
/ | \ / | | \
1 (4,13) 21 27 (34,36) (44,45,50) 650
5. Red-Black Trees
(a)
5 5 5
/ \ / \ / \
Rb1 2 15 2 15 2 15
==> / \ // \ insert(12) / \ // \ RRb / \ // \
1 3 8 18 ===> 1 3 8 18 =====> 1 3 8 18
/ \ / \ / \
6 9 6 9 6 10
\\ \\ // \\
10 10 9 12
\\
12
(b)
Step 1: Search 8 and copy it to x.
Initialize S and B to NULL. (S=NULL, B=NULL)
Step 2: S= 6 B= 9
\\
10
Step 3: S= 6 B=join(B, 15, 18)
B: 15
/ \
9 18
\\
10
S: x = 8 B:
3 / \
/ \\ 9 18
2 5 \\
// / \ 10
1 4 6