28
1 Binary SearchTrees continued - AVL Trees •Definition and construction via running example Lecture 16 ADS2 Lecture 16

1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Embed Size (px)

Citation preview

Page 1: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

1

Binary SearchTrees continued

- AVL Trees•Definition and construction via running example

Lecture 16

ADS2 Lecture 16

Page 2: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

See:

• G. Adelson-Velskii and E.M. Landis, "An algorithm for the organization of information." Doklady Akademii Nauk SSSR, 146:263–266, 1962 (Russian). English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962.

• Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 458–475 of section 6.2.3: Balanced Trees. Note that Knuth calls AVL trees simply "balanced trees".

AVL Trees

2ADS2 Lecture 16

Page 3: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

AVL Trees

• Discovered in 1962, Adelson-Velskii and Landis– Trying to devise a better data structure for a chess engine!

• Depth ensured to be O(log n)– But not always perfectly balanced

• Recursive definition:– The sub-trees of every node differ in height by at most one. – Every sub-tree is an AVL tree.

Height of an empty tree is -1

Height of a single node is 0

3ADS2 Lecture 16

Page 4: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

84

6

111

84

6

AVL tree

not an AVL tree

3

1171

84

6

3

1171

84

6

2

AVL tree

not an AVL tree

5

5

AVL tree

111

84

6

3

13

For more examples, see board

15

4ADS2 Lecture 16

Page 5: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Operations on AVL trees

• Search, as for BSTs• Insert is the same as for BST but

– May have to fix the tree after insertion to restore the balance condition

– Can always do this via a modification known as a rotation• Single rotation or double rotation

• Similarly for deletion

Note that the balance condition ensures that search,insert and delete operations are all O(log n), where n is the number of nodes

balancing is O(1)

5ADS2 Lecture 16

Page 6: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

The basic idea of single rotation

k1

k2

X Y

Z

k1

k2

X

Y Z

In each case:

• k1<k2,

• all elements in subtree X are smaller than k1,

• all elements in subtree Z are larger than k2 and

• all elements in subtree Y are in between k1 and k2

So:

• both trees are BSTs for the same elements

•Conversion of one to the other is known as a rotation 6ADS2 Lecture 16

Page 7: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

To rebalance a binary search tree rooted at r

If too heavy on the left…right rotation

If too heavy on the right… left rotation

k1

r

k2

T1 T2 T3 T4

k2

r

T3

k1

T1 T2

T4

k1

r

k2

T1 T2 T3 T4

k1

rT1

T2

k2

T3 T4

7ADS2 Lecture 16

Page 8: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

If node r is too heavy on the left, rotating about node r and left child:

Rotate the whole subtree rooted at r round one place to the right

Take the right subtree of new root and make it the left subtree of r

If node r is too heavy on the right, rotating about node r and right child:

Rotate the whole subtree rooted at r round one place to left

Take the left subtree of new root and make it the right subtree of r

k1

r

k2

T1 T2 T3 T4

k1

rT1

T2k2

T3 T4

k1

r

k2

T1 T2 T3 T4

k1

r

k2

T1 T2 T3 T4

k1

rT1

T2k2

T3 T4

k1

rT1

T2k2

T3 T4

k1

r

k2

T1 T2 T3 T4

k2

r

T3

k1

T1 T2

T4k1

r

k2

T1 T2 T3 T4

k1

r

k2

T1 T2 T3 T4

k2

r

T3

k1

T1 T2

T4

k2

r

T3

k1

T1 T2

T4

Right rotation

left rotation

8ADS2 Lecture 16

Page 9: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Example: right rotation

103

5

1

2 410

3

5

1

2

4See board for explanation

• Note that the first tree is not an AVL tree, but the second one is

• Rotation involves only a few pointer changes and allows us to fix the structure of a tree whilst preserving search tree property

9ADS2 Lecture 16

Page 10: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Rotation continued

• Doesn’t have to be done at the root of a tree; can be done at any node since that node is the root of some subtree.

We are doing the balancing about this node 21

20

15

103

5

1

2 4

17

21

10

3 20

5

15

1

2

4

17

21

20

15

103

5

1

2 4

103

1

2 4

17

21

10

3 20

5

15

1

2

4

17

10ADS2 Lecture 16

Page 11: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Applying single rotation – a worked example

To maintain balance condition, every time you insert a new node:

Starting at the node just inserted, travel up tree

• If we reach root node without finding a bad node, we are done

• If we reach bad node, do a rotation and we are done

If tree is now balanced, continue.. (otherwise..what?)

Start with an empty AVL tree and insert values 1 to 7 in sequential order then values 8 to 15 in reverse order

11ADS2 Lecture 16

Page 12: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Worked example (adding nodes containing 1..7 in order)

1 1

2

1

2

3

Left rotation.

(Node 2 has no left subtree to move)

2

31

balanced? Yes

continue

1 is unbalanced now (too heavy on the right).

2

31

4

2

31

4

5

add 1

add 2 add 3

add 4 add 5 3 is unbalanced now (too heavy on the right).

12ADS2 Lecture 16

Page 13: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Worked example contd.

2

31

4

5

Left rotation.

(Node 4 has no left subtree to move)

2

41

3 5

balanced? Yes

continue

add 62

41

3 5

6

2 is unbalanced now (too heavy on the right).

13ADS2 Lecture 16

Page 14: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Worked example contd.

2

41

3 5

6

Left rotation.

(Node 4 has left subtree to move)

4

52

3 61

Example for you: add the next two nodes, i.e. node containing the value 7 and node containing the value 15

balanced? Yes

continue

add 7 and 15

4

62

5 731

15

14ADS2 Lecture 16

Page 15: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Worked example contd.

4

62

5 731

15

add 14

4

62

5 731

15

14

7 is unbalanced now (too heavy on the right).

Left rotation.

15 has a left subtree to move

4

62

5 1531

7

14

4

62

5 731

15

14

balanced? No!

What now?Answer:

double rotation

15ADS2 Lecture 16

Page 16: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

AVL tree insert

Let x be the deepest node where an imbalance occurs.Four cases to consider.

The insertion is in the• left subtree of the left child of x.• right subtree of the left child of x.• left subtree of the right child of x.• right subtree of the right child of x.

Cases 1 & 4 are solved by a single rotation.Cases 2 & 3 are solved by a double rotation.

This is the situation we have here

16ADS2 Lecture 16

Page 17: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Double rotation Let

• k1 be the deepest node at which imbalance occurs

• k2 and k3 next two nodes along the path leading to the inserted node

Then

• Rotate about k2 and k3

• Rotate about k1 and k3

k1

k2

k3

4

62

5 731

4

62

5 731

14

15

k1

k3

k2

4

62

531

4

6

5

7

14

15

4

62

5 731

15

14

4

62

5 731

15

14

k2

k3

k1

Example: a right-left double rotation

17ADS2 Lecture 16

Page 18: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Continuing the example

add 13

4

62

531

4

6

5

7

14

15

4

62

531

4

6

5

7

14

15

13

6 is unbalanced now (too heavy on the right). New node is in the left subtree of the right child so double rotation required.

4

62

531

4

6

5

7

14

15

13

k3

k2

k1

right rotate

left rotate4

62

531

4

6

5

13

7

14

15

k3

k2

k1

4

2

31

4

7

6

135

14

15

k3

k2

k1

Again, right-left rotation required

18ADS2 Lecture 16

Page 19: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Example continued

4

2

31

4

7

6

135

14

15

add 12

14

1513

3

72

1

4

6

5

12

4 is unbalanced now (too heavy on the right). New node is in the right subtree of the right child so single (left) rotation required.

14

1513

3

72

1

4

6

5

12

Left rotation.

7 has a left subtree to move

13

3

14

15

7

2

1

4

6

512

19ADS2 Lecture 16

Page 20: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Example contd.

• Inserting 11, 10, 9 single rotation needed, inserting 8 requires no rotation. The resulting tree is:

10

3

1214

7

2

1

4

6

5 9

8

11 13 15

Example for you:

Show this is correct

But we haven’t seen an example of a left-right double rotation!

20ADS2 Lecture 16

Page 21: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

A left-right rotation

10

3

1214

7

2

1

4

6

5 9

8

11 13 15

Suppose we want to add a node containing the value 8.5

add 8.5

10

3

1214

7

2

1

4

6

5 9

8

11 13 15

8.5

9 is unbalanced now (too heavy on the left). New node is in the right subtree of the left child so double rotation required.

21ADS2 Lecture 16

Page 22: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

Left-right rotation contd.

Rotate left10

3

1214

7

2

1

4

6

5 9

8

11 13 15

8.5

k1

k3

k2

10

3

1214

7

2

1

4

6

5 9

8.5

11 13 15

8k2

k3

k1

10

3

1214

7

2

1

4

6

5 8.5 11 13 15

8

Rotate right

9

See example sheet 3 for long worked examples of AVL trees

22ADS2 Lecture 16

Page 23: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

public class AVLNode<E>{//Instance variables: private E element; private AVLNode<E> left; private AVLNode<E> right; int height;

//methods}

AVL implementation in Java• See (outline of) AVLTree.java in BSTrees folder• Need to keep a record of the height of a node

– So AVLnode defined thus:

what’s this? See board

23ADS2 Lecture 16

Height of node p =-1 if p is null0 if p is leafMax(height(p.L),height(p.R)) + 1 otherwise.Height of AVLTree = height of root.

Page 24: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

AVL implementation contd.

ADS2 Lecture 16 24

/**return height of tree rooted at p*/public int height(AVLNode<E> p){ }

/**reset heights of all nodes starting at p */public void resetHeights(AVLNode<E> p){}

Also • (private static) max method to return max of two integers

AVLTree.java includes methods:

Page 25: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

AVLTree.java

• Deletion : Normal BSTree deletion does not maintain balance condition*

• Insert needs to call other methods– sRotateLeft– SRotate_right– dRotateLeftRight– dRotateRightLeft

* But a proper implementation of delete should maintain the balance condition

We don’t look at all of these here, but will look at sRotateLeft and dRotateLeftRight

25ADS2 Lecture 16

Page 26: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

k1

k2

X Y

Z

k1

k2

X

Y Z

26ADS2 Lecture 16

public AVLNode<E> sRotateLeft(AVLNode<E> k1){AVLNode<E> k2=k1.getRight();k1.setRight(k2.getLeft());k2.setLeft(k1);

k1.setHeight(max(height(k1.getLeft()), height(k1.getRight()))+1);

k2.setHeight(max(height(k2.getRight()), height(k1))+1);

return k2;

}

Page 27: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

k2

k1

AB

D

C

k3

k2

k3

k1

B C DA

k2

k1

B

C

Dk3

A

Examples, see board (if time) and Examples sheet 3

27ADS2 Lecture 16

public AVLNode<E> dRotateLeftRight(AVLNode<E> k1){AVLNode<E> k2=k1.getLeft();AVLNode<E> k3=k2.getRight();k1.setLeft(sRotateLeft(k2));sRotateRight(k1);return k3;

}

Page 28: 1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

When do I need to do a rotation?

ADS2 Lecture 16 28

During recursive implementation of insert for AVL tree (value e):

When insertion has been performed to a left subtree of node p: if height of subtrees of p differ by 2, need rotation: if the new root of left subtree < e need a single right rotation else need a double left right rotation

Similar argument for when insertion has been performed to a right subtree of node p.

Only need to reset the heights of the tree when new node added, but no rotation required.