66
1 CSC 2053 -TREES AVL & BALANCED TREES

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

Embed Size (px)

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

Page 1: 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

1

CSC 2053 -TREES

AVL & BALANCED TREES

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

4

Rebalancing a Binary Tree(example from Standish book)

5

73

2 4 6

Page 5: 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

5

Rebalancing a Binary Tree(example from Standish book)

5

73

2 4 6

1

Tree is now unbalanced

Page 6: 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

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)

Page 7: 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

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?

Page 8: 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

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.

Page 9: 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

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

Page 10: 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

10

Examples of AVL Trees

Page 11: 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

11

Examples of AVL Trees – Unbalanced

Page 11

Page 12: 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

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

Page 13: 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

13

Example of Trees and their balance factors

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

Page 14: 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

14

Example of Trees and their balance factors

1

11

-1 1 -1

0 -1

0

0 0

0

Page 15: 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

15

Example of Trees and their balance factors

Page 16: 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

16

Example of Trees and their balance factors

0

-12

-1 0 -1

0 -1

0

0

-2

1

0

Page 17: 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

17

Example of Trees and their balance factors

Page 18: 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

18

Example of Trees and their balance factors

1

-23

-2

-1

0

-1

0

Page 19: 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

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.

Page 20: 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

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

Page 21: 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

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.

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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

Page 28: 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

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

Page 29: 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

29

Single-Right Rotation - Result

C2

B

A

C

B

A

InitialConfiguration

FinalConfiguration

parent

parent

Page 30: 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

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

Page 31: 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

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

Page 32: 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

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

Page 33: 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

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

Page 34: 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

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

Page 35: 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

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

Page 36: 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

36

Single-Left Rotation

C

F

N

InitialConfiguration

FinalConfiguration

C (-2)

F-1

N

parent

parent

Page 37: 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

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

Page 38: 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

38

Double-Right Rotation(left-right rotation)

M(2)

F-1

H

MF

H

InitialConfiguration

FinalConfiguration

P

Parent

Page 39: 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

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

Page 40: 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

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

Page 41: 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

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

Page 42: 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

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

Page 43: 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

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

Page 44: 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

44

Final Configuation Tree rebalanced

F

H

P

M

Page 45: 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

47

LL1 1

MM00

PP

OOSS00FF

Rotate LeftRotate Left

Page 46: 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

49

Building an AVL tree

P 0

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

Page 47: 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

50

Building an AVL tree

P 1

J 0

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

Page 48: 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

51

Building an AVL tree

P 2

J 1

B 0

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

Page 49: 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

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

Page 50: 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

53

Building an AVL tree

J 0

P 0

B 0

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

Page 51: 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

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

Page 52: 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

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

Page 53: 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

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

Page 54: 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

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

Page 55: 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

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

Page 56: 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

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

Page 57: 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

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

Page 58: 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

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

Page 59: 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

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

Page 60: 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

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

Page 61: 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

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?

Page 62: 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

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

Page 63: 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

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

Page 64: 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

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

Page 65: 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

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.

Page 66: 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

69

A leftright rotation - Easy way

Rotate left around the 5 Rotate right around the 7