More Trees B Ramamurthy 1292022 BR 1 Types
More Trees B. Ramamurthy 1/29/2022 BR 1
Types of Trees trees static game trees binary search trees 1/29/2022 dynamic search trees AVL trees BR priority queues and heaps 2 -3 trees graphs tries Huffman coding tree 2
Complete Binary Tree z. Is a tree with height h where: y. Every node is full upto level h-2, y. Level h-1 is completely filled. y. Level h is filled from left to right. z. Yields to array representation. How to compute indices of parent and child? z. How many internal nodes and leafs? 1/29/2022 BR 3
AVL Trees z Balanced binary search tree offer a O(log n) insert and delete. z But balancing itself costs O(n) in the average case. z In this case, even though delete will be O(log n), insert will be O(n). z Is there any way to have a O(log n) insert too? z Yes, by almost but not fully balancing the tree : AVL (Adelson Velskii and Landis) balancing 1/29/2022 BR 4
Height of a Tree z Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node. z Height of a empty tree is -1. z Height of a single node tree is 0. z Recursive definition: z height(t) = 0 if number of nodes = 1 = -1 if T is empty = 1+ max(height(LT), height(RT)) otherwise 1/29/2022 BR 5
AVL Property z. If N is a node in a binary tree, node N has AVL property if the heights of the left subtree and right sub-tree are equal or if they differ by 1. z. Lets look at some examples. 1/29/2022 BR 6
AVL Tree: Example 1/29/2022 BR 7
Non-AVL Tree 1/29/2022 BR 8
Transforming into AVL Tree z. Four different transformations are available called : rotations z. Rotations: single right, single left, double right, double left z. There is a close relationship between rotations and associative law of algebra. 1/29/2022 BR 9
Transformations z Single right : ((T 1 + T 2) + T 3) = (T 1 + (T 2 + T 3) z Single left : (T 1 + (T 2 + T 3)) = ((T 1 + T 2) + T 3) z Double right : ((T 1 + (T 2 + T 3)) + T 4) = ((T 1+T 2) + (T 3+T 4)) z Double left : (T 1 ((T 2+T 3) +T 4)) = ((T 1+T 2) + (T 3+T 4)) 1/29/2022 BR 10
Examples for Transformations 1/29/2022 BR 11
Example: AVL Tree for Airports z. Consider inserting sequentially: ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCM z. Build a binary-search tree z. Build a AVL tree. 1/29/2022 BR 12
Binary Search Tree for Airport Names ORY ZRH JFK MEX BRU ORD ARN DUS GLA 1/29/2022 GCM NRT BR 13
AVL Balancing : Four Rotations X 1 X 3 Double right X 2 X 2 Single right X 3 X 1 X 2 Single left X 1 X 3 Double left X 2 X 3 X 1 X 2 X 3 1/29/2022 BR 14
An AVL Tree for Airport Names After insertion of ORY, JFK and BRU : ORY JFK Single right BRU JFK BRU ORY Not AVL balanced AVL Balanced 1/29/2022 BR 15
An AVL Tree for Airport Names (contd. ) After insertion of DUS, ZRH, MEX and ORD After insertion of NRT? JFK BRU ORY DUS MEX ZRH ORD Still AVL Balanced 1/29/2022 BR NRT 16
An AVL Tree … Not AVL Balanaced JFK BRU ORY DUS MEX DUS ZRH ORD Double Left NRT 1/29/2022 ORY MEX NRT ZRH ORD Now add ARN and GLA; no need for rotations; Then add GCM BR 17
An AVL Tree… JFK BRU DUS ARN BRU ORY GLA NRT MEX GCM ZRH GCM ARN DUS ORD ORY GLA NRT MEX ZRH ORD Double left NOT AVL BALANCED 1/29/2022 BR 18
Search Operation z For successful search, average number of comparisons: sum of all (path length+1) / number of nodes z For the binary search tree (of airports) it is: 39/11 = 3. 55 z For the AVL tree (of airports) it is : 33/11 = 3. 0 1/29/2022 BR 19
Known Performance Results of AVL trees z. AVL tree is a sub optimal solution. z. How to evaluate its performance? z. Bounds (upper bound and lower bound) for number of comparisons: C > log(n+1) + 1 C < 1. 44 log(n+2) - 0. 328 z. AVL trees are no more than 44% worse than optimal trees. 1/29/2022 BR 20
- Slides: 20