Computer notes - AVL Tree
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Computer notes - AVL Tree

on

  • 1,497 views

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).

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).

Statistics

Views

Total Views
1,497
Views on SlideShare
1,495
Embed Views
2

Actions

Likes
0
Downloads
87
Comments
0

1 Embed 2

http://staging.pragyasystems.com 2

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Start of 20 (Nov 29)
  • End of 19
  • End of lecture 20

Computer notes - AVL Tree Presentation Transcript

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