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:
- New node become red, 42
- New root 39, rotate color for 3, 42
- Add 86, rotate color for tree
- Add 49, rotate color for tree 49, 42
- Add 89, rotate color for 42, 86
- Add 99, rotate color for 86, 89
- Add 20, right of 3, red. Rotate color of 3
- Add 88, correct root of 49, balance tree to L-39. R-89
- Add 51, left of 86
- Add 64, 86 comes red, 64 becomes right of 51, rotate color 51, 88, 86
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:
- Collision with 3, chaining with 3
- Collide with 86, chaining with 86
- Collide with 49, chaining with 49
- Collide with 51, chaining with 51
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.
- Collision with 3, increment index to 11
- Collide with 86, increment index to 3
- Collide with 49, index to 5 at 5, collide with 89, index to 6
- Collide with 88, index to 7 at 7, collide with 39, index to 8
- Collide with 88, index to 7 at 7, collide with 39, index to 8 at 8, collide with 51, index to 9
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
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