Top Banner
Algorithms 2 - 3 Tree 2 3 4 Tree Balanced Trees
56
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2-3 Tree

Algorithms

2 - 3 Tree

2 – 3 – 4 Tree

Balanced Trees

Page 2: 2-3 Tree

2-3 Trees

each internal node has either 2 or 3 children

all leaves are at the same level

Features

Page 3: 2-3 Tree

2-3 Trees with Ordered Nodes

2-node 3-node

• leaf node can be either a 2-node or a 3-node

Page 4: 2-3 Tree

Why 2-3 tree

● Faster searching?

■ Actually, no. 2-3 tree is about as fast as an “equally balanced” binary tree, because you sometimes have to make 2 comparisons to get past a 3-node

● Easier to keep balanced?

■ Yes, definitely.

■ Insertion can split 3-nodes into 2-nodes, or promote 2-nodes to 3-nodes to keep tree approximately balanced!

Page 5: 2-3 Tree

Why is this better?

● Intuitively, you unbalance a binary tree when

you add height to one path significantly more

than other possible paths.

● With the 2-3 insert algorithm, you can only

add height to the tree when you create a new

root, and this adds one unit of height to all

paths simultaneously.

● Hence, the average path length of the tree stays

close to log N.

Page 6: 2-3 Tree

Example of 2-3 Tree

Page 7: 2-3 Tree

What did we gain?

What is the time efficiency of searching for an item?

Page 8: 2-3 Tree

Gain: Ease of Keeping the Tree Balanced

Binary Search

Tree

2-3 Tree

both trees after

inserting items

39, 38, ... 32

Page 9: 2-3 Tree

Inserting Items

Insert 39

Page 10: 2-3 Tree

Inserting ItemsInsert 38

insert in leafdivide leaf

and move middle

value up to parent

result

Page 11: 2-3 Tree

Inserting ItemsInsert 37

Page 12: 2-3 Tree

Inserting ItemsInsert 36

insert in leaf

divide leaf

and move middle

value up to parent

overcrowded

node

Page 13: 2-3 Tree

Inserting Items... still inserting 36

divide overcrowded node,

move middle value up to parent,

attach children to smallest and largest

result

Page 14: 2-3 Tree

Inserting ItemsAfter Insertion of 35, 34, 33

Page 15: 2-3 Tree

Inserting so far

Page 16: 2-3 Tree

Inserting so far

Page 17: 2-3 Tree

Inserting Items

How do we insert 32?

Page 18: 2-3 Tree

Inserting Items

creating a new root if necessary

tree grows at the root

Page 19: 2-3 Tree

Inserting Items

Final Result

Page 20: 2-3 Tree

70

Deleting ItemsDelete 70

80

Page 21: 2-3 Tree

Deleting ItemsDeleting 70: swap 70 with inorder successor (80)

Page 22: 2-3 Tree

Deleting ItemsDeleting 70: ... get rid of 70

Page 23: 2-3 Tree

Deleting Items

Result

Page 24: 2-3 Tree

Deleting Items

Delete 100

Page 25: 2-3 Tree

Deleting Items

Deleting 100

Page 26: 2-3 Tree

Deleting Items

Result

Page 27: 2-3 Tree

Deleting Items

Delete 80

Page 28: 2-3 Tree

Deleting Items

Deleting 80 ...

Page 29: 2-3 Tree

Deleting Items

Deleting 80 ...

Page 30: 2-3 Tree

Deleting Items

Deleting 80 ...

Page 31: 2-3 Tree

Deleting ItemsFinal Result

comparison with

binary search tree

Page 32: 2-3 Tree

Deletion Algorithm I

1. Locate node n, which contains item I

2. If node n is not a leaf swap I with inorder successor

deletion always begins at a leaf

3. If leaf node n contains another item, just delete item I

else

try to redistribute nodes from siblings (see next slide)

if not possible, merge node (see next slide)

Deleting item I:

Page 33: 2-3 Tree

Deletion Algorithm II

A sibling has 2 items:

redistribute item

between siblings and

parent

No sibling has 2 items:

merge node

move item from parent

to sibling

Redistribution

Merging

Page 34: 2-3 Tree

Deletion Algorithm III

Internal node n has no item left

redistribute

Redistribution not possible:

merge node

move item from parent

to sibling

adopt child of n

If n's parent ends up without item, apply process recursively

Redistribution

Merging

Page 35: 2-3 Tree

Deletion Algorithm IV

If merging process reaches the root and root is without item

delete root

Page 36: 2-3 Tree

Operations of 2-3 Trees

all operations have time complexity of log n

Page 37: 2-3 Tree

2-3-4 Trees

• similar to 2-3 trees

• 4-nodes can have 3 items and 4 children

4-node

Page 38: 2-3 Tree

2-3-4 Tree Example

Page 39: 2-3 Tree

2-3-4 Tree: Insertion

Insertion procedure:

• similar to insertion in 2-3 trees

• items are inserted at the leafs

• since a 4-node cannot take another item,

4-nodes are split up during insertion process

Strategy

• on the way from the root down to the leaf:

split up all 4-nodes "on the way"

insertion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)

Page 40: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100

Page 41: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 60, 30, 10, 20 ...

... 50, 40 ...

Page 42: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 50, 40 ...

... 70, ...

Page 43: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 70 ...

... 80, 15 ...

Page 44: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 80, 15 ...

... 90 ...

Page 45: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 90 ...

... 100 ...

Page 46: 2-3 Tree

2-3-4 Tree: Insertion

Inserting 100 ...

Page 47: 2-3 Tree

2-3-4 Tree: Insertion Procedure

Splitting 4-nodes during Insertion

Page 48: 2-3 Tree

2-3-4 Tree: Insertion Procedure

Splitting a 4-node whose parent is a 2-node during insertion

Page 49: 2-3 Tree

2-3-4 Tree: Insertion Procedure

Splitting a 4-node whose parent is a 3-node during insertion

Page 50: 2-3 Tree

2-3-4 Tree: Deletion

Deletion procedure:

• similar to deletion in 2-3 trees

• items are deleted at the leafs

swap item of internal node with inorder successor

• note: a 2-node leaf creates a problem

Strategy (different strategies possible)

• on the way from the root down to the leaf:

turn 2-nodes (except root) into 3-nodes

deletion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)

Page 51: 2-3 Tree

2-3-4 Tree: Deletion

Turning a 2-node into a 3-node ...

Case 1: an adjacent sibling has 2 or 3 items

"steal" item from sibling by rotating items and moving subtree

30 50

10 20 40

25

20 50

10 30 40

25

"rotation"

Page 52: 2-3 Tree

2-3-4 Tree: Deletion

Turning a 2-node into a 3-node ...

Case 2: each adjacent sibling has only one item

"steal" item from parent and merge node with sibling

(note: parent has at least two items, unless it is the root)

30 50

10 40

25

50

25

merging

10 30 40

3535

Page 53: 2-3 Tree

2-3-4 Tree: Deletion Practice

Delete 32, 35, 40, 38, 39, 37, 60

Page 54: 2-3 Tree

2-3-4 Insert Example

6, 15, 25

2,4,5 10 18, 20 30

Insert 24, then 19

Page 55: 2-3 Tree

Insert 24: Split root first

6

2,4,5 10 18, 20,24 30

15

25

Page 56: 2-3 Tree

Insert 19, Split leaf (20 up) first

6

2,4,5 10 18, 19 30

15

20, 25

24