CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
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.
1 CSE 326: Data Structures Part Four: Trees Henry Kautz Autumn 2002.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
CSE 326: Data Structures AVL Trees
CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
AVL Trees ITCS6114 Algorithms and Data Structures.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Data Structures AVL Trees.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
AVL Tree: Balanced Binary Search Tree 9.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
UNIT III TREES.
Binary Search Trees.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Wednesday, April 18, 2018 Announcements… For Today…
CS202 - Fundamental Structures of Computer Science II
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
AVL Trees Lab 11: AVL Trees.
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
Lecture 9: Self Balancing Trees
CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters
CSE 326: Data Structures Lecture #8 Balanced Dendrology
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
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
Presentation transcript:

CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1

Today’s Outline Addressing one of our problems Single and Double Rotations AVL Tree Implementation 2

Beauty is Only  (log n) Deep Binary Search Trees are fast if they’re shallow: –perfectly complete –perfectly complete except the one level fringe (like a heap) –anything else? What matters here? Problems occur when one subtree is much taller than the other! 3

Balance –height(left subtree) - height(right subtree) –zero everywhere  perfectly balanced –small everywhere  balanced enough t 5 7 Balance between -1 and 1 everywhere  maximum height of ~1.44 lg n 4

AVL Tree Dictionary Data Structure Binary search tree properties –binary tree invariant –search tree invariant Balance invariant –balance of every node is: -1  b  1 –result: depth is  (log n) 15 5 Note that (statically… ignoring how it updates) an AVL tree is a BST.

Testing the Balance Property NULL s have height -1 6 FIRST calculate heights THEN calculate balances

An AVL Tree data height children 7

Today’s Outline Addressing one of our problems Single and Double Rotations AVL Tree Implementation 8

But, How Do We Stay Balanced? 9 Need: three volunteers proud of their very diverse height.

Beautiful Balance (SIMPLEST version) Insert(middle) Insert(small) Insert(tall) But… BSTs are under-constrained in unfortunate ways; ours may not look like this.

Bad Case #1 (SIMPLEST version) Insert(small) Insert(middle) Insert(tall) How do we fix the bad case? How do we transition among different possible trees?

So you say you want a revolution? Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges. What do we do? Well, first… 12 k Z m Y X

Well.. what do we know? Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges. What’s everything we know about nodes k and m and subtrees X, Y, and Z? 13 k Z m Y X

A real solution? Scenario: k is one of the 1%; we want a revolution to depose k and give m root privileges. Now, how can we make m the root? 14 k Z m Y X

Single Rotation (SIMPLEST version)

General Single Rotation After rotation, subtree’s height same as before insert! Height of all ancestors unchanged. a X Y b Z a XY b Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h + 1  An insert made this BAD! 16 Why couldn’t the bad insert be in X? Why does it matter that ancestors stay the same?

Bad Case #2 (SIMPLEST version) Insert(small) Insert(tall) Insert(middle)

Double Rotation (SIMPLEST version)

General Double Rotation Height of subtree still the same as it was before insert! Height of all ancestors unchanged. a Z b W c XY a Z b W c XY h h - 1? h - 1 h + 2 h + 1 h - 1 h h + 1 h h - 1? 19

Today’s Outline Addressing one of our problems Single and Double Rotations AVL Tree Implementation 20

Insert Algorithm Find spot for value Hang new node Search back up for imbalance If there is an imbalance: case #1: Perform single rotation and exit case #2: Perform double rotation and exit Mirrored cases also possible 21

Easy Insert Insert(3)

Hard Insert (Bad Case #1) Insert(33)

Single Rotation

Hard Insert (Bad Case #2) Insert(18)

Single Rotation (oops!)

Double Rotation (Step #1) Look familiar?

Double Rotation (Step #2)

AVL Algorithm Revisited Recursive 1. Search downward for spot 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate Iterative 1. Search downward for spot, stacking parent nodes 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and exit 29

Single Rotation Code void RotateLeft(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(height(root->right), height(root->left)) + 1; temp->height = max(height(temp->right), height(temp->left) + 1; root = temp; } X Y Z root temp (The “height” function returns -1 for a NULL subtree or the “height” field of a non-NULL subtree.) 30

Double Rotation Code void DoubleRotateLeft(Node *& root) { RotateRight(root->right); RotateLeft(root); } a Z b W c XY a Z c b X Y W First Rotation 31

Double Rotation Completed a Z c b X Y W a Z c b X Y W First Rotation Second Rotation 32

AVL Automatically Virtually Leveled Architecture for inVisible Leveling (the “in” is inVisible) All Very Low Articulating Various Lines Amortizing? Very Lousy! Absolut Vodka Logarithms Amazingly Vexing Letters Adelson-Velskii Landis 33

To Do Posted readings on priority queues and sorts, but de-emphasizing heaps 34

Coming Up Quick Jump Back to Priority Queue ADT –A very familiar data structure gives us O(lg n) performance! On to sorts Then forward again! 35