Avl trees
Download
1 / 16

AVL trees - PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on

AVL trees. AVL Trees. We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can be attained if both subtrees of each node have roughly the same height.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'AVL trees' - xenon


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Avl trees1
AVL Trees

We have seen that all operations depend on the depth of the tree.

We don’t want trees with nodes which have large height

This can be attained if both subtrees of each node have roughly the same height.

  • AVL tree is a binary search tree where the height of the two subtrees of a node differs by at most one

    • Height of a null tree is -1


Avl trees

5

3

8

4

10

1

5

Not AVL Tree

3

4

1

AVL Tree


Avl trees

Section 10.4 KR

  • Suppose an AVL tree of height h contains contains at most S(h) nodes:

  • S(h) = L(h) + R(h) + 1

    • L(h) is the number of nodes in left subtree

    • R(h) is the number of nodes in right subtree

You have larger number of nodes if there is larger imbalance between the subtrees

This happens if one subtree has height h, another h-2

Thus, S(h) = S(h) + S(h-2) + 1


Operations in avl tree
Operations in AVL Tree

O(log N)

Searching, Complexity?

O(log N)

FindMin, Complexity?

Deletion? Insertion?


Insertion
Insertion

Search for the element

If it is not there, insert it in its place.

Any problem?

Insertion may imbalance the tree. Heights of two children of a node may differ by 2 after an insertion.

Tree Rotations used to restore the balance.


Avl trees

If an insertion cause an imbalance, which nodes can be affected?

Nodes on the path of the inserted node.

  • Let U be the node nearest to the inserted one which has an imbalance.

    • insertion in the left subtree of the left child of U

    • insertion in the right subtree of the left child of U

    • insertion in the left subtree of the right child of U

    • insertion in the right subtree of the right child of U


Insertion in left child of left subtree

V affected?

U

X

Z

Y

Insertion in left child of left subtree

Single Rotation

U

V

Z

Y

X

After Rotation

Before Rotation


Avl trees

U affected?

Z

V

3

1

Y

4

4

8

1

0.8

X

3

5

5

8

3

4

1

0.8

5

8

AVL Tree

Insert 0.8

After Rotation


Double rotation

W affected?

V

V

D

D

W

U

A

B

C

C

B

Double Rotation

  • Suppose, imbalance is due to an insertion in the left subtree of right child

    • Single Rotation does not work!

U

Before Rotation

After Rotation

A


Avl trees

U affected?

D

V

3

A

W

8

4

3.5

1

4

B

3.5

5

5

8

3

3

4

1

0.8

5

8

AVL Tree

Insert 3.5

After Rotation


Extended example

2 affected?

2

3

2

3

4

4

1

1

1

2

2

3

3

Fig 1

5

3

3

Fig 4

Fig 2

Fig 3

1

Fig 5

Fig 6

Extended Example

Insert 3,2,1,4,5,6,7, 16,15,14


Avl trees

2 affected?

4

4

2

4

Fig 8

Fig 7

5

6

5

7

6

3

3

7

3

2

1

2

2

1

5

4

5

6

4

6

Fig 10

Fig 9

5

Fig 11

3

1

3

1

1


Avl trees

4 affected?

4

4

7

15

7

16

3

16

3

3

16

2

2

2

Fig 12

Fig 13

15

6

6

6

5

7

Fig 14

5

5

1

1

1


Avl trees

4 affected?

4

6

14

15

16

15

14

3

3

16

2

2

Fig 15

Fig 16

7

6

5

7

5

1

1

Deletions can be done with similar rotations


Avl trees

Rings a bell! Fibonacci numbers affected?

FN N

S(h) h

h is O(log N)

Using this you can show that h = O(log N)