Upcoming SlideShare
×

# Avl trees

5,034
-1

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
5,034
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
192
0
Likes
0
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
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.