AVL Tree Mohammad Asad Abbasi Lecture 12

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Advertisements

AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
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.
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
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.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
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.
AVL Trees ITCS6114 Algorithms and Data Structures.
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.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
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.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
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.
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,
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.
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.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Lecture Efficient Binary Trees Chapter 10 of textbook
BCA-II Data Structure Using C
Data Structures and Algorithms
Balancing Binary Search Trees
UNIT III TREES.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
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.
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.
CS 201 Data Structures and Algorithms
AVL Search Trees Introduction What is an AVL Tree?
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Dissection of AVL tree operations
Balanced Trees (AVL and RedBlack)
AVL Tree.
Trees (Chapter 4) Binary Search Trees - Review Definition
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 Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
AVL Trees Lab 11: AVL Trees.
Data Structures & Algorithms
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
AVL Trees CSE 373 Data Structures.
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL-Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
AVL Trees (Adelson – Velskii – Landis)
CSE 373 Data Structures Lecture 8
AVL-Trees (Part 2).
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

AVL Tree Mohammad Asad Abbasi Lecture 12

Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 Time needed to perform insertion and deletion and many other operations can be O(N) in the worst case Goal is to keep the height of a binary search tree balanced Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree

AVL Tree Named after Adelson-Velskii and Landis The first dynamically balanced trees Its Binary search tree with balance condition in which the sub-trees of each node can differ by at most 1 in their height i.e in the range -1 to 1

Balance Factor The value of the field is the difference between the height of the right and left subtrees of the node balanceFactor = height(right-subtree) - height(left-subtree) If balanceFactor is negative, the node is "heavy on the left" since the height of the left subtree is greater than the height of the right subtree With balanceFactor positive, the node is "heavy on the right" A balanced node has balanceFactor = 0

AVL Tree Nodes Add diagram from AVL_TREES.pdf page 2.

Rotations When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property This is done by using single rotations or double rotations y x x A y B C C B A After Rotation Before Rotation

Rotations Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 If the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2 Rotation will be applied to x to restore the AVL tree property

Rebalancing Suppose the node to be rebalanced is X. There are 4 cases that we might have to fix (two are the mirror images of the other two): An insertion in the left subtree of the left child of X An insertion in the right subtree of the left child of X An insertion in the left subtree of the right child of X An insertion in the right subtree of the right child of X

Balancing Operations: Rotations Case 1 and case 4 are symmetric and requires the same operation for balance Cases 1,4 are handled by single rotation Case 2 and case 3 are symmetric and requires the same operation for balance Cases 2,3 are handled by double rotation

Single Rotation A single rotation switches the roles of the parent and child while maintaining the search order Rotate between a node and its child Child becomes parent. Parent becomes right child in case 1, left child in case 4

Single rotation The new key is inserted in the subtree A. The AVL-property is violated at x Height of left(x) is h+2 Height of right(x) is h

Single rotation The new key is inserted in the subtree C The AVL-property is violated at x Single rotation takes O(1) time. Insertion takes O(log N) time.

Single Rotation

Single Rotation Will Not Work for the Other Case For case 2 After single rotation, k1 still not balanced Double rotations needed for case 2 and case 3

Double Rotation Left-right double rotation to fix case 2 First rotate between k1 and k2 Then rotate between k2 and k3 Case 3 is similar

Double rotation The new key is inserted in the subtree B1 or B2 The AVL-property is violated at x x-y-z forms a zig-zag shape

Double rotation The new key is inserted in the subtree B1 or B2 The AVL-property is violated at x

Node declaration for AVL trees template <class Comparable> class AvlTree; class AvlNode { Comparable element; AvlNode *left; AvlNode *right; int height; AvlNode( const Comparable & theElement, AvlNode *lt, AvlNode *rt, int h = 0 ) : element( theElement ), left( lt ), right( rt ), height( h ) { } friend class AvlTree<Comparable>; };

Height template class <Comparable> int AvlTree<Comparable>::height( AvlNode<Comparable> *t) const { return t == NULL ? -1 : t->height; }

Double Rotation /** * Double rotate binary tree node: first left child. * with its right child; then node k3 with new left child. * For AVL trees, this is a double rotation for case 2. * Update heights, then set new root. */ template <class Comparable> void AvlTree<Comparable>::doubleWithLeftChild( AvlNode<Comparable> * & k3 ) const { rotateWithRightChild( k3->left ); rotateWithLeftChild( k3 ); }

/* Internal method to insert into a subtree. * x is the item to insert. * t is the node that roots the tree. */ template <class Comparable> void AvlTree<Comparable>::insert( const Comparable & x, AvlNode<Comparable> * & t ) const { if( t == NULL ) t = new AvlNode<Comparable>( x, NULL, NULL ); else if( x < t->element ) insert( x, t->left ); if( height( t->left ) - height( t->right ) == 2 ) if( x < t->left->element ) rotateWithLeftChild( t ); else doubleWithLeftChild( t ); } else if( t->element < x ) insert( x, t->right ); if( height( t->right ) - height( t->left ) == 2 ) if( t->right->element < x ) rotateWithRightChild( t ); doubleWithRightChild( t ); ; // Duplicate; do nothing t->height = max( height( t->left ), height( t->right ) ) + 1;

Insertion First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1 If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation For insertion, once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x

Insertion Let x be the node at which left(x) and right(x) differ by more than 1 Assume that the height of x is h+3 There are 4 cases Height of left(x) is h+2 (i.e. height of right(x) is h) Height of left(left(x)) is h+1  single rotate with left child Height of right(left(x)) is h+1  double rotate with left child Height of right(x) is h+2 (i.e. height of left(x) is h) Height of right(right(x)) is h+1  single rotate with right child Height of left(right(x)) is h+1  double rotate with right child

AVL tree? YES Each left sub-tree has height 1 greater than each right sub-tree NO Left sub-tree has height 3, but right sub-tree has height 1

Insertion Insert 6 Imbalance at 8 Perform rotation with 7

Deletion Imbalance at 3 Perform rotation with 2 Imbalance at 5 Delete 4 Imbalance at 3 Perform rotation with 2 Imbalance at 5 Perform rotation with 8

AVL Trees (Adelson – Velskii – Landis) 20 16 25 30 7 18 5 17 19

AVL Trees 20 16 25 Not AVL tree 30 7 18 5 17 19 27

AVL Tree Rotations First insert 14 and 15: Now insert 16. Single rotations: insert 14, 15, 16, 13, 12, 11, 10 First insert 14 and 15: 14 15 Now insert 16.

AVL Tree Rotations Inserting 16 causes AVL violation Need to rotate Single rotations: Inserting 16 causes AVL violation 14 15 16 Need to rotate

AVL Tree Rotations Single rotations: 14 15 16

AVL Tree Rotations Rotation restores AVL balance Single rotations: 15 14 16

AVL Tree Rotations Now insert 13 and 12 AVL violation - need to rotate Single rotations: Now insert 13 and 12 15 14 16 13 12 AVL violation - need to rotate

AVL Tree Rotations Single rotations: 15 14 16 13 12

AVL Tree Rotations Single rotations: 15 13 16 12 14 Now insert 11

AVL Tree Rotations AVL violation – need to rotate Single rotations: 15 13 16 12 14 11 AVL violation – need to rotate

AVL Tree Rotations Single rotations: 15 13 16 12 14 11

AVL Tree Rotations Single rotations: 13 12 15 16 14 11 Now insert 10

AVL Tree Rotations AVL violation – need to rotate Single rotations: 13 12 15 16 14 11 10 AVL violation – need to rotate

AVL Tree Rotations Single rotations: 13 12 15 16 14 11 10

AVL Tree Rotations AVL balance restored Single rotations: 13 11 15 10 12 14 16 AVL balance restored

AVL Tree Rotations First insert 1 and 2 Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8 First insert 1 and 2 13 11 15 10 12 14 16

AVL Tree Rotations AVL violation - rotate Double rotations: 13 11 15 16 14 10 12 1 2

AVL Tree Rotations Double rotations: 13 11 15 16 14 10 12 1 2

AVL Tree Rotations AVL balance restored Now insert 3 Double rotations: 13 AVL balance restored 11 15 2 12 14 16 1 10 Now insert 3

AVL Tree Rotations AVL violation – rotate Double rotations: 13 11 15 2 12 14 16 1 10 3

AVL Tree Rotations Double rotations: 13 11 15 2 12 14 16 1 10 3

AVL Tree Rotations AVL balance restored Now insert 4 Double rotations: 13 AVL balance restored 10 15 2 11 14 16 1 3 12 Now insert 4

AVL Tree Rotations AVL violation - rotate Double rotations: 13 10 15 2 11 14 16 1 3 12 4

AVL Tree Rotations Double rotations: 13 10 15 2 11 14 16 1 3 12 4

AVL Tree Rotations Now insert 5 Double rotations: 10 13 2 15 1 11 3 12 14 16 4 Now insert 5

AVL Tree Rotations AVL violation – rotate Double rotations: 10 13 2 11 15 1 3 12 14 16 4 5 AVL violation – rotate

AVL Tree Rotations Single rotations: 10 13 2 1 11 15 3 12 14 16 4 5

AVL Tree Rotations AVL balance restored Now insert 7 Single rotations: 10 13 2 15 1 11 4 12 14 16 3 5 Now insert 7

AVL Tree Rotations AVL violation – rotate Single rotations: 10 13 2 15 11 4 12 14 16 3 5 7

AVL Tree Rotations Single rotations: 10 13 2 15 1 11 4 12 14 16 3 5 7

AVL Tree Rotations AVL balance restored Now insert 6 Double rotations: 10 13 4 15 2 11 5 12 14 16 7 1 3 Now insert 6

AVL Tree Rotations AVL violation - rotate Double rotations: 10 13 4 2 11 15 5 12 14 7 16 1 3 6

AVL Tree Rotations Double rotations: 10 13 4 11 15 2 5 12 14 16 7 1 3

AVL Tree Rotations AVL balance restored Now insert 9 and 8 Double rotations: AVL balance restored 10 13 4 2 11 15 6 12 14 16 1 3 5 7 Now insert 9 and 8

AVL Tree Rotations AVL violation - rotate Double rotations: 10 13 4 15 2 11 6 12 14 16 1 3 5 7 9 8

AVL Tree Rotations Double rotations: 10 13 4 11 15 2 6 12 14 16 1 3 5 7 9 8

AVL Tree Rotations Tree is almost perfectly balanced Final tree: 10 13 4 15 2 11 6 12 14 16 1 3 5 8 7 9 Tree is almost perfectly balanced

Insert 3,2,1,4,5,6,7, 16,15,14