Top Banner
Data Structures CSCI 2720 Spring 2007 Balanced Trees
66

Data Structures CSCI 2720 Spring 2007 Balanced Trees.

Jan 03, 2016

Download

Documents

Linda Pearson
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: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

Data Structures

CSCI 2720Spring 2007

Balanced Trees

Page 2: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 2

Outline

Balanced Search Trees• 2-3 Trees

• 2-3-4 Trees

• Red-Black Trees

Page 3: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 3

Why care about advanced implementations?

Same entries, different insertion sequence:

Not good! Would like to keep tree balanced.

Page 4: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 4

2-3 Trees

each internal node has either 2 or 3 children all leaves are at the same level

Features

Page 5: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 5

2-3 Trees with Ordered Nodes

2-node 3-node

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

Page 6: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 6

Example of 2-3 Tree

Page 7: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 7

Traversing a 2-3 Tree

inorder(in ttTree: TwoThreeTree)if(ttTree’s root node r is a leaf)

visit the data item(s)else if(r has two data items){

inorder(left subtree of ttTree’s root)visit the first data iteminorder(middle subtree of ttTree’s root)visit the second data iteminorder(right subtree of ttTree’s root)

}else{

inorder(left subtree of ttTree’s root)visit the data iteminorder(right subtree of ttTree’s root)

}

Page 8: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 8

Searching a 2-3 Tree

retrieveItem(in ttTree: TwoThreeTree,in searchKey:KeyType,out treeItem:TreeItemType):boolean

if(searchKey is in ttTree’s root node r){

treeItem = the data portion of rreturn true

}else if(r is a leaf)

return falseelse{

return retrieveItem( appropriate subtree,searchKey, treeItem)

}

Page 9: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 9

What did we gain?

What is the time efficiency of searching for an item?

Page 10: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 10

Gain: Ease of Keeping the Tree Balanced

Binary SearchTree

2-3 Tree

both trees afterinserting items39, 38, ... 32

Page 11: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 11

Inserting Items

Insert 39

Page 12: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 12

Inserting Items

Insert 38

insert in leafdivide leaf

and move middlevalue up to parent

result

Page 13: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 13

Inserting Items

Insert 37

Page 14: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 14

Inserting Items

Insert 36

insert in leaf

divide leafand move middlevalue up to parent

overcrowdednode

Page 15: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 15

Inserting Items

... still inserting 36

divide overcrowded node,move middle value up to parent,

attach children to smallest and largest

result

Page 16: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 16

Inserting Items

After Insertion of 35, 34, 33

Page 17: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 17

Inserting so far

Page 18: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 18

Inserting so far

Page 19: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 19

Inserting Items

How do we insert 32?

Page 20: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 20

Inserting Items

creating a new root if necessary tree grows at the root

Page 21: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 21

Inserting Items

Final Result

Page 22: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 22

70

Deleting Items

Delete 70

80

Page 23: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 23

Deleting Items

Deleting 70: swap 70 with inorder successor (80)

Page 24: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 24

Deleting Items

Deleting 70: ... get rid of 70

Page 25: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 25

Deleting Items

Result

Page 26: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 26

Deleting Items

Delete 100

Page 27: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 27

Deleting Items

Deleting 100

Page 28: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 28

Deleting Items

Result

Page 29: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 29

Deleting Items

Delete 80

Page 30: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 30

Deleting Items

Deleting 80 ...

Page 31: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 31

Deleting Items

Deleting 80 ...

Page 32: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 32

Deleting Items

Deleting 80 ...

Page 33: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 33

Deleting Items

Final Result

comparison withbinary search tree

Page 34: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 34

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 Ielse

try to redistribute nodes from siblings (see next slide)if not possible, merge node (see next slide)

Deleting item I:

Page 35: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 35

Deletion Algorithm II

A sibling has 2 items: redistribute item

between siblings andparent

No sibling has 2 items: merge node move item from parent

to sibling

Redistribution

Merging

Page 36: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 36

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 37: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 37

Deletion Algorithm IV

If merging process reaches the root and root is without item delete root

Page 38: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 38

Operations of 2-3 Trees

all operations have time complexity of log n

Page 39: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 39

2-3-4 Trees

• similar to 2-3 trees• 4-nodes can have 3 items and 4 children

4-node

Page 40: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 40

2-3-4 Tree Example

Page 41: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 41

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 42: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 42

2-3-4 Tree: Insertion

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

Page 43: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 43

2-3-4 Tree: Insertion

Inserting 60, 30, 10, 20 ...

... 50, 40 ...

Page 44: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 44

2-3-4 Tree: Insertion

Inserting 50, 40 ...

... 70, ...

Page 45: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 45

2-3-4 Tree: Insertion

Inserting 70 ...

... 80, 15 ...

Page 46: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 46

2-3-4 Tree: Insertion

Inserting 80, 15 ...

... 90 ...

Page 47: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 47

2-3-4 Tree: Insertion

Inserting 90 ...

... 100 ...

Page 48: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 48

2-3-4 Tree: Insertion

Inserting 100 ...

Page 49: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 49

2-3-4 Tree: Insertion Procedure

Splitting 4-nodes during Insertion

Page 50: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 50

2-3-4 Tree: Insertion Procedure

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

Page 51: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 51

2-3-4 Tree: Insertion Procedure

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

Page 52: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 52

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 53: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 53

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 54: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 54

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

merging10 30 40

35 35

Page 55: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 55

2-3-4 Tree: Deletion Practice

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

Page 56: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 56

Red-Black Tree

• binary-search-tree representation of 2-3-4 tree

• 3- and 4-nodes are represented by equivalent binary trees

• red and black child pointers are used to distinguish betweenoriginal 2-nodes and 2-nodes that represent 3- and 4-nodes

Page 57: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 57

Red-Black Representation of 4-node

Page 58: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 58

Red-Black Representation of 3-node

Page 59: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 59

Red-Black Tree Example

Page 60: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 60

Red-Black Tree Example

Page 61: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 61

Red-Black Tree Operations

Traversals same as in binary search trees

Insertion and Deletion analog to 2-3-4 tree need to split 4-nodes need to merge 2-nodes

Page 62: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 62

Splitting a 4-node that is a root

Page 63: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 63

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

Page 64: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 64

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

Page 65: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 65

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

Page 66: Data Structures CSCI 2720 Spring 2007 Balanced Trees.

CSCI 2720Slide 66

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