© 2004 goodrich, tamassia 1 2-3-4 trees 9 10 14 2 5 7

34
© 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Upload: avice-preston

Post on 17-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

© 2004 Goodrich, Tamassia 1

2-3-4 Trees

9

10 142 5 7

Page 2: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

2

Multi-Way Search TreeA multi-way search tree is an ordered tree such that

Each internal node has at least two children and stores d1 key-element items (ki, oi), where d is the number of children

For a node with children v1 v2 … vd storing keys k1 k2 … kd1

keys in the subtree of v1 are less than k1

keys in the subtree of vi are between ki1 and ki (i = 2, …, d1) keys in the subtree of vd are greater than kd1

The leaves store no items and serve as placeholders

11 24

2 6 8 15

30

27 32

Page 3: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

3

Multi-Way Inorder Traversal

We can extend the notion of inorder traversal from binary trees to multi-way search treesNamely, we visit item (ki, oi) of node v between the recursive traversals of the subtrees of v rooted at children vi and vi1

An inorder traversal of a multi-way search tree visits the keys in increasing order

11 24

2 6 8 15

30

27 32

1 3 5 7 9 11 13 19

15 17

2 4 6 14 18

8 12

10

16

Page 4: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

4

Multi-Way SearchingSimilar to search in a binary search treeA each internal node with children v1 v2 … vd and keys k1 k2 … kd1

k ki (i = 1, …, d1): the search terminates successfully k k1: we continue the search in child v1

ki1 k ki (i = 2, …, d1): we continue the search in child vi

k kd1: we continue the search in child vd

Reaching an external node terminates the search unsuccessfullyExample: search for 30

11 24

2 6 8 15

30

27 32

Page 5: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

5

2-3-4 TreesA 2-3-4 tree (also known as 2-4 tree) is a multi-way search with the following properties

Node-Size Property: every internal node has at least 2 and at most 4 children

Depth Property: all the external nodes have the same depth

10 15 24

2 8 12 27 3218

Perfectly balanced! 4-node3-key node

3-node2-key node

Page 6: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

6

Height of a 2-3-4 TreeTheorem: A 2-3-4 tree storing n items has height O(log n)Proof:

Let h be the height of a 2-3-4 tree with n items Since there are at least 2i and at most 4i items at depth i 0, … ,

h 1 and no items at depth h, we have

Searching in a 2-3-4 tree with n items takes O(log n) time

1

2

2h1

0

items0

1

h1

h

depth

)(logis1log13log

3/1412444222

24

110110

nOhnhn

nn hhhh

Page 7: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Multi-way Search Trees: Find

7

Find12

Find24

Page 8: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Update of 2-3-4 Trees

Bottom-up approach (in the text book) Find, insert, delete: worse-case

O(logn) time

Top-down approach Same time complexity with a smaller

constant factor Recording by Jonathan Shewchuk

8

Page 9: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

9

Insert: Bottom-UpWe insert a new item (k, o) at the parent v of the leaf reached by searching for k

We preserve the depth property but We may cause an overflow (i.e., node v may become a 5-node)

Example: inserting key 30 causes an overflow

27 32 35

10 15 24

2 8 12 18

10 15 24

2 8 12 27 30 32 3518

v

v

Page 10: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

10

Overflow and SplitWe handle an overflow at a 5-node v with a split operation:

let v1 … v5 be the children of v and k1 … k4 be the keys of v node v is replaced nodes v' and v"

v' is a 3-node with keys k1 k2 and children v1 v2 v3

v" is a 2-node with key k4 and children v4 v5

key k3 is inserted into the parent u of v (a new root may be created)

The overflow may propagate to the parent node u

15 24

12 27 30 32 3518v

u

v1 v2 v3 v4 v5

15 24 32

12 27 3018v'

u

v1 v2 v3 v4 v5

35v"

Page 11: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

More Insert Example

11

Page 12: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Cascading Splits

12

Page 13: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Examples of Bottom-Up Insert

Insert 5, 6, 7, 4, 3, 2, 1, 8, 9, 10 into a 2-3 tree. Walkthrough

Insert 9, 7, 6, 1, 3, 2, 5, 4, 8, 10

13

Page 14: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

More Examples

Animation of 2-3 trees OpenDSA

14

Page 15: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Insert: Top-Down

Steps Walks down tree in search of k. Whenever a 3-key node is

encountered, split it by placing the middle key in the parent node. (Note that the parent has at most 2 keys, so it has room for the third.)

15

7

1,3,4 9

3, 7

1 94

Split

3-key

Page 16: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Example of Top-Down Insert

16

20, 40, 50

14 70, 7932 43

1018

25

33

42

47

57, 62, 6674

81

Insert 60

20

14 62, 70, 7932 43

1018

25

33

42

47

5774

81

50

40

66

Insert 60 here!

Page 17: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Observations on Insert

Why we split 3-key nodes To make sure there is room for new key

in leaf To make room for any key that’s kicked

upstairs

Sometimes insertion increases depth of tree by creating a new root, which is the only way a 2-3-4 tree can increase in height.

17

Page 18: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

18

Analysis of Bottom-Up Insert

Algorithm put(k, o)

1. We search for key k to locate the insertion node v

2. We add the new entry (k, o) at node v

3. while overflow(v)

if isRoot(v)

create a new empty root above v

v split(v)

Let T be a 2-3-4 tree with n items

Tree T has O(log n) height

Step 1 takes O(log n) time because we visit O(log n) nodes

Step 2 takes O(1) time Step 3 takes O(log n)

time because each split takes O(1) time and we perform O(log n) splits

Thus, an insertion in a (2,4) tree takes O(log n) time

Page 19: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

19

Bottom-Up DeletionWe reduce deletion of an entry to the case where the item is at the node with leaf childrenOtherwise, we replace the entry with its inorder successor (or, equivalently, with its inorder predecessor) and delete the latter entryExample: to delete key 24, we replace it with 27 (inorder successor)

27 32 35

10 15 24

2 8 12 18

32 35

10 15 27

2 8 12 18

Page 20: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

20

Underflow and FusionDeleting an entry from a node v may cause an underflow, where node v becomes a 1-node with one child and no keysTo handle an underflow at node v with parent u, we consider two casesCase 1: the adjacent siblings of v are 2-nodes

Fusion operation: we merge v with an adjacent sibling w and move an entry from u to the merged node v'

After a fusion, the underflow may propagate to the parent u

9 14

2 5 7 10

u

v

9

10 14

u

v'w2 5 7

Single key

Many fusions possible

Page 21: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

21

Underflow and TransferTo handle an underflow at node v with parent u, we consider two casesCase 2: an adjacent sibling w of v is a 3-node or a 4-node

Transfer operation:1. we move a child of w to v 2. we move an item from u to v3. we move an item from w to u

After a transfer, no underflow occurs

4 9

6 82

u

vw

4 8

62 9

u

vw

Only one transferduring deletion

Page 22: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

More Delete Examples

(2,4) Trees 22

Page 23: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Propagating Fusions

23

Page 24: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Delete: Top Down

Steps for deleting key k Find key k

If it’s in leaf, remove it It it’s internal node, replace it with entry

with next higher key (always a leaf)

Idea During search, eliminate 1-key nodes

(except the root) so key can be removed from leaf w/o emptying it

24

It’s like“always delete a leaf node”

Page 25: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Rule 1 for Top-Down Delete

Rule 1: Transfer When 1-key node (except root) is met,

borrow a key from an adjacent sibling

25

2, 4

1 5,6,7

Transfer

Subtrees have to be adjusted too!

3

2, 5

1 6,73, 4

Rule 1 is no good for this guy(No double rotation)

Page 26: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Rule 2 for Top-Down Delete

Rule 2: Fusion If no adjacent sibling has more than 1

key, borrow a key from parent (Note that parent except root has at least 2 keys.)

26

2, 4

1 5

Fuse

Subtrees have to be adjusted too!

3

4

1, 2, 3 5

Page 27: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Rule 3 for Top-Down Delete

Rule 3: Fusion with root If parent is root and contains only one

key, and sibling has only one key Fuse into a 3-key node as the new root (and tree depth decrease by one)

27

2

1 3

Fuse 1, 2, 3

Page 28: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Example of Top-Down Delete

Delete 40

28

20

14 62, 70, 7932 43

1018

25

33

42

47

57, 6074

81

50

40

66

Page 29: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Example of Top-Down Delete

Delete 40

29

14 62, 70, 7932 43

1018

25

33

42

47

57, 6074

8166

20, X, 50

Place holderto be filled with 42

Page 30: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Example of Top-Down Delete

Delete 40

30

14 70, 7932 43, 50

1018

25

33

42

47

57, 6074

8166

20, X, 62

Place holderto be filled with 42 Uplifted node

Drop-down node

Adjusted subtree

Page 31: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Example of Top-Down Delete

Delete 40

31

14 70, 7932 50

1018

25

33

42, 43, 47 57, 6074

8166

20, X, 62

Now we can move 42 to X

Page 32: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

Example of Top-Down Delete

Final result after deleting 40

32

14 70, 7932 50

1018

25

33

43, 47 57, 6074

8166

20, 42, 62

Page 33: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

33

Analysis of Bottom-Up Delete

Let T be a 2-3-4 tree with n items Tree T has O(log n) height

In a deletion operation We visit O(log n) nodes to locate the node

from which to delete the entry We handle an underflow with a series of

O(log n) fusions, followed by at most one transfer

Each fusion and transfer takes O(1) time

Thus, deleting an item from a 2-3-4 tree takes O(log n) time

Page 34: © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

34

Comparison of Map Implementations

Find Put Erase Notes

Hash Table

1expected

1expected

1expected

o no ordered map methodso simple to implement

Skip List

log nhigh prob.

log nhigh prob.

log nhigh prob.

o randomized insertiono simple to implement

AVL and 2-3-4 Tree

log nworst-case

log nworst-case

log nworst-case

o complex to implement