AVL trees. AVL trees

Similar documents
University Mathematics 2

3.1 Extreme Values of a Function

MVT and Rolle s Theorem

Math 31A Discussion Notes Week 4 October 20 and October 22, 2015

MTH-112 Quiz 1 Name: # :

Continuity and Differentiability Worksheet

Continuity and Differentiability of the Trigonometric Functions

1.5 Functions and Their Rates of Change

Differentiation. Area of study Unit 2 Calculus

MATH1901 Differential Calculus (Advanced)

LIMITS AND DERIVATIVES CONDITIONS FOR THE EXISTENCE OF A LIMIT

Lecture 10: Carnot theorem

1 The concept of limits (p.217 p.229, p.242 p.249, p.255 p.256) 1.1 Limits Consider the function determined by the formula 3. x since at this point

2.8 The Derivative as a Function

MA119-A Applied Calculus for Business Fall Homework 4 Solutions Due 9/29/ :30AM

Integral Calculus, dealing with areas and volumes, and approximate areas under and between curves.

CHAPTER 2 Functions and Their Graphs

Name: Sept 21, 2017 Page 1 of 1

11.6 DIRECTIONAL DERIVATIVES AND THE GRADIENT VECTOR

Efficient algorithms for for clone items detection

4. The slope of the line 2x 7y = 8 is (a) 2/7 (b) 7/2 (c) 2 (d) 2/7 (e) None of these.

Chapter 1 Functions and Graphs. Section 1.5 = = = 4. Check Point Exercises The slope of the line y = 3x+ 1 is 3.

pancakes. A typical pancake also appears in the sketch above. The pancake at height x (which is the fraction x of the total height of the cone) has

Math 34A Practice Final Solutions Fall 2007

Higher Derivatives. Differentiable Functions

Some Review Problems for First Midterm Mathematics 1300, Calculus 1

Investigating Euler s Method and Differential Equations to Approximate π. Lindsay Crowl August 2, 2001

Exponentials and Logarithms Review Part 2: Exponentials

10 Derivatives ( )

232 Calculus and Structures

Chapter 2 Limits and Continuity

INTRODUCTION TO CALCULUS LIMITS

y = 3 2 x 3. The slope of this line is 3 and its y-intercept is (0, 3). For every two units to the right, the line rises three units vertically.

Continuity. Example 1

June : 2016 (CBCS) Body. Load

CSE 5311 Notes 5: Trees. (Last updated 6/4/13 4:12 PM)

DIGRAPHS FROM POWERS MODULO p

5.1 We will begin this section with the definition of a rational expression. We

Numerical Differentiation

CHAPTER (A) When x = 2, y = 6, so f( 2) = 6. (B) When y = 4, x can equal 6, 2, or 4.

Differential Calculus (The basics) Prepared by Mr. C. Hull

2.3. Applying Newton s Laws of Motion. Objects in Equilibrium

MATH Fall 08. y f(x) Review Problems for the Midterm Examination Covers [1.1, 4.3] in Stewart

INTRODUCTION AND MATHEMATICAL CONCEPTS

SFU UBC UNBC Uvic Calculus Challenge Examination June 5, 2008, 12:00 15:00

Click here to see an animation of the derivative

Lecture XVII. Abstract We introduce the concept of directional derivative of a scalar function and discuss its relation with the gradient operator.

MATH1151 Calculus Test S1 v2a

Derivatives of Exponentials

Chapter. Differentiation: Basic Concepts. 1. The Derivative: Slope and Rates. 2. Techniques of Differentiation. 3. The Product and Quotient Rules

Chapter 2. Limits and Continuity 16( ) 16( 9) = = 001. Section 2.1 Rates of Change and Limits (pp ) Quick Review 2.1

Logarithmic functions

1. Which one of the following expressions is not equal to all the others? 1 C. 1 D. 25x. 2. Simplify this expression as much as possible.

WYSE Academic Challenge 2004 State Finals Mathematics Solution Set

The Derivative The rate of change

1 2 x Solution. The function f x is only defined when x 0, so we will assume that x 0 for the remainder of the solution. f x. f x h f x.

2.11 That s So Derivative

f a h f a h h lim lim

158 Calculus and Structures

Lines, Conics, Tangents, Limits and the Derivative

The Derivative as a Function

2.3 Product and Quotient Rules

Preface. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

= 0 and states ''hence there is a stationary point'' All aspects of the proof dx must be correct (c)

Math 212-Lecture 9. For a single-variable function z = f(x), the derivative is f (x) = lim h 0

In Leibniz notation, we write this rule as follows. DERIVATIVE OF A CONSTANT FUNCTION. For n 4 we find the derivative of f x x 4 as follows: lim

MAT 145. Type of Calculator Used TI-89 Titanium 100 points Score 100 possible points

1. Consider the trigonometric function f(t) whose graph is shown below. Write down a possible formula for f(t).

CSCE 478/878 Lecture 2: Concept Learning and the General-to-Specific Ordering

MAT 1339-S14 Class 2

Math Spring 2013 Solutions to Assignment # 3 Completion Date: Wednesday May 15, (1/z) 2 (1/z 1) 2 = lim

Lecture 21. Numerical differentiation. f ( x+h) f ( x) h h

2.2 Derivative. 1. Definition of Derivative at a Point: The derivative of the function f x at x a is defined as

SECTION 3.2: DERIVATIVE FUNCTIONS and DIFFERENTIABILITY

Recall from our discussion of continuity in lecture a function is continuous at a point x = a if and only if

Moments and Product of Inertia

EDML: A Method for Learning Parameters in Bayesian Networks

KEY CONCEPT: THE DERIVATIVE

Exam 1 Review Solutions

Derivatives. By: OpenStaxCollege

11-19 PROGRESSION. A level Mathematics. Pure Mathematics

, meant to remind us of the definition of f (x) as the limit of difference quotients: = lim

Combining functions: algebraic methods

Chapter 2 Limits and Continuity. Section 2.1 Rates of Change and Limits (pp ) Section Quick Review 2.1

1 ode.mcd. Find solution to ODE dy/dx=f(x,y). Instructor: Nam Sun Wang

SECTION 1.10: DIFFERENCE QUOTIENTS LEARNING OBJECTIVES

Solutions Manual for Precalculus An Investigation of Functions

HOMEWORK HELP 2 FOR MATH 151

Test 2 Review. 1. Find the determinant of the matrix below using (a) cofactor expansion and (b) row reduction. A = 3 2 =

Polynomial Interpolation

WYSE Academic Challenge 2004 Sectional Mathematics Solution Set

Chapter 4 Derivatives [ ] = ( ) ( )= + ( ) + + = ()= + ()+ Exercise 4.1. Review of Prerequisite Skills. 1. f. 6. d. 4. b. lim. x x. = lim = c.

Excerpt from "Calculus" 2013 AoPS Inc.

The Verlet Algorithm for Molecular Dynamics Simulations

Section 2.7 Derivatives and Rates of Change Part II Section 2.8 The Derivative as a Function. at the point a, to be. = at time t = a is

1 Calculus. 1.1 Gradients and the Derivative. Q f(x+h) f(x)

Section 3.1: Derivatives of Polynomials and Exponential Functions

MANY scientific and engineering problems can be

Math 102 TEST CHAPTERS 3 & 4 Solutions & Comments Fall 2006

Sin, Cos and All That

Transcription:

Dnamic set DT dnamic set DT is a structure tat stores a set of elements. Eac element as a (unique) ke and satellite data. Te structure supports te following operations. Searc(S, k) Return te element wose ke is k. Insert(S, ) dd to S. Delete(S, ) Remove from S (te operation receives a pointer to ). Minimum(S) Return te element in S wit smallest ke. Maimum(S) Return te element in S wit largest ke. Successor(S, ) Return te element in S wit smallest ke tat is larger tan.ke. Predecessor(S, ) Return te element in S wit largest ke tat is smaller tan.ke.

Motivation In a binar searc tree, all operation take Θ() time in te worst case, were is te eigt of te tree. Te optimal eigt of a binar searc tree is log n. Even if we start wit a balanced tree, insertion/deletion operations can make te tree unbalanced. n VL tree is a special kind of a binar searc tree, wic is alwas kept banalced.

VL tree n VL tree is a binar searc tree suc tat for ever node, eigt(.left) eigt(.rigt) 1 (we assume tat eigt(null) = 1) 1 1 4 8 1 1 16 14 6 are named after teir inventors, Georg delson-velsk and Evgenii Landis.

Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1.

Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1. n 1 =.

Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1. n 1 =. n = + 1 + 1 = 4. 1

Te eigt of an VL tree Teorem Te eigt of an VL tree is Θ(log n). Let n k be te minimum number of nodes in an VL tree wit eigt k. n = 1. n 1 =. n = + 1 + 1 = 4. n = 4 + + 1 = 7. n k = n k 1 + n k + 1. 1

Te eigt of an VL tree n k = F k+ 1 were F k is te k-t Fibonacci number. Te proof is b induction: ase: n = 1 = F 1, n 1 = = F 4 1. n k = n k 1 +n k +1 = (F k+ 1)+(F k+1 1)+1 = F k+ 1 It is known tat ( F k = 1+ 5 ) k ( 5 1 ) k 5 ( 1+ 5 ) k 1 5 Terefore, ( 1+ ) k+ ( 5 1 1+ ) k+ 5 n k 1 1.45 5 5 ( ) k log (1+ 5)/ 5(nk + 1.45) 1.441 log n k. Te eigt of an VL tree wit n nodes is 1.441 log n.

Implementation node in an VL tree as te same fields defined for binar searc tree (ke, left, rigt, p). dditionall, ever node as a field eigt wic stores te eigt of te node.

Searcing Te Searc operation is andled eactl like in regular binar searc trees. Time compleit: Θ() = Θ(log n).

Insertion/Deletion Insertion and deletion are done b first appling te insertion/deletion algoritm of binar searc trees. fter te insertion/deletion, te tree ma not be balanced, so we need to correct it. Te time compleit of insertion/deletion in VL tree is Θ(log n).

Insertion fter a new leaf is inserted, te eigt of some of its ancestors increase b 1. Te eigts of te oter nodes are uncanged. 4 1 4 1 1 1 4 8 6 1 1 16 14 1 1 4 8 6 15 1 14 17 1 16 19 18

Insertion Te eigt fields of te nodes can be updated b going up te tree from te inserted leaf, and for eac ancestor v of te leaf perform v.eigt = 1 + ma(v.left.eigt, v.rigt.eigt) 4 1 4 1 1 1 4 8 6 1 1 14 16 1 1 4 8 6 1 1 14 15 17 16 19 18

Insertion Some of te ancestors of te leaf ma become unbalanced. 4 1 4 1 1 1 4 8 6 1 1 16 14 1 1 4 8 6 15 1 14 17 1 16 19 18

Let be te lowest node on te pat from te new leaf to te root wic is unbalanced (if doesn t eist we are done). Tere are 4 cases. In Case 1 suppose tat te new leaf is in te subtree of.left.left. Let =.left. Let be te eigt of te rigt cild of. Since was balanced before te insertion, te eigt of before te insertion was 1// + 1. -1//+1 C New leaf

Te insertion eiter increases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of is + 1 before te insertion, and + afterward. Te eigt of before te insertion is +, and + afterward. + +1 C New leaf

Te insertion eiter increases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of is + 1 before te insertion, and + afterward. Te eigt of before te insertion is +, and + afterward. + + + +1 C New leaf

fter te insertion, as a cild wit eigt + 1. Tis cild must be te left cild. Te eigt of before te insertion is. Te eigt of te rigt cild of is. + + + +1 C +1 New leaf

To fi te imbalance of, perform te following operation called rigt rotation. Te rotation operation doesn t cange te inorder of te nodes. Terefore, te new tree is a valid binar searc tree. + + + +1 C +1 +1 C

Te eigts of and after te rotations are + 1 and +. fter te rotation, and are balanced. + + + + +1 C +1 +1 +1 C

ssume ad a parent te before te rotation, and denote it b u. Let w be te sibling of before te rotation. Te eigt of w is + 1/ + / +. If te eigt of w is + 1, ten u is unbalanced after te insertion (before te rotation). w +1/+/+ + +1 u +4 +/+/+4 + + C w u +1/+/+ +1 +/+/+4 + +1 +1 C

fter te rotation, te eigt te sibling of w (node ) is +, wic is equal to te eigt of te sibling of w before te rotation. Terefore, u is balanced. Te eigt of u after te rotation is te same as te eigt before te insertion. Repeating tese arguments, ever ancestor of u is balanced and as same eigt as before te insertion. w +1/+/+ + +1 u +4 +/+/+4 + + C w u +1/+/+ +1 +/+/+4 + +1 +1 C

Eample 1 1 8 16 4 16 4 1 14 8 14 6 6 1 In tis eample, =.

In Case, suppose tat te new leaf is in te subtree of.left.rigt. Let =.left. Performing a rotation on and does not work. + + + + +1 C + +1 +1 C

Let z =.rigt. Perform a double rotation on,, z. Te double rotation doesn t cange te inorder of te nodes. fter te double rotation,,, z are balanced. Moreover, te eigt of z is te same as te eigt of before te insertion, and terefore all ancestors of z are balanced. + + z + + +1 z D +1 +1 +1-1 C D C -1-1

Case is wen te new leaf is in te subtree of.rigt.rigt, and Case 4 is wen te new leaf is in te subtree of.rigt.left. Case and Case 4 are smmetric to Case 1 and Case. Case is sown below. + + + + +1 +1 C +1 C +1

Deletion fter a node is deleted, te eigts of some of its ancestors decrease b 1. Te eigts of te oter nodes are uncanged. single ancestor of te deleted node can become unbalanced. 5 1 1 4 8 11 1 6 1 9 1 7

Let be te unbalanced node (if doesn t eist we are done). ssume tat te deleted node is in te subtree of.rigt. Let =.left. Since was balanced before te deletion, te eigt of.rigt before te deletion was 1// + 1. -1//+1 C

Te deletion eiter decreases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of.rigt is 1 before te insertion, and afterward. Te eigt of is + 1 (te insertion doesn t cange te eigt). C - -1

Te deletion eiter decreases te eigt of a node or doesn t cange te eigt. Since is now unbalanced, te onl possible case is tat te eigt of.rigt is 1 before te insertion, and afterward. Te eigt of is + 1 (te insertion doesn t cange te eigt). +1 C - -1

Since as eigt and it is balanced, one of s cildren as eigt 1 and te oter cild as eigt 1/. In Case 1, assume tat te eigt of.left is 1, and te eigt of.rigt is 1/. +1 C - -1-1 -1/-

In Case 1 we perform a rigt rotation on and. fter te rotation, te eigt of is / 1 and te eigt of is + 1/. fter te rotation, and are balanced. +1 +1/ C - -1-1 /-1-1 -1/- -1/- C -

ssume ad a parent te before te rotation, and denote it b u. Let w be te sibling of before te rotation. Te eigt of w is / + 1/ +. u is balanced after te insertion (before te rotation). u +/+/+ u w /+1/+ +1 C - -1 w /+1/+ -1 +1/ /-1-1 -1/- -1/- C -

fter te rotation, u can become unbalanced. Tis occurs wen te eigt of w is +, and te eigt of after te rotation is. If te eigt of w is, and te eigt after te rotation is, ten te eigt of u is + and + 1 afterwards. Tis can cause an imbalance in an ancestor of u. In te two cases above, additional rotation is needed. u +/+/+ u w /+1/+ +1 C - -1 w /+1/+ -1 +1/ /-1-1 -1/- -1/- C -

In Case, assume tat te eigt of.left is, and te eigt of.rigt is 1. Let z =.rigt. In tis case we perform a double rotation of,, z. +1 z - z D -1 - -1 - -1-1 -/- -/- C D - -/- C -/-