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,

Slides:



Advertisements
Similar presentations
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.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
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.
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.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
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.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Trees Basic concepts of trees Implementation of trees Traversals of trees Binary trees Binary search trees AVL trees B-trees.
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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 :
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Trees CS 202 – Fundamental Structures of Computer Science II Bilkent.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
AVL Tree. Ordinary Binary Search Tree Searching time is O(height)Searching time is O(height)  log 2 n   height  n – 1  log 2 n   height  n – 1.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
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 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.
1 CMSC 341 Introduction to Trees Textbook sections:
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
Data Structures for Java William H. Ford William R. Topp
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
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
Dissection of AVL tree operations
AVL Tree Mohammad Asad Abbasi Lecture 12
CSE 373 Data Structures Lecture 7
AVL Trees CENG 213 Data Structures.
Trees.
AVL Trees: AVL Trees: Balanced binary search tree
Trees CSE 373 Data Structures.
Representation of an AVL Node
CE 221 Data Structures and Algorithms
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Tree.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
Trees CSE 373 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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, we let its height == -1. The height of an AVL tree is log N –The exact figure is 1.44log(N+2)-.328 The height difference constraint leads to: –The minimal number of nodes for a tree of height h, s(h) = s(h-1)+s(h-2)+1 When h=0, s(h) =1 When h=1, s(h) =2 –This is similar to fibonacci. 2

AVL Non-AVL

Various functions All functions are O(log(N)), except insert and remove -> these may destroy the properties of AVL. Inserting a new node here destroys AVL

Fix insert+ remove with rotation insert –Only nodes on the path from the insertion point to the root have a chance of height change. –If we follow up from the inserted node, we will eventually find the deepest node that loses its AVL property. We will fix such node. There can only be 4 types of such node. 5

The 4 types: Solve by single rotation Solve by double rotation 6

Single rotation after insert a b X Y Z Pull up a b X YZ Move the attachment place to the problem node. If it is fixed now, no further rotation is needed. 7

example This node loses AVL property

Example 2 Starting from nothing, we insert 3,2,1 loses AVL To be continued rotate 9

Then we insert 4, loses AVL To be continued

Then we insert loses AVL rotate To be continued 11

Then we insert loses AVL rotate

Double rotation When Single rotation is useless a b Y X Z a b Y X Z Y does not change level 13

a b Y X Z c Insertion takes place. There must be something in. a b X Z Y1Y2 Loses AVL 14

The first rotation c a b X Z Y1Y2 c a b X Z Y1 Y2 15

the second rotation c a b X Z Y1 Y2 c b X a Z Y1 Y2 16

We do not know whether Y1 or Y2 is the cause. But it does not matter because in the end both of them reduce in level. It is like letting c release Y1,Y2 and carry a and b instead. c a b X Z Y1Y2 c a b X Z Y1Y2 Y1, Y2 find places to attach 17

example Insert 16,15: loses AVL when inserting Loses AVL rotate

Insert Loses AVL Must jump rotate 19

Insert 13 Loses AVL Two right moves: single rotation rotate 20

Insert 12: this is an obvious single rotation Loses AVL rotate 21

Insert 11: another single rotation Loses AVL rotate

Insert 10: another single rotation Loses AVL rotate

Insert 8 Insert 9: simple double rotation Loses AVL

1. class AvlNode 2. { 3. // Constructors 4. AvlNode( Comparable theElement ) 5. { 6. this( theElement, null, null ); 7. } 8. AvlNode( Comparable theElement, AvlNode lt, AvlNode rt ) 9. { 10. element = theElement; 11. left = lt; 12. right = rt; 13. height = 0; 14. } 15. // Friendly data; accessible by other package routines 16. Comparable element; // The data in the node 17. AvlNode left; // Left child 18. AvlNode right; // Right child 19. int height; // Height 20. } There will be a method that returns height. So we do not have to worry if the node we are looking at is null. 25

1. public class AvlTree 2. { private AvlNode root; 3. /** 4. * Construct the tree. 5. */ 6. public AvlTree( ) 7. { 8. root = null; 9. } 10. /** 11. * Insert into the tree; duplicates are ignored. 12. x the item to insert. 13. */ 14. public void insert( Comparable x ) 15. { 16. root = insert( x, root ); 17. } 26

18. /** 19. * Find the smallest item in the tree. 20. smallest item or null if empty. 21. */ 22. public Comparable findMin( ) 23. { 24. return elementAt( findMin( root ) ); 25. } 26. /** 27. * Find the largest item in the tree. 28. the largest item of null if empty. 29. */ 30. public Comparable findMax( ) 31. { 32. return elementAt( findMax( root ) ); 33. } Same as binary search Tree 27

1. /** 2. * Return the height of node t, or -1, if null. 3. */ 4. private static int height( AvlNode t ) 5. { 6. return t == null ? -1 : t.height; 7. } 8. /** 9. * Return maximum of lhs and rhs. 10. */ 11. private static int max( int lhs, int rhs ) 12. { 13. return lhs > rhs ? lhs : rhs; 14. } Helper functions 28

1. /** 2. * Internal method to insert into a subtree. 3. x the item to insert. 4. t the node that roots the tree. 5. the new root. 6. */ 7. private AvlNode insert( Comparable x, AvlNode t ) 8. { 9. if( t == null ) 10. t = new AvlNode( x, null, null ); 11. else if( x.compareTo( t.element ) < 0 ) 12. { 13. t.left = insert( x, t.left ); 14. if( height( t.left ) - height( t.right ) == 2 ) 15. if( x.compareTo( t.left.element ) < 0 ) 16. t = rotateWithLeftChild( t ); 17. else 18. t = doubleWithLeftChild( t ); 19. } 20. Insert If = =2 is true, x is below. t 29

21. else if( x.compareTo( t.element ) > 0 ) 22. { 23. t.right = insert( x, t.right ); 24. if( height( t.right ) - height( t.left ) == 2 ) 25. if( x.compareTo( t.right.element ) > 0 ) 26. t = rotateWithRightChild( t ); 27. else 28. t = doubleWithRightChild( t ); 29. } 30. else 31. ; // Duplicate; do nothing 32. t.height = max( height( t.left ), height( t.right ) ) + 1; 33. return t; 34. } t 30

1. /** 2. * Rotate binary tree node with left child. 3. * For AVL trees, this is a single rotation for case * Update heights, then return new root. 5. */ 6. private static AvlNode rotateWithLeftChild( AvlNode k2 ) 7. { 8. AvlNode k1 = k2.left; 9. k2.left = k1.right; 10. k1.right = k2; 11. k2.height = max( height( k2.left ), height( k2.right ) ) + 1; 12. k1.height = max( height( k1.left ), k2.height ) + 1; 13. return k1; 14. } k2 k1 k2 k1 31

1. /** 2. * Rotate binary tree node with right child. 3. * For AVL trees, this is a single rotation for case * Update heights, then return new root. 5. */ 6. private static AvlNode rotateWithRightChild( AvlNode k1 ) 7. { 8. AvlNode k2 = k1.right; 9. k1.right = k2.left; 10. k2.left = k1; 11. k1.height = max( height( k1.left ), height( k1.right ) ) + 1; 12. k2.height = max( height( k2.right ), k1.height ) + 1; 13. return k2; 14. } k1 k2 k1 k2 32

1. /** 2. * Double rotate binary tree node: first left child 3. * with its right child; then node k3 with new left child. 4. * For AVL trees, this is a double rotation for case * Update heights, then return new root. 6. */ 7. private static AvlNode doubleWithLeftChild( AvlNode k3 ) 8. { 9. k3.left = rotateWithRightChild( k3.left ); 10. return rotateWithLeftChild( k3 ); 11. } k2 k1 X Y1Y2 k3 Z k2 k3 k1 X Z Y1Y2 k2 k1 X Y1 Y2 k3 Z 33

1. /** 2. * Double rotate binary tree node: first right child 3. * with its left child; then node k1 with new right child. 4. * For AVL trees, this is a double rotation for case * Update heights, then return new root. 6. */ 7. private static AvlNode doubleWithRightChild( AvlNode k1 ) 8. { 9. k1.right = rotateWithLeftChild( k1.right ); 10. return rotateWithRightChild( k1 ); 11. } k2 k1 k3 X Z Y1Y2 k2 k3 X Y1 Y2 k1 Z k2 k3 X Y1 Y2 k1 Z 34

Remove a node from AVL First, do a normal remove Then rebalance, starting from the node just above the removed node up to the root. We need to rebalance every node because balancing a child can make its parent loses AVL. 35

Loses AVL 36

private AVLNode basicRemove( Comparable x, AVLNode t ) { if( t == null ) return t; // Item not found; do nothing if( x.compareTo( t.element ) < 0 ) t.left = basicRemove( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = basicRemove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = basicRemove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; t.setHeight(); return t; } This is the same as BinaryNode’s remove() 37

private AVLNode remove(Comparable x, AVLNode r){ r = basicRemove(x,r); r = rebalance(r); return r; } public void remove(Comparable x){ root = remove(x, root); } private void setHeight(){// this is in AVLNode height = 1 + Math.max(height(left), height(right)); } private static int height(AVLNode n){ // this is in AVLNode return (n == null ? -1 : n.height); } 38

private AVLNode rebalance(AVLNode r){ if (r == null) return r; int balance = balanceValue(r); if (balance == 2){//left hand side has more nodes than it should have if (balanceValue(r.left) ==-1) r.left = rotateWithRightChild(r.left); r = rotateWithLeftChild(r); } else if (balance == -2){//right hand side has more nodes than it should have if (balanceValue(r.right) ==1) r.right = rotateWithLeftChild(r.right); r = rotateWithRightChild(r); } r.setHeight(); return r; } public int balanceValue(AvlNode t){ return height(t.left) – height(t.right); } 39