Problème 1.

Consider the sequence of values

1.1

Draw a left-leaning red-black tree obtained by adding the values in S in sequence. Show each step.

Let the following format as node: [R|B]-<value> where [R|B] denotes whether the node is red or black, followed by its value.

The left-leaning red-black tree obtained by adding the values in S in sequence is as follows:

B-3
  1. New node become red, 42
B-3
  \
   R-42
  1. New root 39, rotate color for 3, 42
  B-39
 /    \
R-3   R-42
  1. Add 86, rotate color for tree
  B-39
 /    \
R-3   R-42
        \
        R-86
  1. Add 49, rotate color for tree 49, 42
  B-39
 /    \
R-3   R-49
      /  \
    R-42   R-86
  1. Add 89, rotate color for 42, 86
  B-39
 /    \
R-3   R-49
      /  \
    B-42  B-86
            \
           R-89
  1. Add 99, rotate color for 86, 89
  B-39
 /    \
R-3   R-49
      /  \
    B-42  B-89
          /  \
        R-86 R-89
  1. Add 20, right of 3, red. Rotate color of 3
      B-39
     /    \
    B-3   R-49
     |    |   \
    R-20 B-42 B-89
              /  \
            R-86 R-89
  1. Add 88, correct root of 49, balance tree to L-39. R-89
      B-49
     /    \
    R-39   R-89
    /  |    |   \
  B-3 R-42 B-86 B-99
   |        |
  R-20     R-88
  1. Add 51, left of 86
      B-49
     /    \
    R-39   R-89
    /  |    |   \
  B-3 R-42 B-86 B-99
   |       /  \
R-20     R-51 R-88
  1. Add 64, 86 comes red, 64 becomes right of 51, rotate color 51, 88, 86
      B-49
     /    \
    R-39   R-89
    /  |    |   \
  B-3 R-42 R-86 B-99
   |       /  \
R-20     B-51 B-88
          |
         R-64

1.2

Consider the hash function a hash-table of 13 table entries that uses hashing with separate chaining. Draw the hash-table obtained by adding the values in in sequence. Show each step.

The hash-table obtained by adding the values in in sequence is as follows:

0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10: 3
11:
12:
  1. Collision with 3, chaining with 3
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10: 3 -> 42
11:
12:
0:
1:
2:
3:
4:
5:
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
0:
1:
2: 86
3:
4:
5:
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
0:
1:
2: 86
3:
4: 49
5:
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
0:
1:
2: 86
3:
4: 49
5: 89
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
  1. Collide with 86, chaining with 86
0:
1:
2: 86 -> 99
3:
4: 49
5: 89
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
0:
1: 27
2: 86 -> 99
3:
4: 49
5: 89
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
  1. Collide with 49, chaining with 49
0:
1: 27
2: 86 -> 99
3:
4: 49 -> 88
5: 89
6:
7: 39
8:
9:
10: 3 -> 42
11:
12:
0:
1: 27
2: 86 -> 99
3:
4: 49 -> 88
5: 89
6: 51
7: 39
8:
9:
10: 3 -> 42
11:
12:
  1. Collide with 51, chaining with 51
0:
1: 27
2: 86 -> 99
3:
4: 49 -> 88
5: 89
6: 51 -> 64
7: 39
8:
9:
10: 3 -> 42
11:
12:

1.3

Consider the hash function a hash-table of 13 table entries that uses hashing with linear probing. Draw the hash-table obtained by adding the values in in sequence. Show each step.

0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10: 3
11:
12:
  1. Collision with 3, increment index to 11
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10: 3
11: 42
12:
0:
1:
2:
3:
4:
5:
6:
7: 39
8:
9:
10: 3
11: 42
12:
0:
1:
2: 86
3:
4:
5:
6:
7: 39
8:
9:
10: 3
11: 42
12:
0:
1:
2: 86
3:
4: 49
5:
6:
7: 39
8:
9:
10: 3
11: 42
12:
0:
1:
2: 86
3:
4: 49
5: 89
6:
7: 39
8:
9:
10: 3
11: 42
12:
  1. Collide with 86, increment index to 3
0:
1:
2: 86
3: 99
4: 49
5: 89
6:
7: 39
8:
9:
10: 3
11: 42
12:
0:
1: 27
2: 86
3: 99
4: 49
5: 89
6:
7: 39
8:
9:
10: 3
11: 42
12:
  1. Collide with 49, index to 5 at 5, collide with 89, index to 6
0:
1: 27
2: 86
3: 99
4: 49
5: 89
6: 88
7: 39
8:
9:
10: 3
11: 42
12:
  1. Collide with 88, index to 7 at 7, collide with 39, index to 8
0:
1: 27
2: 86
3: 99
4: 49
5: 89
6: 88
7: 39
8: 51
9:
10: 3
11: 42
12:
  1. Collide with 88, index to 7 at 7, collide with 39, index to 8 at 8, collide with 51, index to 9
0:
1: 27
2: 86
3: 99
4: 49
5: 89
6: 88
7: 39
8: 51
9: 64
10: 3
11: 42
12:

Problème 2.

Consider a list of sorted values. Show how to construct a valid left-leaning red-black tree holding the values in in

Solution

The following depicts the pseudocode implementation of the program

Algorithm 30 BuildLLRBTree(L,start,endL, \text{start}, \text{end})

1:if start>end\text{start} > \text{end} then

2:return NULLNULL

3:end if

4:mid(start+end)/2\text{mid} \gets (start + end) / 2

5:leftBuildLLRBTree(L,start,mid1)\text{left} \gets BuildLLRBTree(L, start, mid-1)

6:rightBuildLLRBTree(L,mid+1,end)\text{right} \gets BuildLLRBTree(L, mid+1, end)

7:node\text{node} \gets new Node(L[mid]L[mid])

8:node.leftleft\text{node.left} \gets left

9:node.rightright\text{node.right} \gets right

10:node.colorBLACK\text{node.color} \gets \text{BLACK}

11:return nodenode

Problème 3.

Consider a set of strings . We want to figure out whether has duplicates efficiently. We do not want to do so by sorting and then checking for duplicates: comparing strings can be a lot of work (e.g., they might differ in only a single character). Assume that you have a hash function that can compute a suitable hash code for any string in . Show how one can use hashing to find whether has duplicates without performing many comparisons between strings. Your algorithm should have an expected runtime of in which represents the total length of all strings in .

Solution

Algorithm 31 Check for Duplicates Using Hashing

Require: A set of strings SS

Ensure: True if there are duplicates in SS, False otherwise

1:Initialize an empty hash table HH

2:for each string sSs \in S do

3:Compute h(s)h(s) using the hash function

4:if h(s)h(s) is in HH then

5:for each string sH[h(s)]s' \in H[h(s)] do

6:if s=ss = s' then

7:

8:return True

9:end if

10:end for

11:Append ss to H[h(s)]H[h(s)]

12:else

13:Insert ss into HH at h(s)h(s)

14:end if

15:end for

16:

17:return False