CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
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.
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
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.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
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.
Data Structures AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CO4301 – Advanced Games Development Week 11 AVL Trees
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.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
CSE373: Data Structures & Algorithms
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
Introduction Applications Balance Factor Rotations Deletion Example
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
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.
AVL Tree A Balanced Binary Search Tree
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Introduction to Hash Tables
Data Structures and Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
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
AVL Trees Lab 11: AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions AVL Trees
CSE 373: Data Structures and Algorithms
Sorting and recurrence analysis techniques
CSE 373: Data Structures and Algorithms
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CSE 373: Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
Richard Anderson Spring 2016
Lecture 10: BST and AVL Trees
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CSE 373: Data Structures and Algorithms AVL Trees Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Outline So far Today List Dictionaries Add and remove operations on dictionaries implemented with arrays or lists are O(n) Trees, BSTs in particular, offer speed up because of their branching factors BSTs are in the average case, but not in the worse case Today Can we do better? Can we adapt our BST so we never get the worst case Insert() Find() Remove() Average case O(log n) Worst case O(n) CSE 373 AU 18 – Shri mare

Review: Worksheets CSE 373 AU 18 – Shri mare

Balanced BST observation BST: the shallower the better! For a BST with n nodes inserted in arbitrary order – Average height is O(log n) – Worst case height is O(n) Solution: Require a Balance Condition that Simple cases such as inserting in key order lead to the worst-case scenario 1. ensures depth is always O(log n) – strong enough! 2. is easy to maintain – not strong enough! CSE 332 WI 18 – Ruth anderson

AVL trees: Balanced BSTs AVL Trees must satisfy the following properties: binary trees: every node must have between 0 and 2 children binary search tree (BST property): for every node, all keys in the left subtree must be smaller and all keys in the right subtree must be larger than the root node Balanced (AVL property): for every node, there can be no more than a difference of 1 in the height of the left subtree from the right. Math.abs(height(left subtree) – height(right subtree)) ≤ 1 AVL stands for Adelson-Velsky and Landis (the inventors of the data structure) The AVL property: 1. ensures depth is always O(log n) – Yes! 2. is easy to maintain – Yes! (using single and double rotations) CSE 373 AU 18 – Shri mare

Potential balance conditions (1) CSE 332 WI 18 – Ruth anderson

Potential balance conditions (1) CSE 332 WI 18 – Ruth anderson

Potential balance conditions (2) CSE 332 WI 18 – Ruth anderson

Potential balance conditions (2) CSE 332 WI 18 – Ruth anderson

AVL balance condition AVL condition: For every node, the height of its left subtree and right subtree differ by at most 1. balance(node) = Math.abs( height(node.left) – height(node.right) ) AVL condition: for every node, balance(node) ≤ 1 CSE 373 AU 18 – Shri mare

Worksheet (Q9) CSE 373 AU 18 – Shri mare

Insertion What happens if when we do an insert(3), we break the AVL condition? 1 1 2 3 2 3 CSE 332 SU 18 – Robbie weber

Left Rotation y x x z y z A A B B C D C D Rest of the tree BALANCED Right subtree is 1 longer UNBALANCED Right subtree is 2 longer y x x z y A A B z B C D C D CSE 332 SU 18 – Robbie weber

Tree Rotations: Right rotation C B Y Z A W X CSE 373 SU 18 – Ben Jones

It Gets More Complicated There’s a “kink” in the tree where the insertion happened. 1 1 3 2 1 2 3 3 2 Now do a left rotation. Can’t do a left rotation Do a “right” rotation around 3 first. CSE 332 SU 18 – Robbie weber

Right Left Rotation y x x z z y A D B A C D C B Rest of the tree BALANCED Right subtree is 1 longer UNBALANCED Right subtree is 2 longer y x Left subtree is 1 longer x z z A A B D y C D C B CSE 332 SU 18 – Robbie weber

Four cases to consider y x z B C A D Insert location Solution Left subtree of left child of y Single right rotation Right subtree of left child of y Double (left-right) rotation Left subtree of right child of y Double (right-left) rotation Right subtree of right child of y Single left rotation y x z A B C D CSE 332 SU 18 – Robbie weber

Four cases to consider y x z A B C D CSE 373 AU 18 – Shri mare

AVL Example: 8,9,10,12,11 8 9 10 This is the line case. CSE 373 SU 18 – Ben Jones

AVL Example: 8,9,10,12,11 8 9 10 Do a left rotation to correct CSE 373 SU 18 – Ben Jones

AVL Example: 8,9,10,12,11 9 8 11 10 12 This is the kink case CSE 373 SU 18 – Ben Jones

AVL Example: 8,9,10,12,11 9 8 10 12 11 Do a right rotation to get into a line case CSE 373 SU 18 – Ben Jones

AVL Example: 8,9,10,12,11 9 8 10 11 12 Now do a left rotation to re-balance the line! CSE 373 SU 18 – Ben Jones

Worksheet (Q10A) CSE 373 AU 18 – Shri mare

Worksheet (Q10B) CSE 373 AU 18 – Shri mare

How Long Does Rebalancing Take? Assume we store in each node the height of its subtree. How do we find an unbalanced node? How many rotations might we have to do? CSE 332 SU 18 – Robbie weber

How Long Does Rebalancing Take? Assume we store in each node the height of its subtree. How do we find an unbalanced node? Just go back up the tree from where we inserted. How many rotations might we have to do? Just a single or double rotation on the lowest unbalanced node. A rotation will cause the subtree rooted where the rotation happens to have the same height it had before insertion. CSE 332 SU 18 – Robbie weber

Lots of cool Self-Balancing BSTs out there! Popular self-balancing BSTs include: AVL tree Splay tree 2-3 tree AA tree Red-black tree Scapegoat tree Treap (Not covered in this class, but several are in the textbook and all of them are online!) (From https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations) CSE 373 SU 17 – lilian de Greef