Transcript
Page 1: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

Page 2: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

BST PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.

9

Page 3: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

BST PropertyAt every node X, values inleft subtree are smaller thanthe value in X, and values in right subtree are largerthan the value in X.

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

Page 4: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

Page 5: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

Is this is anAVL tree?

Page 6: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

00

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

Page 7: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

00 1

1

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

Page 8: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

00 1

12

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

Page 9: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

00 1

12

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

3

Yes,this is anAVL tree.

Page 10: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

00 1

12

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

3

Suppose we deletethe 9 node.

Page 11: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

0 1

02

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

3

Page 12: AVL Tree: Balanced Binary Search Tree

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

AVL Balance PropertyAt every node X, the heightof the left subtree differs from the height of the right subtree by at most 1.

0

0 1

02

height(X) = max(height(left(X)), height(right(X))) + 1height() = 1

3

AVL balancepropertyfails at6 node.

Page 13: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X Y

Z

Suppose we have balance . . .

Page 14: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Suppose we have balance . . .

. . . but we insert here,destroying the balance.

Page 15: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Do a “single rotation”

Page 16: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Do a “single rotation”

Page 17: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Do a “single rotation”

Page 18: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY Z

Do a “single rotation”

Page 19: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

XY Z

Do a “single rotation”

Page 20: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

X Y Z

Do a “single rotation”

Page 21: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

X Y Z

Do a “single rotation”

Done!

Page 22: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

In one step:

Page 23: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

X Y Z

Page 24: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X Y

Z

Again, suppose we have balance . . .

Page 25: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Again, suppose we have balance . . .

. . . but this time weinsert here.

Page 26: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Again, we do a single rotation . . .

Page 27: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

X

YZ

Again, we do a single rotation . . .

. . . but it fails torestore balance

Page 28: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Go back to where westarted . . .

Page 29: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

XY

Z

Go back to where westarted . . .

. . . and consider structure of Y

Page 30: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Page 31: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Page 32: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Page 33: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Page 34: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Page 35: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Page 36: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

X V ZW

i Now do a “double rotation”. . .

Page 37: AVL Tree: Balanced Binary Search Tree

Tree Rotations to Restore Balance

jk

X V ZW

i Now do a “double rotation”. . .

Done!


Top Related