Uses for Binary Trees -- Binary Search Trees



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

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

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

Oracle PL/SQL Programming Advanced

Fundamentals of Tensor Analysis

Chapter 3 Chemical Equations and Stoichiometry

Quick Guide to Lisp Implementation

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

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

Words Symbols Diagram. abcde. a + b + c + d + e

Homework 3 Solutions

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

Data Structures and Algorithms V Otávio Braga

Integration by Substitution

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

4.1 Interval Scheduling. Chapter 4. Greedy Algorithms. Interval Scheduling. Interval Scheduling: Greedy Algorithms

Binary Representation of Numbers Autar Kaw

One Minute To Learn Programming: Finite Automata

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

CPS 220 Theory of Computation REGULAR LANGUAGES. Regular expressions

Diagram Editing with Hypergraph Parser Support

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

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

Java CUP. Java CUP Specifications. User Code Additions You may define Java code to be included within the generated parser:

Regular Sets and Expressions

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

Discovering Block-Structured Process Models From Event Logs Containing Infrequent Behaviour

Menu Structure. Section 5. Introduction. General Functions Menu

WHAT HAPPENS WHEN YOU MIX COMPLEX NUMBERS WITH PRIME NUMBERS?

1 Fractions from an advanced point of view

Link-Disjoint Paths for Reliable QoS Routing

AC Circuits Three-Phase Circuits

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

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

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

Lecture 3: Diffusion: Fick s first law

Lec 2: Gates and Logic

Usability Test Checklist

CompactPCI Connectors acc. to PIGMG 2.0 Rev. 3.0

5.4 Exponential Functions: Differentiation and Integration TOOTLIFTST:

ESCI 241 Meteorology Lesson 6 Humidity

Firm Objectives. The Theory of the Firm II. Cost Minimization Mathematical Approach. First order conditions. Cost Minimization Graphical Approach

Active Directory Service

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

Review guide for the final exam in Math 233

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

1.2 The Integers and Rational Numbers

OxCORT v4 Quick Guide Revision Class Reports

MATH 150 HOMEWORK 4 SOLUTIONS

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

Higher. Exponentials and Logarithms 160

December Homework- Week 1

Appendix D: Completing the Square and the Quadratic Formula. In Appendix A, two special cases of expanding brackets were considered:

Lecture 5. Inner Product

A122 MARION COUNTY HEALTH BUILDING HVAC, GLAZING AND LIGHTING RENOVATION 75% DOCUMENTS 08/31/2015

Math 135 Circles and Completing the Square Examples

1 Look at the text in each question. What does it say? Circle the correct letter A, B or C. Example

GENERAL OPERATING PRINCIPLES

Chapter. Contents: A Constructing decimal numbers

SKILL TEST IR(H) HELICOPTER SE ME Application and report form A. Udfyldes af ansøgeren/to be filled out by the applicant:

Ratio and Proportion

KEY SKILLS INFORMATION TECHNOLOGY Level 3. Question Paper. 29 January 9 February 2001

Vectors Recap of vectors

AP Calculus AB 2008 Scoring Guidelines

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

Recall from Last Time: Disjoint Set ADT

Where preparation meets opportunity. My Academic Planner. Early Academic Outreach Program (EAOP)

SE3BB4: Software Design III Concurrent System Design. Sample Solutions to Assignment 1

A Note on Approximating. the Normal Distribution Function

Long run: Law of one price Purchasing Power Parity. Short run: Market for foreign exchange Factors affecting the market for foreign exchange

Reasoning to Solve Equations and Inequalities

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

CS 316: Gates and Logic

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

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Boğaziçi University Department of Economics Spring 2016 EC 102 PRINCIPLES of MACROECONOMICS Problem Set 5 Answer Key

OUTLINE SYSTEM-ON-CHIP DESIGN. GETTING STARTED WITH VHDL August 31, 2015 GAJSKI S Y-CHART (1983) TOP-DOWN DESIGN (1)

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

c b N/m 2 (0.120 m m 3 ), = J. W total = W a b + W b c 2.00

Victims Compensation Claim Status of All Pending Claims and Claims Decided Within the Last Three Years

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

- DAY 1 - Website Design and Project Planning

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

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100

The Principle of No Punishment Without a Law for It LEARNING OBJECTIVES: CRLA.GAAN:

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

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

PLWAP Sequential Mining: Open Source Code

Welch Allyn CardioPerfect Workstation Installation Guide

Repeated multiplication is represented using exponential notation, for example:

MATH PLACEMENT REVIEW GUIDE

tis, cis cunc - cunc - tis, cis tis, cis cunc - tis, func - def - def - tis, U func - def - func - tis, pa - tri pa - tri pa - tri tu - per - tu -

Homeomorphic Alignment of Weighted Trees

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

Transcription:

CS122 Algorithms n Dt Struturs MW 11:00 m 12:15 pm, MSEC 101 Instrutor: Xio Qin Ltur 10: Binry Srh Trs n Binry Exprssion Trs Uss or Binry Trs Binry Srh Trs n Us or storing n rtriving inormtion n Insrt, lt, n srh str thn with link list n Tk vntg o logn hight n I: Stor inormtion in n orr wy (kys) A Proprty o Binry Srh Trs Trvrsing Binry Srh Trs n Th ky o th root is lrgr thn ny ky in th lt sutr n Th ky o th root is smllr thn ny ky in th right sutr n Not: Duplit kys r not llow n Thr r thr wys to trvrs inry tr n Inorr Trvrsl: Visit lt sutr; Visit root; Visit right sutr; Trvrsing Binry Srh Trs (ont.) n An Exmpl: voi inorr_print(no root) i (root!= NULL) inorr_print(root>lt_hil); ount << root>ino; inorr_print(root>right_hil); Srhing Binry Srh Trs n Lot n lmnt in inry srh tr voi srh(no root, ojt ky) i (root == NULL) rturn 1; i (root>ino == ky) rturn root>ino; ls i (ky < root>ino) srh(root>lt_hil, ky); ls srh(root>right_hil, ky); 1

Insrting n Elmnt in Binry Srh Trs n Srh or th Position in th tr whr th lmnt woul oun n Insrt th lmnt in th position n Not: nwly insrt no is l n Running tim is: O(n) th worst s O(lgn) i th tr is ln Uss or Binry Trs Binry Exprssion Trs n Binry trs r goo wy to xprss rithmti xprssions. Th lvs r oprns n th othr nos r oprtors. Th lt n right sutrs o n oprtor no rprsnt suxprssions tht must vlut or pplying th oprtor t th root o th sutr. Binry Exprssion Trs: Exmpls / Mrits o Binry Tr Form n Lt n right oprns r sy to visuliz n Co optimiztion lgorithms work with th inry tr orm o n xprssion n Simpl rursiv vlution o xprssion / Lvls Init Prn Th lvls o th nos in th tr init thir rltiv prn o vlution (w o not n prnthss to init prn). Oprtions t lowr lvls o th tr r vlut ltr thn thos t highr lvls. Th oprtion t th root is lwys th lst oprtion prorm. A Binry Exprssion Tr 3 4 2 Wht vlu os it hv? 11 ( 4 2 ) 3 = 18 12 2

Inorr Trvrsl: (A H) / (M Y) Inorr Trvrsl (ont.) tr Print son g Print lt sutr irst Print right sutr lst 13 Inorr trvrsl yils: ( ( )) ((( ) ) g) Prorr Trvrsl: / A H M Y Prorr Trvrsl (ont.) tr Print irst g Print lt sutr son Print right sutr lst 15 Prorr trvrsl yils: ( ( ( )) ( ( ( ) ) g)) Postorr Trvrsl: A H M Y / Postorr Trvrsl (ont.) tr Print lst g Print lt sutr irst Print right sutr son 17 Postorr trvrsl yils: g 3

Trvrsls n Exprssions lss ExprTr n Not tht th postorr trvrsl prous th postix rprsnttion o th xprssion. n Inorr trvrsl prous th inix rprsnttion o th xprssion. n Prorr trvrsl prous rprsnttion tht is th sm s th wy tht th progrmming lngug Lisp prosss rithmti xprssions! ExprTr ~ExprTr uil xprssion vlut ExprTr privt: root 4 2 3 lr 20 lss ExprTr puli: ExprTr ( ); // Construtor ~ExprTr ( ); // Dstrutor voi uil ( ); // uil tr rom prix xprssion voi xprssion ( ) onst; // output xprssion in ully prnthsiz inix orm lot vlut ( ) onst; // vlut xprssion voi lr ( ); // lr tr voi showstrutur ( ) onst; // isply tr privt: voi showsu( ); strut TrNo root; ; // rursiv prtnrs Eh no ontins two pointrs strut TrNo InoNo ino ; // Dt mmr TrNo lt ; // Pointr to lt hil TrNo right ; // Pointr to right hil ; NULL 6000 OPERAND 7 whihtyp oprn lt ino right 22 InoNo hs 2 orms num OpTyp OPERATOR, OPERAND ; strut InoNo OpTyp whihtyp; union hr oprtion ; int oprn ; ; OPERATOR whihtyp oprtion // ANONYMOUS union OPERAND 7 whihtyp oprn 23 int Evl ( TrNo ptr ) swith ( ptr>ino.whihtyp ) s OPERAND : rturn ptr>ino.oprn ; s OPERATOR : swith ( tr>ino.oprtion ) s : rturn ( Evl ( ptr>lt ) Evl ( ptr>right ) ) ; s : rturn ( Evl ( ptr>lt ) Evl ( ptr>right ) ) ; s : rturn ( Evl ( ptr>lt ) Evl ( ptr>right ) ) ; s : rturn ( Evl ( ptr>lt ) / Evl ( ptr>right ) ) ; 24 4

Construting n Exprssion Tr n Thr is simpl O(N) stks lgorithm to onvrt postix xprssion into n xprssion tr. n Rll w lso hv n lgorithm to onvrt n inix xprssion into postix, so w n lso onvrt n inix xprssion into n xprssion tr without iiulty (in O(N) tim). Exprssion Tr Algorithm n R th postix xprssion on symol t t tim: I th symol is n oprn, rt onno tr n push pointr to it onto th stk. I th symol is n oprtor, pop two tr pointrs T1 n T2 rom th stk, n orm nw tr whos root is th oprtor, n whos hilrn r T1 n T2. Push th nw tr pointr on th stk. Exmpl : Exmpl : Not: Ths stks r pit horizontlly. Exmpl : Exmpl : 5