height-balanced binary search trees

38
1 Height-Balanced Binary Search Trees AVL Trees

Upload: gwen

Post on 23-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Height-Balanced Binary Search Trees. AVL Trees. Background. Binary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average case search time (like arrays). Problem: they still have O(n) worst case search time. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Height-Balanced Binary Search Trees

1

Height-Balanced Binary Search Trees

AVL Trees

Page 2: Height-Balanced Binary Search Trees

2

Background

Binary Search Trees allow dynamic allocation (like linked lists), but O(log2(n)) average case search time (like arrays).

Problem: they still have O(n) worst case search time.

Solution: if we can balance the trees on insert, we can have worst case O(log2(n)) search time.

Page 3: Height-Balanced Binary Search Trees

3

AVL Trees

Named for inventors Adelsson, Velski, and Landis.

One way to maintain a balanced tree.Idea: keep the height of every node in the

tree.When the difference between the height

of a node’s left and right subtrees exceeds 1, rotate the nodes to eliminate the imbalance.

Page 4: Height-Balanced Binary Search Trees

4

AVL Details

Each node will have an additional piece of information – its height. It is calculated as: H(node) = MAX(H(left), H(right)) + 1

Each node’s balance factor will be calculated when needed; the formula is BF(node) = H(left) – H(right)

When BF(node) = +2 or –2, a fix is needed.

Page 5: Height-Balanced Binary Search Trees

5

AVL Algorithm

Insert a new key value in the normal way Call the newly inserted node “newnode”

Set H(newnode) = 1;For each node from newnode back to root

Recalculate H(node) and BF(node) If BF(node) = +2 or BF(node) = –2

Perform appropriate rotationOnce a rotation is performed, stop.

Page 6: Height-Balanced Binary Search Trees

6

AVL Rotations

The appropriate rotation depends on where “newnode” is with respect to the node that is out of balance.

The four types are Right-Right (newnode was inserted into the

right subtree of the right subtree of the node) Left-Left Left-Right Right-Left

Page 7: Height-Balanced Binary Search Trees

7

AVL Rotations II

The rotations are usually specified in general, since a rotation can occur anywhere in the tree.

Subtrees move as whole unitsNote in the following examples that the

height of the tree after rotation is the same as before the insertion.

This means that no heights or balance factors above this node will change.

Page 8: Height-Balanced Binary Search Trees

8

AVL Tree: LL Rotation

This is the general tree set up.Now suppose a new node is inserted

into BL that increases its height:

A

B

BL BR

AR

h

h

H : h+1BF : 0

H : h+2BF : +1

Page 9: Height-Balanced Binary Search Trees

9

AVL Tree: LL Rotation

BF of node A is now +2, so a rotation is necessary:

A

B

BL BR

AR

h

h

H : h+2BF : +1

H : h+2BF : +2

New

Page 10: Height-Balanced Binary Search Trees

10

AVL Tree: LL Rotation

This is the result of the rotation.Note the height of the root is the

same as before the insertion.

A

B

BL

BR AR

h

h

H : h+1BF : 0

H : h+2BF : +0

New

Page 11: Height-Balanced Binary Search Trees

11

LL Rotation Algorithm

Set up pointers for A, B, BL, BR, ARA->left = BRB->right = ARoot = BSet H and BF for nodes A and B.

NOTE: the book does this more “cleverly”, with fewer pointers. I think this is more clear.

Page 12: Height-Balanced Binary Search Trees

12

RR Rotation

This is the general tree set up.Now suppose a new node is inserted

into BR that increases its height:

A

B

BL BR

AL

h

h

H : h+1BF : 0

H : h+2BF : -1

Page 13: Height-Balanced Binary Search Trees

13

RR Rotation

BF of node A is –2, so a rotation is needed

A

B

BL BR

AL

h

h

H : h+2BF : -1

H : h+3BF : -2

New

Page 14: Height-Balanced Binary Search Trees

14

RR Rotation

This is the result of the rotation.Note the height of the root returns to h+2

B

A

BL

BR

AL

h

h

H : h+2BF : 0

H : h+1BF : 0

New

Page 15: Height-Balanced Binary Search Trees

15

RR Rotation Algorithm

Set up pointers for A, B, BL, BR, ALA->right = BLB->left = ARoot = BSet H and BF for nodes A and B.

Page 16: Height-Balanced Binary Search Trees

16

LR Rotation

This is the general tree set up.Now suppose a new node is inserted

into CL that increases its height:

A

B

BL CL

hAR

h

H : h+1BF : 0

H : h+2BF : +1

C

CRh–1

H : hBF : 0

Page 17: Height-Balanced Binary Search Trees

17

LR Rotation

The balance factor of A goes to +2, so a left-right rotation is needed.

A

B

BL CL

hAR

h

H : h+2BF : -1

H : h+3BF : +2

C

CRh–1

H : h+1BF : +1

New

Page 18: Height-Balanced Binary Search Trees

18

LR Rotation

Again, note the height of the root returns to h+2.

C

B

BL

CLhAR

h

H : h+1BF : 0

H : h+2BF : 0

A

CRh–1

H : h+1BF : -1

New

Page 19: Height-Balanced Binary Search Trees

19

LR Rotation Algorithm

Set up pointers for A, B, C, BL, AR, CL, CR

A->left = CRB->right = CLC->left = BC->right = ARoot = CSet H and BF for nodes A, B and C.

Page 20: Height-Balanced Binary Search Trees

20

RL Rotation

This is the general tree set up.Now suppose a new node is inserted

into CL that increases its height:

A

B

CLBR

h

H : h+1BF : 0

H : h+2BF : -1

C

CR

h–1

H : hBF : 0

AL

h

Page 21: Height-Balanced Binary Search Trees

21

RL Rotation

BF of Node A is now –2, so a rotation is necessary.

A

B

CLBR

h

H : h+2BF : +1

H : h+3BF : -2

C

CR

h–1

H : h+1BF : +1

AL

h

New

Page 22: Height-Balanced Binary Search Trees

22

RL Rotation

This shows the result of the rotation.

A B

CL

BR

h

H : h+1BF : -1

H : h+2BF : 0 C

CR

h–1

H : h+1BF : 0

AL

h

New

Page 23: Height-Balanced Binary Search Trees

23

RL Rotation Algorithm

Set up pointers for A, B, C, BR, AL, CL, CR

A->right = CLB->left = CRC->left = AC->right = BRoot = CSet H and BF for nodes A, B and C.

Page 24: Height-Balanced Binary Search Trees

24

RL Rotation Method 2

First, Perform a LL rotation around B:

A

B

CLBR

h

H : h+2BF : +1

H : h+3BF : -2

C

CR

h–1

H : h+1BF : +1

AL

h

New

Page 25: Height-Balanced Binary Search Trees

25

RL Rotation Method 2

Next, perform a RR Rotation around A:

A

BCL

BR

h

H : h+1BF : -1

H : h+3BF : -2

C

CR

h–1

H : h+2BF : -1

AL

h

New

Page 26: Height-Balanced Binary Search Trees

26

RL Rotation Method 2

This result is the same as the first method.

A B

CL

BR

h

H : h+1BF : -1

H : h+2BF : 0 C

CR

h–1

H : h+1BF : 0

AL

h

New

Page 27: Height-Balanced Binary Search Trees

27

AVL Example

Now let’s do an extended example, inserting a series of key values into an AVL tree and rotating as necessary.

We will be inserting 25, 50, 90, 10, 15, 20, 75.

Let’s start with 25:

Page 28: Height-Balanced Binary Search Trees

28

AVL Example

25 is a new root; no problem.Now, insert 50:

25

Left to insert: 50, 90, 10, 15, 20, 75

H : 1BF : 0

Page 29: Height-Balanced Binary Search Trees

29

AVL Example

No problem here.Now, insert 90:

25

50

Left to insert: 90, 10, 15, 20, 75

H : 2BF : -1

H : 1BF : 0

Page 30: Height-Balanced Binary Search Trees

30

AVL Example

BF(25) = –2; what do we do?This is a RR rotation:

25

50

90

Left to insert: 10, 15, 20, 75

H : 3BF : -2

H : 2BF : -1

H : 1BF : 0

Page 31: Height-Balanced Binary Search Trees

31

AVL Example

This is the result; note height of the root.Next, insert 10:

50

25 90

Left to insert: 10, 15, 20, 75

H : 2BF : 0

H : 1BF : 0

H : 1BF : 0

Page 32: Height-Balanced Binary Search Trees

32

AVL Example

No problems here.Next, insert 15:

50

25

10

90

Left to insert: 15, 20, 75

H : 3BF : +1

H : 2BF : +1

H : 1BF : 0

H : 1BF : 0

Page 33: Height-Balanced Binary Search Trees

33

AVL Example

BF(25)=+2, so time to rotateThis is a Left-Right (LR) rotation:(Note that I didn’t update 50)

50

25

10

15

90

Left to insert: 20, 75

H : 3BF : +1

H : 3BF : +2

H : 1BF : 0

H : 2BF : -1

H : 1BF : 0

Page 34: Height-Balanced Binary Search Trees

34

AVL Example

This shows the result.Note that I didn’t need to update 50…Now, insert 20:

50

15

10

90

25

Left to insert: 20, 75

H : 3BF : +1

H : 2BF : 0

H : 1BF : 0

H : 1BF : 0

H : 1BF : 0

Page 35: Height-Balanced Binary Search Trees

35

AVL Example

BF(50) = +2, so time to rotate…This is a left right (LR) rotation again:

50

15

10

20

90

25

Left to insert: 75

H : 4BF : +2

H : 3BF : -1

H : 1BF : 0

H : 2BF : +1

H : 1BF : 0

H : 1BF : 0

Page 36: Height-Balanced Binary Search Trees

36

AVL Example

This is the result. Note the movements of the subtrees.

Finally, insert 75:

25

15

10

50

20 90

Left to insert: 75

H : 3BF : 0

H : 2BF : 0

H : 2BF : -1

H : 1BF : 0

H : 1BF : 0

H : 1BF : 0

Page 37: Height-Balanced Binary Search Trees

37

AVL Example

H(50) = –2, so time to rotate again. This is a right-left (RL) rotation:

25

15

10

75

50

20 90

Left to insert: done

H : 3BF : 0

H : 2BF : 0

H : 3BF : -2

H : 1BF : 0

H : 1BF : 0

H : 2BF : +1

H : 1BF : 0

Page 38: Height-Balanced Binary Search Trees

38

AVL Example

This is the result. Done.

25

15

10

75

20 50 90

Left to insert: done

H : 3BF : 0

H : 2BF : 0

H : 2BF : 0

H : 1BF : 0

H : 1BF : 0

H : 1BF : 0

H : 1BF : 0