Transcript
Page 1: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

2-4 tree

•k=2

•Each node has 2,3,or 4 children

Page 2: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete

15 30

14 30 40 5016 179

delete(14,T).

1 2 3 4 5 6 7 8

7 9

5 14

3

Page 3: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete

30 40 5016 179

delete(14,T).

1 2 3 4 5 6 7 8

7 9

5 14

303

Page 4: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete

30 40 5016 179

delete(17,T).

1 2 3 4 5 6 7 8

7 9

5 14

303

Page 5: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete

30 40 509

delete(17,T).

1 2 3 4 5 6 7 8

7 9

5 14

30

16

3

Page 6: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete

30 40 509

delete(16,T).

1 2 3 4 5 6 7 8

7 9

5 14

30

16

3

Page 7: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete

30 40 509

delete(16,T).

1 2 3 4 5 6 7 8

7 9

5 14

303

Page 8: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Borrow

30 40 509

delete(16,T).

1 2 3 4 5 6 7 8

7 9

5 14

303

Page 9: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Borrow

30 40 509

delete(16,T).

1 2 3 4 5 6 7 8

5 9

303 7

Page 10: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

30 40 509

delete(9,T).

1 2 3 4 5 6 7 8

5 9

303 7

Page 11: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

30 40 50

delete(9,T).

1 2 3 4 5 6 7 8

5 9

303 7

Page 12: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

30 40 50

delete(9,T).

1 2 3 4 5 6 7 8

5 9

3

Fusion

7 30

Page 13: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

30 40 50

delete(9,T).

1 2 3 4 5 6 7 8

3

Fusion

7 30

5

Page 14: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Delete -- definitionRemove the key.

If it is the only key in the node remove the node, and let v be the parent that looses a child, otherwise return

(*) If v has one child, and v is the root discard v.

Otherwise (v is not a root), if v has a sibling w of degree 3 or 4, borrow a child from w to v and terminate.

Otherwise, fuse v with its sibling to a degree 3 node and repeat (*) with the parent of v.

Page 15: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

2 )Orthogonal range searching

Range trees

Page 16: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Reporting )1-D)

Given a set of points S on the line, preprocess them to build structure that allows efficient queries of the from:

Given an interval I=[x1,x2] find all points in S that are in the interval.

2 4 5 7 8 12 15 19

6 17

Page 17: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Reporting )1-D)

Build a balanced tree over the pointsConcatenate them in a list

7

7 19151282 4 5

2

4

5 8

12

15

2 4 5 7 8 12 15 19

6 17

query: O(log n+k)space: O(n)

Page 18: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Reporting )2-D)

Given a set of points S on the plane, preprocess them to build structure that allows efficient queries of the from:Given an rectangle R=[x1,x2][y1,y2] find all points in S that are in the rectangle.

P1

P2

P3

P4

P5 P8

P6

P7

Page 19: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Range Trees )2-D)

P4P3P2P1

P3

P2

P4

P1

P1

P2

P3

P4

Maintain the points in a balanced search tree ordered by x-coor. In each internal node maintain the points in its subtree in a balanced search tree ordered by y

Page 20: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Range Trees )Contd.)

P4P3P2P1

P3

P2

P4

P1

P1

P2

P3

P4

P8P7P6P5

P5 P8

P6

P7

Page 21: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Range Trees )Contd.)

P4P3P2P1

P3

P2

P4

P1

P1

P2

P3

P4

P8P7P6P5

P5 P8

P6

P7

Page 22: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Query processing

•Search by the first dimension gives us O)logn) trees which together contain the

output.

•We search each of these trees to get the answer

Page 23: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

Analysis )2-D)

•Space O)nlog n)

•Query O)log2 n+k)

Page 24: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

95הוספת מפתח

. (90, 85, 80, 75(שוב צריך לפצל, הפעם את העלה במקומו יהיו שני עלים בעץ, בשמאלי יהיו המפתחות

.(95, 90, 85(ובימני המפתחות ( 80, 75(

אבל עכשיו צריך להוסיף מפתח (ומצביע( להורה. . (85, 75, 60, 50, 25(אמורים להיות בו המפתחות:

אך יש מקום לארבעה מפתחות בלבד. מה עושים? מפצלים (אך לא בדיוק כמו מקודם(

Page 25: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

(המשך(95הוספת מפתח אחרי שכבר פיצלנו את העלה, נפצל גם את הרמה הבאה ונארגן

( נשלח עוד רמה כלפי 60את המצביעים. את המפתח האמצעי(מעלה

60

Page 26: 2-4 tree k=2 Each node has 2,3,or 4 children. Delete 15 30 14 30 40 50 16 17 9 delete(14,T). 1 23 4 5 6 7 8 7 9 5 14 3

(סיום(95הוספת מפתח

לא הייתה רמה נוספת, לכן נוצר שורש חדש (מותר שלא יהיה ½ מלא לפחות(


Recommended