2-4 Trees
-
Upload
linus-deleon -
Category
Documents
-
view
24 -
download
1
description
Transcript of 2-4 Trees
![Page 1: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/1.jpg)
1
2-4 Trees
![Page 2: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/2.jpg)
2
Goal
Keep sorted lists subject to the following operations:
find(x,L)
insert(x,L)
delete(x,L)
catenate(L1,L2) : Assumes that all items in L2 are greater than all items in L1.
split(x,L) : returns two lists one with all item less than or equal to x and the other with all items greater than x.
![Page 3: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/3.jpg)
3
2-4 trees
•Items are at the leaves.
•All leaves are at the same distance from the root.
•Every internal node has 2, 3, or 4 children.
1231 181514 20 2821 40
. . . . . . . .. . . . . . .
. .
![Page 4: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/4.jpg)
4
Insert
16
1231 181514 20 2821 40
. . . . . . . .. . . . . . .
. .
![Page 5: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/5.jpg)
5
Insert (cont)
12 181514 1631 20 2821 40
. . . . . .
. . . . . . .
. .
![Page 6: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/6.jpg)
6
Insert (cont)
12 181514 1631 20 2821 40
. . . . . .
. . . . . . .
. .
23
![Page 7: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/7.jpg)
7
Insert (cont)
12 181514 1631
. . . . . . . .
20 402321 28
. . .
![Page 8: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/8.jpg)
8
Insert (cont)
12 181514 1631
. . . . . . . .
20 402321 28
. . .
![Page 9: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/9.jpg)
9
Insert (cont)
12 181514 1631
. . . . . . . .
20 402321 28
. . .
![Page 10: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/10.jpg)
10
Insert -- definition
Add the new leaf in its position. Say under a node v.
(*) If the degree of v is 5 split v into a 2-node u and a 3-node w.
If v was the root then create a new root r parent of u and w and stop.
Replace v by u and w as children of p(v).
Repeat (*) for v := p(v).
![Page 11: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/11.jpg)
11
Delete
12 181514 1631
. . . . . . . .
20 402321 28
. . .
![Page 12: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/12.jpg)
12
Delete
12 18151431
. . . . . . . .
20 402321 28
. . .
![Page 13: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/13.jpg)
13
Delete
12 18151431
. . . . . . . .
20 402321 28
. . .
![Page 14: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/14.jpg)
14
Delete
12 181431
. . . . . . . .
20 402321 28
. . .
![Page 15: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/15.jpg)
15
Delete
12 181431
. . . . . . . .
20 402321 28
. . .
![Page 16: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/16.jpg)
16
Delete -- definition
Remove the leaf. Let v be the parent of the removed leaf.
(*) If the degree of v is one, 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 17: 2-4 Trees](https://reader036.fdocuments.us/reader036/viewer/2022082818/56813024550346895d95aa0e/html5/thumbnails/17.jpg)
17
Summary
Delete and insert take O(log n) on the worst case.
Theorem (homework) : A sequence of m delete and insert operations on an initial tree with n nodes takes O(m+n) time
Can do catenate and split in a way similar to red-black trees.