tr0 TREES Hanan Samet

Similar documents
tr0 TREES Hanan Samet

Tree. Tree. Siblings Grand Parent Grand Child H I J I J K. Tree Definitions

Depth-first search and strong connectivity in Coq

Noncrossing Trees and Noncrossing Graphs

Red-Black Trees Goodrich, Tamassia Red-Black Trees 1

arxiv:cs/ v1 [cs.ds] 8 Dec 1998

10 Torque. Lab. What You Need To Know: Physics 211 Lab

CS3350B Computer Architecture. Lecture 6.2: Instructional Level Parallelism: Hazards and Resolutions

Lecture Topics. Overview ECE 486/586. Computer Architecture. Lecture # 9. Processor Organization. Basic Processor Hardware Pipelining

Multi-Robot Forest Coverage

Complexity of Data Tree Patterns over XML Documents

ABriefIntroductiontotheBasicsof Game Theory

Design Engineering Challenge: The Big Dig Contest Platter Strategies: Ball Liberation

The Solution to the Bühlmann - Straub Model in the case of a Homogeneous Credibility Estimators

Multi-Robot Flooding Algorithm for the Exploration of Unknown Indoor Environments

Torque. Physics 2. Prepared by Vince Zaccone For Campus Learning Assistance Services at UCSB

Faster Nearest Neighbors: Voronoi Diagrams and k-d Trees

The Properties of. Model Rocket Body Tube Transitions

British Prime Minister Benjamin Disraeli once remarked that

CORESTA RECOMMENDED METHOD N 68

Fundamental Algorithms for System Modeling, Analysis, and Optimization

Rearranging trees for robust consensus

The Study About Stopping Distance of Vehicles

55CM ROUND CHARCOAL KETTLE BBQ

Efficient Algorithms for finding a Trunk on a Tree Network and its Applications

Multiple Vehicle Driving Control for Traffic Flow Efficiency

f i r e - p a r t s. c o m

Data Sheet. Linear bearings

Trees & Routing (1) BFS

POSSIBLE AND REAL POWERFLOWS IN CONNECTED DIFFERENTIAL GEAR DRIVES WITH η 0 <i pq <1/η 0 INNER RATIO

Lesson 33: Horizontal & Vertical Circular Problems

A Force Platform Free Gait Analysis

Cyclostrophic Balance in Surface Gravity Waves: Essay on Coriolis Effects

EC-FRM: An Erasure Coding Framework to Speed up Reads for Erasure Coded Cloud Storage Systems

An integrated supply chain design model with random disruptions consideration

Overlap-Based Genome Assembly from Variable-Length Reads

Cluster trees and message propagation

I. FORMULATION. Here, p i is the pressure in the bubble, assumed spatially uniform,

CS 173: Discrete Structures, Spring 2010 Homework 9 Solutions

Motivation. Prize-Collecting Steiner Tree Problem (PCSTP) Kosten und Profite. Das Fraktionale Prize-Collecting Steiner Tree Problem auf Baumgraphen

SHRiMP: Accurate Mapping of Short Color-space Reads

Wind and extremely long bridges a challenge for computer aided design

Color Encodings: srgb and Beyond

Incorporating Location, Routing and Inventory Decisions in Dual Sales Channel - A Hybrid Genetic Approach

Trends in Cycling, Walking & Injury Rates in New Zealand

Interior Rule of the Quebec Open 2017

Operating Instructions Compressors

Session 6. Global Imbalances. Growth. Macroeconomics in the Global Economy. Saving and Investment: The World Economy

PlacesForBikes City Ratings Methodology. Overall City Rating

THE performance disparity between processor speed and the

An Auction Based Mechanism for On-Demand Transport Services

DETC A NEW MODEL FOR WIND FARM LAYOUT OPTIMIZATION WITH LANDOWNER DECISIONS

MODEL 1000S DIGITAL TANK GAUGE

RESOLUTION No A RESOLUTION OF THE CITY OF SALISBURY, MARYLAND AUTHORIZING THE MAYOR TO ENTER INTO AN AGREEMENT BETWEEN THE CITY OF

Accel. (m/s 2 ) Time (sec) Newton s 3 rd Law and Circular Motion. Group Problem 04

A CONCEPTUAL WHEELED ROBOT FOR IN-PIPE INSPECTION Ioan Doroftei, Mihaita Horodinca, Emmanuel Mignon

Alternate stable states in coupled fishery-aquaculture systems. Melissa Orobko

"A Home for Gracious Living" (real estate brochure for Batten House)

MODELLING THE INTERACTION EFFECTS OF THE HIGH-SPEED TRAIN TRACK BRIDGE SYSTEM USING ADINA

Design and Simulation Model for Compensated and Optimized T-junctions in Microstrip Line

OPTIMAL SCHEDULING MODELS FOR FERRY COMPANIES UNDER ALLIANCES

Real World Search Problems. CS 331: Artificial Intelligence Uninformed Search. Simpler Search Problems. Example: Oregon. Search Problem Formulation

Diana Krall Straighten Up And Fly Right SONG TITLE: STRAIGHTEN UP AND FLY RIGHT ALBUM: STEPPING OUT RELEASED: 1993 LABEL: JUSTIN TIME GENRE: JAZZ

TLV493D-A1B6 3D Magnetic Sensor

Numerical study of super-critical carbon dioxide flow in steppedstaggered

Example. The information set is represented by the dashed line.

EcoMobility World Festival 2013 Suwon: an analysis of changes in citizens awareness and satisfaction

Experiment #10 Bio-Physics Pre-lab Questions

Matlab Simulink Implementation of Switched Reluctance Motor with Direct Torque Control Technique

STUDY OF IRREGULAR WAVE-CURRENT-MUD INTERACTION

CLASS: XI: MATHEMATICS

Phase Behavior Introduction to Phase Behavior F.E. Londono M.S. Thesis (2001)

Bubble clustering and trapping in large vortices. Part 1: Triggered bubbly jets investigated by phase-averaging

Problem Solving Agents

Carnegie Mellon University Forbes Ave., Pittsburgh, PA command as a point on the road and pans the camera in

A Deceleration Control Method of Automobile for Collision Avoidance based on Driver's Perceptual Risk

Watford Half Marathon. Watford Half Marathon. Sunday February 4th Starting at 10.30am. Enjoy Your Run!!! Notice to all Entrants.

UNIVERSITÀ DEGLI STUDI DI PADOVA. Dipartimento di Scienze Economiche Marco Fanno

DAM SITE 2017 STAGE 1

Complexity of Data Tree Patterns over XML Documents

Lecture 24. Wind Lidar (6) Direct Motion Detection Lidar

Cheat-Proof Playout for Centralized and Distributed Online Games

Uninformed Search Strategies

ANALYSIS AND TESTING OF AN INTEGRATED REFRIGERATION AND STORAGE SYSTEM FOR LIQUID HYDROGEN ZERO BOIL-OFF, LIQUEFACTION, AND DENSIFICATION

CENG 466 Artificial Intelligence. Lecture 4 Solving Problems by Searching (II)

Watford Half Marathon. Watford Half Marathon. Sunday February 5th Starting at 10.30am. Enjoy Your Run!!! Notice to all Entrants.

ELDORADO 2017 STAGE 1 - DRUNK & LOUD

Fire-Safety Analysis Timber. FRILO Software GmbH As of 29/06/2016

Electrical Equipment of Machine Tools

THE IMPACTS OF CONGESTION ON COMMERCIAL VEHICLE TOUR CHARACTERISTICS AND COSTS

The Dockline AUGUST Board of Directors. Greetings from the Commodore. Jim Manlick

OUTDOOR FOOTBALL FIELD

Problem Solving as Search - I

Project Proposal: Characterization of Tree Crown Attributes with High Resolution Fixed-Base Aerial Photography. by Rich Grotefendt and Rob Harrison

TECHNIQUE. Parental generation (P) Stamens Carpel 3. RESULTS First filial. offspring (F 1 )

High Axial Load Capacity, High speed, Long life. Spherical Roller Bearings

Angle-restricted Steiner arborescences for flow map layout Buchin, K.A.; Speckmann, B.; Verbeek, K.A.B.

Performance Characteristics of Parabolic Trough Solar Collector System for Hot Water Generation

Range Extension Control System for Electric Vehicles Based on Front and Rear Driving Force Distribution Considering Load Transfer

0ur Ref:CL/Mech/ Cal /BID-01(11-12) Date: 29 July 2011

Transcription:

t0 TREES Hanan Samet ompute Science epatment and ente fo utomation Reseach and Institute fo dvanced ompute Studies Univesity of Mayland ollege Pak, Mayland 074 e-mail: hjs@umiacs.umd.edu opyight 1997 Hanan Samet These notes may not e epoduced y any means (mechanical o electonic o any othe) without the expess witten pemission of Hanan Samet

TREE EFINITION t1 TREE a anching stuctue etween nodes finite set T of one o moe nodes such that: 1. one element of the set is distinguished, ROOT(T). the emaining nodes of T ae patitioned into m 0 disjoint sets T 1, T, T m and each of these sets is in tun a tee. tees T 1, T, T ae the sutees of the oot Recusive definition easy to pove theoems aout popeties of tees. Ex: pove tue fo 1 node assume tue fo n nodes pove tue fo n+1 nodes ORERE TREE if the elative ode of the sutees T 1, T, T is impotant ORIENTE TREE ode is not impotant m m odeed? = E E ompute epesentation odeed! opyight 1998 y Hanan Samet

TERMINOLOGY 1 t E F G ounteintuitive! EGREE nume of sutees of a node Teminal node leaf degee 0 RNH NOE non-teminal node Root is the fathe of the oots of its sutees Roots of sutees of a node ae othes Roots of sutees of a node ae sons of the node The oot of the tee has no fathe! is an ancesto of, E, G, G is a descendant of level(x) if fathe(x)=ω then 0 else 1+level(fathe(X)); Ex: level(g) = 1+level(F) 1+level() 1+level() 0 opyight 1998 y Hanan Samet

TERMINOLOGY 1 t level 0 ounteintuitive! E F G level 1 level level 3 EGREE nume of sutees of a node Teminal node leaf degee 0 RNH NOE non-teminal node Root is the fathe of the oots of its sutees Roots of sutees of a node ae othes Roots of sutees of a node ae sons of the node The oot of the tee has no fathe! is an ancesto of, E, G, G is a descendant of level(x) if fathe(x)=ω then 0 else 1+level(fathe(X)); Ex: level(g) = 1+level(F) 1+level() 1+level() 0 opyight 1998 y Hanan Samet

FORESTS N INRY TREES t3 FOREST a set (usually odeed) of 0 o moe disjoint tees, o equivalently: the nodes of a tee excluding the oot has the foest E F E F INRY TREE a finite set of nodes which eithe is empty o a oot and two disjoint inay tees called the left and ight sutees of the oot Is a inay tee a special case of a tee? opyight 1998 y Hanan Samet

FORESTS N INRY TREES t3 FOREST a set (usually odeed) of 0 o moe disjoint tees, o equivalently: the nodes of a tee excluding the oot has the foest E F E F INRY TREE a finite set of nodes which eithe is empty o a oot and two disjoint inay tees called the left and ight sutees of the oot Is a inay tee a special case of a tee? NO! n entiely diffeent concept 1 and ae diffeent inay tees opyight 1998 y Hanan Samet

FORESTS N INRY TREES 3 z t3 FOREST a set (usually odeed) of 0 o moe disjoint tees, o equivalently: the nodes of a tee excluding the oot has the foest E F E F INRY TREE a finite set of nodes which eithe is empty o a oot and two disjoint inay tees called the left and ight sutees of the oot Is a inay tee a special case of a tee? NO! n entiely diffeent concept 1 and 1 has an empty ight sutee has an empty left sutee ut as tees 1 and ae identical! ae diffeent inay tees opyight 1998 y Hanan Samet

OTHER REPRESENTTIONS OF TREES t4 Nested sets (also known as ule diagams ) G E F G E F Nested paentheses Tee (oot sutee 1 sutee... sutee n ) ( ( () ()) (G (E (F)))) inay tee Indentation G E F (oot left ight) ( ( ( () ()) ( () ())) (G (E (F () ()) ()) ())) ewey decimal notation:.1...3.4.5 opyight 1998 y Hanan Samet

t5 PPLITIONS Segmentation of lage ectangula aays [n,m] each ow is a segment (uoughs computes) [1] [] [n] [1,1] [1,] [1,m] [n,1] [n,] [n,m] lgeaic fomulas opeato opeand opeand +(( ) ) + 1. no need fo paentheses ut + = ( )+ (+). code geneation LW 1, LW, W, MW, W,1 opyight 1998 y Hanan Samet

LISTs (with a capital L!) t6 LIST a finite sequence of 0 o moe atoms o LISTS L=(,(,,),(),,((()))) () empty list Index notation: L[]=(,,) L[,1]= L[5,] L[5,1,1] iffeences etween LISTS and tees: 1. no data appeas in the nodes epesenting LISTS - i.e.,. LISTS may e ecusive M=(M) [M] Lael [M] 3. LISTS may ovelap (i.e., need not e disjoint) equivalently, sutees may e shaed N=(M,M,,N) [N] [M] [M] [N] [M] opyight 1998 y Hanan Samet

TRVERSING INRY TREES t7 Repesentation LLINK INFO RLINK Ω Ω Ω Ω Ω Ω Ω pplications: 1. code geneation in compiles. game tees in atificial intelligence 3. detect if a stuctue is eally a tee TREE one path fom each node to anothe node (unlike gaph) no cycles and opyight 1998 y Hanan Samet

TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G H F J inode = postode = Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet

TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G F H J I K E G H F J inode = postode = Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet

TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation 3 z t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G F H J I K E G H F J inode = postode = I K E G H F J Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet

TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation 4 g 3 z t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G F H J I K E G H F J inode = postode = I K E G H F J I K G E H J F Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet

INORER TRVERSL LGORITHM t9 pocedue inode(tee pointe T); egin stack ; tee pointe P; Ω; P T; while not(p=ω and =Ω) do egin if P=Ω then egin P ; /* Pop the stack */ visit(root(p)); P RLINK(P); end else egin P; /* Push on the stack */ P LLINK(P); Using ecusion: pocedue inode(tee pointe T); egin if T=Ω then etun else egin inode(llink(t)); visit(root(t)); inode(rlink(t)); opyight 1998 y Hanan Samet

THREE INRY TREES inay tee epesentation has too many Ω links Use 1-it tag fields to indicate pesence of a link If Ω link, then use field to stoe links to othe pats of the stuctue to aid the tavesal of the tee t10 Untheaded: Theaded: LLINK(p) = Ω LTG(p) = 0, LLINK(p) = $p = inode pedecesso of p LLINK(p) = q Ω LTG(p) = 1, LLINK(p) = q RLINK(p) = Ω RTG(p) = 0, RLINK(p) = p$ = inode successo of p RLINK(p) = q Ω RTG(p) = 1, RLINK(p) = q LLINK LTG INFO RTG RLINK Ex: HE E F G H J If addess of ROOT(T) < addess of left and ight sons, then don't need the TG fields Theads will point to lowe addesses! opyight 1998 y Hanan Samet

THREE INRY TREES t10 inay tee epesentation has too many Ω links Use 1-it tag fields to indicate pesence of a link If Ω link, then use field to stoe links to othe pats of the stuctue to aid the tavesal of the tee Untheaded: Theaded: LLINK(p) = Ω LTG(p) = 0, LLINK(p) = $p = inode pedecesso of p LLINK(p) = q Ω LTG(p) = 1, LLINK(p) = q RLINK(p) = Ω RTG(p) = 0, RLINK(p) = p$ = inode successo of p RLINK(p) = q Ω RTG(p) = 1, RLINK(p) = q LLINK LTG INFO RTG RLINK Ex: HE E F G H J If addess of ROOT(T) < addess of left and ight sons, then don't need the TG fields Theads will point to lowe addesses! opyight 1998 y Hanan Samet

THREE INRY TREES 3 z t10 inay tee epesentation has too many Ω links Use 1-it tag fields to indicate pesence of a link If Ω link, then use field to stoe links to othe pats of the stuctue to aid the tavesal of the tee Untheaded: Theaded: LLINK(p) = Ω LTG(p) = 0, LLINK(p) = $p = inode pedecesso of p LLINK(p) = q Ω LTG(p) = 1, LLINK(p) = q RLINK(p) = Ω RTG(p) = 0, RLINK(p) = p$ = inode successo of p RLINK(p) = q Ω RTG(p) = 1, RLINK(p) = q LLINK LTG INFO RTG RLINK Ex: HE E F G H J If addess of ROOT(T) < addess of left and ight sons, then don't need the TG fields Theads will point to lowe addesses! opyight 1998 y Hanan Samet

OPERTIONS ON THREE INRY TREES Find the inode successo of node P (P$) t11 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet

OPERTIONS ON THREE INRY TREES Find the inode successo of node P (P$) t11 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet

OPERTIONS ON THREE INRY TREES 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet

OPERTIONS ON THREE INRY TREES 4 g 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet

OPERTIONS ON THREE INRY TREES 5 v 4 g 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet

OPERTIONS ON THREE INRY TREES 6 5 v 4 g 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet

SUMMRY OF THREING t1 1. dvantages no need fo a stack fo tavesal will not un out of memoy duing inode tavesal can find inode successo of any node without having to tavese the entie tee. isadvantages insetion and deletion of nodes is slowe can t shae common sutees in the theaded epesentation Ex: two choices fo the inode successo of F G? G E F E F E F 3. Right-theaded tees inode algoithms make little use of left theads LTG(P)=1 test can e eplaced y LLINK(P)=Ω test opyight 1998 y Hanan Samet

PRINIPLES OF REURSION t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? opyight 1998 y Hanan Samet

PRINIPLES OF REURSION t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not opyight 1998 y Hanan Samet

PRINIPLES OF REURSION 3 z t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not Simplifying: and = if then o = else F opyight 1998 y Hanan Samet

PRINIPLES OF REURSION 4 g 3 z t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not Simplifying: and = if then o = else F if then T else simila(t 1,T ) = if empty(t 1 ) then if empty(t ) then T else F else if empty(t ) then F else if simila(left(t 1 ),left(t )) then simila(ight(t 1 ),ight(t )) else F ; opyight 1998 y Hanan Samet

PRINIPLES OF REURSION 5 v 4 g 3 z t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not Simplifying: and = if then o = else F if then T else simila(t 1,T ) = if empty(t 1 ) then empty(t ) if empty(t ) then T else F else if empty(t ) then F and else if simila(left(t 1 ),left(t )) then simila(ight(t 1 ),ight(t )) else F ; opyight 1998 y Hanan Samet

EQUIVLENE OF INRY TREES t14 Two inay tees T1 and T ae said to e equivalent if they ae simila and coesponding nodes contain the same infomation? equivalent(t1,t) = if empty(t1) and empty(t) then T else if empty(t1) o empty(t) then F else oot(t1)=oot(t) and equivalent(left(t1),left(t)) and equivalent(ight(t1),ight(t)); opyight 1998 y Hanan Samet

EQUIVLENE OF INRY TREES t14 Two inay tees T1 and T ae said to e equivalent if they ae simila and coesponding nodes contain the same infomation? NO! we ae dealing with inay tees and the left sutee of is not the same in the two cases equivalent(t1,t) = if empty(t1) and empty(t) then T else if empty(t1) o empty(t) then F else oot(t1)=oot(t) and equivalent(left(t1),left(t)) and equivalent(ight(t1),ight(t)); opyight 1998 y Hanan Samet

REURSION SUMMRY t15 voids having to use an explicit stack in the algoithm Polem fomulation is analogous to induction ase case, inductive case Ex: Factoial n! = n (n 1)! fact(n) = if n=0 then 1 else n*fact(n-1); The esult is otained y peeling one s way ack along the stack fact(3) = 3*fact() *fact(1) 1*fact(0) 1 = 6 Using an accumulato vaiale and a call fact(n,1): fact(n,total) = if n=0 then total else fact(n-1,n*total); Solution is iteative Recusion implemented on compute using stack instuctions. ec-system 10: PUSH, POP, PUSHJ, POPJ Stack pointe fomat: (count, addess) an simulate stack if no stack instuctions opyight 1998 y Hanan Samet

OMPLETE INRY TREES t16 When a inay tee is easonaly complete (most Ω links ae at the highest level), use a sequential stoage allocation scheme so that links ecome unnecessay E F level 0 level 1 level 1 3 4 5 6 7 Ω E F If n is the highest level at which a node is found, then at most n+1 1 wods ae needed Stoage allocation method: 1. oot has addess 1. left son of x has addess addess(x) 3. ight son of x has addess addess(x) + 1 When should a complete inay tee e used? n = highest level of the tee at which a node is found x = # of nodes in tee 3 wods pe node (left link, ight link, info) use a complete inay tee when x > ( n+1 1) / 3 opyight 1998 y Hanan Samet

FORESTS t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode opyight 1998 y Hanan Samet

FORESTS t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode peode = K E H F J G opyight 1998 y Hanan Samet

FORESTS 3 z t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode peode = K E H F J G postode = K H E J F G opyight 1998 y Hanan Samet

FORESTS 4 g 3 z t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode peode = K E H F J G postode = K H E J F G inode of inay tee opyight 1998 y Hanan Samet

EQUIVLENE RELTION t18 Given: elations as to what is equivalent to what (a ) Goal: is x y? Fomal definition of an equivalence elation 1. if x y and y z then x z (tansitivity). if x y then y x (symmety) 3. x x (eflexivity) Ex: S = {1.. 9} 1 5 6 8 7 9 8 3 7 4 9 3 is 6? opyight 1998 y Hanan Samet

EQUIVLENE RELTION t18 Given: elations as to what is equivalent to what (a ) Goal: is x y? Fomal definition of an equivalence elation 1. if x y and y z then x z (tansitivity). if x y then y x (symmety) 3. x x (eflexivity) Ex: S = {1.. 9} 1 5 6 8 7 9 8 3 7 4 9 3 is 6? Yes, since 7 3 9 8 6 Patitions S into disjoint susets o equivalence classes Two elements equivalent iff they elong to same class What ae the equivalence classes in this example? opyight 1998 y Hanan Samet

EQUIVLENE RELTION 3 z t18 Given: elations as to what is equivalent to what (a ) Goal: is x y? Fomal definition of an equivalence elation 1. if x y and y z then x z (tansitivity). if x y then y x (symmety) 3. x x (eflexivity) Ex: S = {1.. 9} 1 5 6 8 7 9 8 3 7 4 9 3 is 6? Yes, since 7 3 9 8 6 Patitions S into disjoint susets o equivalence classes Two elements equivalent iff they elong to same class What ae the equivalence classes in this example? {1,5} and {,3,4,6,7,8,9} opyight 1998 y Hanan Samet

LGORITHM t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 5 1 8 7 3 4 fathe(k): k: 1 3 4 5 6 7 8 9 9 6 opyight 1998 y Hanan Samet

LGORITHM t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 k: 1 3 4 5 6 7 8 9 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet

LGORITHM 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 6 8 k: 1 3 4 5 6 7 8 9 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet

LGORITHM 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 6 8 k: 1 3 4 5 6 7 8 9 7 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet

LGORITHM 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet

LGORITHM 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 9 6 opyight 1998 y Hanan Samet

LGORITHM 7 z 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 4 9 6 opyight 1998 y Hanan Samet

LGORITHM 8 g 7 z 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 4 9 6 9 3 opyight 1998 y Hanan Samet

LGORITHM 9 8 g 7 z 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 4 9 6 9 3 Moe efficient with path compession and weight alancing Execution time almost linea (invese of ckemann function) opyight 1998 y Hanan Samet