AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search...

29
AVL Trees See Section 19.4of the text, p. 706-714.

Transcript of AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search...

Page 1: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

AVL Trees

See Section 19.4of the text, p. 706-714.

Page 2: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that the height remains a logarithm of the number of nodes. No matter what order we insert the nodes, we can search an AVL in O( log(n) ) time.

AVL trees were the first such self-balancing trees. They were invented by Georgy Adelson-Velski and Evgenii Landis 1962.

Page 3: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Definition: An AVL tree is a Binary Search Tree with the additional property that for every node in the tree, the left and right subtrees have height that differ by at most 1. We say that the height of a null tree is -1 and the height of a single node is 0; the height of any other node is 1 more than the max of the heights of its children. Here are some AVL trees

height 0 height 1

height 0

height 2

height 1

height 0 height 0

height 0

Page 4: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Here is a tree that is not an AVL tree; the children of the root have heights that differ by 2:

height 3

height 2 height 0

height 1 height 1

height 0height 0

Page 5: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

One issue with implementing AVL trees is having access to the height of each node. We change the Node class so it has fields

E data;int height;Node left, right;

Page 6: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

We also change the insert() and remove() methods to adjust the height at each node on the path between the root and the modified node. The recursive version of the insert method is easy to modify. After we come back from the recursive call we reevaluate the height:

Page 7: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

private Node insert(E x, Node t) {if (t == null) {

Node s = new Node(x);s.height = 0;return s;

}else {

int comparison = x.compareTo(t.data);if (comparison == 0)

t.data = x;else if (comparison < 0)

t.left = insert(x, t.left);else

t.right = insert(x, t.right);t.height = 1+ max( height(t.left), height(t.right));return t;

}}

Page 8: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

This much is easy; the interesting part of AVL trees comes lies in the adjustments we need to do when a tree becomes imbalanced. Consider inserts. We have a balanced tree and insert a node and the tree is then unbalanced. Since inserting can add at most one to the height of a subtree this must mean that we have a node Z on the path between the root and the inserted node where the height of one child is 2 more than the height of the other. There are 4 possible cases:

Page 9: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

a) The insertion was in the left subtree of the left child of Z

b) The insertion was in the right subtree of the left child of Z

c) The insertion was in the left subtree of the right child of Z

d) The insertion was in the right subtree of the right child of Z

Page 10: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Consider case (a):

Z

Y

X

heighth+1

heighth

heighth

Suppose that after the insert the left child of Node Z has height h+2, while the right child has height h.

Page 11: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

We modify this as follows:

Z

Y

X

heighth+1

heighth

heighth

Y

X

heighth+1

heighth

heighth

Z

a

b

a b

Page 12: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Note that we produce this new tree by just reassigning a few pointers, so it is quick to produce. It is easy to check that this is a BST tree, and it now satisfies the AVL property: the two children of the top node now have height h+1. Note that the top node has height h+2, which is what it had before the insertion, so no further changes are needed in the tree.

Page 13: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Here is code for this:

Node rotateWithLeftChild( Node Z) {Node Y = Z.left;Z.left = Y.right;Y.right = Z;return Y;

}

Page 14: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Example:

23

17 45

9 21 32 61

4 13 22

2

23

17

45

9

21

32 61

4 13

222

Z

Page 15: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

There is a symmetric case for inserting into the right subtree of the right child of Z:

Z

Y

B X

heighth+1

heighth

A

Y

X

Aheight

h+1

heighth

heighth

Z

B

heighth

Page 16: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

The code for this is:

Node rotateWithRightChild( Node Z) {Node Y = Z.right;Z.right = Y.left;Y.left = Z;return Y;

}

Page 17: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

We started out with 4 cases:a) Insertion in the left subtree of the left child

of Zb) Insertion in the right subtree of the left child

of Zc) Insertion in the left subtree of the right child

of Zd) Insertion in the right subtree of the right

child of Z

Our simple rotations have fixed cases (a) and (d). The other cases are a bit more complex and require a double rotation.

Page 18: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

23

17 45

9 21 32 61

4 13 2219

20

Consider this example, where we have just inserted 20. The left child of node 23 has height 3, the right child has height 1.

23

17

45

9

21

32 61

4 13

22

19

20

We first do a rotation from node 21 to node 17, but that doesn't fix the problem; the left child of 23 has height 3, the right child height 1.

Page 19: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

23

17

45

9

21

32 61

4 13

22

19

20

2317

459

21

32 61

4 13

2219

20

We then rotate node 21 to node 23 and this does fix the problem:

Page 20: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

In general, if we have a situation like this, where we have inserted into the right child X of the left child Y of Z, we first rotate node X to Y:

Z

Y

XA

B C

D

Z

Y

X

A B

C

D

Page 21: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Z

Y

X

A B

C

D ZY

X

A B C D

We then rotate node X to Z:

Now both children of the root have height h+1.

Page 22: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

The code for this is simple:Node doubleRotationWithLeftChild( node Z) {

Node Y = Z.left;Node X = Y.right;Z.left = rotateWithRightChild(Y);return rotateWithLeftChild(Z);

}

Of course there is a symmetric method:Node doubleRotationWithRightChild( node Z) {

Node Y = Z.right;Node X = Y.left;Z.right = rotateWithLeftChild(Y);return rotateWithRightChild(Z);

}

Page 23: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

In Lab 6 we have slightly different notation for this. Let Z be the unbalanced node, let Y be Z's tallest child, and let X be Y's tallest child. Now introduce three new variables a, b, c where a is the one of X, Y, Z with the smallest value, b the one with the middle value, and c the one with the largest value.

Page 24: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

For example, in this tree

Z

Y

X

heighth+1

heighth

heighth

We have a is X, b is Y, and c is Z.

Page 25: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Z

Y

X

In this tree

a is Y, b is X and c is Z.

Page 26: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

We then let t1, t2, t3 and t4 be the left-to-right children of nodes a, b and c. It turns out that all four cases of our rotation can be reduced to building the tree

ca

b

t1 t2 t3 t4

Page 27: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

For example,

Z

Y

X

t1 t2

t3

t4 ZY

X

t1 t2 t3 t4

a

b

c b

ac

Page 28: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

Z

Y

Xt1

t2 t3

t4ZY

X

t1 t2 t3 t4

a

b

cb

ac

Here is another example; the remaining two cases are symmetrical to these

Page 29: AVL Trees - cs.oberlin.edubob/cs151.spring17... · AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that

In Lab 6, once you have identified nodes a, b, and c, and their children t1 through t4, code that is given to you completes the rotation by building the resulting tree in terms of these 7 variables.