Sunteți pe pagina 1din 8

Technische Universitat M

unchen
Fakultat f
ur Informatik
Lehrstuhl f
ur Effiziente Algorithmen
Prof. Dr. Harald Racke
Chintan Shah

Wintersemester 2012
Losungsblatt 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 that
it remains a red-black tree and show what the tree looks like after each operation(always
carry out each operation on the result of the previous operation):

15

26

12

21

35

14

29

38

1. Insert 10
2. Delete 29
3. Delete 21
4. Delete 3
L
osungsvorschlag
1. Insert 10
Standard Insert

15

26

12

10

21

14

35

29

38

2. Delete 29
Standard Delete

15

26

12

21

10

35

14

38

3. Delete 21
Case 4 (for dummy right child of 21)

15

35

12

10

26

38

14

4. Delete 3
z(5) is red

15

35

12

10

26

14

38

Aufgabe 2 (10 Punkte)


Carry out the following operations sequentially on the red-black tree shown below so that
it remains a red-black tree and show what the tree looks like after each operation(always
carry out each operation on the result of the previous operation):

33

20

42

12

27

15

25

37

48

29

1. Insert 5
2. Delete 27
3. Insert 27
L
osungsvorschlag
1. Insert 5

33

20

42

12

27

15

37

25

29

48

Case-1 (z:5)

33

20

42

12

27

15

37

25

48

29

5
Case-2b (z:12)

20

12

33

15

27

25

42

29

37

48

2. Delete 27
Standard Delete

20

12

33

15

29

25

42

37

48

3. Insert 27

20

12

33

15

29

42

25

37

48

27
Case-2a (z:27)

20

12

33

15

29

42

27

37

48

25
Case-2b (z:25)

20

12

33

15

27

25

42

29

37

48

Aufgabe 3 (10 Punkte)


Is the following binary tree an AVL tree? If not, rebalance it to an AVL tree.

21

10

30

14

26

37

28

39

Carry out the following operations sequentially on the tree so that it remains an AVL tree
and show what the tree looks like after each operation(always carry out each operation
on the result of the previous operation):
1. Insert 16
2. Delete 30
3. Insert 22
4. Insert 27
Lo
sungsvorschlag
Yes, the given binary tree is an AVL tree.
1. Insert 16
Case-1 (v:10)

21

14

10

30

16

26

37

28

39

2. Delete 30

21

14

37

10

16

26

39

28
3. Insert 22

21

14

37

10

16

26

39

22

28

4. Insert 27
DoubleRightRotate(37)

21

14

10

28

16

26

22

37

27

39

Aufgabe 4 (10 Punkte)


1. While inserting a node z in a red-black tree, what happens if z and its parent are
red but z has no grandparent?
2. While deleting a node from an AVL tree, if node z is the only unbalanced
node(|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.
L
osungsvorschlag
1. This case cannot occur, since it would imply that the root is coloured red, which
cannot be the case.
2. LeftRotate(z)
3. No, disprove by counter-example.

S-ar putea să vă placă și