Download presentation
Presentation is loading. Please wait.
Published byRhoda Andrews Modified over 8 years ago
1
AVL Trees
2
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.
3
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. 18 20 23 4414 12 1921 11 211 23 4 0 0000 00
4
Advantage Searching in a height-balanced tree is more efficient 8 23 20 14 12 18 23 18 20 44 5212 14 8 52 44 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
5
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
6
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; } };
7
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 ); };
8
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
9
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
10
Rebalancing When a new node is added, the heights of some of the existing nodes in the tree may change 23 12 18 2052 44 814 11 211 23 4 0 0000 00 myAVLTree.insertItem(4);
11
Rebalancing All of the height changes occur along the path from the new node w to the root 23 12 18 2052 44 814 21 311 24 5 0 0000 0 4 1 0 w
12
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 23 12 18 2052 44 814 21 311 24 5 0 0000 0 4 1 0 w z
13
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 23 12 18 2052 44 814 4 z y x
14
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 23 12 18 2052 44 814 4 z y x
15
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 23 12 18 52 44 8 z y x a b 4 14 20 c
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 23 12 18 52 44 8 z y x T0T0 4 14 20 T1T1 T2T2 T3T3 a b c
17
Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b 23 12 18 52 44 8 z y x T0T0 4 14 T2T2 20 T3T3 a b c T1T1
18
Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b 23 12 52 44 8 T0T0 414 T2T2 a b 18 20 T3T3 c temp T1T1 z
19
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 23 12 52 44 8 T0T0 414 T2T2 a b 18 20 T3T3 c temp T1T1 z
20
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 23 12 52 44 8 T0T0 414 T2T2 a b 18 20 T3T3 c temp T1T1 z
21
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 23 12 52 44 8 T0T0 414 T2T2 a b 18 20 T3T3 c T1T1 z
22
Rebalanced Tree Finally, set the height of z and its children 23 12 52 44 8 18 z 4 1420 2 2 3
23
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
24
4 Cases Require Rebalancing 12 18 20 814 4 z y x 12 18 20 814 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 14 8 4 1812 20 z y x Case 2. Insertion in the right subtree of the right child of z 14 8 4 1812 z y x 9 Case 4. Insertion in the left subtree of the right child of z
25
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
26
Case 3 Insertion in the right subtree of the left child of z 23 12 18 2052 44 814 z y x 16 myAVLTree.insertItem(16);
27
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 23 12 18 52 44 8 z y b a 14 20 c 16 x
28
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 23 12 18 52 44 z y T0T0 8 20 T1T1 16 T2T2 T3T3 a c 14 x b
29
Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b 23 12 18 52 44 z y 20 T3T3 a c T0T0 8 T1T1 16 T2T2 14 x b
30
Algorithm restructure( x ) Step 2: Replace the subtree rooted at z with the subtree rooted at b 23 14 52 44 16 T2T2 b 18 20 T3T3 c temp z 12 a T0T0 8 temp T1T1
31
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 23 14 52 44 16 T2T2 b 18 20 T3T3 c temp T1T1 z 12 a T0T0 8 temp
32
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 23 14 52 44 12 T0T0 816 T2T2 a b 18 20 T3T3 c temp T1T1 z
33
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 23 14 52 44 12 T0T0 816 T2T2 a b 18 20 T3T3 c temp T1T1 z
34
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 23 14 52 44 12 T0T0 816 T2T2 a b 18 20 T3T3 c T1T1 z
35
Rebalanced Tree Finally, set the height of z and its children 23 14 52 44 12 18 z 8 16 20 2 2 3
36
Rotation Another term used to describe the restructuring operation Single rotation used in Cases 1 and 2 12 18 20 814 4 12 8 18 4 1420
37
Double Rotation Double rotation used in Cases 3 and 4 14 18 20 12 16 8 12 18 20 814 16 14 12 18 8 16 20
38
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
39
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); 18 20 23 4414 12 1921
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.