Multi-way Search Trees Each internal node of a multi-way search tree - - PDF document

multi way search trees
SMART_READER_LITE
LIVE PREVIEW

Multi-way Search Trees Each internal node of a multi-way search tree - - PDF document

(2,4) T REES Search Trees (but not binary) also known as 2-4, 2-3-4 trees very important as basis for Red-Black trees (so pay attention!) (2,4) Trees 1 Multi-way Search Trees Each internal node of a multi-way search tree T :


slide-1
SLIDE 1

1 (2,4) Trees

(2,4) TREES

  • Search Trees (but not binary)
  • also known as 2-4, 2-3-4 trees
  • very important as basis for Red-Black trees (so pay

attention!)

slide-2
SLIDE 2

2 (2,4) Trees

Multi-way Search Trees

  • Each internal node of a multi-way search tree T:
  • has at least two children
  • stores a collection of items of the form (k, x),

where k is a key and x is an element

  • contains d - 1 items, where d is the number of

children

  • “contains” 2 pseudo-items:

,

  • Children of each internal node are “between” items
  • all keys in the subtree rooted at the child fall

between keys of those items

  • External nodes are just placeholders

k0 ∞ – = kd ∞ =

slide-3
SLIDE 3

3 (2,4) Trees

Multi-way Searching

  • Similar to binary searching
  • If search key

, search the leftmost child

  • If

, search the rightmost child

  • That’s it in a binary tree; what about if

?

  • Find two keys

and between which s falls, and search the child .

  • What would an in-order traversal look like?

s k1 < s kd

1 –

> d 2 > ki

1 –

ki vi

3 4 6 8 23 24 27 22 5 10 25 11 13 14 Searching for s = 8 Searching for s = 12 Not found! 17 18 19 20 21

slide-4
SLIDE 4

4 (2,4) Trees

(2,4) Trees

  • At most 4 children
  • All external nodes have same depth
  • Height h of (2,4) tree is

.

  • How is this fact useful in searching?

O n log ( )

3 4 11 6 8 13 14 17 12 5 10 15

slide-5
SLIDE 5

5 (2,4) Trees

(2,4) Insertion

  • Always maintain depth condition
  • Add elements only to existing nodes
  • What if that makes a node too big?
  • overflow
  • Must perform a split operation
  • replace node

with two nodes and

  • gets the first two keys
  • gets the last key
  • send the other key up the tree
  • if

is root, create new root with third key

  • otherwise just add third key to parent
  • Much clearer with a few pictures...

4 4 6 4 6 12 Empty tree Insert 4 Insert 6 Insert 12 Insert 15

?

v v' v'' v' v''

v

slide-6
SLIDE 6

6 (2,4) Trees

(2,4) Insertion (cont.)

  • Tree always grows from the top, maintaining

balance

  • What if parent is full?

15 4 12 6 15 4 6 12 4 6 15 12 3 4 6 15 12 6 3 5 4 15 12 5 6 3 4 12 15 5 12 15 3 4 6 Insert 15 Insert 5 Insert 3

slide-7
SLIDE 7

7 (2,4) Trees

(2,4) Insertion (cont.)

  • Do the same thing:
  • Overflow cascade all the way up to the root
  • still at most

3 4 5 12 10 11 6 8 13 15 14 17 15 3 4 11 6 8 13 14 17 5 12 10 12 3 4 5 10 11 6 8 15 13 14 17 Insert 17

O n log ( )

slide-8
SLIDE 8

8 (2,4) Trees

(2,4) Deletion

  • A little trickier
  • First of all, find the key
  • simple multi-way search
  • Then, reduce to the case where deletable item is at

the bottom of the tree

  • Find item which precedes it in in-order traversal
  • Swap them
  • Remove the item
  • Easy, right?
  • ...but what about removing from 2-nodes?

14 17 15 5 11 6 8 10 13 Delete 13

slide-9
SLIDE 9

9 (2,4) Trees

(2,4) Deletion (cont.)

  • Not enough items in the node
  • underflow
  • Pull an item from the parent, replace it with an item

from a sibling

  • called transfer
  • Still not good enough! What happens if siblings are

2-nodes?

  • Could we just pull one item from the parent?
  • too many children
  • But maybe...

Delete 4 11 6 8 5 10 4 11 10 8 5 6 u v w

slide-10
SLIDE 10

10 (2,4) Trees

(2,4) Deletion (cont.)

  • We know that the node’s sibling is just a 2-node
  • So we fuse them into one
  • after stealing an item from the parent, of course
  • Last special case, I promise: what if the parent was a

2-node?

12 10 5 6 8 5 6 8 10 u v 5 6 8 10 u Delete 12

slide-11
SLIDE 11

11 (2,4) Trees

(2,4) Deletion (cont.)

  • Underflow can cascade up the tree, too.

17 15 5 11 6 8 10 14 17 5 11 6 8 10 15 u v 5 8 10 11 15 17 6 u 5 6 11 15 17 8 10 Delete 14

slide-12
SLIDE 12

12 (2,4) Trees

(2,4) Conclusion

  • The height of a (2,4) tree is

.

  • Split, transfer, and fusion each take

.

  • Search, insertion and deletion each take

.

  • Why are we doing this?
  • (2,4) trees are fun! Why else would we do it?
  • Well, there’s another reason, too.
  • They’re pretty fundamental to the idea of Red-Black trees as

well.

  • And you’re covering Red-Black trees on Monday.
  • Perhaps more importantly, your next project is a Red-Black tree.
  • Have a nice weekend!

O n log ( ) O 1 ( ) O n log ( )