CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Chapter 4: Trees Part II - AVL Tree
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.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
CS202 - Fundamental Structures of Computer Science II
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
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:
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.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures AVL Trees
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
AVL Trees ITCS6114 Algorithms and Data Structures.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
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.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Data Structures AVL Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
AVL Tree 1. is a binary search tree that For each node: –The height of its left and right subtree can only differ at most 1. –If a tree is an empty tree,
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSE373: Data Structures & Algorithms
UNIT III TREES.
CSIT 402 Data Structures II
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.
CS 201 Data Structures and Algorithms
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
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
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
AVL Trees CENG 213 Data Structures.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 332: Data Abstractions AVL Trees
CE 221 Data Structures and Algorithms
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics

Definition of AVL An AVL tree is indeed a BST with every node satisfying the property that the heights of left and right subtrees can differ only by one

AVL Trees An AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance condition. It must be easy to maintain, and must ensure that the depth of the tree is O (log N). Idea 1: left and right subtrees are of the same height (not shallow). Izmir University of Economics3

Balance Condition for AVL Trees Idea 2: Every node must have left and right subtrees of the same height. The height of an empty tree is -1 (Only perfectly balanced trees with 2 k -1 nodes would satisfy). The tree on the left is an AVL tree. The height info is kept for each node. Izmir University of Economics4

Height of an AVL Tree Fact: The height of an AVL tree storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2 i n(h-2i) Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) 3 4 n(1) n(2)

Height of an AVL Tree - I For an AVL Tree with N nodes, the height is at most 1.44log(N+2) In practice it is slightly more than log N. Example: An AVL tree of height 9 with fewest nodes (143). Izmir University of Economics6

The minimum number of nodes, S(h), in an AVL tree of height h is given by S(0)=1, S(1)=2, S(h) = S(h-1) + S(h-2) +1 Recall Fibonacci Numbers? (F(0) = F(1) = 1, F(n)=F(n-1) + F(n-2)) Claim: S(h) = F(h+2) -1 for all h ≥ 0. Proof: By induction. Base cases; S(0)=1=F(2)-1=2-1, S(1)=2=F(3)-1=3-1. Assume by inductive hypothesis that claim holds for all heights k ≤ h. Then, S(h+1) = S(h) + S(h-1) + 1 = F(h+2) -1 +F(h+1) (by inductive hypothesis) = F(h+3) – 1 We also know that Izmir University of Economics7 The Height of an AVL Tree - II

Thus, all the AVL tree operations can be performed in O (log N) time. We will assume lazy deletions. Except possibly insertion (update all the balancing information and keep it balanced) Izmir University of Economics8 The Height of an AVL Tree - III

AVL Tree Insertion Example: Let’s try to insert 6 into the AVL tree below. This would destroy the AVL property of the tree. Then this property has to be restored before the insertion step is considered over. It turns out that this can always be done with a simple modification to the tree known as rotation. After an insertion, only the nodes that are on the path from the insertion point to the root might have their balance altered. As we follow the path up to the root and update the balancing information there may exist nodes whose new balance violates the AVL condition. We will prove that our rebalancing scheme performed once at the deepest such node works. Izmir University of Economics9 6

node to be balanced α

Let’s call the node to be balanced α. Since any node has at most 2 children, and a height imbalance requires that α‘s 2 subtrees’ height differ by 2. There are four cases to be considered for a violation: 1)An insertion into left subtree of the left child of α. (LL) 2)An insertion into right subtree of the left child of α. (LR) 3)An insertion into left subtree of the right child of α. (RL) 4)An insertion into right subtree of the right child of α. (RR) Cases 1 and 4 are mirror image symmetries with respect to α, as are Cases 2 and 3. Consequently; there are 2 basic cases. Case I (LL, RR) (insertion occurs on the outside) is fixed by a single rotation. Case II (RL, LR) (insertion occurs on the inside) is fixed by double rotation. Izmir University of Economics11 AVL Tree Rotations

Single Rotation (LL) Let k 2 be the first node on the path up violating AVL balance property. Figure below is the only possible scenario that allows k 2 to satisfy the AVL property before the insertion but violate it afterwards. Subtree X has grown an extra level (2 levels deeper than Z now). Y cannot be at the same level as X (k 2 then out of balance before insertion) and Y cannot be at the same level as Z (then k 1 would be the first to violate). Izmir University of Economics12

Izmir University of Economics13 Note that in single rotation inorder traversal orders of the nodes are preserved. The new height of the subtree is exactly the same as before. Thus no further updating of the nodes on the path to the root is needed. Single Rotation (RR)

AVL property destroyed by insertion of 6, then fixed by a single rotation. BST node structure needs an additional field for height. 14Izmir University of Economics Single Rotation-Example I

Izmir University of Economics15 Single Rotation-Example II Start with an initially empty tree and insert items 1 through 7 sequentially. Dashed line joins the two nodes that are the subject of the rotation.

Izmir University of Economics16 Insert 6. Balance problem at the root. So a single rotation is performed. Finally, Insert 7 causing another rotation. Single Rotation-Example III

Izmir University of Economics17 Double Rotation (LR, RL) - I The algorithm that works for cases 1 and 4 (LL, RR) does not work for cases 2 and 3 (LR, RL). The problem is that subtree Y is too deep, and a single rotation does not make it any less deep. The fact that subtree Y has had an item inserted into it guarantees that it is nonempty. Assume it has a root and two subtrees.

Izmir University of Economics18 Below are 4 subtrees connected by 3 nodes. Note that exactly one of tree B or C is 2 levels deeper than D (unless all empty). To rebalance, k 3 cannot be root and a rotation between k 1 and k 3 was shown not to work. So the only alternative is to place k 2 as the new root. This forces k 1 to be k 2 ’s left child and k 3 to be its right child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored; so, all the balancing and and height updating is complete. Double Rotation (LR) - II

Izmir University of Economics19 Double Rotation (RL) - III In both cases (LR and RL), the effect is the same as rotating between α’s child and grandchild and then between α and its new child. Every double rotation can be modelled in terms of 2 single rotations. Inorder traversal orders are always preserved between k 1, k 2, and k 3. Double RL = Single LL (α->right)+ Single RR (α) Double LR = Single RR (α->left)+ Single LL (α )

Izmir University of Economics20 Continuing our example, suppose keys 8 through 15 are inserted in reverse order. Inserting 15 is easy but inserting 14 causes a height imbalance at node 7. The double rotation is an RL type and involves 7, 15, and 14. Double Rotation Example - I

Izmir University of Economics21 insert 13: double rotation is RL that will involve 6, 14, and 7 and will restore the tree. Double Rotation Example - II

22Izmir University of Economics Double Rotation Example - III If 12 is now inserted, there is an imbalance at the root. Since 12 is not between 4 and 7, we know that the single rotation RR will work.

Izmir University of Economics23 Double Rotation Example - IV Insert 11: single rotation LL; insert 10: single rotation LL; insert 9: single rotation LL; insert 8: without a rotation.

Izmir University of Economics24 Double Rotation Example - V Insert 8½: double rotation LR. Nodes 8, 8½, 9 are involved.

Implementation Issues - I To insert a new node with key X into an AVL tree T, we recursively insert X into the appropriate subtree of T (let us call this T LR ). If the height of T LR does not change, then we are done. Otherwise, if a height imbalance appears in T, we do the appropriate single or double rotation depending on X and the keys in T and T LR, update the heights (making the connection from the rest of the tree above), and are done. Izmir University of Economics25

Izmir University of Economics26 Implementation Issues - II Another efficiency issue concerns storage of the height information. Since all that is really required is the difference in height, which is guaranteed to be small, we could get by with two bits (to represent +1, 0, -1) if we really try. Doing so will avoid repetitive calculation of balance factors but results in some loss of clarity. The resulting code is somewhat more complicated than if the height were stored at each node.

Izmir University of Economics27 Implementation Issues - III First, the declarations. Also, a quick function to return the height of a node dealing with the annoying case of a NULL pointer. typedef int ElementType; struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; struct AvlNode { ElementType Element; AvlTree Left; AvlTree Right; int Height; }; static int Height( Position P ) { if( P == NULL ) return -1; else return P->Height; }

Izmir University of Economics28 Implementation - LL /* This function can be called only if K2 has */ /* a left child. Perform a rotate between */ /* a node (K2) and its left child. Update heights, */ /* then return new root */ static Position SingleRotateWithLeft( Position K2 ){ Position K1; K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; K2->Height=Max(Height(K2->Left), Height(K2->Right))+1; K1->Height=Max(Height(K1->Left), K2->Height)+1; return K1; /* New root */ }

Izmir University of Economics29 Implementation - RR /* This function can be called only if K1 has */ /* a right child. Perform a rotate between */ /* a node (K1) and its right child. Update heights, */ /* then return new root */ static Position SingleRotateWithRight( Position K1 ) { Position K2; K2 = K1->Right; K1->Right = K2->Left; K2->Left = K1; K1->Height=Max(Height(K1->Left), Height(K1->Right))+1; K2->Height=Max(Height(K2->Right), K1->Height)+1; return K2; /* New root */ }

Izmir University of Economics30 Implementation - LR /* Double LR = Single RR (α->left)+ Single LL (α ) */ /* This function can be called only if K3 has a left */ /* child and K3's left child has a right child */ /* Do the left-right double rotation */ /* Update heights, then return new root */ static Position DoubleRotateWithLeft( Position K3 ){ /* Rotate between K1 and K2 */ K3->Left = SingleRotateWithRight( K3->Left ); /* Rotate between K3 and K2 */ return SingleRotateWithLeft( K3 ); }

Izmir University of Economics31 Implementation - RL /* Double RL = Single LL (α->right)+ Single RR (α) */ /* This function can be called only if K1 has a right */ /* child and K1's right child has a left child */ /* Do the right-left double rotation */ /* Update heights, then return new root */ static Position DoubleRotateWithRight( Position K1 ){ /* Rotate between K3 and K2 */ K1->Right = SingleRotateWithLeft( K1->Right ); /* Rotate between K1 and K2 */ return SingleRotateWithRight( K1 ); }

Izmir University of Economics32 AvlTree Insert ( ElementType X, AvlTree T ){ if( T == NULL ){ /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); if( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } else if( X Element ){ T->Left = Insert( X, T->Left ); if( Height( T->Left ) - Height( T->Right ) == 2 ) if( X Left->Element ) T = SingleRotateWithLeft( T ); /* LL */ else T = DoubleRotateWithLeft( T ); /* LR */ } /* continued on the next slide */ /*... */ Implementation Insertion - I

Izmir University of Economics33 Implementation – Insertion II else if( X > T->Element ){ T->Right = Insert( X, T->Right ); if( Height( T->Right ) - Height( T->Left ) == 2 ) if( X > T->Right->Element ) T = SingleRotateWithRight( T ); /* RR */ else T = DoubleRotateWithRight( T ); /* RL */ } /* Else X is in the tree already; we'll do nothing */ T->Height=Max(Height(T->Left),Height(T->Right))+1; return T; } Implementation Insertion - II

AVL Tree Deletion* - I Izmir University of Economics34 AvlTree Delete( ElementType X, AvlTree T ){ if( T == NULL ) Error("Item not Found); else if( X Element ){ T->Left = Delete( X, T->Left ); if( Height( T->Left ) - Height( T->Right ) == -2 ) if( Height(T->Right->Right) > Height(T->Right->Left) ) T = SingleRotateWithRight( T ); /* RR */ else T = DoubleRotateWithRight( T ); /* RL */ } else if( X > T->Element ){ T->Right = Delete( X, T->Right ); if( Height( T->Right ) - Height( T->Left ) == -2 ) if( Height(T->Left->Left) > Height(T->Left->Right) ) T = SingleRotateWithLeft( T ); /* LL */ else T = DoubleRotateWithLeft( T ); /* LR */ } /* Continued on the next slide */ /*... */

Izmir University of Economics35 AVL Tree Deletion* - II else if( T->Left && T->Right ){ /* Found with two children */ /* Replace with smallest in right subtree */ TmpCell = FindMin( T->Right ); T->Element = TmpCell->Element; T->Right = Delete( T->Element, T->Right ); if( Height( T->Right ) - Height( T->Left ) == -2 ) if( Height(T->Left->Left) > Height(T->Left->Right) )/*LL*/ T = SingleRotateWithLeft( T ); else T = DoubleRotateWithLeft( T ); /*LR*/ } else { /* Found with one or zero child */ TmpCell = T; T = T->Left ? T->Left : T->Right; /* Also handles 0 child */ free( TmpCell ); } if( T!= NULL ) T->Height=Max(Height(T->Left), Height(T->Right))+1; return T; }

Homework Assignments 4.18, 4.19, 4.25 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics36