BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree...

25
Bm267 BM267 - Introduction to Data Structures Ankara University Computer Engineering Department Bulent Tugrul 13. Balanced Search Trees

Transcript of BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree...

Page 1: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

BM267 - Introduction to Data

Structures

Ankara University

Computer Engineering DepartmentBulent Tugrul

13. Balanced Search Trees

Page 2: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

Balanced Search Trees

• Binary search trees in the average requires ‘logn’

comparison for each operation (searching, deletion

and insertion), unfortunately in the worst case they

need ‘n’ comparisons.

• Computer scientist come up with some solutions

to find a structure that preserves the good

properties of the classical binary search tree.

– AVL Trees

– 2-3 Trees

– Red Black Trees

Page 3: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

• Definition: An AVL tree is binary search tree in

which the balance factor of every node, which is

defined as the difference between the heights of the

node’s left and right subtrees, is either 0 or +1 or –1.

10

207

5 15

4

2 8 00

-11

0

0

1

1 10

7

5 15

4

2 8 00

-11

0 0

2

Page 4: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Fall 2005 Bm267

AVL Trees

• If an insertion of a new node makes an AVL tree

unbalanced, we transform the tree by rotation.

• A rotation in an AVL tree is a local

transformations of its subtree rooted at a node

whose balance has become either +2 or –2.

• There are only four types of rotations; in fact two

of them are mirror images of the other two.

10

5

40

1

2 5

4 100 0

0

Single R rotation

Page 5: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

2

1 30 0

0

Single L rotation

3

2

1

0

-1

-2

2

1 30 0

0

Double LR rotation

2

1

3

0

-1

2

1

2

3

0

2

1

Page 6: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

2

1 30 0

0

Double RL rotation

2

3

1-2

0

1

3

2

1-2

0

1

Page 7: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

R

C

T1 T2

T3

C

T1

R

T2 T3

Single R rotation

{T1} < C < {T2} < R < {T3}

Page 8: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

R

C

T1

T2 T3

C

R

T1 T2

T3

Single L rotation

{T1} < R < {T2} < C < {T3}

Page 9: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

C

T1

T4G

T2 T3

R

or

{T1} < C < {T2} < G < {T3} <R < {T4}

Double LR rotation

Page 10: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

C

T1 T4

R

T2 T3

G

or

{T1} < C < {T2} < G < {T3} <R < {T4}

Page 11: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

5 5

6

5

6

8

0

5

6

8

5

6

8

3

5

6

8

3

2

3

6

8

2 5

L

R

-1

0

0

-1

-2

0 0

0

0

01

1

0

1

2

2

0

00

0 0

1

Page 12: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

3

6

8

2 5

0

0

-1

2

3

5

6

2 4 00

0

0

4

1

0

8

-1

0

LR

Page 13: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

3

5

6

2 4 00

0

-1

8

-2

1

RL

7 0

3

5

7

2 4 00

0

86

0

0

0

0

Page 14: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

AVL Trees

• How efficient is an AVL trees?

• The height h of any AVL tree with n nodes

satisfies the inequalities

log2n h < 1.4405log2(n+2)–1.3277

Page 15: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• The second idea of balancing a search tree is to

allow more than one key in the same node.

• The simplest implementation of this idea is 2-3

trees.

• A 2-3 tree is a tree that can have nodes of two

kinds: 2-nodes and 3-nodes.

• A 2-node contains a single key K and has two

children: the left child serves as the root of a

subtree whose keys are less than K and the right

child serves as the root of a subtree whose keys

are greater than K.

Page 16: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

K

< K > K

2-node

Page 17: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• A 3-node contains two ordered keys K1 and K2

(K1 < K2 ) and has three children.

• The leftmost subtree has keys less than K1.

• The middle subtree has keys between K1 and K2.

• The rightmost subtree has keys greater than K2

Page 18: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

< K1> K2

K1 < K2

(K1, K2)

Page 19: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• The last requirement of the 2-3 tree is that its

leaves must be on the same level.

• A 2-3 tree is always height balanced;the length of

a path from the root of the tree to a leaf must be

same for every leaf.

• Searching for a given key K in a 2-3 tree quite

straightforward. We start with the root. If the root

is a 2-node, we act as if it were a binary search

tree: we either stop if K is equal to the root’s key

or continue the search in the left or right subtree.

Page 20: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• If the root is a 3-node, we know after no more than

two key comparisons whether the search can be

stopped ( If K is equal to one of the root’s keys) or

in which of the root’s three subtrees it needs to be

continued.

3, 8

2 4,5 9

Page 21: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• Inserting a new key in a 2-3 tree is done asfollows:

– We always insert a new key K at a leaf except for theempty tree.

– The appropriate leaf is found by performing a searchfor K.

• If the leaf in question is a 2-node key, we insert Kthere as either the first or the second key, dependingon whether K is smaller or larger than the node’s oldkey.

• If the leaf is a 3- node, we split the leaf in two; Thesmallest of the three is put in the first leaf, thelargest key is put in the second leaf , while themiddle key is promoted to the old leaf’s parent.

Page 22: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• If the leaf happens to be the tree’s root, a new root

is created to accept the middle key.

• Note that promotion of a middle key to its parent

can cause the parent’s overflow and hence can

lead to several node splits along the chain of the

leaf’s ancestors.

9

9,5,8,3,2,4,7

5, 9 5, 8, 9 8

5 9

Page 23: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

8

93, 5

8

92, 3, 5 2 9

3, 8

5

2 9

3, 8

4,5 2 9

3, 8

4, 5, 7

Page 24: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

2 9

3, 5, 8

4 7

5

3 8

2 4 7 9

Page 25: BM267 - Introduction to Data Structures...AVL Trees •Definition: An AVL tree is binary search tree in which the balance factor of every node, which is defined as the difference between

Bm267

2-3 Search Trees

• The efficiency of the dictionary operations depends on thetree’s height.

• Let’s find an upper bound for a 2-3 tree. A 2-3 tree ofheight h with the smallest number of keys is full tree of 2-nodes.

n >= 1+2+…+2h = 2h+1 –1

and hence

h <= log2(n+1) –1

• On the other hand, a 2-3 tree of height h with the largestnumber of keys is a full of 3-nodes, each with two keysand three children.

n <= 2*1+2*3+…+2*3h = 3h+1 –1

h>=log3(n+1) –1

Therefore: log3(n+1)–1 <= h <= log2(n+1)–1