CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Advertisements

AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
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.
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 CSE 326: Data Structures Part Four: Trees Henry Kautz Autumn 2002.
CSE 326: Data Structures AVL Trees
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
Chapter 4: Trees AVL Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
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.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
(B+-Trees, that is) Steve Wolfman 2014W1
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
Splay Trees and B-Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
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.
CSE 326 Killer Bee-Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001 Where was that root?
CSE 326: Data Structures Lecture #12 Splay It Again, Sam Steve Wolfman Winter Quarter 2000.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Data Structures AVL Trees.
1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.
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.
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 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000.
AVL Tree: Balanced Binary Search Tree 9.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
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
AVL Tree 27th Mar 2007.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
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.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
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
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
Presentation transcript:

CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000

Today’s Outline Double-tailed distributions and the mean AVL trees What AVL Stands For Bonus! (Deletion)

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 branch is much longer than the other!

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 log n

AVL Tree Dictionary Data Structure Binary search tree properties –binary tree property –search tree property Balance property –balance of every node is: -1  b  1 –result: depth is  (log n) 15

Testing the Balance Property NULL s have height -1

An AVL Tree data height children

But, How Do We Stay Balanced? I need: –the smallest person in the class –the tallest person in the class –the averagest (?) person in the class

Beautiful Balance Insert(middle) Insert(small) Insert(tall) 00 1

Bad Case #1 Insert(small) Insert(middle) Insert(tall) 0 1 2

Single Rotation

General Single Rotation Height of subtree same as it was before insert! Height of all ancestors unchanged. So? a X Y b Z a XY b Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h + 1

Bad Case #2 Insert(small) Insert(tall) Insert(middle) 0 1 2

Double Rotation

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?

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

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? 18 0

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

Single Rotation Code void RotateRight(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(root->right->height, root->left->height) + 1; temp->height = max(temp->right->height, temp->left->height) + 1; root = temp; } X Y Z root temp

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

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

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

Bonus: Deletion (Easy Case) Delete(15)

Deletion (Hard Case #1) Delete(12)

Single Rotation on Deletion Something very bad happened!

To Do Finish Project II Read chapter 4 in the book Understand Zasha’s worksheet Prepare for the midterm

Coming Up A bit more AVL trees Splay trees Another worksheet and midterm study guide Second project due (January 31 st ) Midterm (February 4 th )