Avl trees

8,156 views

Published on

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,156
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
229
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Avl trees

  1. 1. Chapter 10Search Structures AVL Trees
  2. 2. Introduction• Searching on dynamic tables – Insert/delete symbols from the set – Using a binary search to maintain • Eg. JAN, Feb, … # comp. to find “NOV” = 6 Avg. # comp. = 42/12 = 3.5 Entering in a different order…
  3. 3. Enter months to Generate Balanced tree• A balanced tree: JUL, FEB, MAY, …• Any path from root-to-leaf is of the same length (balanced) // vs. the skewed tree – Max. # of id. Comparisons = 4, avg.=37/12
  4. 4. Form a Tree (degenerate to a chain)• Lexicographic order Max. # = 12 Avg. = 6.5
  5. 5. Objective• Minimize (Max. & avg. search) time if the binary tree is complete at all time• To maintain that by re-structuring will cause some insertions time-consuming• It is possible to maintain a balanced tree!• Avg. / worst case retrieval time O(log n)• Adelson-Velskii-Landis binary – Balanced with respect to heights of sub-trees • O(log n): insert/delete/retrieve
  6. 6. Definition• Height-balanced tree – (1) Empty tree is height-balanced – (2) non-empty binary tree T, left-subtree TL, right-subtree TR: T is height-balanced if (i) TR and TL are height-balanced (ii) |hL – hR| ≤ 1 where hL and hR are the heights of TR and TL• Balance factor – BF(T) = hL – hR – BF(T) = -1, 0, or 1for any node in an AVL tree
  7. 7. Construct AVL Tree• Insert order: MAR, MAY, NOV, …• Monitor BF(*), Rotate if necessary – (1) RR – (2) LL – (3) LR – (4) RL
  8. 8. Example
  9. 9. Example (cont.)
  10. 10. Example (cont.)
  11. 11. Example (cont.)
  12. 12. Example (cont.)
  13. 13. Memo.• Nearest ancestor A, inserted node Y – LL • Y insert A’s L-subtree’s L-subtree – LR • Y insert A’s L-subtree’s L-subtree – RR • Y insert A’s R-subtree’s R-subtree – RL • Y insert A’s R-subtree ‘s L-subtree

×