3. Building a Binary Search Tree. 5. Splay Trees: A Self-Adjusting Data Structure



Similar documents
Uses for Binary Trees -- Binary Search Trees

Last time Interprocedural analysis Dimensions of precision (flow- and context-sensitivity) Flow-Sensitive Pointer Analysis

Outline. Binary Tree

Oracle PL/SQL Programming Advanced

Chapter 3 Chemical Equations and Stoichiometry

Fundamentals of Tensor Analysis

Reading. Minimum Spanning Trees. Outline. A File Sharing Problem. A Kevin Bacon Problem. Spanning Trees. Section 9.6

CompactPCI Connectors acc. to PIGMG 2.0 Rev. 3.0

December Homework- Week 1

Change Your History How Can Soccer Knowledge Improve Your Business Processes?

QUANTITATIVE METHODS CLASSES WEEK SEVEN

5.4 Exponential Functions: Differentiation and Integration TOOTLIFTST:

Important result on the first passage time and its integral functional for a certain diffusion process

CPS 220 Theory of Computation REGULAR LANGUAGES. Regular expressions

Schedule C. Notice in terms of Rule 5(10) of the Capital Gains Rules, 1993

AC Circuits Three-Phase Circuits

The example is taken from Sect. 1.2 of Vol. 1 of the CPN book.

Distributed Systems Principles and Paradigms. Chapter 11: Distributed File Systems. Distributed File Systems. Example: NFS Architecture

How fast can we sort? Sorting. Decision-tree model. Decision-tree for insertion sort Sort a 1, a 2, a 3. CS Spring 2009

Applications: Lifting eyes are screwed or welded on a load or a machine to be used as lifting points.

Instruction: Solving Exponential Equations without Logarithms. This lecture uses a four-step process to solve exponential equations:

Math 135 Circles and Completing the Square Examples

Reasoning to Solve Equations and Inequalities

Higher. Exponentials and Logarithms 160

Algorithmic Aspects of Access Networks Design in B3G/4G Cellular Networks

Regular Sets and Expressions

INTERSECTIONS OF LINE SEGMENTS AND POLYGONS

Bayesian Updating with Continuous Priors Class 13, 18.05, Spring 2014 Jeremy Orloff and Jonathan Bloom

TC Appendix 4E Appropriate Qualification tables. (Unless otherwise indicated all qualifications are valid if awarded by examination only)

Lec 2: Gates and Logic

Vectors Recap of vectors

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers.

Approximate Subtree Identification in Heterogeneous XML Document Collections

Predicting Current User Intent with Contextual Markov Models

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

Hospitals. Internal Revenue Service Information about Schedule H (Form 990) and its instructions is at

Physics 43 Homework Set 9 Chapter 40 Key

Homework 3 Solutions

Usability Test Checklist

A.7.1 Trigonometric interpretation of dot product A.7.2 Geometric interpretation of dot product

AVAIL A. Magnum or Banana Cut. Here is the HOTTEST fletching on the market today.

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( )

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall Final Exam

Integration by Substitution

A Note on Approximating. the Normal Distribution Function

Revised Conditions (January 2009) LLOYDS BANKING GROUP SHARE ISA CONDITIONS

Binary Search Trees. Definition Of Binary Search Tree. The Operation ascend() Example Binary Search Tree

Lecture 3: Diffusion: Fick s first law

One Minute To Learn Programming: Finite Automata

Operational Procedure: ACNC Data Breach Response Plan

SEE PAGE 2 FOR BRUSH MOTOR WIRING SEE PAGE 3 FOR MANUFACTURER SPECIFIC BLDC MOTOR WIRING EXAMPLES A

Econ 371: Answer Key for Problem Set 1 (Chapter 12-13)

DlNBVRGH + Sickness Absence Monitoring Report. Executive of the Council. Purpose of report

Taiwan Stock Forecasting with the Genetic Programming

Link-Disjoint Paths for Reliable QoS Routing

Network Analyzer Error Models and Calibration Methods

Binary Search Trees. Definition Of Binary Search Tree. Complexity Of Dictionary Operations get(), put() and remove()

Back left Back right Front left Front right. Blue Shield of California. Subscriber JOHN DOE. a b c d

1.2 The Integers and Rational Numbers

DATABASDESIGN FÖR INGENJÖRER F

Treatment Spring Late Summer Fall Mean = 1.33 Mean = 4.88 Mean = 3.

Factorials! Stirling s formula

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions.

Example A rectangular box without lid is to be made from a square cardboard of sides 18 cm by cutting equal squares from each corner and then folding

Mathematics. Mathematics 3. hsn.uk.net. Higher HSN23000

REVIEW ON COMPARATIVE STUDY OF SOFTWARE PROCESS MODEL

C H A P T E R 1 Writing Reports with SAS

Improved PKC Provably Secure against Chosen Cipher text Attack

Incomplete 2-Port Vector Network Analyzer Calibration Methods

Binary Representation of Numbers Autar Kaw

Automated Specification-based Testing of Interactive Components with AsmL

4.11 Inner Product Spaces

Graph Theoretical Analysis and Design of Multistage Interconnection Networks

Experiment 6: Friction

5 2 index. e e. Prime numbers. Prime factors and factor trees. Powers. worked example 10. base. power

Standard Conditions for Street Traders The Royal Borough of Kensington and Chelsea. Revised standard conditions for street trading

and thus, they are similar. If k = 3 then the Jordan form of both matrices is

Enhancing Downlink Performance in Wireless Networks by Simultaneous Multiple Packet Transmission

9 CONTINUOUS DISTRIBUTIONS

Version 1.0. General Certificate of Education (A-level) January Mathematics MPC3. (Specification 6360) Pure Core 3. Final.

MAXIMAL CHAINS IN THE TURING DEGREES

SEE PAGE 2 FOR BRUSH MOTOR WIRING SEE PAGE 3 FOR MANUFACTURER SPECIFIC BLDC MOTOR WIRING EXAMPLES

by John Donald, Lecturer, School of Accounting, Economics and Finance, Deakin University, Australia

IncrEase: A Tool for Incremental Planning of Rural Fixed Broadband Wireless Access Networks

Authenticated Encryption. Jeremy, Paul, Ken, and Mike

Graphs on Logarithmic and Semilogarithmic Paper

Diagram Editing with Hypergraph Parser Support

RIGHT TRIANGLES AND THE PYTHAGOREAN TRIPLETS

Electric power can be transmitted or dis

Algebra Review. How well do you remember your algebra?

Transistor is a semiconductor device with fast respond and accuracy. There are two types

PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY

Free ACA SOLUTION (IRS 1094&1095 Reporting)

Unit 29: Inference for Two-Way Tables

Network Decoupling for Secure Communications in Wireless Sensor Networks

One Ring to Rule them All: Service Discovery and Binding in Structured Peer-to-Peer Overlay Networks

Quality and Pricing for Outsourcing Service: Optimal Contract Design

Distributions. (corresponding to the cumulative distribution function for the discrete case).

Five-Layer Density Column

Transcription:

Chptr 10 BINARY TREES 1. Gnrl Binry Trs 2. Binry Srch Trs 3. Builing Binry Srch Tr 4. Hight Blnc: AVL Trs 5. Sply Trs: A Slf-Ajusting Dt Structur Outlin Trnsp. 1, Chptr 10, Binry Trs 243 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Binry Trs DEFINITION A inry tr is ithr mpty, or it consists of no cll th root togthr with two inry trs cll th lft sutr n th right sutr of th root. Thr is on mpty inry tr, on inry tr with on no, n two with two nos: n Ths r iffrnt from ch othr. W nvr rw ny prt of inry tr to look lik Th inry trs with thr nos r: Binry Trs Trnsp. 2, Sct. 10.1, Introuction to Binry Trs 244 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Trvrsl of Binry Trs At givn no thr r thr tsks to o in som orr: Visit th no itslf (V); trvrs its lft sutr (L); trvrs its right sutr (R). Thr r six wys to rrng ths tsks: VLR LVR LRV VRL RVL RLV. By stnr convntion, ths r ruc to thr y consiring only th wys in which th lft sutr is trvrs for th right. VLR LVR LRV prorr inorr postorr Ths thr nms r chosn ccoring to th stp t which th givn no is visit. With prorr trvrsl w first visit no, thn trvrs its lft sutr, n thn trvrs its right sutr. With inorr trvrsl w first trvrs th lft sutr, thn visit th no, n thn trvrs its right sutr. With postorr trvrsl w first trvrs th lft sutr, thn trvrs th right sutr, n finlly visit th no. Trvrsl of Binry Trs Trnsp. 3, Sct. 10.1, Introuction to Binry Trs 245 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Exprssion Trs + log! x n + log x n! or < < c c ( c) ( < ) or (c < ) Exprssion: + log x n! ( c) ( < ) or (c < ) Prorr : + log x! n c or < < c Inorr : + log x n! c < or c < Postorr : + x log n! c < c < or Exprssion Trs Trnsp. 4, Sct. 10.1, Introuction to Binry Trs 246 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

:= / + 0.5 2 c 4 x := ( + ( 2 4 c) 0.5)/(2 ) 2 x Exprssion tr of th qurtic formul Trnsp. 5, Sct. 10.1, Introuction to Binry Trs 247 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Link Binry Trs Comprison tr: Jim Dot Ron Amy Guy Ky Tim Ann Ev Jn Jon Kim Roy Tom Link implmnttion of inry tr: Jim Dot Ron Amy Guy Ky Tim Ann Ev Jn Jon Kim Roy Tom Link Binry Trs Trnsp. 6, Sct. 10.1, Introuction to Binry Trs 248 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Link Binry Tr Spcifictions Binry tr clss: tmplt <clss Entry> clss Binry tr { pulic: // A mthos hr. protct: // A uxiliry function prototyps hr. Binry no<entry> *root; ; Binry no clss: tmplt <clss Entry> struct Binry no { // t mmrs: Entry t; Binry no<entry> *lft; Binry no<entry> *right; // constructors: Binry no( ); Binry no(const Entry &x); ; Link Binry Tr Spcifictions Trnsp. 7, Sct. 10.1, Introuction to Binry Trs 249 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Constructor: tmplt <clss Entry> Binry tr<entry> ::Binry tr( ) /* Post: An mpty inry tr hs n crt. */ { root = NULL; Empty: tmplt <clss Entry> ool Binry tr<entry> ::mpty( ) const /* Post: A rsult of tru is rturn if th inry tr is mpty. Othrwis, fls is rturn. */ { rturn root == NULL; Binry tr mthos Trnsp. 8, Sct. 10.1, Introuction to Binry Trs 250 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Inorr trvrsl: tmplt <clss Entry> voi Binry tr<entry> ::inorr(voi (*visit)(entry &)) /* Post: Th tr hs n n trvrs in inorr squnc. Uss: Th function rcursiv inorr */ { rcursiv inorr(root, visit); Most Binry tr mthos scri y rcursiv procsss cn implmnt y clling n uxiliry rcursiv function tht pplis to sutrs. tmplt <clss Entry> voi Binry tr<entry> :: rcursiv inorr(binry no<entry> *su root, voi (*visit)(entry &)) /* Pr: su root is ithr NULL or points to sutr of th Binry tr. Post: Th sutr hs n n trvrs in inorr squnc. Uss: Th function rcursiv inorr rcursivly */ { if (su root!= NULL) { rcursiv inorr(su root->lft, visit); (*visit)(su root->t); rcursiv inorr(su root->right, visit); Binry tr mthos Trnsp. 9, Sct. 10.1, Introuction to Binry Trs 251 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Binry Tr Clss Spcifiction tmplt <clss Entry> clss Binry tr { pulic: Binry tr( ); ool mpty( ) const; voi prorr(voi (*visit)(entry &)); voi inorr(voi (*visit)(entry &)); voi postorr(voi (*visit)(entry &)); int siz( ) const; voi clr( ); int hight( ) const; voi insrt(const Entry &); Binry tr (const Binry tr<entry> &originl); Binry tr & oprtor = (const Binry tr<entry> &originl); Binry tr( ); protct: // A uxiliry function prototyps hr. Binry no<entry> *root; ; Binry Tr Clss Spcifiction Trnsp. 10, Sct. 10.1, Introuction to Binry Trs 252 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Binry Srch Trs Cn w fin n implmnttion for orr lists in which w cn srch quickly (s with inry srch on contiguous list) n in which w cn mk insrtions n ltions quickly (s with link list)? DEFINITION A inry srch tr is inry tr tht is ithr mpty or in which th t ntry of vry no hs ky n stisfis th conitions: 1. Th ky of th lft chil of no (if it xists) is lss thn th ky of its prnt no. 2. Th ky of th right chil of no (if it xists) is grtr thn th ky of its prnt no. 3. Th lft n right sutrs of th root r gin inry srch trs. W lwys rquir: No two ntris in inry srch tr my hv qul kys. W cn rgr inry srch trs s nw ADT. W my rgr inry srch trs s spciliztion of inry trs. W my stuy inry srch trs s nw implmnttion of th ADT orr list. Binry Srch Trs Trnsp. 11, Sct. 10.2, Binry Srch Trs 253 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Th Binry Srch Tr Clss Th inry srch tr clss will riv from th inry tr clss; hnc ll inry tr mthos r inhrit. tmplt <clss Rcor> clss Srch tr: pulic Binry tr<rcor> { pulic: Error co insrt(const Rcor &nw t); Error co rmov(const Rcor &ol t); Error co tr srch(rcor &trgt) const; privt: // A uxiliry function prototyps hr. ; Th inhrit mthos inclu th constructors, th structor, clr, mpty, siz, hight, n th trvrsls prorr, inorr, n postorr. A inry srch tr lso mits spciliz mthos cll insrt, rmov, n tr srch. Th clss Rcor hs th hvior outlin in Chptr 7: Ech Rcor is ssocit with Ky. Th kys cn compr with th usul comprison oprtors. By csting rcors to thir corrsponing kys, th comprison oprtors pply to rcors s wll s to kys. Th Binry Srch Tr Clss Trnsp. 12, Sct. 10.2, Binry Srch Trs 254 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Tr Srch Error co Srch tr<rcor> :: tr srch(rcor &trgt) const; Post: If thr is n ntry in th tr whos ky mtchs tht in trgt, th prmtr trgt is rplc y th corrsponing rcor from th tr n co of succss is rturn. Othrwis co of not prsnt is rturn. This mtho will oftn cll with prmtr trgt tht contins only ky vlu. Th mtho will fill trgt with th complt t longing to ny corrsponing Rcor in th tr. To srch for th trgt, w first compr it with th ntry t th root of th tr. If thir kys mtch, thn w r finish. Othrwis, w go to th lft sutr or right sutr s pproprit n rpt th srch in tht sutr. W progrm this procss y clling n uxiliry rcursiv function. Th procss trmints whn it ithr fins th trgt or hits n mpty sutr. Th uxiliry srch function rturns pointr to th no tht contins th trgt ck to th clling progrm. Sinc it is privt in th clss, this pointr mnipultion will not compromis tr ncpsultion. Binry no<rcor> *Srch tr<rcor> ::srch for no( Binry no<rcor>* su root, const Rcor &trgt) const; Pr: su root is NULL or points to sutr of Srch tr Post: If th ky of trgt is not in th sutr, rsult of NULL is rturn. Othrwis, pointr to th sutr no contining th trgt is rturn. Tr Srch Trnsp. 13, Sct. 10.2, Binry Srch Trs 255 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Rcursiv uxiliry function: tmplt <clss Rcor> Binry no<rcor> *Srch tr<rcor> ::srch for no( Binry no<rcor>* su root, const Rcor &trgt) const { if (su root == NULL su root->t == trgt) rturn su root; ls if (su root->t < trgt) rturn srch for no(su root->right, trgt); ls rturn srch for no(su root->lft, trgt); Nonrcursiv vrsion: tmplt <clss Rcor> Binry no<rcor> *Srch tr<rcor> ::srch for no( Binry no<rcor> *su root, const Rcor &trgt) const { whil (su root!= NULL && su root->t!= trgt) if (su root->t < trgt) su root = su root->right; ls su root = su root->lft; rturn su root; Auxiliry functions, tr srch Trnsp. 14, Sct. 10.2, Binry Srch Trs 256 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Pulic mtho for tr srch: tmplt <clss Rcor> Error co Srch tr<rcor> :: tr srch(rcor &trgt) const /* Post: If thr is n ntry in th tr whos ky mtchs tht in trgt, th prmtr trgt is rplc y th corrsponing rcor from th tr n co of succss is rturn. Othrwis co of not prsnt is rturn. Uss: function srch for no */ { Error co rsult = succss; Binry no<rcor> *foun = srch for no(root, trgt); if (foun == NULL) rsult = not prsnt; ls trgt = foun->t; rturn rsult; Tr srch functions Trnsp. 15, Sct. 10.2, Binry Srch Trs 257 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Binry Srch Trs with th Sm Kys f f c g g () c () g g c f f c c (c) f () g () Binry Srch Trs with th Sm Kys Trnsp. 16, Sct. 10.2, Binry Srch Trs 258 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Anlysis of Tr Srch Drw th comprison tr for inry srch (on n orr list). Binry srch on th list os xctly th sm comprisons s tr srch will o if it is ppli to th comprison tr. By Sction 7.4, inry srch prforms O(log n) comprisons for list of lngth n. This prformnc is xcllnt in comprison to othr mthos, sinc log n grows vry slowly s n incrss. Th sm kys my uilt into inry srch trs of mny iffrnt shps. If inry srch tr is nrly compltly lnc ( ushy ), thn tr srch on tr with n vrtics will lso o O(log n) comprisons of kys. If th tr gnrts into long chin, thn tr srch coms th sm s squntil srch, oing (n) comprsions on n vrtics. This is th worst cs for tr srch. Th numr of vrtics twn th root n th trgt, inclusiv, is th numr of comprisons tht must on to fin th trgt. Th ushir th tr, th smllr th numr of comprisons tht will usully n to on. It is oftn not possil to prict (in vnc of uiling it) wht shp of inry srch tr will occur. In prctic, if th kys r uilt into inry srch tr in rnom orr, thn it is xtrmly unlikly tht inry srch tr gnrts ly; tr srch usully prforms lmost s wll s inry srch. Anlysis of Tr Srch Trnsp. 17, Sct. 10.2, Binry Srch Trs 259 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Insrtion into Binry Srch Tr Error co Srch tr<rcor> :: insrt(const Rcor &nw t); Post: If Rcor with ky mtching tht of nw t lry longs to th Srch tr co of uplict rror is rturn. Othrwis, th Rcor nw t is insrt into th tr in such wy tht th proprtis of inry srch tr r prsrv, n co of succss is rturn. () Insrt () Insrt (c) Insrt f f () Insrt f () Insrt f f g g c (f) Insrt g (g) Insrt c Insrtion into Binry Srch Tr Trnsp. 18, Sct. 10.2, Binry Srch Trs 260 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458

Mtho for Insrtion tmplt <clss Rcor> Error co Srch tr<rcor> ::insrt(const Rcor &nw t) { rturn srch n insrt(root, nw t); tmplt <clss Rcor> Error co Srch tr<rcor> ::srch n insrt( Binry no<rcor> * &su root, const Rcor &nw t) { if (su root == NULL) { su root = nw Binry no<rcor>(nw t); rturn succss; ls if (nw t < su root->t) rturn srch n insrt(su root->lft, nw t); ls if (nw t > su root->t) rturn srch n insrt(su root->right, nw t); ls rturn uplict rror; Th mtho insrt cn usully insrt nw no into rnom inry srch tr with n nos in O(log n) stps. It is possil, ut xtrmly unlikly, tht rnom tr my gnrt so tht insrtions rquir s mny s n stps. If th kys r insrt in sort orr into n mpty tr, howvr, this gnrt cs will occur. Mtho for Insrtion Trnsp. 19, Sct. 10.2, Binry Srch Trs 261 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458