CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Advertisements

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.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CPSC 320: Intermediate Algorithm Design & Analysis Splay Trees (for Amortized Analysis) Steve Wolfman 1.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
Tirgul 5 AVL trees.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
CSC 213 Lecture 9: Red-Black Trees. Announcements Reminder: Daily Quizzes should take 15 minutes Goal is to provide chance to see if you really understand.
CSE 326: Data Structures AVL Trees
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
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!
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black 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
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
Splay Trees Splay trees are binary search trees (BSTs) that:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
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.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
CSE 326: Data Structures Lecture #12 Splay It Again, Sam Steve Wolfman Winter Quarter 2000.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Data Structures AVL Trees.
1 CSE 326: Data Structures Trees. 2 Today: Splay Trees Fast both in worst-case amortized analysis and in practice Are used in the kernel of NT for keep.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000.
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
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE 326: Data Structures Lecture #9 AVL II
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CSE 326: Data Structures Lecture #5 Heaps More
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
Presentation transcript:

CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000

Today’s Outline Extra operations in AVL trees Splaying and Splay Trees

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

Deletion (Hard Case #1) Delete(12)

Single Rotation on Deletion Something very bad happened!

Deletion (Hard Case #2-4) Delete(9)

Double Rotation on Deletion

Deletion with Propagation: Choose Your Own Adventure! We get to choose whether to single or double rotate! If you take the gold and single rotate, flip to Slide 10. If you decide to search further into the scary cavern, continue on to the next slide. If you decide not to rotate at all, jump to Slide

Propagated Double Rotation

Propagated Single Rotation

You Didn’t Rotate Casually walking away from the AVL tree, minding your own business, you fall into a sewer and are eaten by wild Red-Black trees (some people say they grow as big as B-Trees in the sewers!). You die.

AVL Deletion Algorithm Recursive 1. Search downward for node 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate

AVL buildTree Divide & Conquer –Divide the problem into parts –Solve each part recursively –Merge the parts into a general solution How long does divide & conquer take?

BuildTree Example

BuildTree Analysis (Approximate) T(1) = 1 T(n) = 2T(n/2) + 1 T(n) = 2(2T(n/4)+1) + 1 T(n) = 4T(n/4) T(n) = 4(2T(n/8)+1) T(n) = 8T(n/8) T(n) = 2 k T(n/2 k ) + let 2 k = n, log n = k T(n) = nT(1) + T(n) =  (n)

BuildTree Analysis (Exact) Precise Analysis: T(0) = b T(n) = T( ) + T( ) + c By induction on n: T(n) = (b+c)n + b Base case : T(0) = b = (b+c)0 + b Induction step : T(n) = (b+c) + b + (b+c) + b + c = (b+c)n + b QED : T(n) = (b+c)n + b =  (n)

Thinking About AVL Observations +Worst case height of an AVL tree is about 1.44 log n +All operations supported in worst case O(log n) +Only one (single or double) rotation needed on insertion –O(log n) rotations needed on deletion –Height fields must be maintained (or 2-bit balance) ?Coding complexity

Splay Trees Problems with AVL Trees –extra storage/complexity for height fields –ugly delete code Solution: splay trees –blind adjusting version of AVL trees –amortized time for all operations is O(log n) –worst case time is O(n) –insert/find always rotates node to the root!

Idea You’re forced to make a really deep access: Since you’re down there anyway, fix up a lot of deep nodes!

Zig-Zig * * I told you it was a technical term! n Z Y p X g W g W X p Y n Z

Zig-Zag * g X p Y n Z W * This is just a double rotation n Y g W p ZX

Zig p X n Y Z n Z p Y X root

Splaying Example Find(6) zig-zig

Still Splaying 6 zig-zig

Almost There, Stay on Target * zig

Splay Again Find(4) zig-zag

Example Splayed Out zig-zag

To Do Turn in Project II Read chapter 4 in the book Do the new worksheet Prepare for the midterm Finish HW#4 –Just kidding

Coming Up Polish off Splay Trees Talk about Project III Second project due (today) Midterm (Friday)