Docsity

CS 357 Exam 4 Solutions - Binary and Red-Black Trees, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CS 357 Exam 4 Fall 2008 Solution
Grade will be based on your best 5 solutions to these 6 problems. Either omit one or try all six.
1. Begin with an empty binary search tree.
a. First draw the final tree after inserting keys 13, 20, 6, 15, 1, 25, 11, 21, 19, 5, 7, 23, 9, 17,
3, 26, 12, 0, 14, 22, 8, 18, 4 (in that order).
b. Next redraw the final tree after removing keys 13, 20, 6 (in that order).
2. Let T denote the 2-3-4 tree shown below.
a. First redraw the tree after removing key 0 from tree T.
b. Next redraw the tree after inserting key 35 into the original tree T.
It is sufficient to redraw only the nodes of T that are affected by each operation.
1
0
11
12
5
3
4
7
9
8
6
15
14
25
26
19
17
18
21
23
22
20
13
1
0
11
12
5
3
4
9
8
7
15
25
26
19
17
18
23
22
21
14
a.
b.
1
0
5
6
2
4
3
9
8
13
14
10
12
11 15 23
7
16
22
18
20
24
26
17 19 21
25 27 31
28 29 30
3 5
6
1 2
4
9
11
11
24
26
25
28 29 30
34 35
a.
b.
7
15 23
32
31 33
23 27
7 15
17 19 21
9
13
3
13
pf3

Partial preview of the text

Download CS 357 Exam 4 Solutions - Binary and Red-Black Trees and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 357 Exam 4 Fall 2008 Solution

Grade will be based on your best 5 solutions to these 6 problems. Either omit one or try all six.

1. Begin with an empty binary search tree.

a. First draw the final tree after inserting keys 13, 20, 6, 15, 1, 25, 11, 21, 19, 5, 7, 23, 9, 17,

3, 26, 12, 0, 14, 22, 8, 18, 4 (in that order).

b. Next redraw the final tree after removing keys 13, 20, 6 (in that order).

2. Let T denote the 2-3-4 tree shown below.

a. First redraw the tree after removing key 0 from tree T.

b. Next redraw the tree after inserting key 35 into the original tree T.

It is sufficient to redraw only the nodes of T that are affected by each operation.

a. b. 14

a. b.

3. Let T denote the red-black tree shown below. Red nodes are indicated, and other nodes are black.

a. First redraw the tree after removing key 6 from tree T.

b. Next redraw the tree after inserting key 8 into the original tree T.

It is sufficient to redraw only the nodes of T that are affected by each operation.

4. Suppose we insert n distinct random values into an initially empty binary search tree. Determine

the probability that all n nodes will form a single search path, or equivalently, that the resulting

tree will contain exactly n levels. Provide convincing justification for your answer. Also

determine this probability in the specific cases when n=1, 2, 3, 4, and 5.

Inserting the 1

st

value always yields a tree with 1 level. For each k>1, when inserting the

k

th

value, there are k possible positions relative to the previous k–1 values, and exactly 2

of these k positions can cause the tree to grow to k levels. Therefore the probability is

Prob(n) = (2/2)  (2/3)  (2/4)  (2/5)  …  (2/n) = 2

n– 1

/n!

So Prob(1) = 1, Prob(2) = 1, Prob(3) = 2/3, Prob(4) = 1/3, and Prob(5) = 2/15.

red 20 32 red

red

22

red 26

red 12

red 18

red

red

red

red

20 red

4 red

red 1

a.

b.

red

red

red

red

black

black black

black

black

black

black