0% found this document useful (0 votes)
12 views

02 2002 Su Sol

1. The document provides solutions to exam questions about F-heaps and red-black trees. It shows the steps of deleteMin and other operations on F-heaps through diagrams. 2. Algorithms for insertion and deletion in red-black trees are explained through examples and pseudocode. Different representations of solutions are provided. 3. Examples of rotations in AVL trees are given to balance the trees during insertion.

Uploaded by

Grace Evangelene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

02 2002 Su Sol

1. The document provides solutions to exam questions about F-heaps and red-black trees. It shows the steps of deleteMin and other operations on F-heaps through diagrams. 2. Algorithms for insertion and deletion in red-black trees are explained through examples and pseudocode. Different representations of solutions are provided. 3. Examples of rotations in AVL trees are given to balance the trees during insertion.

Uploaded by

Grace Evangelene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

=================================================

Exam02 Solution
=================================================
1.
F-heap (ChildCut: T(true), F(false))

(a)The solution can be represented in more than one way.


After deleteMin operation, we need to combine steps like below:

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

(b) two pass method


12 8 7 meld(12,8) 12 meld(12,7) 12
| | | ===== => / | ========> / / |
2 4 5 8 2 7 8 2
/| | | |
1 3 4 5 4
/ |
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

Step 4: S= join(3, 5, S) B= same as above

S: x = 8 B:
3 / \
/ \\ 9 18
2 5 \\
// / \ 10
1 4 6

You might also like