220 likes | 231 Views
More Trees. B.Ramamurthy. binary search trees. AVL trees. 2-3 trees. tries. Types of Trees. trees. dynamic. static. game trees. search trees. priority queues and heaps. graphs. Huffman coding tree. First, We discussed trees in general.
E N D
More Trees B.Ramamurthy BR
binary search trees AVL trees 2-3 trees tries Types of Trees trees dynamic static game trees search trees priority queues and heaps graphs Huffman coding tree BR
First, We discussed trees in general. Then binary search trees and complete binary trees. We took a detour and looked at Big-O notation and growth rate functions as means of expressing algorithmic complexity. Lets look at some trees that offer efficient insert and delete algorithms. (Efficiency can be now compared using Big-O notation) Review BR
AVL Trees • Balanced binary search tree offer a O(log n) insert and delete. • But balancing itself costs O(n) in the average case. • In this case, even though delete will be O(log n), insert will be O(n). • Is there any way to have a O(log n) insert too? • Yes, by almost but not fully balancing the tree : AVL (Adelson Velskii and Landis) balancing BR
Height of a Tree • Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node. • Height of a empty tree is -1. • Height of a single node tree is 0. • Recursive definition: • height(t) = 0 if number of nodes = 1 = -1 if T is empty = 1+ max(height(LT), height(RT)) otherwise BR
AVL Property • If N is a node in a binary tree, node N has AVL property if the heights of the left sub-tree and right sub-tree are equal or if they differ by 1. • Lets look at some examples. BR
Non-AVL Tree BR
Transforming into AVL Tree • Four different transformations are available called : rotations • Rotations: single right, single left, double right, double left • There is a close relationship between rotations and associative law of algebra. BR
Transformations • Single right : ((T1 + T2) + T3) = (T1 + (T2 + T3) • Single left : (T1 + (T2 + T3)) = ((T1 + T2) + T3) • Double right : ((T1 + (T2 + T3)) + T4) = ((T1+T2) + (T3+T4)) • Double left : (T1 ((T2+T3) +T4)) = ((T1+T2) + (T3+T4)) BR
Example: AVL Tree for Airports • Consider inserting sequentially: ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCM • Build a binary-search tree • Build a AVL tree. BR
Binary Search Tree for Airport Names ORY ZRH JFK MEX BRU ORD DUS ARN NRT GLA GCM BR
X1 X2 X2 X3 X1 X3 X2 X3 AVL Balancing : Four Rotations X1 X3 Double right X2 X2 X1 Single right X3 X1 X1 X3 X2 Single left Double left X2 X1 X3 X1 X2 X3 BR
ORY ORY JFK JFK BRU BRU An AVL Tree for Airport Names After insertion of ORY, JFK and BRU : Single right Not AVL balanced AVL Balanced BR
ORY JFK JFK BRU BRU ORY DUS MEX ZRH ORD NRT An AVL Tree for Airport Names (contd.) After insertion of DUS, ZRH, MEX and ORD After insertion of NRT? DUS MEX ZRH ORD Still AVL Balanced BR
JFK JFK Double Left ORY BRU BRU ORY DUS NRT ZRH DUS MEX ZRH MEX ORD ORD NRT An AVL Tree … Not AVL Balanaced Now add ARN and GLA; no need for rotations; Then add GCM BR
JFK ORY ORY DUS ARN BRU BRU NRT NRT ZRH ZRH GLA MEX ORD MEX ORD GCM An AVL Tree… JFK GCM ARN DUS GLA Double left NOT AVL BALANCED BR
For successful search, average number of comparisons: sum of all (path length+1) / number of nodes For the binary search tree (of airports) it is: 39/11 = 3.55 For the AVL tree (of airports) it is : 33/11 = 3.0 Search Operation BR
Known Performance Results of AVL trees • AVL tree is a sub optimal solution. • How to evaluate its performance? • Bounds (upper bound and lower bound) for number of comparisons: C > log(n+1) + 1 C < 1.44 log(n+2) - 0.328 • AVL trees are no more than 44% worse than optimal trees. BR
Exam 2 Review • Topics : Chapter 6 and Chapter 8 1. Stacks ADT (Design, implementation and application) 2. Queue ADT (Design, implementation and application) 3. Binary Search Tree 4. Complete binary trees and Heaps 5. AVL Trees BR
Questions • Four questions from: 1. Simulation using queues (Hwk3) 2. Complete Binary tree contiguous representation 3. Heap building 4. AVL Balancing 5. Constructing Stack or Queue like ADTs 6. Binary Search tree construction 7. Expression evaluation like examples using stack ADT BR