Top Banner
Balanced Trees Balanced Trees
54

Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Jan 02, 2016

Download

Documents

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: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Balanced TreesBalanced Trees

Page 2: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Maintaining BalanceMaintaining Balance

Binary Search Tree– Height governed by

Initial order Sequence of insertion/deletion

– Changes occur at leaf nodes Need a structure that tends to maintain balance

– How? Grow in ‘width’ first, then height Accommodate horizontal growth More data at each level Nodes of two forms

– One data member and two children (“Two node”)– Two data members and three children (“Three node”)

Page 3: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Nodes2-3 Tree Nodes

> L< S

S

> S< S>S <L

S L

Page 4: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

BST InsertionBST Insertion60

30

5010

90

10080

704020

39

60

30

5010

90

10080

704020

39

39

60

30

5010

90

10080

704020

Page 5: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (39)2-3 Tree Insertion (39)

70 90

60 10080

30

10 20

50

40

70 90

60 10080

30

10 20

50

39 40

Page 6: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (38)2-3 Tree Insertion (38)30

10 20 39 4030

10 20 38 39 40

3810 20

30 39

40

Page 7: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (37)2-3 Tree Insertion (37)

70 90

60 10080

50

3810 20

30 39

40

10 20

30 39

4037 38

50

70 90

60 10080

Page 8: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (36)2-3 Tree Insertion (36)

10 20

30 39

4037 38

70 90

60 10080

50

Page 9: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (36)2-3 Tree Insertion (36)

10 20

30 39

4037 38 10 20

30 39

4036 37 38

10 20

30 37 39

403836

50 37 50

10 20

30

36

39

4038

70 90

60 10080

Page 10: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3-4 Trees, Red-Black 2-3-4 Trees, Red-Black TreesTrees

Page 11: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (38 – 2-3 Tree Insertion (38 – Leaf Node)Leaf Node)30

10 20 39 4030

10 20 38 39 40

3810 20

30 39

40

Page 12: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Generalized Insertion – Generalized Insertion – Leaf NodeLeaf Node

P

S M L e

M P

S eL

Page 13: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (36 2-3 Tree Insertion (36 Internal Node)Internal Node)

10 20

30 39

4037 38

70 90

60 10080

50

Page 14: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3 Tree Insertion (36)2-3 Tree Insertion (36)

10 20

30 39

4037 38 10 20

30 39

4036 37 38

10 20

30 37 39

403836

50 37 50

10 20

30

36

39

4038

70 90

60 10080

Page 15: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Generalized InsertionGeneralized Insertion,, Internal NodeInternal Node

P

S M L e

cba d

e

cba d

LS

M P

Page 16: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

2-3-4 Trees2-3-4 Trees If allowing

– 2 values/node– 3 children/node is better…

What about– 3 values/node– 4 children/node ???

Definition

S M L

> L< S >S <M >M <L

>L<S >S<L

S L

S

>S<S

–See 2-3 Tree

“3-Node”

…AND…

Page 17: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Insertion in 2-3-4 TreeInsertion in 2-3-4 Tree

Same scheme as 2-3 Tree– Traverse down tree– If node is 4-node

Split tree Insert into new tree

– This means what in terms of… Growth in Height versus Width? How far ‘up’ will insertion cascade?

Page 18: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Insertion in 2-3-4 Tree (20)Insertion in 2-3-4 Tree (20)

10 30 60

20

10

30

60 10 20

30

60

Page 19: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Insertion in 2-3-4 Tree (70)Insertion in 2-3-4 Tree (70)

10 20

30

40 50 60

10 20

30 50

40 60

10 20

30 50

40 60 70

70

Page 20: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Generalized Insertion in 2-Generalized Insertion in 2-3-4 Tree3-4 Tree

P

S M L e

cba d

e

cba d

LS

M P

Splitting a 4-Node with a 2-Node Parent

P

S M L e

cba d

Page 21: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Generalized Insertion in 2-Generalized Insertion in 2-3-4 Tree3-4 Tree

Splitting a 4-Node with a 3-Node Parent

f

cba d

P Q

S M L ee

cba d

M P Q

LS ff

cba d

P Q

S M L e

Page 22: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Binary Search Tree Binary Search Tree RepresentationRepresentation

Color-code node-type information– 2-Node

Ordinary binary search tree node

– 4-Node with values A, B, and C Center B is Black Children A and C are Red

– 3-Node with values A, and B (two choices) A is Black parent, B is Red right child

– or – B is Black parent, A is Red left child

– Ignore color-code Structure of binary search tree

Two ways to change tree– Color change– Rotations

Page 23: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

B

A C

S T U V

Red-Black Tree Red-Black Tree RepresentationRepresentation

A B C

S T U V

4-Node

Page 24: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Red-Black Tree Red-Black Tree RepresentationRepresentation

A B

S T U

B

A U

S T

A

S B

T U

3-Node

Page 25: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Red-Black Tree TranslationRed-Black Tree Translation10

8 20

12 40

301541

3 9

8 10 20

1 3 4 12 15 30 409

Page 26: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Red-Black Tree InsertionRed-Black Tree Insertion

B

A C

S T U V

A B C

S T U V

Parent (root) is 4-NodeB

A C

S T U V

B

A C

S T U V

2-3

-4 R

ep

res

en

tati

on

Re

d-B

lac

k R

ep

res

en

tati

on

Page 27: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Red-Black Tree InsertionRed-Black Tree InsertionParent is 2-Node

2-3

-4 R

ep

res

en

tati

on

Re

d-B

lac

k R

ep

res

en

tati

on

P

S M L e

cba d

e

cba d

LS

M P

M

S L

A B C D

P

E M

S L

A B C D

P

E

Page 28: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Red-Black Tree InsertionRed-Black Tree InsertionParent is 3-Node (version 1 of 2)

2-3

-4 R

ep

res

en

tati

on

Re

d-B

lac

k R

ep

res

en

tati

on

e

cba d

M P Q

LS ff

cba d

P Q

S M L e

M

S L

A B C D

P

Q

FE

M

S L

A B C D

P

Q

FE

Page 29: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Red-Black Tree InsertionRed-Black Tree InsertionParent is 3-Node (version 2 of 2) - Rotation

M

S L

A B C D

P

Q

FEM

S L

A B C D

P

Q

F

Ef

cba d

P Q

S M L e

Page 30: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Adelson-Velskii and Landis Adelson-Velskii and Landis (AVL)(AVL)

30

10

20

10 30

20

40

10 30

20

Page 31: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

AVL TreeAVL Tree Binary tree For every node x, define its

balance factor

balance factor of x =

height of left subtree of x - height of right subtree of x

Balance factor of every node x is -1, 0, or 1

The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2).

The height of every n node binary tree is at least log2 (n+1).

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

Page 32: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

AVL Search TreeAVL Search Tree

0 0

0 0

1

0

-1 0

1

0

-1

1

-110

7

83

1 5

30

40

20

25

35

45

60

Page 33: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

insert(9)insert(9)

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

90

-1

0

10

7

83

1 5

30

40

20

25

35

45

60

Page 34: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

insert(29)insert(29)

0 0

0 0

1

0

0

1

0

-1

1

-110

7

83

1 5

30

40

20

25

35

45

60

29

0

-1

-1-2

RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)

Page 35: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

insert(29)insert(29)

0 0

0 0

1

0

0

1

0

-1

1

-110

7

83

1 5

30

40

2535

45

600

RR rotation. 20

029

Page 36: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

AVL RotationsAVL Rotations

Single Rotation– RR– LL

Double Rotation– RL

new node in left subtree of right subtree Go right down and then go left down

– LR

Page 37: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

LL Rotation AlgorithmLL Rotation AlgorithmBinaryNode LL_Rotate ( BinaryNode k2) {

BinaryNode k1 = k2.left;k2.left = k1.right;k1.right = k2;return k1;

}

Page 38: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

RR Rotation AlgorithmRR Rotation AlgorithmBinaryNode RR_Rotate ( BinaryNode k1 ) {

BinaryNode k2 = k1.right;k1.right = k2.left;k2.left = k1;return k2;

}

Page 39: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Single Rotation Running Single Rotation Running ExampleExample

Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially

1

0

-1

-21

2

3

1

20

2

13

0

2

13

0

4

5

2

14

5

3

Page 40: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Single Rotation (2)Single Rotation (2)

Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially

2

14

5

3

6

2

1

4

5

3 6

-2

Page 41: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Why Double Rotation?Why Double Rotation?Single Rotation cannot solve LR or RL

Page 42: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

LR Double RotationLR Double RotationBinaryNode LR_doubleRotate ( BinaryNode k3 ){

k3.left = RR_Rotate ( k3.left );return LL_Rotate ( k3 );

}

Page 43: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

LR Double Rotation LR Double Rotation Example Example

Page 44: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

RL Double RotationRL Double RotationBinaryNode RL_doubleRotate ( BinaryNode k1 ) {

k1.right = LL_Rotate ( k1.right );return RR_Rotate ( k1 );

}

Page 45: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

More Double Rotation More Double Rotation ExampleExample

Consider the insertion of 15, 14, 13 into the AVL tree built by inserting 1, ...,7 sequentially

2

1

4

6

3 75

15

14

2

1

4

6

3 145

157

Page 46: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Double Rotation Example Double Rotation Example (2)(2)

Consider the insertion of 15, ..., 10, 9, 8, and 8.5 into the AVL tree built by inserting 1, ...,7 sequentially

2

1

4

6

3145

157

13

2

1

4

7

3146

15135

Page 47: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Single Left RotationSingle Left Rotation

50

10 40

20

60

30

50

10

40

20

6030

Page 48: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Double Rotation (Step Double Rotation (Step One)One)

50

10

40

20

6030

3525

22

50

20

40

30

6035

2510

22

Page 49: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Double Rotation (Step Double Rotation (Step Two)Two)

50

20

40

30

6035

2510

22

40

10

30

20

503525

22 60

Page 50: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

B-TreesB-Trees

Binary tree is still not efficient enough for searching

A perfectly balanced binary tree has 5 levels for 25 nodes, while a 5-ary tree has only 3 levels

Especially useful for external usage

Page 51: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

B-treeB-tree A B-tree of order m has the following properties: The data items are stored at the leaves in order The root is either a leaf or has 2 to m children All non-leaf nodes (except the root) have m/2 to m children Each non-leaf node has at most m-1 keys: key i is the data value

in subtree i+1 All leaves are at the same depth and have L/2 to L items

Page 52: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

B-tree Insertion - SplitB-tree Insertion - Split L and M are determined by the sizes of a node, the size of an item

and the size of a key Requiring nodes to be half full guarantees that the B-tree will be

pretty balanced and will not degenerate into a simple binary tree, or even into a linked list

A B-tree of order 3 is also known as a 2-3 tree Insertion of 55 in the example B-tree causes a split into two leaves

Page 53: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

B-tree Insertion - B-tree Insertion - Cascaded SplitCascaded Split Further insertion of 40 in the B-tree causes a split into

two leaves and then a split of the parent node

Page 54: Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

B-tree DeletionB-tree Deletion Deletion is done similarly If the number of items in a leaf falls below the

minimum, adopt an item from a neighboring leaf If the number of items in the neighboring leaves are

also minimum, combine two leaves. Their parent will then lose a child and it may need to be combined with its neighbor

This combination process should be recursively executed up the tree until:– Getting to the root– A parent has more than the minimum number of children