1 CSC 2053 -TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that...

Preview:

DESCRIPTION

3 Balanced Trees Keeping a tree balanced guarantees an efficient search Why don't we do this then? Why don't we rebalance a tree every time a new node is inserted? – Because rebalancing can take up to O(n) operations.

Citation preview

1

CSC 2053 -TREES

AVL & BALANCED TREES

2

Balanced Trees

The advantage of balanced trees is that we can perform most operation in time proportional to O(log n)

But what is a balanced tree?

– A balanced binary tree is a binary tree where all leaves are within a specified distance from the root.

3

Balanced Trees Keeping a tree balanced guarantees an efficient search

Why don't we do this then? Why don't we rebalance a tree every time a new node is inserted?

– Because rebalancing can take up to O(n) operations.

4

Rebalancing a Binary Tree(example from Standish book)

5

73

2 4 6

5

Rebalancing a Binary Tree(example from Standish book)

5

73

2 4 6

1

Tree is now unbalanced

6

Rebalancing a Binary Tree(example from Standish book)

5

73

2 4 6

14

62

1 3 5 7

Compare the two trees .Every single node changes its position

Rebalancing can indeed be O(n)

7

The Cost of Rebalancing

To include complete rebalancing as part of the insert function, insertion may take O(n) instead of O(log n)

What we need is the guarantee that search will take O(log n) in the worst case

Can we do better?

Can we find a way to get O(log n) for searches and O(log n) for insertion?

8

AVL Trees

In 1960, two Russian mathematicians Georgii Maksimovich Adelson-Velskii and Evgenii Mikhailovich Landis developed a technique for keeping a binary search tree balanced as items are inserted into it.

A binary search tree that incorporate their techniques into the operations is called an AVL tree

You will be asked to spell their names in the final exam.

9

AVL TREES An AVL tree is a balanced binary search tree where the height

of the two subtrees (children) of a node differs by at most one.

A binary search tree has the AVL property if the heights of the left and right subtrees are either equal or differ by 1, and both subtrees hold the AVL property

10

Examples of AVL Trees

11

Examples of AVL Trees – Unbalanced

Page 11

12

Balance factors

We judge a tree to be balanced using the balance factor of nodes.

– The balance factor of node A is the difference between the height of the left and the height of the right subtrees –

– the left minus the right

A tree is said to be an AVL tree if all of its nodes have balance factors 1, 0 or -1 0

-11

0 0

13

Example of Trees and their balance factors

subtract # of levels in the Left sub tree from the # of levels in the Right subtree

14

Example of Trees and their balance factors

1

11

-1 1 -1

0 -1

0

0 0

0

15

Example of Trees and their balance factors

16

Example of Trees and their balance factors

0

-12

-1 0 -1

0 -1

0

0

-2

1

0

17

Example of Trees and their balance factors

18

Example of Trees and their balance factors

1

-23

-2

-1

0

-1

0

19

Balance Factors and insertion There are only two ways that a tree can become unbalanced and

that is through insertion or deletion.

Thus each time one of these operations is performed,

the balance factors must be checked starting at the point of insertion or removal of node and working back up the tree.

Because of the need to go back up the tree,

AVL trees are usually implemented with a reference to the parent node kept inside each node.

20

Rotations

The most important characteristic of AVL trees is that:

rebalancing can be achieved using one of four possible shape transformations which are called rotations. which are called rotations.

These rotations are used when a new node is insertednew node is inserted

21

Rotations

– Single-right rotationSingle-right rotation:: This rotation is used when the new item is in the left subtree of the left child of a node that has a balance factor of 2

– the node’s left sub tree is too long

– Single-left rotationSingle-left rotation: This rotation is used when the new item is in the right subtree of the right child of an ancestor with balance factor -2 the nodes right sub tree is too long.

22

ROTATIONS

– Double-right rotation(Left Right)Double-right rotation(Left Right): : This rotation is used when the new item is in the right subtree of the left child of the nearest ancestor with balance factor -2

– Double-left rotation(Right LeftDouble-left rotation(Right Left:) :) This rotation is used when the new item is in the left subtree of the right child of the nearest ancestor with balance factor 2

23

AVL Trees

If the balance factor of a node is 2balance factor of a node is 2, this means the left sub-tree left sub-tree has a path that is too longhas a path that is too long

If the balance factor of the left child is 1balance factor of the left child is 1, this means that the long path is the left sub-tree of the left childlong path is the left sub-tree of the left child

In this case, a simple simple right rotationright rotation of the left child of the left child around the original node will solve the imbalance

24

Single-Right Rotation- balance factor of node is 2 balance factor of node is 2

C 2

B 1

A

C

B

A

InitialConfiguration

FinalConfiguration

Parent

Parent

25

Single-Right Rotation

C2

B

A

1. Reset the link from the parent of C to Blink from the parent of C to B2. Set the left link of C equal to the right link left link of C equal to the right link of B. ( currently null)of B. ( currently null)3. Set the right link of B to point to C.right link of B to point to C.

Parent

26

Single-Right Rotation

C

B

A

null

1. Reset the link from the parent of C to B

2. Set the left link of C equal to the right link of B.

3. Set the right link of B to point to C.P

Represents the right link of B

27

Single-Right Rotation

C

B

A

X

1. Reset the link from the parent of C to B2. . Set the left link of C equal to the right link Set the left link of C equal to the right link of B. ( which is null)of B. ( which is null)3. Set the right link of B to point to C.

P

28

Single-Right Rotation

C

B

A

1. Reset the link from the parent of C to B2. Set the left link of C equal to the right link of B.3. Set the right link of B to point to CSet the right link of B to point to C..

X

P

29

Single-Right Rotation - Result

C2

B

A

C

B

A

InitialConfiguration

FinalConfiguration

parent

parent

30

AVL Trees

If the balance factor of a node is -balance factor of a node is -22, this means the right sub-tree has a path that is too longright sub-tree has a path that is too long

Then if the balance factor of the right child is balance factor of the right child is -1-1, , this means that the long path is the right sub-tree of the right childlong path is the right sub-tree of the right child

In this case, a simple simple left rotationleft rotation of the right child of the right child around the original node will solve the imbalance

31

Single-Left Rotation - Right subtree too longSingle-Left Rotation - Right subtree too long

C

F

N

InitialConfiguration

FinalConfiguration

P

C (-2)

F

N

P

32

Single-Left RotationSingle-Left Rotation

C

F

N

1.1. Reset the link from the parent of C Reset the link from the parent of C to Fto F

2. Set the right link of C equal to the left link of F.

3. Set the left link of F to point to C.P

33

Single-Left Rotation

C

F

N

1.1. Reset the link from the parent of C to FReset the link from the parent of C to F

2. Set the right link of C equal to the left link of F. (which is null, for this example)

3. Set the left link of F to point to C.

P

34

Single-Left Rotation

C

F

NX

1. Reset the link from the parent of C to F

2. Set the right link of C equal to the Set the right link of C equal to the left link of F.left link of F.

3. Set the left link of F to point to C.P

Represents the left link of F (null)

Could have a value

35

Single-Left Rotation

C

F

N

1. Reset the link from the parent of C to F2. Set the right link of C equal to the left link of F.

3. Set the left link of F to point to C.Set the left link of F to point to C.

P

36

Single-Left Rotation

C

F

N

InitialConfiguration

FinalConfiguration

C (-2)

F-1

N

parent

parent

37

AVL Trees

If the balance factor of a node is 2, this means the left sub-tree has a path that is too long

Then if the balance factor of the left child is -1, this means that the long path is the right sub-tree of the left child

In this case, a leftright rotation will solve the imbalance

38

Double-Right Rotation(left-right rotation)

M(2)

F-1

H

MF

H

InitialConfiguration

FinalConfiguration

P

Parent

39

Double-Right Rotation(left-right rotation)

M

F

H

1. Reset the link from the parent of F(M) to HReset the link from the parent of F(M) to H

2. Set the left link of H to point to F.

3. Reset the link from the parent of M to H

4. Set the right link of H to point to M.

P

We need to rotate left around F so M points to HM points to H

40

Double-Right Rotation (left-right rotation)

M

F

H

1. Reset the link from the parent of F (M)to H

2. Set the left link of H to point to F.

3. Reset the link from the parent of M to H4. Set the right link of H to point to M.

P

M points to HM points to H

41

Double-Right Rotation (left-right rotation)

M

F

H

1. Reset the link from the parent of F to H2. Set the left link of H to point to F.Set the left link of H to point to F.

3. Reset the link from the parent of M to H4. Set the right link of H to point to M.

P

We now need a right rotation around H

42

Double-Right Rotation(left-right rotation)

B

1. Reset the link from the parent of F to H2. Set the left link of H to point to F.

3. Reset the link from the parent of M (P) to HReset the link from the parent of M (P) to H4. Set the right link of H to point to M.

M

F

H

P

Make P reference HMake P reference H

43

Double-Right Rotation(left-right rotation)

M

F

H

1. Reset the link from the parent of F to H2. Set the left link of H to point to F.3. Reset the link from the parent of M to H

4. Set the right link of H to point to M.

P

44

Final Configuation Tree rebalanced

F

H

P

M

47

LL1 1

MM00

PP

OOSS00FF

Rotate LeftRotate Left

49

Building an AVL tree

P 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

50

Building an AVL tree

P 1

J 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

51

Building an AVL tree

P 2

J 1

B 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

52

Building an AVL tree

P 2

J 1

B 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

The new item (B) is in the left subtree

of a left child(J) of a node that hasbalance factor 2 (P)

We need to apply a single-right rotation

53

Building an AVL tree

J 0

P 0

B 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

54

Building an AVL tree

J 1

P 0

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

55

Building an AVL tree

J 0

P -1

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

56

Building an AVL tree

J 0

P 0

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

M 0

57

Building an AVL tree

J 1

P 1

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

M -1

O 0

58

Building an AVL tree

J -2

P 2

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

M -2

O 1

N 0

59

Building an AVL tree J -2

P 2

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

M -2

O 1

N 0

The new item (N) is in the left subtree

of a right child(O) of a node that has

balance factor -2 (M)We need to apply a rightleft

rotation

60

Building an AVL tree J -2

P 2

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

M -2

O 0

N-1

First we need a right rotation around the O and then a left

rotation around the M

61

Building an AVL tree

J -1

P 1

B -1

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

Then a left rotation around the M

62

Building an AVL tree

J -1

P 1

B 0

D 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

A 0

63

Building an AVL tree

J 0

P -1

B -1

D -1

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

A 0

G 0

64

Building an AVL tree

J 1

P 1

B -2

D -2

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

A 0

G 1

E 0

What rotation is necessary?

65

Building an AVL tree

J 1

P 1

B -2

D -2

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

A 0

G 1

E 0

The new item (E) is in the left subtreeof a right child(G) of a node that has

balance factor -2 (D)We need to apply a rightleft rotation

First a right rotation around the G

66

Building an AVL tree

J 1

P 1

B -2

D -2

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

A 0

G 1

E 0

We execute a right rotation around the G and then a left around the D

67

Building an AVL tree

J 0

P 1

B -1

E 0

Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

Z 0

N 0

O 0

M 0

A 0

G 0

D 0

We execute a right rotation around the G and then a left around the D

68

Rotations

These were simple examples and were done when building a tree.

In the homework, we will look at another way to rotate in the manner of the next slide when the tree already exists and needs to be fixed.

69

A leftright rotation - Easy way

Rotate left around the 5 Rotate right around the 7

Recommended