AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD 1 Balanced Binary Trees Binary search trees.

Slides:



Advertisements
Similar presentations
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
CS202 - Fundamental Structures of Computer Science II
1 AVL Trees Drozdek Section pages
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
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:
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
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.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Data Structures AVL Trees.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
1 CSC TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Binary Trees Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Trees A binary tree is either.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
1 Balanced trees. Binary Search Tree: Balancing Problem nodeT *t=NULL; … InsertNode(&t, ‘G’); InsertNode(&t, ‘F’); InsertNode(&t, ‘E’); InsertNode(&t,
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science 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.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Trees (Chapter 4) Binary Search Trees - Review Definition
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees CENG 213 Data Structures.
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.
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
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.
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
CSC 143 Binary Search Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 1 Balanced Binary Trees Binary search trees provide O(log N) search times provided that the nodes are distributed in a reasonably “balanced” manner. Unfortunately, that is not always the case and performing a sequence of deletions and insertions can often exacerbate the problem. When a BST becomes badly unbalanced, the search behavior can degenerate to that of a sorted linked list, O(N). There are a number of strategies for dealing with this problem; most involve adding some sort of restructuring to the insert/delete algorithms. That can be effective only if the restructuring reduces the average depth of a node from the root of the BST, and if the cost of the restructuring is, on average, O(log N). We will examine one such restructuring algorithm…

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 2 AVL Trees AVL tree*:a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1, and in which the left and right subtrees are themselves AVL trees. *G. M. Adelson-Velskii and E. M. Landis, Each AVL tree node has an associated balance factor indicating the relative heights of its subtrees (left-higher, equal, right-higher). Normally, this adds one data element to each tree node and an enumerated type is used. How effective is this? The height of an AVL tree with N nodes never exceeds 1.44 log N and is typically much closer to log N.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 3 Examples \ \/ –– –– – \\ –/ /– – This is an AVL tree......and this is not.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 4 Unbalance from Insertion Consider inserting the value 45 into the AVL tree: The result would be unbalanced at the node containing 25: The unbalance is repaired by applying one of two types of “rotation” to the unbalanced subtree…

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 5 Rebalancing via Subtree Restructuring The subtree rooted at 25 is right-higher. We restructure the subtree, resulting in a balanced subtree: The transformation is relatively simple, requiring only a few operations, and results in a subtree that has equal balance

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 6 AVL Insertion Case: right-higher There are two unbalance cases to consider, each defined by the state of the subtree that just received a new node. For simplicity, assume for now that the insertion was to the right subtree (of the subtree). Let root be the root of the newly unbalanced subtree, and suppose that its right subtree is now right-higher: In this case, the subtree rooted at right was previously equally balanced (why?) and the subtree rooted at root was previously right- higher (why?). The height labels follow from those observations. Balance can be restored by “rotating” the values so that right becomes the subtree root node and root becomes the left child. \\ \ T1T1 T2T2 T3T3 h h h+1 root right

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 7 AVL Left Rotation The manipulation just described is known as a “left rotation” and the result is: \\ \ T1T1 T2T2 T3T3 h h h+1 root right – – T1T1 T2T2 T3T3 Overall height of each subtree is now the same. right root That covers the case where the right subtree has become right-higher… the case where the left subtree has become left-higher is analogous and solved by a right rotation.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 8 AVL Insertion Case: left-higher Now suppose that the right subtree has become left-higher: \\ / T1T1 h h-1 or h root right T4T4 ? T2T2 T3T3 right-left The insertion occurred in the left subtree of the right subtree of root. In this case, the left subtree of the right subtree (rooted at right-left ) may be either left-higher or right-higher, but not balanced (why?). Surprisingly (perhaps), this case is more difficult. The unbalance cannot be removed by performing a single left or right rotation.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 9 AVL Double Rotation Applying a single right rotation to the subtree rooted at right produces… \\ / h-1 or h root right T4T4 ? T2T2 T3T3 right-left \\ \ root right T4T4 ? T2T2 T3T3 right-left …a subtree rooted at right-left that is now right-higher…

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 10 AVL Double Rotation Now, applying a single left rotation to the subtree rooted at root produces… \\ \ root right T4T4 ? T2T2 T3T3 right-left …a balanced subtree. The case where the left subtree of root is right-higher is handled similarly (by a double rotation). h h-1 or h root – ? right T4T4 T3T3 right-left ? T1T1 T2T2 h Balance factors here depend on original balance factor of right-left.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 11 AVL Node Class Interface An AVL tree implementation can be derived from the BinNodeT and BST classes seen earlier… however, the AVLNode class must add a data member for the balance factor, and a mutator member function for the balance factor: enum BFactor {LEFTHI, RIGHTHI, EQUALHT, DBLRIGHTHI, DBLLEFTHI, BALUNKNOWN}; template class AVLNode { public: T Element; AVLNode * Left; AVLNode * Right; AVLNode * Parent; BFactor Balance; AVLNode(); AVLNode(const T& Elem, AVLNode * L = NULL, AVLNode * R = NULL, AVLNode * P = NULL); BFactor adjustBalance(BFactor Grew); bool isLeaf() const; ~AVLNode(); }; Because of that, using deriving the AVLNode from the more general BSTNode creates more problems than it solves, and so inheritance is not used in this implementation.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 12 AVL Tree Class Interface The AVL tree public interface includes: template class AVL { public: AVL(); AVL(const AVL & Source); AVL & operator=(const AVL & Source); ~AVL(); T* const Insert(const T& Elem); bool Delete(const T& Elem); T* const Find(const T& Elem); void Clear(); void Display(ostream& Out) const; // Iterator declarations omitted to save space... IOiterator iFind(const T& D); // return access to D // continues...

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 13 AVL Tree Class Interface The AVL tree private section includes: // continues... private: AVLNode * Root; // private helper functions omitted... string BFtoString(BFactor BF) const; // used in Display() fn void RightBalance(AVLNode * sRoot); // re-balance managers used by void LeftBalance(AVLNode * sRoot); // used by insert/delete fns void RotateLeft(AVLNode * sRoot); // rotation functions used by void RotateRight(AVLNode * sRoot); // used by managers };

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 14 AVL Left Rotation Implementation template void AVL ::RotateLeft(AVLNode * sRoot) { if ( (sRoot == NULL) || (sRoot->Right == NULL) ) return; AVLNode * Temp = new AVLNode (sRoot->Element); Temp->Left = sRoot->Left; sRoot->Left = Temp; Temp->Right = sRoot->Right->Left; AVLNode * toDelete = sRoot->Right; sRoot->Element = toDelete->Element; sRoot->Right = toDelete->Right; sRoot->Balance = toDelete->Balance; delete toDelete; }

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 15 AVL Right Balance Implementation template void AVL ::RightBalance(AVLNode * sRoot) { if ( (sRoot == NULL) || (sRoot->Right == NULL) ) return; AVLNode * rightSubTreeRoot = sRoot->Right; switch ( sRoot->Right->Balance ) { case RIGHTHI: sRoot->Balance = EQUALHT; rightSubTreeRoot->Balance = EQUALHT; RotateLeft(sRoot); break; // continues... The implementation uses two high-level functions to manage the rebalancing, one for when the imbalance is to the right and one for when it is to the left. RightBalance() would be called when a condition of double-right-high has been detected at a node following an insertion:

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 16 AVL Right Balance Implementation // continued... case LEFTHI: AVLNode * leftSubtreeRightSubTree = rightSubTreeRoot->Left; switch ( leftSubtreeRightSubTree->Balance ) { // code to reset balance factors... }; leftSubtreeRightSubTree->Balance = EQUALHT; RotateRight(rightSubTreeRoot); RotateLeft(sRoot); break; }

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 17 AVL Insert Implementation template T* const AVL ::Insert(const T& Elem) { bool Taller = false; return InsertHelper(Elem, Root, NULL, Taller); } As usual, insertion involves a relatively simple public "stub" and a recursive helper. template T* const AVL ::InsertHelper(const T& Elem, AVLNode *& sRoot, AVLNode * Par, bool& Taller) { if ( sRoot == NULL ) { // found the right location sRoot = new AVLNode (Elem); sRoot->Parent = Par; Taller = true; return &(sRoot->Element); } // continues... This leaves compensating for the effect of the insertion to be handled when the recursion backs out.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 18 AVL Insert Helper: Descent and Rebalance // continued... if ( Elem Element) ) { T* insertResult = InsertHelper(Elem, sRoot->Left, sRoot, Taller); if ( Taller ) { // left subtree just got taller BFactor sRootBalance = sRoot->adjustBalance(LEFTHI); if ( sRootBalance == EQUALHT ) { Taller = false; } else if ( sRootBalance == LEFTHI ) { Taller = true; } else if ( sRootBalance == DBLLEFTHI ) { Taller = false; LeftBalance( sRoot ); } return insertResult; } else { //... continues to handle descent down right subtree...

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 19 Unbalance from Deletion Deleting a node from an AVL tree can also create an imbalance that must be corrected. The effects of deletion are potentially more complex than those of insertion. The basic idea remains the same: delete the node, track changes in balance factors as the recursion backs out, and apply rotations as needed to restore AVL balance at each node along the path followed down during the deletion. However, rebalancing after a deletion may require applying single or double rotations at more than one point along that path. As usual, there are cases… Here, we will make the following assumptions: -the lowest imbalance occurs at the node root (a subtree root) -the deletion occurred in the left subtree of root

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 20 AVL Deletion Case: right-higher Suppose we have the subtree on the left prior to deletion and that on the right after deletion: \ \ 1 2 root right h 3 h-1 h \\ \ 1 2 root right h-1 3 h – – 2 right root 3 h-1 h 1 Then a single left rotation at root will rebalance the subtree. Note: "right-higher" refers to the balance factor of the root of the right subtree (labeled right here).

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 21 AVL Deletion Case: equal-height Suppose we the right subtree root has balance factor equal-height: \ – 1 root right h 3 h \ / 2 root 3 h h 1 h-1 Again, a single left rotation at root will rebalance the subtree. 2 h The difference is the resulting balance factor at the old subtree root node, root, which depends upon the original balance factor of the node right.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 22 AVL Deletion Case: left-higher If the right subtree root was left-higher, we have the following situation: As you should expect, the resulting imbalance can be cured by first applying a right rotation at the node right, and then applying a left rotation at the node root. Deleting a node from the left subtree of root now will cause root to become double right higher. However, we must be careful because the balance factors will depend upon the original balance factor at the node labeled right-left … h \ / 1 root right 4 h-1 h-1 or h-2? ? 23 right-left

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 23 AVL Deletion Case: left-higher, left-higher If the right-left subtree root was also left-higher, we obtain: h-1 \\ / 1 root right 4 h-1 / 23 right-left h-1 h-2 h-1 – – 1 right-left root \ right h-1 2 h-2 3 h-1 4

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 24 AVL Deletion Case: left-higher, right-higher If the right-left subtree root was right-higher, we obtain: h-1 \\ / 1 root right 4 h-1 \ 23 right-left h-2 h-1 – / 1 right-left root – right h-2 2 h And, finally, if the right-left subtree root was equal-height, we'd obtain a tree where all three of the labeled nodes have equal-height.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 25 AVL Deletion Cases: Summary We have considered a number of distinct deletion cases, assuming that the deletion occurred in the left subtree of the imbalanced node. There are an equal number of entirely similar, symmetric cases for the assumption the deletion was in the right subtree of the imbalanced node. Drawing diagrams helps… This discussion also has some logical implications for how insertion is handled in an AVL tree. The determination of the balance factors in the tree, following the rotations, involves similar logic in both cases.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 26 Implementing AVL Deletion Turning the previous discussion into an implementation involves a considerable amount of work. At the top level, the public delete function must take into account whether the deletion shortened either subtree of the tree root node. If that was the case, then it may be necessary to perform a rotation at the tree root itself. Thus, the helper function(s) must be recursive and must indicate whether a subtree was shortened; this may be accomplished by use of a bool parameter, as was done for the insertion functions. Deletion of the tree root node itself is a special case, and should take into account whether the tree root node had more than one subtree. If not, the root pointer should simply be retargeted to the appropriate subtree and no imbalance occurs. If the tree root node has two subtrees, its value should be swapped with the minimum value in the right subtree and then the corresponding node should be deleted from the right subtree… which may lead to an imbalance that must be corrected.

AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 27 Testing In testing the AVL implementation, I found a few useful ideas: -Test with small cases first, and vary them to cover all the different scenarios you can think of. -Work out the solutions manually as well. If you don't understand how to solve the problem by hand, you'll NEVER program a correct solution. -Test insertion first, thoroughly. -Then build an initial, correct AVL tree and test deletion, thoroughly. -Then test alternating combinations of insertions and deletions. -Modify your tree print function to show the balance factors: c\ d- e\ h- j- k- o/ s-