1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section 4.1 - 4.4 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms
Advertisements

AVL Trees binary tree for every node x, define its balance factor
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.
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.
1 AVL Trees Drozdek Section pages
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Trees and Red-Black Trees Gordon College Prof. Brinton.
Red Black Trees Colored Nodes Definition Binary search tree.
CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
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. 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.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
AVL Trees ITCS6114 Algorithms and Data Structures.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Binary Search Trees CSE 331 Section 2 James Daly.
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.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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)
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
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.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
© 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.
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 (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.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AA Trees.
Balanced Binary Search Trees
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.
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.
Chapter 29 AVL Trees.
Cinda Heeren / Geoffrey Tien
Red-Black Trees Bottom-Up Deletion.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Tree A Balanced Binary Search Tree
CSE 373 AVL trees read: Weiss Ch. 4, section
Red-Black Trees Bottom-Up Deletion.
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Dynamic Dictionaries Primary Operations: Additional operations:
Red-Black Trees Bottom-Up Deletion.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
Presentation transcript:

1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp © University of Washington, all rights reserved.

2 Right rotation 1.Detach left child (11)'s right subtree (27) (don't lose it!) 2.Consider left child (11) be the new parent. 3.Attach old parent (43) onto right of new parent (11). 4.Attach new parent (11)'s old right subtree (27) as left subtree of old parent (43)

3 Left rotation 1.Detach right child (65)'s left subtree (51) (don't lose it!) 2.Consider right child (65) be the new parent. 3.Attach old parent (43) onto left of new parent (65). 4.Attach new parent (65)'s old left subtree (51) as right subtree of old parent (43)

4 Problem cases A single right rotation does not fix Case 2 (LR).  (Similarly, a single left rotation does not fix Case 3 (RL).)

5 Left-right double rotation left-right double rotation:(fixes Case 2 (LR))  1) left-rotate k 3 's left child... reduces Case 2 into Case 1  2) right-rotate k 3 to fix Case 1

6 Left-right rotation steps 1.Left-rotate the overall parent's left child (11). This reduces Case 2 (LR) to Case 1 (LL). 2.Right-rotate the overall parent (43). This repairs Case 1 to be balanced

7 Left-right rotation example What is the balance factor of k 1, k 2, k 3 before and after rotating?

8 Right-left double rotation right-left double rotation:(fixes Case 3 (RL))  1) right-rotate k 1 's right child... reduces Case 3 into Case 4  2) left-rotate k 1 to fix Case 4

9 Right-left rotation steps 1.Right-rotate the overall parent's right child (11). This reduces Case 3 (RL) to Case 4 (RR). 2.Right-rotate the overall parent (43). This repairs Case 4 to be balanced

10 AVL add example Draw the AVL tree that would result if the following numbers were added in this order to an initially empty tree:  20, 45, 90, 70, 10, 40, 35, 30, 99, 60, 50,

11 Implementing add Perform normal BST add. But as recursive calls return, update each node's height from new leaf back up to root.  If a node's balance factor becomes +/- 2, rotate to rebalance it. How do you know which of the four Cases you are in?  Current node BF < -1  Case 1 (LL) or 2 (LR). look at current node's left child BF.  left child BF < 0  Case 1(fix with R rotation)  left child BF > 0  Case 2(fix with LR rotations)  Current node BF > 1  Case 3 (RL) or 4 (RR). look at current node's right child BF.  right child BF < 0  Case 3(fix with RL rotations)  right child BF > 0  Case 4(fix with L rotation)

12 AVL remove Removing from an AVL tree can also unbalance the tree.  Similar cases as with adding: LL, LR, RL, RR  Can be handled with the same remedies: rotate R, LR, RL, L set.remove(8);

13 Remove extra cases AVL remove has 2 more cases beyond the 4 from adding:  In these cases, the offending subtree has a balance factor of 0. (The cause of imbalance is both LL and LR relative to k 1 below.)  When an add makes a node imbalanced, the child on the imbalanced side always has a balance factor of either -1 or 1. k1k1 C A k2k2 B k1k1 C A k2k2 B After removing from subtree C:

14 Labeling the extra cases Let's label these two new cases of remove imbalance:  Case 5: Problem is in both the LL and LR subtrees of the parent.  Case 6: Problem is in both the RL and RR subtrees of the parent. k1k1 C A k2k2 B Case 5 (After removing from subtree C) k1k1 A B k2k2 C Case 6 (After removing from subtree A)

15 Fixing remove cases Each of these new cases can be fixed through a single rotation:  To fix Case 5, we right rotate (shown below)  To fix Case 6, we left rotate (symmetric case) k1k1 C A k2k2 B Right rotate to fix imbalance: k1k1 C A k2k2 B Case 5 (After removing from subtree C)

16 Implementing remove Perform normal BST remove. But as recursive calls return, update each node's height from new leaf back up to root.  If a node's balance factor becomes +/- 2, rotate to rebalance it.  Current node BF < -1  Case 1 (LL) or 2 (LR) or 5 (L-both). look at current node's left child BF.  left child BF < 0  Case 1(fix with R rotation)  left child BF > 0  Case 2(fix with LR rotations)  left child BF = 0  Case 5(fix with R rotation)  Current node BF > 1  Case 3 (RL) or 4 (RR) or 6 (R-both). look at current node's right child BF.  right child BF < 0  Case 3(fix with RL rotations)  right child BF > 0  Case 4(fix with L rotation)  right child BF = 0  Case 6(fix with L rotation)

17 AVL remove example Suppose we start with the AVL tree below.  Draw the AVL tree that would result if the following numbers were removed in this order from the tree: 10, 30, 40, 35, 70, 90,

18 How efficient is AVL? An AVL tree has the same general operations as a BST, with rebalancing added to the add/remove operations.  How much time does it take to rebalance the tree? How much time does it take to rebalance one node? How many nodes at most will we need to rebalance per add/remove?  add: O(log N) to walk down the tree and add the element; O(log N) number of nodes' heights to update; O(1) single node may need to be rotated. O(log N) overall.  contains: Unmodified. O(log N).  remove: O(log N) to walk down the tree and remove the element; O(log N) number of nodes' heights to update; O(1) single node may need to be rotated. O(log N) overall.

19 Other interesting trees splay tree: Rotates each element you access to the top/root  very efficient when that element is accessed again (happens a lot)  easy to implement and does not need height field in each node

20 Other interesting trees red-black tree: Gives each node a "color" of red or black.  Root is black. Root's direct children are red.  If a node is red, its children must all be black.  Every path downward from a node to the bottom must contain the same number of "black" nodes.