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.

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

Rotating Nodes How to balance a node that has become unbalanced after the addition of one new node.
AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
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.
AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
Course: Programming II - Abstract Data Types AVL TreesSlide Number 1 AVL Trees Performance of Binary Search Trees (BST) depends on how well balanced the.
CS202 - Fundamental Structures of Computer Science II
AVL Tree Iris Jiaonghong Shi. AVL tree operations AVL find : – Same as BST find AVL insert : – First BST insert, then check balance and potentially.
Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
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:
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):
1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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.
Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Trees. Knowing More How many nodes? – Determine on demand.
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 Binary Search Trees (BSTs) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from.
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.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
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.
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Tree: Balanced Binary Search Tree 9.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AVL Trees CSE, POSTECH.
Balancing Binary Search Trees, Rotations
Introduction Applications Balance Factor Rotations Deletion Example
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
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
Binary Search Trees (BST)
CS202 - Fundamental Structures of Computer Science II
AVL Search Trees Inserting in an AVL tree Insertion implementation
AVL Search Trees Inserting in an AVL tree Insertion implementation
ITCS6114 Algorithms and Data Structures
Tree Balancing: AVL Trees
CS202 - Fundamental Structures of Computer Science II
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
AVL Search Trees Inserting in an AVL tree Insertion implementation
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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

What is an AVL Tree? An AVL tree is a binary search tree with a height balance property: For each node v, the heights of the subtrees of v differ by at most 1. A subtree of an AVL tree is also an AVL tree. For each node of an AVL tree: Balance factor = height(right subtree) - height(left subtree) An AVL node can have a balance factor of ……………………… Determine whether the trees below are AVL trees or not

AVL Trees Implementation public class AVLTree extends BinarySearchTree{ protected int height; public AVLTree(){ height = -1;} public int getHeight(){ return height } ; protected void adjustHeight(){ if(isEmpty()) height = -1; else height = 1 + Math.max(left.getHeight(), right.getHeight()); } protected int getBalanceFactor(){ if( isEmpty()) return 0; else return right.getHeight() - left.getHeight(); } //... }

Why AVL Trees? Insertion or deletion in an ordinary Binary Search Tree can cause large imbalances. In the worst case searching an imbalanced Binary Search Tree is O( ) An AVL tree is rebalanced after each insertion or deletion. The height-balance property ensures that the height of an AVL tree with n nodes is O(log n). Searching, insertion, and deletion are all O(log n).

What is a Rotation? A rotation is a process of switching children and parents among two or three adjacent nodes to restore balance to a tree. An insertion or deletion may cause an imbalance in an AVL tree. The deepest node, which is an ancestor of a deleted or an inserted node, and whose balance factor has changed to -2 or +2 requires rotation to rebalance the tree Insert 35 Deepest unbalanced node

What is a Rotation? (contd.) There are two kinds of single rotation: Right Rotation. Left Rotation. A double right-left rotation is a right rotation followed by a left rotation. A double left-right rotation is a left rotation followed by a right rotation rotation

Single Right Rotation Single right rotation: The left child x of a node y becomes y's parent. y becomes the right child of x. The right child T 2 of x, if any, becomes the left child of y. x T1T1 T2T2 y T3T3 deepest unbalanced node a right rotation of x about y y T3T3 T2T2 x T1T1 Note: The pivot of the rotation is the deepest unbalanced node

Single Left Rotation Single left rotation: The right child y of a node x becomes x's parent. x becomes the left child of y. The left child T 2 of y, if any, becomes the right child of x. y T3T3 T2T2 x T1T1 deepest unbalanced node a left rotation of y about x x T1T1 T2T2 y T3T3 Note: The pivot of the rotation is the deepest unbalanced node

Single Right Rotation Implementation protected void rightRotate(){ if( isEmpty()) throw new InvalidOperationException(); BinaryTree temp = right; right = left; left = right.left; right.left = right.right; right.right = temp; Object tmpObj = key; key = right.key; right.key = tempObj; getRightAVL().adjustHeight(); adjustHeight(); }

Single Right Rotation Implementation (example)

Single Right Rotation Implementation (example) contd

Double Right-Left Rotation z T4T4 T3T3 y T2T2 x T1T1 y T3T3 T2T2 z T4T4 x T1T1 z T4T4 T3T3 x T1T1 y T2T2 right rotation of y about z deepest unbalanced node left rotation of Y about X Note: First pivot is the right child of the deepest unbalanced node; second pivot is the deepest unbalanced node

Double Left-Right Rotation w T2T2 T3T3 v T1T1 x T4T4 v T1T1 T2T2 w T3T3 x T4T4 x T4T4 T3T3 v T1T1 w T2T2 left rotation of w about v deepest unbalanced node left rotation of W about X Note: First pivot is the left child of the deepest unbalanced node; second pivot is the deepest unbalanced node

Double Rotation implementation 1 protected void rotateRightLeft() 2 { 3 if( isEmpty()) 4 throw new InvalidOperationException(); 5 getRightAVL().rotateRight(); 6 rotateLeft(); 7 } 1 protected void rotateLeftRight() 2 { 3 if( isEmpty()) 4 throw new InvalidOperationException(); 5 getLeftAVL().rotateLeft(); 6 rotateRight(); 7 }

BST ordering property after a rotation A rotation does not affect the ordering property of a BST (Binary Search Tree). y T3T3 T2T2 x T1T1 x T1T1 T2T2 y T3T3 a right rotation of x about y BST ordering property requirement: T 1 < x < yT 1 < x < y x < T 2 < y Similar x < T 2 < y x < y < T 3 x < y < T 3 Similarly for a left rotation.

Insertion Insert using a BST insertion algorithm. Rebalance the tree if an imbalance occurs. An imbalance occurs if a node's balance factor changes from -1 to -2 or from+1 to +2. Rebalancing is done at the deepest or lowest unbalanced ancestor of the inserted node. There are three insertion cases: 1.Insertion that does not cause an imbalance. 2.Same side (left-left or right-right) insertion that causes an imbalance. Requires a single rotation to rebalance. 3.Opposite side (left-right or right-left) insertion that causes an imbalance. Requires a double rotation to rebalance.

Insertion: case 1 Example: An insertion that does not cause an imbalance. Insert 14

Insertion: case 2 Case 2a: The lowest node (with a balance factor of -2) had a taller left-subtree and the insertion was on the left-subtree of its left child. Requires single right rotation to rebalance. Insert 3 right rotation, with node 10 as pivot -2

Insertion: case 2 (contd) Case 2b: The lowest node (with a balance factor of +2) had a taller right-subtree and the insertion was on the right-subtree of its right child. Requires single left rotation to rebalance. Insert 45 left rotation, with node 30 as the pivot +2 +1

Insertion: case 3 Case 3a: The lowest node (with a balance factor of -2) had a taller left-subtree and the insertion was on the right-subtree of its left child. Requires a double left-right rotation to rebalance. Insert 7 left rotation, with node 5 as the pivot right rotation, with node 10 as the pivot -2 +1

Insertion: case 3 (contd) Case 3b: The lowest node (with a balance factor of +2) had a taller right-subtree and the insertion was on the left-subtree of its right child. Requires a double right-left rotation to rebalance. Insert 15 right rotation, with node 16 as the pivot left rotation, with node 9 as the pivot +2

AVL Rotation Summary Single right rotation Double left-right rotation Single left rotation Double right-left rotation

Insertion Implementation The insert method of the AVLTree class is: Recall that the insert method of the BinarySearchTree class is: public void insert(Comparable comparable){ if(isEmpty()) attachKey(comparable); else { Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) throw new IllegalArgumentException("duplicate key"); else if (comparable.compareTo(key)<0) getLeftBST().insert(comparable); else getRightBST().insert(comparable); } public void insert(Comparable comparable){ super.insert(comparable); balance(); }

Insertion Implementation (contd) The AVLTree class overrides the attachKey method of the BinarySearchTree class: public void attachKey(Object obj) { if(!isEmpty()) throw new InvalidOperationException(); else { key = obj; left = new AVLTree(); right = new AVLTree(); height = 0; }

Insertion Implementation (contd) protected void balance(){ adjustHeight(); int balanceFactor = getBalanceFactor(); if(balanceFactor == -2){ if(getLeftAVL().getBalanceFactor() < 0) rotateRight(); else rotateLeftRight(); } else if(balanceFactor == 2){ if(getRightAVL().getBalanceFactor() > 0) rotateLeft(); else rotateRightLeft(); }

Deletion Delete by a BST deletion by copying algorithm. Rebalance the tree if an imbalance occurs. There are three deletion cases: 1.Deletion that does not cause an imbalance. 2.Deletion that requires a single rotation to rebalance. 3.Deletion that requires two or more rotations to rebalance. Deletion case 1 example: Delete 14

Deletion: case 2 examples Delete 40 right rotation, with node 35 as the pivot

Deletion: case 2 examples (contd) Delete 32 left rotation, with node 44 as the pivot

Deletion: case 3 examples Delete 40 0 right rotation, with node 35 as the pivot right rotation, with node 30 as the pivot