Upload
hisuin
View
225
Download
0
Embed Size (px)
DESCRIPTION
lo04
Citation preview
Technische Universitat MunchenFakultat fur InformatikLehrstuhl fur Effiziente AlgorithmenProf. Dr. Harald RackeChintan Shah
Wintersemester 2012Losungsblatt 4
22 November 2012
Effiziente Algorithmen und Datenstrukturen I
Aufgabe 1 (10 Punkte)Carry out the following operations sequentially on the red-black tree shown below so thatit remains a red-black tree and show what the tree looks like after each operation(alwayscarry out each operation on the result of the previous operation):
15
8 26
3 12 21 35
5 14 29 38
1. Insert 10
2. Delete 29
3. Delete 21
4. Delete 3
Losungsvorschlag
1. Insert 10
Standard Insert
15
8 26
3 12 21 35
5 14 29 3810
2. Delete 29
Standard Delete
15
8 26
3 12 21 35
5 14 3810
3. Delete 21
Case 4 (for dummy right child of 21)
15
8
3 12
5 1410
26
35
38
4. Delete 3
z(5) is red
15
8
12
1410
26
35
385
2
Aufgabe 2 (10 Punkte)Carry out the following operations sequentially on the red-black tree shown below so thatit remains a red-black tree and show what the tree looks like after each operation(alwayscarry out each operation on the result of the previous operation):
33
42
37 48
20
12 27
15 29258
1. Insert 5
2. Delete 27
3. Insert 27
Losungsvorschlag
1. Insert 5
33
42
37 48
20
12 27
15 29258
5
3
Case-1 (z:5)
33
42
37 48
20
12 27
15 29258
5
Case-2b (z:12)
20
12
158
5
33
27 42
25 29 37 48
2. Delete 27
Standard Delete
20
12
158
5
33
42
25 37 48
29
4
3. Insert 27
20
12
158
5
33
42
25 37 48
29
27
Case-2a (z:27)
20
12
158
5
33
42
37 48
29
25
27
Case-2b (z:25)
20
12
158
5
33
27 42
25 29 37 48
5
Aufgabe 3 (10 Punkte)Is the following binary tree an AVL tree? If not, rebalance it to an AVL tree.
21
10
14
30
26 37
28 39
Carry out the following operations sequentially on the tree so that it remains an AVL treeand show what the tree looks like after each operation(always carry out each operationon the result of the previous operation):
1. Insert 16
2. Delete 30
3. Insert 22
4. Insert 27
Losungsvorschlag
Yes, the given binary tree is an AVL tree.
1. Insert 16
Case-1 (v:10)
21
30
26 37
28 39
10 16
14
6
2. Delete 30
21
26
28
10 16
14 37
39
3. Insert 22
21
26
28
10 16
14 37
39
22
4. Insert 27
DoubleRightRotate(37)
21
2610 16
14
22 27
28
37
39
7
Aufgabe 4 (10 Punkte)
1. While inserting a node z in a red-black tree, what happens if z and its parent arered but z has no grandparent?
2. While deleting a node from an AVL tree, if node z is the only unbalancednode(|balancenode| > 1) with a balance of −2(the right subtree of z has more height)and the right child of z has a balance of 0, how do we rebalance this tree?
3. If we insert a node in a red-black tree and then immediately delete the same node,is the resulting tree always identical to the original tree? Explain.
Losungsvorschlag
1. This case cannot occur, since it would imply that the root is coloured red, whichcannot be the case.
2. LeftRotate(z)
3. No, disprove by counter-example.
8