avl tree: balanced binary search tree

Post on 02-Jan-2016

46 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

AVL Tree: Balanced Binary Search Tree. 6. 2. 8. 4. 9. 1. 3. AVL Tree: Balanced Binary Search Tree. BST Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. 6. 2. 8. 4. 9. 1. 3. - PowerPoint PPT Presentation

TRANSCRIPT

6

2

4

3

1

8

AVL Tree: Balanced Binary Search Tree

9

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

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.

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

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?

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

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

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

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.

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.

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

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.

Tree Rotations to Restore Balance

j

k

X Y

Z

Suppose we have balance . . .

Tree Rotations to Restore Balance

j

k

XY

Z

Suppose we have balance . . .

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

Tree Rotations to Restore Balance

j

k

XY

Z

Do a “single rotation”

Tree Rotations to Restore Balance

j

k

XY

Z

Do a “single rotation”

Tree Rotations to Restore Balance

j

k

XY

Z

Do a “single rotation”

Tree Rotations to Restore Balance

j

k

XY Z

Do a “single rotation”

Tree Rotations to Restore Balance

jk

XY Z

Do a “single rotation”

Tree Rotations to Restore Balance

jk

X Y Z

Do a “single rotation”

Tree Rotations to Restore Balance

jk

X Y Z

Do a “single rotation”

Done!

Tree Rotations to Restore Balance

j

k

XY

Z

In one step:

Tree Rotations to Restore Balance

jk

X Y Z

Tree Rotations to Restore Balance

j

k

X Y

Z

Again, suppose we have balance . . .

Tree Rotations to Restore Balance

j

k

XY

Z

Again, suppose we have balance . . .

. . . but this time weinsert here.

Tree Rotations to Restore Balance

j

k

XY

Z

Again, we do a single rotation . . .

Tree Rotations to Restore Balance

jk

X

YZ

Again, we do a single rotation . . .

. . . but it fails torestore balance

Tree Rotations to Restore Balance

j

k

XY

Z

Go back to where westarted . . .

Tree Rotations to Restore Balance

j

k

XY

Z

Go back to where westarted . . .

. . . and consider structure of Y

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Tree Rotations to Restore Balance

j

k

X

V

Z

W

i

Now do a “double rotation”. . .

Tree Rotations to Restore Balance

jk

X V ZW

i Now do a “double rotation”. . .

Tree Rotations to Restore Balance

jk

X V ZW

i Now do a “double rotation”. . .

Done!

top related