You are on page 1of 18

1) Draw a red-black tree for the following values inserted in this order.

Illustrate each operation that


occurs: kwosytpr 10 points
2) Draw a red-black tree for the following values inserted in this order. Illustrate each operation that
occurs: 30 20 11 28 16 13 55 52 26 50 87 10 points
3) Draw a 2-3-4 B-tree that corresponds to your red-black tree in problem #2.

30 20 11 28 16 13 55 52 26 50 87

30 20 30 11 20 30

20

11 28 30

20

11 16 28 30

20

11 13 16 28 30
20

11 13 16 28 30 55

20 30

11 13 16 28 52 55

20 30

11 13 16 26 28 52 55

20 30

11 13 16 26 28 50 52 55
20 30 52

11 13 16 26 28 50 55 87

Use a tablesize of 13 for these hashing questions: 10 points

4) Given the input {3823, 8806, 8783, 2850, 3593, 8479, 1941, 4290, 8818, 7413}

and a hash function h(x) = x mod 13, show the resulting separate chaining table.

H(3823) = 3823 mod 13

h(x) = x mod
x 13
3823 1
8806 5
8783 8
2850 3
3593 5
8479 3
1941 4
4290 0
8818 4
7413 3
) Repeat #4 using open addressing with linear probing.

In linear probing, we have ,h(x,i) = (x+i) (mod k) and given Table Size k = 13

H(3823,0) = 3823 mod 13 = 1

H(8806,0) = 8806 mod 13 = 5

H(8783,0) = 8783 mod 13 = 8

H(2850,0) = 2850 mod 13 = 3

H(3593,0) = 3593 mod 13 = 5 , Collision occurs, increment the value i to 1. h((3593+1 )mod 13) = 6

H(8479,0) = 8479 mod 13 = 3, Collision occurs. Increment the value i to 1. h(8479 + 1) mod 13 = 4

H(1941,0) = 1941 mod 13 = 4 Collision occurs. Increment the value i to 1. h(1941 + 1) mod 13 = 5,

Still collision, again increment the value i, now h(1941 + 2) mod 13 = 6,

again collision occurs, increment the value i, now h(1941 + 3) mod 13 = 7

H(4290,0) = 4290 mod 13 = 0

H(8818,0) = 8818 mod 13 = 4 , collision occurs

Similary we increment the value I until the h value is an empty index.

(8818 +1) mod 13 = 5 , (8818+2) mod 13 = 6, (8818 +3) mod 13 = 7, (8818 +4 ) mod 13 = 8

H(7413,0) = 7413 mod 13 = 3

Similary we increment the value I until the h value is an empty index.


(7413+1) mod 13 = 4 , (7413 + 2) mod 13 = 5 , (7413+3) mod 13 = 6 , (7413+4) mod 13 = 7 ,

(7413+5) mod 13 = 8 , (7413+6) mod 13 = 9 , (7413+7) mod 13 = 10 .

0 4290
1 3823
2  
3 2850
4 8479
5 8806
6 3593
7 1941
8 8783
9 8818
10 7413
11  
12  

6) Repeat #4 using open addressing with quadratic probing.

{3823, 8806, 8783, 2850, 3593, 8479, 1941, 4290, 8818, 7413}

H(k,i) = (k + i2) mod m where I = 0,1,2,3,4……….

H(3823,0) = {3823 mod 13} = 1

H(8806,0) = {8806 mod 13} = 5

H(8783,0) = {8783 mod 13} = 8

H(2850,0) = {2850 mod 13 } = 3

H(3593,0) = {3593 mod 13} = 5

Collision Occurs

H(3593,1) = {(3593+ 12) mod 13} = {3594 mod 13} = 6

H(8479,0) = {8479 mod 13} = 3

Collision occurs

H(8479,1) = {(8479 + 12) mod 13} = 4

H(1941,0) = { 1941 mod 13} = 4

Collision Occurs

H(1941,1) = {(1941 + 12)mod 13} = {1942 mod 13} = 5


Again Collision Occurs

H(1941,2) = {(1941 + 22)mod 13} = {1945 mod 13} = 8

Again collision occurs

H(1941,3) = {(1941 + 32)mod 13} = {1950 mod 13} =0

H(4290,0) = {4290 mod 13} = 0

Collision occurs

H(4290,1) = {(4290 + 12)mod 13} = {4291 mod 13} = 1

Collision Occurs

H(4290,2) = {(4290 + 22)mod 13} = {4294 mod 13} = 4

Collision Occurs

H(4290,3) = {(4290 + 32)mod 13} = {4299 mod 13} =9

H(8818,0) = { 8818 mod 13} = 4

Collision occurs

H(8818,1) = {(8818 + 12)mod 13} = {8819 mod 13} = 5

Collision occurs

H(8818,2) = {(8818 + 22)mod 13} = {8822 mod 13} = 8

Collision occurs

H(8818,3) = {(8818 + 32)mod 13} = {8827 mod 13} =0

Collision occurs

H(8818,4) = {(8818 + 42)mod 13} = {8834 mod 13} =7

H(7413,0) = {7413 mod 13} = 3

Collision occurs

H(7413,1) = {(7413 + 12)mod 13} = {7414 mod 13} = 4

Collision occurs

H(7413,2) = {(7413 + 22)mod 13} = {7417 mod 13} = 7

Collision occurs

H(7413,3) = {(7413+ 32)mod 13} = {7422 mod 13} =12


7) Repeat #4 using open addressing with double hashing where the second hash function is

11 - (x mod 11).

H1(x) = x mod 13 H2(x) = 11 - x mod 11 Hi(k) = [h1(k) + i*h2(k)] % 13


H(3823) = 3823 mod 13 = 1 1
H(8806) = 8806 mod 13 = 5 5
H(8783) = 8783 mod 13 = 8 8
H(2850) = 2850 mod 13 = 3 3
H(3593) = 3593 mod 13 = 5 11- 3593 mod 11 = 11 -7 = 4 (5 + 1*4)% 13 = 9
H(8479) = 8479 mod 13 = 3 11 - 8479 mod 11 = 11 – 9 = 2 (3 + 1*2)%13 = 5, (3+2*2)%13 = 7
H(1941) = 1941 mod 13 = 4 11 – 1941 mod 11 = 11-5 = 6 (4+1*6)%13 = 10
H(4290) = 4290 mod 13 = 0 0
H(8818) = 8818 mod 13 = 4 11 – 8818 mod 11 = 11 – 7 = 4 (4+1*4)%13 = 8
(4 + 2*4)%13 = 12%13 = 1
(4+3*4)%13 = 16%13 = 3
(4 + 4*4)%13 =20%13 = 7
(4+5*4)%13 = 24%13 = 11
H(7413) = 7413 mod 13 = 3 11 – 7413 mod 11 = 11 – 10 = 1 (3+1*1)%13 = 4

Hash Table
Suppose these names have the following hash values. Insert them into the extendible hash

table shown below. Each leaf can only hold 4 entries. Note that the first two names

have already been inserted. Illustrate each operation that occurs.

Bob 0100

Sue 1000

Tim 1110

Ron 0010

Ann 1010

Jan 1101

Ben 0001

Don 0101

Tom 1111

Sam 1011

---------------

| 0 | 1 |

---------------

---------- ----------

| (1) | | (1) |

| Bob 0100 | | Sue 1000 |

| | | |

| | | |

| | | |

---------- ----------
0 1

Bob 0100 Sue 1000

Consider extreme left bit (either 0 or 1) then place in appropriate location

Insert “Tim 1110”

0 1

Bob 0100 Sue 1000


Tim 1110

Insert “Ron 0010”

0 1

Bob 0100 Sue 1000


Ron 0010 Tim 1110

Insert “Ann 1010”

0 1

Bob 0100 Sue 1000


Ron 0010 Tim 1110
Ann 1010

Insert “Jan 1101”

0 1

Bob 0100 Sue 1000


Ron 0010 Tim 1110
Ann 1010
Jan 1101
Insert ”Ben 0001”

0 1

Bob 0100 Sue 1000


Ron 0010 Tim 1110
Ben 0001 Ann 1010
Jan 1101
Insert Don “0101”

0 1

Bob 0100 Sue 1000 Each leaf can only


Ron 0010 Tim 1110 hold 4 entries
Ben 0001 Ann 1010
Don 0101 Jan 1101

Insert “Tom 1111”

Here if “Tom 1111” data is inserted then the overflow occurs at leaf 1 in the table. The reason is that,
each leaf can only hold 4 entries. Then, it requires additional bits to avoid the problem. So, use 4
bits(00,01, 10 and 11) instead of 2 bits ( 0 and 1)

00 01 10 11

Ron 0010 Bob 0100 Sue 1000 Tim 1110


Ben 0001 Den 0101 Ann 1010 Jan 1101
Sam 1011 Tom 1111
9) Using Cuckoo hashing, hash the following keys using the (h1,h2) pairs shown.

A: 2,0

B: 0,0

C: 4,1

D: 0,1

E: 2,3

10 points
10) Using Hopscotch hashing with a max hop of 4, hash the following keys.

A: 6

B: 7

C: 9

D: 7

E: 6

F: 7

G: 8

In hopscotch hashing, neighborhoods can be stored using bitmaps. Each bucket requires a bitmap
in addition to all the data already needed for that bucket. Each bit in that bitmap shows if the
current bucket or one of its following (H-1) buckets are holding an entry which belongs to the
neighborhood of the current bucket.

You might also like