1 Lecture 12 AVL Trees. 2 trees static dynamic game treessearch trees priority queues and heaps graphs binary search trees AVL trees 2-3 treestries Huffman.

Slides:



Advertisements
Similar presentations
AVL Trees When bad trees happen to good programmers.
Advertisements

AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group Introduction to Computer Science 2 Binary.
CS202 - Fundamental Structures of Computer Science II
CS Data Structures Chapter 10 Search Structures (Selected Topics)
1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
AVL Trees / Slide 1 Delete Single rotation Deletion.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CS121 Data Structures CS121 © JAS Trees Each entry in a List (Stack, Queue) has at most one predecessor and one successor. In a Tree each entry.
CS Data Structures Chapter 10 Search Structures.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
Copyright Curt Hill Balance in Binary Trees Impact on Performance.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Data Structures AVL Trees.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Foundation of Computing Systems Lecture 4 Trees: Part I.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
Trees Chapter 15.
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Balanced Trees (AVL and RedBlack)
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
Lecture 18. Basics and types of Trees
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 6 Transform and Conquer.
Data Structures and Programming Techniques
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS223 Advanced Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
Lecture No.20 Data Structures Dr. Sohail Aslam
CS223 Advanced Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
More Trees B.Ramamurthy 4/6/2019 BR.
AVL-Trees (Part 1).
AVL Trees B.Ramamurthy 4/27/2019 BR.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

1 Lecture 12 AVL Trees

2 trees static dynamic game treessearch trees priority queues and heaps graphs binary search trees AVL trees 2-3 treestries Huffman coding tree Types of Trees

3 Complete Binary Tree Is a tree with height h where: Is a tree with height h where: Every node is full upto level h-2, Every node is full upto level h-2, Level h-1 is completely filled. Level h-1 is completely filled. Level h is filled from left to right. Level h is filled from left to right. Yields to array representation. How to compute indices of parent and child? Yields to array representation. How to compute indices of parent and child? How many internal nodes and leafs? How many internal nodes and leafs?

4 AVL Trees Balanced binary search tree offer a O(log n) insert and delete. Balanced binary search tree offer a O(log n) insert and delete. But balancing itself costs O(n) in the average case. 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). 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? 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 Yes, by almost but not fully balancing the tree : AVL (Adelson Velskii and Landis) balancing

5 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. 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 empty tree is -1. Height of a single node tree is 0. Height of a single node tree is 0. Recursive definition: Recursive definition: height(t) = 0 if number of nodes = 1 height(t) = 0 if number of nodes = 1 = -1 if T is empty = -1 if T is empty = 1+ max(height(LT), height(RT)) otherwise = 1+ max(height(LT), height(RT)) otherwise

6 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. 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. Lets look at some examples.

7 AVL Tree: Example

8 Non-AVL Tree

9 Transforming into AVL Tree Four different transformations are available called : rotations Four different transformations are available called : rotations Rotations: single right, single left, double right, double left Rotations: single right, single left, double right, double left There is a close relationship between rotations and associative law of algebra. There is a close relationship between rotations and associative law of algebra.

10 Transformations Single right : Single right : ((T1 + T2) + T3) = (T1 + (T2 + T3) Single left : Single left : (T1 + (T2 + T3)) = ((T1 + T2) + T3) Double right : Double right : ((T1 + (T2 + T3)) + T4) = ((T1+T2) + (T3+T4)) Double left : Double left : (T1 ((T2+T3) +T4)) = ((T1+T2) + (T3+T4))

11 Example: AVL Tree for Airports Consider inserting sequentially: ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCM Consider inserting sequentially: ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCM Build a binary-search tree Build a binary-search tree Build a AVL tree. Build a AVL tree.

12 Binary Search Tree for Airport Names ORY ZRH JFK BRU MEX ARN DUS GLA ORD NRT GCM

13 AVL Balancing : Four Rotations Single right X3 X2 X1 X2 X1X3 X1 X2 X3 Single left X2 X3 X1 X3 X2 X1 Double right X3 X2X1 X3 X1X2 X3 X1 Double left

14 An AVL Tree for Airport Names ORY JFK BRU Not AVL balanced Single right JFK BRU ORY AVL Balanced After insertion of ORY, JFK and BRU :

15 An AVL Tree for Airport Names (contd.) After insertion of DUS, ZRH, MEX and ORD JFK BRU ORY DUSMEX ZRH ORD Still AVL Balanced After insertion of NRT? JFK BRU ORY DUSMEX ZRH ORD NRT

16 An AVL Tree … JFK BRU ORY DUSMEX ZRH ORD NRT Not AVL Balanaced Double Left JFK BRU ORY DUSNRT ZRH ORDMEX Now add ARN and GLA; no need for rotations; Then add GCM

17 An AVL Tree … JFK BRU DUS ORY NRT ZRH ORDMEX ARN GLA GCM NOT AVL BALANCED JFK BRU GCM ORY NRT ZRH ORDMEX ARN GLA Double left DUS

18 Search Operation For successful search, average number of comparisons: 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: For the binary search tree (of airports) it is: 39/11 = 3.55 For the AVL tree (of airports) it is : For the AVL tree (of airports) it is : 33/11 = 3.0

19 Known Performance Results of AVL trees AVL tree is a sub optimal solution. AVL tree is a sub optimal solution. How to evaluate its performance? How to evaluate its performance? Bounds (upper bound and lower bound) for number of comparisons: Bounds (upper bound and lower bound) for number of comparisons: C > log(n+1) + 1 C < 1.44 log(n+2) AVL trees are no more than 44% worse than optimal trees. AVL trees are no more than 44% worse than optimal trees.

20

21

22

23 The definition of an AVL tree indicates that the minimum number of nodes in a tree is determined by the recurrence equation: AVL h = AVL h-1 + AVL h-2 +1 where AVL 0 = 0 and AVL 1 = 1. Example: If the height is 1, there must be at least 2 nodes in the AVL tree. Proposed by Adel’son-Vel’s Skii G.M. and Landis Y.M. (1962)

24 EXAMPLES OF AVL TREES: BALANCE FACTORS: Indicated by the numbers in the nodes Equal the height of right subtree minus the height of left subtree The height of empty node in AVL is -1; otherwise it is 0 All numbers should be +1, 0, or -1

25

26

27

28 1) FIRST CASE: -Resulted from inserting a node in the right subtree of the right child New node insertedAVL tree is unbalanced. P Q P QP Q The AVL tree is now balanced

29

30

31

32

33 2) SECOND CASE: - Resulted from inserting a node into the left subtree of the right child. (it is more complex.) P Q P Q P Q R R R R Q Q P P R A A A

34 FACTS ABOUT NODE DELETION AND INSERTION: Require at most 1.44lg(n+2) searches. Require at most 1.44lg(n+2) searches. Require 1 single or 1 double rotation, depend the balance factors Require 1 single or 1 double rotation, depend the balance factors Deletion can require 1.44lg(n+2) rotations in the worst case. Deletion can require 1.44lg(n+2) rotations in the worst case. May or may not need rotation at all May or may not need rotation at all

35 If append a node to a path of all zero balance factor in AVL tree, no rotations required except updating the the balance factors of nodes Before updating the nodes After updating the nodes

36 Example of insertion (with rotation):

Example of insertion (with rotation):

38 Example of insertion (with rotation)

39

40

41 Deletion of a node may or may not requires rotation Rotation required. No rotation required A B C C B A

42 FACTS ABOUT NODE DELETION AND INSERTION: (cont.) 22% cases of deletion require rebalancing 22% cases of deletion require rebalancing 47% cases of insertion require rebalancing 47% cases of insertion require rebalancing Because the more time-consuming deletion Because the more time-consuming deletion occurs less frequently than insertion, it will not endangering the efficiency of rebalancing AVL trees.

43

44