Upcoming SlideShare
×

# Computer notes - AVL Tree

1,803 views

Published on

AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a BST except height of the left and right subtrees can differ by at most 1.height of an empty tree is defined to be (–1).

1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• nice

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total views
1,803
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
103
1
Likes
0
Embeds 0
No embeds

No notes for slide
• Start of 20 (Nov 29)
• End of 19
• End of lecture 20
• ### Computer notes - AVL Tree

1. 1. Class No.20 Data Structures http://ecomputernotes.com
2. 2. AVL Tree <ul><li>AVL (Adelson-Velskii and Landis) tree. </li></ul><ul><li>An AVL tree is identical to a BST except </li></ul><ul><ul><li>height of the left and right subtrees can differ by at most 1. </li></ul></ul><ul><ul><li>height of an empty tree is defined to be (–1). </li></ul></ul>http://ecomputernotes.com
3. 3. AVL Tree <ul><li>An AVL Tree </li></ul>height 0 1 2 3 http://ecomputernotes.com 5 8 2 4 3 1 7
4. 4. AVL Tree <ul><li>Not an AVL tree </li></ul>height 0 1 2 3 http://ecomputernotes.com 6 8 1 4 3 1 5
5. 5. Balanced Binary Tree <ul><li>The height of a binary tree is the maximum level of its leaves (also called the depth). </li></ul><ul><li>The balance of a node in a binary tree is defined as the height of its left subtree minus height of its right subtree. </li></ul><ul><li>Here, for example, is a balanced tree. Each node has an indicated balance of 1, 0, or –1. </li></ul>http://ecomputernotes.com
6. 6. Balanced Binary Tree http://ecomputernotes.com -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
7. 7. Balanced Binary Tree <ul><li>Insertions and effect on balance </li></ul>U 1 U 2 U 3 U 4 U 5 U 6 U 7 U 8 U 9 U 10 U 11 U 12 B B B B B B http://ecomputernotes.com -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
8. 8. Balanced Binary Tree <ul><li>Tree becomes unbalanced only if the newly inserted node </li></ul><ul><ul><li>is a left descendant of a node that previously had a balance of 1 (U 1 to U 8 ), </li></ul></ul><ul><ul><li>or is a descendant of a node that previously had a balance of –1 (U 9 to U 12 ) </li></ul></ul>http://ecomputernotes.com
9. 9. Balanced Binary Tree <ul><li>Insertions and effect on balance </li></ul>U 1 U 2 U 3 U 4 U 5 U 6 U 7 U 8 U 9 U 10 U 11 U 12 B B B B B B http://ecomputernotes.com -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
10. 10. Balanced Binary Tree <ul><li>Consider the case of node that was previously 1 </li></ul>U 1 U 2 U 3 U 4 U 5 U 6 U 7 U 8 U 9 U 10 U 11 U 12 B B B B B B http://ecomputernotes.com -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
11. 11. Inserting New Node in AVL Tree A B T 3 1 http://ecomputernotes.com 1 0 T 1 T 2
12. 12. Inserting New Node in AVL Tree A B T 3 new 1 2 http://ecomputernotes.com 2 1 T 1 T 2
13. 13. Inserting New Node in AVL Tree A B T 3 new 1 2 A B T 3 new Inorder: T 1 B T 2 A T 3 Inorder: T 1 B T 2 A T 3 http://ecomputernotes.com 2 1 T 1 T 2 0 0 T 1 T 2
14. 14. AVL Tree Building Example <ul><li>Let us work through an example that inserts numbers in a balanced search tree. </li></ul><ul><li>We will check the balance after each insert and rebalance if necessary using rotations. </li></ul>http://ecomputernotes.com
15. 15. AVL Tree Building Example <ul><li>Insert(1) </li></ul>http://ecomputernotes.com 1
16. 16. AVL Tree Building Example <ul><li>Insert(2) </li></ul>http://ecomputernotes.com 1 2
17. 17. AVL Tree Building Example <ul><li>Insert(3) single left rotation </li></ul>-2 http://ecomputernotes.com 1 2 3
18. 18. AVL Tree Building Example <ul><li>Insert(3) single left rotation </li></ul>-2 http://ecomputernotes.com 1 2 3
19. 19. AVL Tree Building Example <ul><li>Insert(3) </li></ul>http://ecomputernotes.com 1 2 3