Problème 1. Consider the sequence of values S=[3, 42, 39, 86, 49, 89, 99, 20, 88, 51, 64] 1.1 Draw a left-leaning red-black tree obtained by adding the values in S in sequence.
Consider the hash function h(x)=(x+7) mod 13 a hash-table of 13 table entries that uses hashing with separate chaining. Draw the hash-table obtained by adding the values in S in sequence. Show each step.
The hash-table obtained by adding the values in S in sequence is as follows:
S=[3]h(3)=10 mod 13=10
0:1:2:3:4:5:6:7:8:9:10: 311:12:
S=[3,42]h(42)=49 mod 13=10
Collision with 3, chaining with 3
0:1:2:3:4:5:6:7:8:9:10: 3 -> 4211:12:
S=[3,42,39]h(39)=46 mod 13=7
0:1:2:3:4:5:6:7: 398:9:10: 3 -> 4211:12:
S=[3,42,39,86]h(86)=93 mod 13=2
0:1:2: 863:4:5:6:7: 398:9:10: 3 -> 4211:12:
S=[3,42,39,86,49]h(49)=56 mod 13=4
0:1:2: 863:4: 495:6:7: 398:9:10: 3 -> 4211:12:
S=[3,42,39,86,49,89]h(89)=96 mod 13=5
0:1:2: 863:4: 495: 896:7: 398:9:10: 3 -> 4211:12:
S=[3,42,39,86,49,89,99]h(99)=106 mod 13=2
Collide with 86, chaining with 86
Consider the hash function h(x)=(x+7) mod 13 a hash-table of 13 table entries that uses hashing with linear probing. Draw the hash-table obtained by adding the values in S in sequence. Show each step.
S=[3]h(3)=10 mod 13=10
0:1:2:3:4:5:6:7:8:9:10: 311:12:
S=[3,42]h(42)=49 mod 13=10
Collision with 3, increment index to 11
0:1:2:3:4:5:6:7:8:9:10: 311: 4212:
S=[3,42,39]h(39)=46 mod 13=7
0:1:2:3:4:5:6:7: 398:9:10: 311: 4212:
S=[3,42,39,86]h(86)=93 mod 13=2
0:1:2: 863:4:5:6:7: 398:9:10: 311: 4212:
S=[3,42,39,86,49]h(49)=56 mod 13=4
0:1:2: 863:4: 495:6:7: 398:9:10: 311: 4212:
S=[3,42,39,86,49,89]h(89)=96 mod 13=5
0:1:2: 863:4: 495: 896:7: 398:9:10: 311: 4212:
S=[3,42,39,86,49,89,99]h(99)=106 mod 13=2
Collide with 86, increment index to 3
Consider a list L of N sorted values. Show how to construct a valid left-leaning red-black tree holding the values in L in Θ(N)
Solution
The following depicts the pseudocode implementation of the program
Algorithm 30 BuildLLRBTree(L,start,end)
1:if start>end then
2:return NULL
3:end if
4:mid←(start+end)/2
5:left←BuildLLRBTree(L,start,mid−1)
6:right←BuildLLRBTree(L,mid+1,end)
7:node← new Node(L[mid])
8:node.left←left
9:node.right←right
10:node.color←BLACK
11:return node
Problème 3.
Consider a set of strings S. We want to figure out whether S has duplicates efficiently.
We do not want to do so by sorting S 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 h that can compute a suitable hash code for any string s∈S in O(∣s∣).
Show how one can use hashing to find whether S has duplicates without performing many comparisons between strings. Your algorithm should have an expected runtime of O(∣S∣) in which ∣S∣=∑s∈S∣s∣ represents the total length of all strings in S.
Solution
Algorithm 31 Check for Duplicates Using Hashing
Require: A set of strings S
Ensure: True if there are duplicates in S, False otherwise