AVL trees.

Slides:



Advertisements
Similar presentations
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advertisements

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 (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
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.
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.
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
Tirgul 5 Comparators AVL trees. Comparators You already know interface Comparable which is used to compare objects. By implementing the interface, one.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
AVL trees. Today AVL delete and subsequent rotations Testing your knowledge with interactive demos!
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.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Data Structures AVL Trees.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.
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 Tree: Balanced Binary Search Tree 9.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Binary Search Trees Chapter 7 Objectives
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Balancing Binary Search Trees
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
UNIT III TREES.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Binary Search Tree (BST)
Introduction Applications Balance Factor Rotations Deletion Example
CS 201 Data Structures and Algorithms
Red-Black Trees.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
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.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CS223 Advanced Data Structures and Algorithms
AVL Search Tree put(9)
Binary Search Trees Chapter 7 Objectives
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Lecture 9: Self Balancing Trees
AVL Trees (a few more slides)
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CSE 326: Data Structures Lecture #9 AVL II
B-Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

AVL trees

Today AVL delete and subsequent rotations Testing your knowledge with interactive demos!

AVL tree Is a binary search tree Has an additional height constraint: For each node x in the tree, Height(x.left) differs from Height(x.right) by at most 1 I promise: If you satisfy the height constraint, then the height of the tree is O(lg n). (Proof is easy, but no time! =])

AVL tree To be an AVL tree, must always: (1) Be a binary search tree (2) Satisfy the height constraint Suppose we start with an AVL tree, then delete as if we’re in a regular BST. Will the tree be an AVL tree after the delete? (1) It will still be a BST… (2) Will it satisfy the height constraint? (Not covering insert, since you already did in class)

BST Delete breaks an AVL tree 7 7 Delete(9) 4 9 4 3 3 h(left) > h(right)+1 so NOT an AVL tree!

Replacing the height constraint with balance factors Instead of thinking about the heights of nodes, it is helpful to think in terms of balance factors The balance factor bf(x) = h(x.right) – h(x.left) bf(x) values -1, 0, and 1 are allowed If bf(x) < -1 or bf(x) > 1 then tree is NOT AVL

Same example with bf(x), not h(x) 7 7 -1 -2 Delete(9) 4 9 4 -1 -1 3 3 bf < -1 so NOT an AVL tree!

What else can BST Delete break? Balance factors of ancestors… 7 7 -1 -1 Delete(3) 4 9 4 9 -1 -1 3

Need a new Delete algorithm Goal: if tree is AVL before Delete, then tree is AVL after Delete. Step 1: do BST delete. This maintains the BST property, but can cause the balance factors of ancestors to be outdated! Step 2: fix the height constraint and update balance factors. Update any invalid balance factors affected by delete. After updating them, they can be < -1 or > 1. Do rotations to fix any balance factors that are too small or large while maintaining the BST property. Rotations can cause balance factors to be outdated also!

Bad balance factors 2 cases Start with an AVL tree, then do a BST Delete. What bad values can bf(x) take on? Delete can reduce a subtree’s height by 1. So, it might increase or decrease h(x.right) – h(x.left) by 1. So, bf(x) might increase or decrease by 1. This means: if bf(x) = 1 before Delete, it might become 2. BAD. If bf(x) = -1 before Delete, it might become -2. BAD. If bf(x) = 0 before Delete, then it is still -1, 0 or 1. OK. 2 cases

Problematic cases for Delete(a) bf(x) = -2 is just symmetric to bf(x) = 2. So, we just look at bf(x) = 2. x x 2 -2 h h+2 a a (deleted)

Delete(a): 3 subcases for bf(x)=2 Since tree was AVL before, bf(z) = -1, 0 or 1 Case bf(z) = -1 Case bf(z) = 0 Case bf(z) = 1 x x T2 T1 z 2 T3 a x T2 T1 z 2 1 T3 a 2 T1 z -1 h The goal is to teach you NOT to memorize the rotations, but rather to be able to easily reconstruct the cases and the rotations that fix them from scratch. a T2 T3 h h+1 h+1

Fixing case bf(x) = 2, bf(z) = 0 We do a single left rotation Preserves the BST property, and fixes bf(x) = 2 x z 2 -1 T1 z x T3 1 h Right subtree is too damn big, so we rotate x and z to the left. Which of these two nodes have to contain key x? Which one have to contain key z? (MUST preserve inorder traversal) Which order do we have to reattach the subtrees in? (maintain inorder traversal…) Note: height of this whole subtree x is the same before and after… so won’t affect balance factor of any ancestors higher up. Since the tree was an AVL tree before the delete, all other nodes in the tree have balance factors -1, 0 or 1, and we are done. T2 T3 T2 T1 h+1

Fixing case bf(x) = 2, bf(z) = 1 We do a single left rotation (same as last case) Preserves the BST property, and fixes bf(x) = 2 x z 2 T1 z x T3 1 h Right subtree is too damn big, so we again rotate x and z to the left. Which of these two nodes has to contain key x? Which one has to contain key z? (MUST preserve inorder traversal) Which order do we have to reattach the subtrees in? (maintain inorder traversal…) Height of this entire subtree decreases by 1 when we do this rotation. Thus, we might need to change ancestors’ balance factors. This means we have to check ancestors to see if we need to do any rotations. Recurse up the tree! T2 T3 T2 T1 h h+1

Delete(a): bf(x)=2, bf(z)=-1 subcases Case bf(z) = -1: we have 3 subcases. (More details) Case bf(y) = 0 Case bf(y) = -1 Case bf(y) = 1 x T1 z 2 h -1 T3 x x 2 2 T1 z z -1 T1 -1 Link is for symmetric case… y a a y T3 a y T3 -1 1 T21 T22 T21 h-1 T22 T21 T22 h

Double right-left rotation All three subcases of bf(x)=2, bf(z)=-1 simply perform a double right-left rotation. x x y z y x z y z

Delete subcases for bf(x)=2, bf(z)=-1 Case bf(y)=0: double right-left rotation! x y 2 T1 z x z -1 h What are the heights of T3, T21 and T22? What are the keys in each node on the right? What order are the subtrees reattached in? What are the balance factors? T1 T21 T22 y T3 T3 h T21 T22 h

Delete subcases for bf(x)=2, bf(z)=-1 Case bf(y)=-1: double right-left rotation! x y 2 T1 z x z -1 1 h What are the heights of T3, T21 and T22? This is basically the same as the previous case. We place keys in the same places and attach subtrees in the same order. We just have to be a tiny bit careful with balance factors at the end! T1 T21 T22 y T3 T3 -1 h T21 T22 h-1 h

Delete subcases for bf(x)=2, bf(z)=-1 Case bf(y)=1: double right-left rotation! x y 2 T1 z x z -1 -1 h Again, we do the same thing, just being a bit careful with the balance factors. [Go through this quickly!] T1 T21 T22 y T3 T3 1 h T21 T22 h-1 h

Recursively fixing balance factors Idea: start at the node we deleted, fix a problem, then recurse up the tree to the root. At each node x, we update the balance factor: bf(x) := h(bf.right) - h(bf.left). If bf(x) = -2 or +2, we perform a rotation. Then, we update the balance factors of every node that was changed by the rotation. Finally, we recurse one node higher up.

Interactive AVL Deletes Interactive web applet Do interactive deletes (and maybe inserts) and get audience to tell you what balance factors will be, what order things will be attached in, etc.