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. 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:
S=[3]
S=[3,42]
New node become red, 42
S=[3,42,39]
New root 39, rotate color for 3, 42
S=[3,42,39,86]
Add 86, rotate color for tree
S=[3,42,39,86,49]
Add 49, rotate color for tree 49, 42
S=[3,42,39,86,49,89]
Add 89, rotate color for 42, 86
S=[3,42,39,86,49,89,99]
Add 99, rotate color for 86, 89
S=[3,42,39,86,49,89,99,20]
Add 20, right of 3, red. Rotate color of 3
S=[3,42,39,86,49,89,99,20,88]
Add 88, correct root of 49, balance tree to L-39. R-89
S=[3,42,39,86,49,89,99,20,88,51]
Add 51, left of 86
S=[3,42,39,86,49,89,99,20,88,51,64]
Add 64, 86 comes red, 64 becomes right of 51, rotate color 51, 88, 86
1.2
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
S=[3,42]h(42)=49 mod 13=10
Collision with 3, chaining with 3
S=[3,42,39]h(39)=46 mod 13=7
S=[3,42,39,86]h(86)=93 mod 13=2
S=[3,42,39,86,49]h(49)=56 mod 13=4
S=[3,42,39,86,49,89]h(89)=96 mod 13=5
S=[3,42,39,86,49,89,99]h(99)=106 mod 13=2
Collide with 86, chaining with 86
S=[3,42,39,86,49,89,99,20]h(20)=27 mod 13=1
S=[3,42,39,86,49,89,99,20,88]h(88)=95 mod 13=4
Collide with 49, chaining with 49
S=[3,42,39,86,49,89,99,20,88,51]h(51)=58 mod 13=6
S=[3,42,39,86,49,89,99,20,88,51,64]h(64)=71 mod 13=6
Collide with 51, chaining with 51
1.3
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
S=[3,42]h(42)=49 mod 13=10
Collision with 3, increment index to 11
S=[3,42,39]h(39)=46 mod 13=7
S=[3,42,39,86]h(86)=93 mod 13=2
S=[3,42,39,86,49]h(49)=56 mod 13=4
S=[3,42,39,86,49,89]h(89)=96 mod 13=5
S=[3,42,39,86,49,89,99]h(99)=106 mod 13=2
Collide with 86, increment index to 3
S=[3,42,39,86,49,89,99,20]h(20)=27 mod 13=1
S=[3,42,39,86,49,89,99,20,88]h(88)=95 mod 13=4
Collide with 49, index to 5
at 5, collide with 89, index to 6
S=[3,42,39,86,49,89,99,20,88,51]h(51)=58 mod 13=6
Collide with 88, index to 7
at 7, collide with 39, index to 8
S=[3,42,39,86,49,89,99,20,88,51,64]h(64)=71 mod 13=6
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 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 35 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 36 Check for Duplicates Using Hashing
Require: A set of strings S
Ensure: True if there are duplicates in S, False otherwise