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.

Slides:



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

AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
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.
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 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.
CS202 - Fundamental Structures of Computer Science II
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Balanced Binary Search Trees
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
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
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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 (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures AVL Trees
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 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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
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 :
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:
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
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 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
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.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
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,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
CSE332: Data Abstractions Lecture 7: AVL Trees
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:
BCA-II Data Structure Using C
Representation of an AVL Node
UNIT III TREES.
CSIT 402 Data Structures II
Balanced Binary Search Trees
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 Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Trees CENG 213 Data Structures.
Trees.
Representation of an AVL Node
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Data Structures & Algorithms
AVL Trees CSE 373 Data Structures.
CE 221 Data Structures and Algorithms
AVL-Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CSE 373 Data Structures Lecture 8
Presentation transcript:

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 the number of nodes in an AVL tree of height h. AVL tree insertion Discussion of 4 cases – LL, RR, RL and LR Code for insertion (and single and double rotations) Discussion of deletion Experiments with AVL tree code

Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We want a tree with small height  Height of a binary tree with N node is at least O(log N)  Complete binary tree has height log N.  Goal: keep the height of a binary search tree O(log N)  Balanced binary search trees  Examples: AVL tree, red-black tree

Lecture 16 Oct 20, 2010 Complete BST deletion l Height-balanced BST l Application of BST

AVL tree for each node, the height of the left and right subtrees can differ by at most d = 1. Maintaining a stricter condition (e.g above condition with d = 0) is difficult. Note that our goal is to perform all the operations search, insert and delete in O(log N) time, including the operations involved in adjusting the tree to maintain the above balance condition.

AVL Tree  An AVL tree is a binary search tree in which  for every node in the tree, the height of the left and right subtrees differ by at most 1.  Height of subtree: Max # of edges to a leaf  Height of an empty subtree: -1  Height of one node: 0 AVL tree property violated here AVL tree

AVL Tree with Minimum Number of Nodes N 1 = 2 N 2 =4N 3 = N 1 +N 2 +1=7 N 0 = 1

Smallest AVL tree of height 9 Smallest AVL tree of height 7 Smallest AVL tree of height 8

Height of AVL Tree with N nodes  Denote N h the minimum number of nodes in an AVL tree of height h  N 0 = 1, N 1 =2 (base) N h = N h-1 + N h-2 +1 (recursive relation)  N > N h = N h-1 + N h-2 +1 > 2 N h-2 > 4 N h-4 >... >2 i N h-2i

Height of AVL Tree with N nodes  If h is even, let i=h/2–1. The equation becomes N>2 h/2-1 N 2  N > 2 h/2-1 x4  h=O(log N)  If h is odd, let i=(h-1)/2. The equation becomes N>2 (h-1)/2 N 1  N > 2 (h-1)/2 x2  h=O(log N)  Recall the cost of operations search, insert and delete is O(h).  cost of searching = O(log N). (Just use the search algorithm for the binary search tree.)  But insert and delete are more complicated.

Insertion in AVL Tree  Basically follows insertion strategy of binary search tree  But may cause violation of AVL tree property  Restore the destroyed balance condition if needed Original AVL tree Insert 6 Property violated Restore AVL property

Some Observations  After an insertion (using the insertion algorithm for a binary search tree), only nodes that are on the path from the insertion point to the root might have their balance altered.  Because only those nodes have their subtrees altered  So it’s enough to adjust the balance on these nodes.  We will see that AVL tree property can be restored by performing a balancing operation at a single node.

Node at which balancing must be done  The node of smallest depth on the path from to the newly inserted node.  We will call this node pivot. Example: Suppose we inserted key is 2.5 Pivot is the node containing 4. This is the lowest depth node in the path where the AVL tree property is violated. 2.5 *

Different Cases for Rebalance  Let α be the pivot node  Case 1: inserted node is in the left subtree of the left child of α (LL-rotation)  Case 2: inserted node is in the right subtree of the left child of α (RL-rotation)  Case 3: inserted node is in the left subtree of the right child of α (LR-rotation)  Case 4: inserted node is in the right subtree of the right child of α (RR-rotation)  Cases 3 & 4 are mirror images of cases 1 & 2 so we will focus on 1 & 2.

Rotations  Rebalance of AVL tree are done with simple modification to tree, known as rotation  Insertion occurs on the “outside” (i.e., left-left or right-right) is fixed by single rotation of the tree  Insertion occurs on the “inside” (i.e., left-right or right-left) is fixed by double rotation of the tree

Insertion Algorithm (outline)  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  Note: once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x.

Single Rotation to Fix Case 1(LL-rotation) Questions:  Can Y have the same height as the new X?  Can Y have the same height as Z? k2 is the pivot node

Single Rotation Case 1 Example k2 k1 X k2 X

Informal proof/argument for the LL-case We need to show the following:  After the rotation, the balance condition at the pivot node is restored and at all the nodes in the subtree rooted at the pivot.  After the rotation, the balance condition is restored at all the ancestors of the pivot.  We will prove both these informally.

Single Rotation to Fix Case 4 (RR-rotation)  Case 4 is a symmetric case to case 1  Insertion takes O(h) time (h = height of the tree), single rotation takes O(1) time. (including locating the pivot node). Details are not obvious, but see the code … An insertion in subtree Z k1 violates AVL tree balance condition

Each node stores height information which is updated when insertion and deletion is done. void insert( const Comparable & x, AvlNode * & t ) { if( t == NULL ) t = new AvlNode( x, NULL, NULL ); else if( x element ) { insert( x, t->left ); if( height( t->left ) - height( t->right ) == 2 ) if( x 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 ); else doubleWithRightChild( t ); } else ; // Duplicate; do nothing t->height = max( height( t->left ), height( t->right ) ) + 1; }

Code for left (single) rotation /* Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. * Update heights, then set new root. */ void rotateWithLeftChild( AvlNode * & k2 ) { AvlNode *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; k2 = k1; }

Single Rotation Fails to fix Case 2&3  Single rotation fails to fix case 2&3  Take case 2 as an example (case 3 is a symmetry to it )  The problem is subtree Y is too deep  Single rotation doesn’t make it any less deep Single rotation result Case 2: violation in k2 because of insertion in subtree Y

Code for double rotation void doubleWithLeftChild( AvlNode * & k3 ) { rotateWithRightChild( k3->left ); rotateWithLeftChild( k3 ); }

Summary l AVL trees with n nodes will have height at most 1.5 log 2 n l To implement AVL tree, we use an extra field at each node that stores the height of the subtree rooted at the node. l To insert a key, at most a constant number of pointer changes need to be performed. This is done by a single rotation or a double rotation at the pivot node. l All dictionary operations can be performed in O(log n) time in the WORST-CASE using AVL trees.