CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms
Advertisements

AVL Trees binary tree for every node x, define its balance factor
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.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
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 Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
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):
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
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.
AVL Trees ITCS6114 Algorithms and Data Structures.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
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.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
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.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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.
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.
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees binary tree for every node x, define its balance factor
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.
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.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
singleRightRotation 
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Data Structures & Algorithms
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
CSE 373 Data Structures and Algorithms
Lecture 9: Self Balancing Trees
Data Structures Lecture 21 Sohail Aslam.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 12: Trees IV

Problem cases for AVL insert 1. LL Case: insertion into left subtree of node's left child 2. LR Case: insertion into right subtree of node's left child

Problem cases for AVL insert, cont. 3. RL Case: insertion into left subtree of node's right child 4. RR Case: insertion into right subtree of node's right child

Maintaining Balance Maintain balance using rotations The idea: locally reorganize the nodes of an unbalanced subtree until they are balanced, by "rotating" a trio of parent - leftChild – rightChild Maintaining balance will result in searches (contains) that take (log n)

Right rotation to fix Case 1 (LL) right rotation (clockwise): left child becomes parent; original parent demoted to right

Right rotation example

Left rotation to fix Case 4 (RR) left rotation (counter-clockwise): right child becomes parent; original parent demoted to left

Left rotation, steps detach right child (70)'s left subtree (60) (don't lose it!) consider right child (70) be the new parent attach old parent (50) onto left of new parent (70) attach old right child (70)'s old left subtree (60) as right subtree of new left child (50)

Problem: Cases 2, 3 a single right rotation does not fix Case 2! a single left rotation also does not fix Case 3

Left-right rotation for Case 2 left-right double rotation: a left rotation of the left child, followed by a right rotation at the parent

Left-right rotation example

Left-right rotation, steps perform left-rotate on left child perform right-rotate on parent (current node)

Right-left rotation for Case 3 right-left double rotation: a right rotation of the right child, followed by a left rotation at the parent

Right-left rotation, steps perform right-rotate on right child perform left-rotate on parent (current node)

AVL tree practice problem Draw the AVL tree that would result if the following numbers were added in this order to an initially empty tree: 40, 70, 90, 80, 30, -50, 10, 60, 40, -70, 20, 35, 37, 32, 38, 39 Then give the following information about the tree: size height balance factor at each node

Implementing AVL add After normal BST add, update heights from new leaf up towards root If balance factor changes to > +1 or < -1, then use rotation(s) to rebalance Let n be the first unbalanced node found Case 1: n has balance factor -2 and n's left child has balance factor of –1 fixed by performing right-rotation on n Case 2: n has balance factor -2 and n's left child has balance factor of 1 fixed by perform left-rotation on n's left child, then right-rotation on n (left-right double rotation)

AVL add, cont'd Case 3: n has balance factor 2 and n's right child has balance factor of –1 fixed by perform right-rotation on n's right child, then left-rotation on n (right-left double rotation) Case 4: n has balance factor 2 and n's right child has balance factor of 1 fixed by performing left-rotation on n After rebalancing, continue up the tree updating heights What if n's child has balance factor 0? What if another imbalance occurs higher up? If A has balance factor –2, then A’s left child cannot have balance factor 0 A’s left child has balance factor is –1 if and only if new node was added to subtree rooted at A’s leftmost grandchild A’s left child has balance factor is 1 if and only if new node was added to subtree rooted at A’s left child’s right child

AVL add outline public class TrackingStreeSet extends StreeSet { protected StringTreeNode add(StringTreeNode node, String value) { // perform StreeSet add (i.e. regular BST add) // update node's height return node; } ... public class AVLStreeSet extends TrackingStreeSet { // perform TrackingStreeSet add and update node's height // rebalance the node protected StringTreeNode rebalance(StringTreeNode node) { int bf = balanceFactor(node); if (bf < -1) { if (balanceFactor(node.left) < 0) { // case 1 (LL) node = rightRotate(node); } else { // case 2 (LR) node.left = leftRotate(node.left); } else if (bf > 1) { // take care of symmetric cases // case 3 (RL) // case 4 (RR)

Problems for AVL remove removal from AVL tree can also unbalance the tree

Right-left rotation on remove

AVL remove, cont'd perform normal BST remove (with replacement of node to be removed with its successor) update heights from successor node location upwards towards root if balance factor changes to +2 or -2, then use rotation(s) to rebalance remove has the same 4 cases (and fixes) as insert are there any additional cases? After rebalancing, continue up the tree updating heights; must continue checking for imbalances in balance factor, and rebalancing if necessary Are all cases handled?

Additional AVL Remove Cases Two additional cases cause AVL tree to become unbalanced on remove In these cases, a node (e.g., k1 below) violates balance condition after removing from one of its subtrees when its other subtree has a balance factor of 0 these cases do not occur for insertion: when insertion causes a tree to have a balance factor of 2 or -2, the child containing the subtree where the insertion occurred either has a balance factor of -1 or 1 k1 C A k2 B Before removing from subtree C k1 C A k2 B After removing from subtree C height(t) = 1 + max {h_left,h_right}

Fixing AVL Remove Cases Each of these cases can be fixed through a single rotation If remove from right subtree of node creates imbalance and left subtree has balance factor of 0 we right rotate (shown below) If remove from left subtree of node creates imbalance and right subtree has balance factor of 0 we left rotate (symmetric case) k1 C A k2 B After removing from subtree C After right rotate to fix imbalance k2 k1 A height(t) = 1 + max {h_left,h_right} C B