The AVL Balance Condition. CSE 326: Data Structures. AVL Trees. The AVL Tree Data Structure. Is this an AVL Tree? Height of an AVL Tree

Similar documents
Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST

Priority queues and heaps Professors Clark F. Olson and Carol Zander

CSE 332: Data Structures & Parallelism Lecture 8: AVL Trees. Ruth Anderson Winter 2019

Announcements. Lilian s office hours rescheduled: Fri 2-4pm HW2 out tomorrow, due Thursday, 7/7. CSE373: Data Structures & Algorithms

Wrap up Amortized Analysis; AVL Trees. Riley Porter Winter CSE373: Data Structures & Algorithms

CE 221 Data Structures and Algorithms

CS 234. Module 6. October 16, CS 234 Module 6 ADT Dictionary 1 / 33

CE 221 Data Structures and Algorithms

CS 234. Module 6. October 25, CS 234 Module 6 ADT Dictionary 1 / 22

Recall from Last Time: AVL Trees

AVL Trees Outline and Required Reading: AVL Trees ( 11.2) CSE 2011, Winter 2017 Instructor: N. Vlajic

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CSE 326: Data Structures Quicksort Comparison Sorting Bound

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22

each node in the tree, the difference in height of its two subtrees is at the most p. AVL tree is a BST that is height-balanced-1-tree.

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Algorithms. AVL Tree

Design and Analysis of Algorithms

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

A Polylog Time Wait-Free Construction for Closed Objects

When a BST becomes badly unbalanced, the search behavior can degenerate to that of a sorted linked list, O(N).

GSLM Operations Research II Fall 13/14

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Ch04 Balanced Search Trees

The Codesign Challenge

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

AMath 483/583 Lecture 21 May 13, Notes: Notes: Jacobi iteration. Notes: Jacobi with OpenMP coarse grain

Data Structures and Programming Spring 2014, Midterm Exam.

Data Structures and Algorithms

Geometric Transformations and Multiple Views

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Machine Learning. K-means Algorithm

1 Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

Hash-Based Indexes. Chapter 11. Comp 521 Files and Databases Spring

Prof. Feng Liu. Spring /24/2017

Hash-Based Indexes. Chapter 11. Comp 521 Files and Databases Fall

Data Structures in Java

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10

Advanced Tree. Structures. AVL Tree. Outline. AVL Tree Recall, Binary Search Tree (BST) is a special case of. Splay Tree (Ch 13.2.

Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

ICS 691: Advanced Data Structures Spring Lecture 3

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Greedy Technique - Definition

2D Raster Graphics. Integer grid Sequential (left-right, top-down) scan. Computer Graphics

Deletion The Two Child Case 10 Delete(5) Deletion The Two Child Case. Balanced BST. Finally

Non-Split Restrained Dominating Set of an Interval Graph Using an Algorithm

Problem Set 3 Solutions

4.2 The Derivative. f(x + h) f(x) lim

Sorting. Sorting. Why Sort? Consistent Ordering

CS350: Data Structures AVL Trees

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Balanced Trees Part One

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

The ray density estimation of a CT system by a supervised learning algorithm

SUV Color Space & Filtering. Computer Vision I. CSE252A Lecture 9. Announcement. HW2 posted If microphone goes out, let me know

Fault Localization Using Tarantula

An Optimal Algorithm for Prufer Codes *

Sorting and Algorithm Analysis

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

CHAPTER 10 AVL TREES. 3 8 z 4

Loop Transformations, Dependences, and Parallelization

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

Haar Transform CS 430 Denbigh Starkey

CSE 373: Data Structures and Algorithms. Hash Tables. Autumn Shrirang (Shri) Mare

More on Functions and Their Graphs

CS 310: Tree Rotations and AVL Trees

AVL trees and rotations

Section 2.3: Calculating Limits using the Limit Laws

Materials: Whiteboard, TI-Nspire classroom set, quadratic tangents program, and a computer projector.

Design Patterns for Data Structures. Chapter 10. Balanced Trees

19.2 Surface Area of Prisms and Cylinders

COMP171. AVL-Trees (Part 1)

Investigations of Topology and Shape of Multi-material Optimum Design of Structures

AVL Trees (10.2) AVL Trees

Introduction to Geometrical Optics - a 2D ray tracing Excel model for spherical mirrors - Part 2

A Distributed First and Last Consistent Global Checkpoint Algorithm

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

ELEC 377 Operating Systems. Week 6 Class 3

Load Balancing for Hex-Cell Interconnection Network

Design Patterns for Data Structures. Chapter 10. Balanced Trees

CMPS 10 Introduction to Computer Science Lecture Notes

Graph-based Clustering

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Smoothing Spline ANOVA for variable screening

AVL Trees. CSE260, Computer Science B: Honors Stony Brook University

ECE250: Algorithms and Data Structures AVL Trees (Part A)

Linear Interpolating Splines

CSE 326: Data Structures Lecture #8 Binary Search Trees

15-122: Principles of Imperative Computation, Summer 2011 Assignment 6: Trees and Secret Codes

Transcription:

CSE : Data Structures AL Trees Neva Cernavsy Summer Te AL Balance Condton AL balance property: Left and rgt subtrees of every node ave egts dfferng by at most Ensures small dept ll prove ts by sowng tat an AL tree of egt must ave a lot of (.e. O( )) nodes Easy to mantan Usng sngle and double rotatons Te AL Tree Data Structure Structural propertes. Bnary tree property (,, or cldren). Hegts of left and rgt subtrees of every node dffer by at most Result: orst case dept of any node s: O(log n) Is ts an AL Tree? How do we trac te balance? How do we detect mbalance? How do we restore balance? Orderng property Same as for BST Crcle One: AL Hegt of an AL Tree Not AL AL Not AL M() = mnmum number of nodes n an AL tree of egt. Bass M() =, M() = Inducton M() = M(-) + M(-) + Soluton - - M() > φ - (φ = (+ )/.) Student Actvty If not AL, put a box around nodes were AL property s volated.

Proof tat M() > φ Bass: M() = > φ -, M() = > φ - Inducton step. M() = M(-) + M(-) + > (φ - - ) + (φ - - ) + = φ - (φ +) - = φ - (φ = φ +) Hegt of an AL Tree M() > φ (φ.) Suppose we ave N nodes n an AL tree of egt. N > M() N > φ - log φ (N+) > (relatvely well balanced tree!!) Node Hegts Node Hegts after Insert egt of node = balance factor = left - rgt empty egt = - balance factor -(-) = - egt of node = balance factor = left - rgt empty egt = - Insert and Rotaton n AL Trees Insert operaton may cause balance factor to become or for some node only nodes on te pat from nserton pont to root node ave possbly canged n egt So after te Insert, go bac up to te root node by node, updatng egts If a new balance factor (te dfference left - rgt ) s or, adust tree by rotaton around te node Sngle Rotaton n an AL Tree

Insertons n AL Trees Bad Case # Let te node tat needs rebalancng be α. Tere are cases: Outsde Cases (requre sngle rotaton) :. Inserton nto left subtree of left cld of α.. Inserton nto rgt subtree of rgt cld of α. Insde Cases (requre double rotaton) :. Inserton nto rgt subtree of left cld of α.. Inserton nto left subtree of rgt cld of α. Te rebalancng s performed troug four separate rotaton algortms. Insert() Insert() Insert() Fx: Apply Sngle Rotaton AL Property volated at ts node (x) Sngle Rotaton:. Rotate between x and cld AL Inserton: Outsde Case Consder a vald AL subtree AL Inserton: Outsde Case + Insertng nto destroys te AL property at node AL Inserton: Outsde Case + Do a rotaton from left

Sngle rotaton from left + Outsde Case Completed + rotaton from left done! ( rotaton from rgt s mrror symmetrc) AL property as been restored! Sngle rotaton example Insert() Insert() Insert() Bad Case # Fx: Apply Double Rotaton AL Property volated at ts node (x) Double Rotaton. Rotate between x s cld and grandcld. Rotate between x and x s new cld AL Inserton: Insde Case Consder a vald AL subtree

AL Inserton: Insde Case Insertng nto destroys te AL property at node + Does rotaton from left restore balance? AL Inserton: Insde Case + Rotaton from left does not restore balance now s out of balance AL Inserton: Insde Case Consder te structure of subtree + AL Inserton: Insde Case = node and subtrees and or - + AL Inserton: Insde Case e wll do a double rotaton... Double rotaton : frst rotaton

Double rotaton : second rotaton Double rotaton : second rotaton rgt rotaton complete or - Balance as been restored to te unverse Double rotaton, step Double rotaton, step Imbalance at node Insert nto an AL tree: a b e c d Sngle Rotaton. Rotate between x and cld Double Rotaton. Rotate between x s cld and grandcld. Rotate between x and x s new cld Student Actvty

Sngle and Double Rotatons: Insertng wat nteger values would cause te tree to need a:. sngle rotaton?. double rotaton? Inserton nto AL tree. Fnd spot for new ey. Hang new node tere wt ts ey. Searc bac up te pat for mbalance. If tere s an mbalance: case #: Perform sngle rotaton and ext. no rotaton? Student Actvty case #: Perform double rotaton and ext Bot rotatons eep te subtree egt uncanged. Hence only one rotaton s suffcent! Insert() Unbalanced? Easy Insert Hard Insert (Bad Case #) Insert() Unbalanced? How to fx? Sngle Rotaton Hard Insert (Bad Case #) Insert() Unbalanced? How to fx?

Sngle Rotaton (oops!) Double Rotaton (Step #) Double Rotaton (Step #) AL Trees Revsted Balance condton: For every node x, - balance(x) Strong enoug : orst case dept s O(log n) Easy to mantan : one sngle or double rotaton Guaranteed O(log n) runnng tme for Fnd? Insert? Delete? buldtree? AL Trees Revsted at extra nfo dd we mantan n eac node? ere were rotatons performed? How dd we locate ts node? Oter Possbltes? Could use dfferent balance condtons, dfferent ways to mantan balance, dfferent guarantees on runnng tme, y aren t AL trees perfect? Many oter balanced BST data structures Red-Blac trees AA trees Splay Trees - Trees B-Trees

Implementaton Sngle Rotaton left balance (,,-) ey rgt RotateFromRgt(n : reference node ponter) { p : node ponter; p := n.rgt; n n.rgt := p.left; p.left := n; n := p } Double Rotaton Class partcpaton Implement Double Rotaton n two lnes. DoubleRotateFromRgt(n : reference node ponter) {???? n } AL Tree Deleton Smlar to nserton Rotatons and double rotatons needed to rebalance Imbalance may propagate upward so tat many rotatons may be needed. Pros and Cons of AL Trees Arguments for AL trees:. Searc s O(log N) snce AL trees are always well balanced.. Te egt balancng adds no more tan a constant factor to te speed of nserton, deleton, and fnd. Arguments aganst usng AL trees:. Dffcult to program & debug; more space for egt nfo.. Asymptotcally faster but rebalancng costs tme.. Most large searces are done n database systems on ds and use oter structures (e.g. B-trees).. May be OK to ave O(N) for a sngle operaton f total run tme for many consecutve operatons s fast (e.g. Splay trees).