Balanced Binary Trees

Similar documents
Red Green Black Trees: Extension to Red Black Trees

16.1 Volume of Prisms and Cylinders

234 The National Strategies Secondary Mathematics exemplification: Y7

Physics Engineering PC 1431 Experiment P2 Heat Engine. Section B: Brief Theory (condensed from Serway & Jewett)

Math Practice Use a Formula

Applications. 38 Looking for Pythagoras. Find the missing length(s).

To find the volume of a pyramid and of a cone

Overall stability of multi-span portal sheds at right-angles to the portal spans

Optimization Model of Oil-Volume Marking with Tilted Oil Tank

Goal: Measure the pump curve(s)

Math GPS. 2. Art projects include structures made with straws this week.

Revision Topic 12: Area and Volume Area of simple shapes

青藜苑教育 Example : Find te area of te following trapezium. 7cm 4.5cm cm To find te area, you add te parallel sides 7

Numerical Simulation of Stresses in Thin-rimmed Spur Gears with Keyway B. Brůžek, E. Leidich

ANALYSIS OF WORK ROLL THERMAL BEHAVIOR FOR 1450MM HOT STRIP MILL WITH GENETIC ALGORITHM

Calculation of Theoretical Torque and Displacement in an Internal Gear Pump

Fixation effects: do they exist in design problem solving?

Volumes of Pyramids. Essential Question How can you find the volume of a pyramid?

Applying Trigonometric Functions. ENTERTAINMENT The circus has arrived and the roustabouts must put

2 2D 2F. 1pc for each 20 m of wire. h (min. 45) h (min. 45) 3AC. see details J, E

Point Pollution Sources Dimensioning

Prediction of steel plate deformation due to triangle heating using the inherent strain method

Analysing the energy consumption of air handling units by Hungarian and international methods

20.1 Heights and distances

Ground Improvement Using Preloading with Prefabricated Vertical Drains

1/1 FULL SIZE 3/4 QUARTER SIZE 1/2 HALF SIZE EXTRA LARGE SIZE EXTRA LONG SIZE

4.2 Using Similar Shapes

1/1 FULL SIZE 3/4 QUARTER SIZE 1/2 HALF SIZE EXTRA LARGE SIZE EXTRA LONG SIZE

Essential Question How can you find the surface area and the volume of a cone? 3 in. π

OD DVOSTRUKO ZASTAKLJENOG PROZORA DO DVOSTRUKE FASADE INDIKATORI PRENOSA TOPLOTE STACIONARNOG STANJA

Calculation Methodology of Translucent Construction Elements in Buildings and Other Structures

THE REDESIGNED CANADIAN MONTHLY WHOLESALE AND RETAIL TRADE SURVEY: A POSTMORTEM OF THE IMPLEMENTATION

Reflections on the drinking bowl 'Balance'

Bribery and Favoritism by Auctioneers in Sealed-Bid Auctions

Geometry Supplement for Math 60 Perimeter, Area, and Volume

Influence of the mass flow ratio water-air on the volumetric mass transfer coefficient in a cooling tower

Description of Danish Practices in Retail Trade Statistics.

5.10. Area and Perimeter INSERT

Annex 16. Methodological Tool. Tool to determine project emissions from flaring gases containing methane

p x The revenue function is 5. What is the maximum vertical distance between the line

Installation the DELTABEAM Frame

We Protect Your Yarn Like No One Else

Ice Cream. Ice Cream. 1 of 9. Copyright 2007, Exemplars, Inc. All rights reserved.

Russell James Department of Scientific and Industrial Research Taupo-ldairakei, New Zealand

10 Fingers of Death: Algorithms for Combat Killing Roger Smith and Don Stoner Titan Corporation

László Mester. The new physical-mechanical theory of granular materials

Using tree-grammars for training set expansion in page classification

MAMA SID'S PIZZA by Faith Goddard-Allen

International Plant Protection Convention Page 1 of 10

10. Consider the following problem: A box with an open top is to. 11. A farmer wants to fence an area of 1.5 million square feet in a

CO-ROTATING FULLY INTERMESHING TWIN-SCREW COMPOUNDING: ADVANCEMENTS FOR IMPROVED PERFORMANCE AND PRODUCTIVITY

Background. Sample design

The household budget and expenditure data collection module (IOF 2014/2015) within a continuous multipurpose survey system (INCAF)

Study of Steam Export Transients in a Combined Cycle Power Plant

2. The differential pressure across control valves must not vary too much

Farm Structure Survey 2009/2010 Survey on agricultural production methods 2009/2010

Study of microrelief influence on optical output coefficient of GaN-based LED

OXYGEN CONTENT OF COMMERCIAL FROZEN ORANGE

HCR OF HEAT PUMP ROOM AIR CONDITIONER IN CHINA. Beijing , China

Questions. denotes answer available in Student Solutions Manual/Study Guide; O denotes objective question

Is urban food demand in the Philippines different from China?

Structures of Life. Investigation 1: Origin of Seeds. Big Question: 3 rd Science Notebook. Name:

Managing Measurement Uncertainty in Building Acoustics

Variance Estimation of the Design Effect

h h h h h h Corporate Retreats Conferences Training Events Reunions Social Gatherings Getaways

Do Regional Trade Pacts Benefit the Poor?

TEMPERATURE CONDITIONS AND TOLERANCE OF AVOCADO FRUIT TISSUE

Unit 2, Lesson 15: Part-Part-Whole Ratios

EXTERNAL SHOCKS AND POVERTY: HOW RECESSION IN EUROPE, JAPAN, AND CHINA AFFECTS THE INDONESIAN POOR

Effect of Twisted-tape Inserts on Heat Transfer in a Tube

Investigation 1: Ratios and Proportions and Investigation 2: Comparing and Scaling Rates

Bribery and Favoritism by Auctioneers in Sealed-Bid Auctions

Total surface area: the area of the lateral faces combined with the area of both bases

Structural Changes in Household Income Inequality in Korea: A New Decomposition Including Labor Supply Changes * Chulhee Lee

APPENDIX C2: Design of Canard Aircraft

Gas Flow into Rotary Valve Intake and Exhaust Mechanism in Internal Combustion Engine

The Violin Bow: Taper, Camber and Flexibility

A FIRST ANALYSIS OF THE FLOOD EVENTS OF AUGUST 2002 IN LOWER AUSTRIA BY USING A HYDRODYNAMIC MODEL

Feeling Hungry. How many cookies were on the plate before anyone started feeling hungry? Feeling Hungry. 1 of 10

All-Day Menu. Served daily from 12 midday til 9pm. Please also see our blackboard for any last minute additions to our current Specials

THE ANNALS OF "DUNAREA DE JOS" UNIVERSITY OF GALATI FASCICLE III, 2003 ISSN X ELECTROTECHNICS, ELECTRONICS, AUTOMATIC CONTROL, INFORMATICS

Section 2.3 Fibonacci Numbers and the Golden Mean

All-Day Menu. Served daily from 12 midday til 9pm. Please also see our blackboard for any last minute additions to our current Specials

MasterTop BC 309. A two component, clear, non-solvented (total solid), epoxy binder

An application of cumulative prospect theory to travel time variability

Every second of your day is

Prepare and serve wines. unit 614

Characteristics and dead-time of GM-tube

Lesson 41: Designing a very wide-angle lens

UNIT TITLE: PROVIDE GUERIDON SERVICE NOMINAL HOURS: 25

Pevzner P., Tesler G. PNAS 2003;100: Copyright 2003, The National Academy of Sciences

Chapter 1: The Ricardo Model

Rail Haverhill Viability Study

Gail E. Potter, Timo Smieszek, and Kerstin Sailer. April 24, 2015

CARCINOMA OF THE ESOPHAGUS*

FABRICATION AND TESTING OF A HIGH-TEMPERATURE PRINTED CIRCUIT HEAT EXCHANGER

Subject to sale, withdrawal or error. Published on 09/19/16

Sources and Propagation of International Cycles: Common. Shocks or Transmission? Universitat Pompeu Fabra. This revision: November, 1996.

Conjoint Analysis: A Study of Canned Coffee in Taiwan

Transcription:

Balanced Binary Trees 1 Binary searc trees provide O(log N) searc times provided tat te nodes are distributed in a reasonably balanced manner. Unfortunately, tat is not always te case and performing a sequence of deletions and insertions can often exacerbate te problem. Wen a BST becomes badly unbalanced, te searc beavior can degenerate to tat of a sorted linked list, O(N). Tere are a number of strategies for dealing wit tis problem; most involve adding some sort of restructuring to te insert/delete algoritms. Tat can be effective only if te restructuring reduces te average dept of a node from te root of te BST, and if te cost of te restructuring is, on average, O(log N). We will examine one suc restructuring algoritm

2 AVL tree*: a binary searc tree in wic te eigts of te left and rigt subtrees of te root differ by at most 1, and in wic te left and rigt subtrees are temselves AVL trees. Eac AVL tree node as an associated balance factor indicating te relative eigts of its subtrees (left-iger, equal, rigt-iger). Normally, tis adds one data element to eac tree node and an enumerated type is used. How effective is tis? Te eigt of an AVL tree wit N nodes never exceeds 1.44 log N and is typically muc closer to log N. *G. M. Adelson-Velskii and E. M. Landis, 192.

Examples Tis is an AVL tree... Notation: - means te subtrees of te node are te same eigt means tat te rigt subtree of te node is iger (by one) tan te left subtree (rigt-iger) / means tat te left subtree of te node is iger (by one) tan te rigt subtree (left-iger) / 3...and tis is not. / /

Unbalance from Insertion 4 Consider inserting te value 4 into te AVL tree: 0 2 0 30 8 Te result would be unbalanced at te node containing 2: 0 2 0 Te unbalance is repaired by applying one of two types of rotation to te unbalanced subtree 30 4 8

Rebalancing via Subtree Restructuring Te subtree rooted at 2 is rigt-iger. 0 2 0 We restructure te subtree, resulting in a balanced subtree: 30 4 8 0 2 30 4 0 Te transformation is relatively simple, requiring only a few operations, and results in a subtree tat as equal balance. 8

AVL Insertion Case: rigt-iger Tere are two unbalance cases to consider, eac defined by te state of te subtree tat just received a new node. For simplicity, assume for now tat te insertion was to te rigt subtree (of te subtree). Let be te root of te newly unbalanced subtree, and suppose tat its rigt subtree is now rigt-iger: rigt In tis case, te subtree rooted at rigt was previously equally balanced (wy?) and te subtree rooted at was previously rigtiger (wy?). T 1 Te eigt labels follow from tose observations. T 2 +1 T 3 Balance can be restored by rotating te values so tat rigt becomes te subtree root node and becomes te left cild.

AVL Left Rotation Te manipulation just described is known as a left rotation and te result is: rigt rigt T 1 T 2 +1 T 3 Overall eigt of eac subtree is now te same. T 1 T 2 T 3 Tat covers te case were te rigt subtree as become rigt-iger te case were te left subtree as become left-iger is analogous and solved by a rigt rotation.

Unbalance from Insertion 8 Consider inserting te value 1 into tis AVL tree: 0 2 0 20 8 Te result would be unbalanced at te node containing 2: Te unbalance is repaired by applying one of two types of rotation to te unbalanced subtree 20 2 0 0 1 8

Rebalancing via Subtree Restructuring 9 0 Te subtree rooted at 2 is left-iger. 2 0 20 1 8 We restructure te subtree, resulting in a balanced subtree: 0 20 Te transformation is again relatively simple, requiring only a few operations, and results in a subtree tat as equal balance. 1 2 0 8

AVL Rigt Rotation Te previous manipulation is known as a rigt rotation and te result is: 10 // left left / +1 T 3 T 2 T 1 Overall eigt of eac subtree is now te same. T 3 T 2 T 1 Tat covers te case were te left subtree as become left-iger.

AVL Insertion Case: left-iger Now suppose tat te rigt subtree as become left-iger: 11 T 1 rigt-left? / rigt Te insertion occurred in te left subtree of te rigt subtree of. In tis case, te left subtree of te rigt subtree (rooted at rigt-left) may be eiter left-iger or rigt-iger, but not balanced (wy?). -1 T 2 or T 3 T 4 Surprisingly (peraps), tis case is more difficult. Te unbalance cannot be removed by performing a single left or rigt rotation.

AVL Double Rotation Applying a single rigt rotation to te subtree rooted at rigt produces 12 / rigt rigt-left rigt-left?? rigt -1 T 4 T 2 T 2 or T 3 T 3 T 4 a subtree rooted at rigt-left tat is now rigt-iger

AVL Double Rotation 13 Now, applying a single left rotation to te subtree rooted at produces rigt-left? Balance factors ere depend on original balance factor of rigt-left. rigt? rigt-left? rigt T 2 T 3 T 4 T 1 T 2 T 3 T 4 a balanced subtree. -1 or Te case were te left subtree of is rigt-iger is andled similarly (by a double rotation: left: rigt).

Unbalance from Deletion 14 Deleting a node from an AVL tree can also create an imbalance tat must be corrected. Te effects of deletion are potentially more complex tan tose of insertion. Te basic idea remains te same: delete te node, track canges in balance factors as te recursion backs out, and apply rotations as needed to restore AVL balance at eac node along te pat followed down during te deletion. However, rebalancing after a deletion may require applying single or double rotations at more tan one point along tat pat. As usual, tere are cases Here, we will make te following assumptions: - te lowest imbalance occurs at te node root (a subtree root) - te deletion occurred in te left subtree of root

AVL Deletion Case: rigt-iger 1 Suppose we ave te subtree on te left prior to deletion and tat on te rigt after deletion: rigt rigt 1 1 2 3-1 2 3-1 rigt -1 Note: "rigt-iger" refers to te balance factor of te root of te rigt subtree (labeled rigt ere). 1 2 3-1 -1 Ten a single left rotation at will rebalance te subtree.

AVL Deletion Case: equal-eigt Suppose te rigt subtree root as balance factor equal-eigt: 1 rigt Again, a single left rotation at root will rebalance te subtree. / rigt 1 2 3 1-1 2 3 Te difference is te resulting balance factor at te old subtree root node,, wic depends upon te original balance factor of te node rigt.

AVL Deletion Case: left-iger If te rigt subtree root was left-iger, we ave te following situation: 1 1 rigt-left? 2 3-1 or -2? / rigt 4-1 Deleting a node from te left subtree of now will cause to become double rigt iger. As you sould expect, te resulting imbalance can be cured by first applying a rigt rotation at te node rigt, and ten applying a left rotation at te node. However, we must be careful because te balance factors will depend upon te original balance factor at te node labeled rigt-left

AVL Deletion Case: left-iger, left-iger If te rigt-left subtree root was also left-iger, we obtain: 18 rigt-left / rigt rigt 1 rigt-left / 4-1 2 3-1 -2-1 1-1 2-1 3-2 4-1

AVL Deletion Case: left-iger, rigt-iger If te rigt-left subtree root was rigt-iger, we obtain: 19 rigt-left / rigt / rigt 1 rigt-left 4-1 2 3-2 -1 1 2-2 3 4-1 -1-1 -1 And, finally, if te rigt-left subtree root was equal-eigt, we'd obtain a tree were all tree of te labeled nodes ave equal-eigt.

AVL Deletion Cases: Summary 20 We ave considered a number of distinct deletion cases, assuming tat te deletion occurred in te left subtree of te imbalanced node. Tere are an equal number of entirely similar, symmetric cases for te assumption te deletion was in te rigt subtree of te imbalanced node. Drawing diagrams elps Tis discussion also as some logical implications for ow insertion is andled in an AVL tree. Te determination of te balance factors in te tree, following te rotations, involves similar logic in bot cases.