Instructor: Lilian de Greef Quarter: Summer 2017

Slides:



Advertisements
Similar presentations
AVL Trees CSE 373 Data Structures Lecture 8. 12/26/03AVL Trees - Lecture 82 Readings Reading ›Section 4.4,
Advertisements

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.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
CSE 326: Data Structures AVL Trees
AVL Trees ITCS6114 Algorithms and Data Structures.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Data Structures AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Instructor: Lilian de Greef Quarter: Summer 2017
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
File Organization and Processing Week 3
Data Structures – LECTURE Balanced trees
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Week 7 - Friday CS221.
April 19th – Avl Operations
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CSE373: Data Structures & Algorithms
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree 27th Mar 2007.
Trees (Chapter 4) Binary Search Trees - Review Definition
Instructor: Lilian de Greef Quarter: Summer 2017
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Monday, April 16, 2018 Announcements… For Today…
TCSS 342, Winter 2006 Lecture Notes
Friday, April 13, 2018 Announcements… For Today…
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CSE 373: Data Structures and Algorithms
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
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
Richard Anderson Spring 2016
Lecture 10: BST and AVL Trees
CSE 373 Data Structures Lecture 8
326 Lecture 9 Henry Kautz Winter Quarter 2002
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
Presentation transcript:

Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 10: AVL Trees Instructor: Lilian de Greef Quarter: Summer 2017

Today Announcements BSTs continued (this time, bringing buildTree Balance Conditions AVL Trees Tree rotations

Announcements Reminder: homework 3 due Friday Homework 2 grades should come out today Section Will especially go over material from today (it’s especially tricky) TAs can go over some of the tougher hw2 questions in section if you want/ask

Back to Binary Search Trees

buildTree for BST Let’s consider buildTree (insert values starting from an empty tree) Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST If inserted in given order, what is the tree? What big-O runtime for buildTree on this sorted input? Is inserting in the reverse order any better?

buildTree for BST Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them median first, then left median, right median, etc. 5, 3, 7, 2, 1, 4, 8, 6, 9 What tree does that give us? What big-O runtime?

Balancing Binary Search Trees

BST: Efficiency of Operations? Problem: Worst-case running time: find, insert, delete buildTree

How can we make a BST efficient? Observation Solution: Require a Balance Condition that When we build the tree, make sure it’s balanced. BUT…Balancing a tree only at build time is insufficient. We also need to also keep the tree balanced as we perform operations.

Potential Balance Conditions Left and right subtrees

Potential Balance Conditions Left and right subtrees

Potential Balance Conditions Left and right subtrees

AVL Tree (Bonus material: etymology) Invented by Georgy Adelson-Velsky and Evgenii Landis in 1962

The AVL Tree Data Structure An AVL tree is a self-balancing binary search tree. Structural properties Binary tree property (same as BST) Order property (same as for BST) Balance condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right) Result: Worst-case depth is

Example #1: Is this an AVL Tree? Balance Condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right) 11 1 8 4 6 10 12 7

Example #2: Is this an AVL Tree? 3 11 7 1 8 4 6 2 5 Balance Condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right)

AVL Trees Good News: Because height of AVL tree is O(log(n)), then find But as we insert and delete elements, we need to:

AVL Trees 3 10 2 2 5 20 1 1 2 9 15 30 7 17

AVL tree operations AVL find: AVL insert: AVL delete: Same as usual BST find AVL insert: AVL delete: The “easy way” is lazy deletion Otherwise, do the deletion and then check for several imbalance cases (we will skip this)

First insert example Insert(6) Insert(3) Insert(1) Third insertion What’s the only way to fix it?

Fix: Apply “Single Rotation” Single rotation: The basic operation we’ll use to rebalance Move child of unbalanced node into parent position Parent becomes the “other” child (always okay in a BST!) Other subtrees move in only way BST allows (we’ll see in generalized example) AVL Property violated at node 6 3 6 3 1 6 1

Tree Rotations: Generalized

Generalizing our examples… 30 9 2 23 5 12 7 10 18 4 1

Generalizing our examples… 30 9 2 23 5 12 7 10 18 4 1

Generalizing our examples… 12 5 E A C

Generalizing our examples… d 12 b E A C

Generalized Single Rotation C A b d

Generalized Single Rotation b A d C E

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Single Rotations (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

AVL Tree insert (more specific): Insert the new node as in our generic BST (a new leaf) For each node on the path from the root to the new leaf, the insertion may (or may not) have changed the node’s height So after insertion in a subtree,

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Case #1: E C A b d a’ (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Example #2 for left-left case: insert(16) 10 4 22 8 15 3 6 19 17 20 24 16

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Case #2: (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Example for right-right case: insert(26) 10 4 22 8 15 6 19 23 25 26 3 24 10 4 22 8 15 6 19 23 25 26 3 24

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Case #3: (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) A Better Look at Case #3: (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Case #3: Right-Left Case (after one rotation) right rotate A way to remember it: Move d to grandparent’s position. Put everything else in their only legal positions for a BST. (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Practice time! Example of Case #4 50 10 80 30 60 Starting with this AVL tree: Which of the following is the updated AVL tree after inserting 42? D) A) B) C) 10 30 42 80 50 60 42 10 30 80 60 50 10 30 50 80 42 60 10 30 50 80 42 60

(Extra space for your scratch-work)

Practice time! Example of Case #4 50 10 80 30 60 Starting with this AVL tree: Which of the following is the updated AVL tree after inserting 42? What’s the name of this case? What rotations did we do?

Insert, summarized Insert as in our generic BST Check back up path for imbalance, which will be 1 of 4 cases: Node’s left-left grandchild is too tall Node’s left-right grandchild is too tall Node’s right-left grandchild is too tall Node’s right-right grandchild is too tall Only occurs because After the appropriate single or double rotation, the smallest-unbalanced subtree has the same height as before the insertion So all ancestors are now balanced

AVL Tree Efficiency Worst-case complexity of find: Worst-case complexity of insert: Worst-case complexity of buildTree: Takes some more rotation action to handle delete…

Pros and Cons of AVL Trees Arguments for AVL trees: All operations logarithmic worst-case because trees are always balanced Height balancing adds no more than a constant factor to the speed of insert and delete Arguments against AVL trees: Difficult to program & debug [but done once in a library!] More space for height field Asymptotically faster but rebalancing takes a little time If amortized logarithmic time is enough, use splay trees (also in the text, not covered in this class)

AVL Tree Rotation Cheat-Sheet (Just two of the four cases)

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Single Rotations (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Case #2: Left-Left Case left rotate (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Case #3: Right-Left Case (after two rotations) left rotate right rotate A way to remember it: Move d to grandparent’s position. Put everything else in their only legal positions for a BST. (Figures by Melissa O’Neill, reprinted with her permission to Lilian)