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.

Slides:



Advertisements
Similar presentations
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advertisements

AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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. 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.
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 (10.2) CSE 2011 Winter April 2015.
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 Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
Trees Types and Operations
CS202 - Fundamental Structures of Computer Science II
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
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):
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 Search Trees What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations. Insertion into an AVL tree Deletion from an AVL tree.
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 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.
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.
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.
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.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
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.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
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.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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,
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.
AVL Trees CSE, POSTECH.
AA Trees.
AVL Trees AVL Trees.
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.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
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.
AVL Search Trees Introduction What is an AVL Tree?
Chapter 29 AVL Trees.
AVL Tree Mohammad Asad Abbasi Lecture 12
Red-Black Trees 9/12/ :44 AM AVL Trees v z 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 Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373: Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Lecture 9: Self Balancing Trees
ITCS6114 Algorithms and Data Structures
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
Presentation transcript:

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 sub-trees of any node differ by at most one. Therefore it is also known as a height- balanced tree.

Height-Balance Property The balance factor of a node is the height of its right subtree minus the height of its left subtree. A node with balance factor 1, 0, or -1 is considered balanced

Advantage Searching in a height-balanced tree is more efficient n = 8 for both trees (where n = number of data elements) maximum number of comparisons = 8 maximum number of comparisons = 3 or 4 O(n) in the worst case O(logn) AVL tree

Balancing an AVL Tree A node with balance factor 1, 0, or -1 is considered balanced A node with any other balance factor is considered unbalanced and requires rebalancing the tree The balance factor is either stored directly at each node or computed from the heights of the sub-trees. Insertion or removal of nodes can cause a balanced tree to become unbalanced After a node is inserted or removed, a restructure operation must be invoked to rebalance the tree

AVLItem Class Objects of this class are stored in the nodes of an AVL tree template class AVLItem : public Item { // AVLItem inherits data members _key, _elem private: int hgt; // It has an additional data member hgt public: AVLItem( const Key& k = Key(), const Element& e = Element(), int h = 0) : Item (k,e), hgt(h) { } int height() const { return hgt; } void setHeight(int h) { hgt = h; } };

AVLTree Class Inherits from the BinarySearchTree class Adds only two public member functions template class AVLTree : public BinarySearchTree >{ //...typedefs and other code inserted here protected: //Utility functions int height( const BTPosition& p ); void setHeight( BTPosition p ); bool isBalanced( const BTPosition& p ); BTPosition tallGrandchild( const BTPosition& p ); void rebalance( BTPosition& p ); public: AVLItem() : BST() { } void insertItem( const Key& k, const Element& e ); void removeElement( const Key& k ); };

Inserting into an AVL Tree Inserting into an AVL tree may be carried out by inserting the given value into the tree as if it were an unbalanced binary search tree, and then retracing one's steps toward the root and rotating about any nodes which have become unbalanced during the insertion A new element is always inserted at an external node Implemented with the insertItem() member function

Inserting an Item void insertItem(const Key& k, const Element& e){ BTPosition p = inserter( k, e ); setHeight(p); rebalance(p); } Begins with the inserter function used by the binary search tree After a new node is inserted, the height of the item at that position is set Finally, the tree is rebalanced

Rebalancing When a new node is added, the heights of some of the existing nodes in the tree may change myAVLTree.insertItem(4);

Rebalancing All of the height changes occur along the path from the new node w to the root w

Rebalancing The rebalance function searches for the first unbalanced node by following the path from w to the root The first unbalanced node it finds is labeled z w z

Rebalancing Three nodes must be identified – z – the first unbalanced node on the path – y – the child of z that has the higher height – x – the grandchild of z that has the higher height z y x

Trinode Restructuring The rebalance function then uses the trinode restructuring function of the binary tree to restore the height balance Trinode restructuring moves nodes x, y, and z by reassigning the pointers z y x

Algorithm restructure( x ) Input: node x that has parent y and grandparent z Output: restructured tree containing x, y, and z Step 1: Rename x, y, and z to a, b, c, from left to right Name their four subtrees T 0, T 1, T 2, T 3, from left to right z y x a b c

Algorithm restructure( x ) Input: node x that has parent y and grandparent z Output: restructured tree containing x, y, and z Step 1: Rename x, y, and z to a, b, c, from left to right Name their four subtrees T 0, T 1, T 2, T 3, from left to right z y x T0T T1T1 T2T2 T3T3 a b c

Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b z y x T0T T2T2 20 T3T3 a b c T1T1

Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b T0T0 414 T2T2 a b T3T3 c temp T1T1 z

Algorithm restructure( x ) Step 3: Let T 0, T 1, be the left and right subtrees of a Let a be the left child of b T0T0 414 T2T2 a b T3T3 c temp T1T1 z

Algorithm restructure( x ) Step 4: Let T 2, T 3, be the left and right subtrees of c Let c be the right child of b T0T0 414 T2T2 a b T3T3 c temp T1T1 z

Algorithm restructure( x ) Step 4: Let T 2, T 3, be the left and right subtrees of c Let c be the right child of b T0T0 414 T2T2 a b T3T3 c T1T1 z

Rebalanced Tree Finally, set the height of z and its children z

rebalance() Member Function void rebalance( BTPosition& z ){ while( !T.isRoot(z) ){ z = T.parent(z); setHeight(z); if( !isBalanced(z) ){ BTPosition x = tallGrandchild(z); z = T.restructure(x); setHeight(T.leftChild(z)); setHeight(T.rightChild(z)); setHeight(z); } Search for an unbalanced node by following the path from the new node to the root Restore the height balance with the trinode restructuring function of the binary tree Set the height of z and its children

4 Cases Require Rebalancing z y x z y x 16 Case 1. Insertion in the left subtree of the left child of z Case 3. Insertion in the right subtree of the left child of z z y x Case 2. Insertion in the right subtree of the right child of z z y x 9 Case 4. Insertion in the left subtree of the right child of z

Restructuring the Four Cases All four cases are restructured by the trinode restructure function Similarities in the cases – Case 1 and Case 2 are mirror images – Case 3 and Case 4 are mirror images

Case 3 Insertion in the right subtree of the left child of z z y x 16 myAVLTree.insertItem(16);

Algorithm restructure( x ) Input: node x that has parent y and grandparent z Output: restructured tree containing x, y, and z Step 1: Rename x, y, and z to a, b, c, from left to right Name their four subtrees T 0, T 1, T 2, T 3, from left to right z y b a c 16 x

Algorithm restructure( x ) Input: node x that has parent y and grandparent z Output: restructured tree containing x, y, and z Step 1: Rename x, y, and z to a, b, c, from left to right Name their four subtrees T 0, T 1, T 2, T 3, from left to right z y T0T T1T1 16 T2T2 T3T3 a c 14 x b

Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b z y 20 T3T3 a c T0T0 8 T1T1 16 T2T2 14 x b

Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b T2T2 b T3T3 c temp z 12 a T0T0 8 temp T1T1

Algorithm restructure( x ) Step 3: Let T 0, T 1, be the left and right subtrees of a Let a be the left child of b T2T2 b T3T3 c temp T1T1 z 12 a T0T0 8 temp

Algorithm restructure( x ) Step 3: Let T 0, T 1, be the left and right subtrees of a Let a be the left child of b T0T0 816 T2T2 a b T3T3 c temp T1T1 z

Algorithm restructure( x ) Step 4: Let T 2, T 3, be the left and right subtrees of c Let c be the right child of b T0T0 816 T2T2 a b T3T3 c temp T1T1 z

Algorithm restructure( x ) Step 4: Let T 2, T 3, be the left and right subtrees of c Let c be the right child of b T0T0 816 T2T2 a b T3T3 c T1T1 z

Rebalanced Tree Finally, set the height of z and its children z

Rotation Another term used to describe the restructuring operation Single rotation used in Cases 1 and

Double Rotation Double rotation used in Cases 3 and

Removing an Element Implemented with the removeElement() member function void removeElement(const Key& k){ BTPosition p = finder( k, T.root() ); if( p.isNull() ) throw NonexistentElementException("Error"); BTPosition r = remover(p); rebalance(r); } Begins with the finder function used by the binary search tree After the node containing that element is located, then it is removed with the remover function of the binary search tree Finally, the tree is rebalanced

Sample Problem Show the result of removing the element with the key of 14 from the AVL tree shown below. The result must be a valid AVL tree. myAVLTree.removeElement(14);