time using O( n log n ) processors on the EREW PRAM. Thus, our algorithm improves on the previous results, either in time complexity or in the model o

Similar documents
Fast Parallel String Prex-Matching. Dany Breslauer. April 6, Abstract. n log m -processor CRCW-PRAM algorithm for the

[25] T. Watanabe, An ecient way for edge-connectivity augmentation, Tech. Rep. ACT-

We assume uniform hashing (UH):

Optimal Parallel Randomized Renaming

An Optimal Parallel Algorithm for Merging using Multiselection

A Fast Algorithm for Optimal Alignment between Similar Ordered Trees

Theorem 2.9: nearest addition algorithm

A new NC Algorithm for Perfect Matching in Bipartite Cubic Graphs

THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

Heap-on-Top Priority Queues. March Abstract. We introduce the heap-on-top (hot) priority queue data structure that combines the

Parallel Recognition of Series-Parallel Graphs

THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

II (Sorting and) Order Statistics

A SIMPLE APPROXIMATION ALGORITHM FOR NONOVERLAPPING LOCAL ALIGNMENTS (WEIGHTED INDEPENDENT SETS OF AXIS PARALLEL RECTANGLES)

Fast and Simple Algorithms for Weighted Perfect Matching

Distributed minimum spanning tree problem

A novel algorithm to determine the leaf (leaves) of a binary tree from its preorder and postorder traversals

Graph Algorithms Using Depth First Search

Optimum Alphabetic Binary Trees T. C. Hu and J. D. Morgenthaler Department of Computer Science and Engineering, School of Engineering, University of C

Tilings of the Euclidean plane

st-orientations September 29, 2005

16 Greedy Algorithms

A technique for adding range restrictions to. August 30, Abstract. In a generalized searching problem, a set S of n colored geometric objects

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

A New Graph Triconnectivity Algorithm and Its Parallelization*

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

FOUR EDGE-INDEPENDENT SPANNING TREES 1

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

Efficient pebbling for list traversal synopses

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

PAPER Constructing the Suffix Tree of a Tree with a Large Alphabet

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Lowest Common Ancestor(LCA)

Localization in Graphs. Richardson, TX Azriel Rosenfeld. Center for Automation Research. College Park, MD

An Ecient Approximation Algorithm for the. File Redistribution Scheduling Problem in. Fully Connected Networks. Abstract

Computing intersections in a set of line segments: the Bentley-Ottmann algorithm

A Linear-Processor Algorithm for Depth-First Search in Planar Graphs

Fast Parallel and Serial Approximate. Consider the string matching problem, where dierences between characters

Multiway Blockwise In-place Merging

A General Class of Heuristics for Minimum Weight Perfect Matching and Fast Special Cases with Doubly and Triply Logarithmic Errors 1

On the Complexity of the Policy Improvement Algorithm. for Markov Decision Processes

On the other hand, the main disadvantage of the amortized approach is that it cannot be applied in real-time programs, where the worst-case bound on t

An Optimal Parallel Algorithm for Merging using Multiselection y

Courant Institute. This research was supported in part by NSF grant DCR and by an IBM faculty development award.

would be included in is small: to be exact. Thus with probability1, the same partition n+1 n+1 would be produced regardless of whether p is in the inp

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

4 Basics of Trees. Petr Hliněný, FI MU Brno 1 FI: MA010: Trees and Forests

on Mesh Connected Computers and Meshes with Multiple Broadcasting Ion Stoica Abstract

where is a constant, 0 < <. In other words, the ratio between the shortest and longest paths from a node to a leaf is at least. An BB-tree allows ecie

Notes on Binary Dumbbell Trees

Lecture 15. Error-free variable length schemes: Shannon-Fano code

arxiv: v3 [cs.ds] 18 Apr 2011

Finger Search Trees with Constant. Insertion Time. Gerth Stlting Brodal. Max-Planck-Institut fur Informatik. Im Stadtwald. D Saarbrucken

An improved algorithm to reconstruct a binary tree from its inorder and postorder traversals

r (1,1) r (2,4) r (2,5) r(2,6) r (1,3) r (1,2)

Algorithms. Lecture Notes 5

15 212: Principles of Programming. Some Notes on Induction

Implementation of Hopcroft's Algorithm

A Simplified Correctness Proof for a Well-Known Algorithm Computing Strongly Connected Components

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Lecture 8: The Traveling Salesman Problem

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

A Simplied NP-complete MAXSAT Problem. Abstract. It is shown that the MAX2SAT problem is NP-complete even if every variable

ADAPTIVE SORTING WITH AVL TREES

A Note on Scheduling Parallel Unit Jobs on Hypercubes

FUTURE communication networks are expected to support

Interval Stabbing Problems in Small Integer Ranges

Efficient Parallel Binary Search on Sorted Arrays

Module 7. Independent sets, coverings. and matchings. Contents

A Linear Time Algorithm for the Minimum Spanning Tree Problem. on a Planar Graph. Tomomi MATSUI. (January 1994 )

Verifying a Border Array in Linear Time

Binary Search Trees Treesort

Lecture 3 February 20, 2007

Lecture 2 February 4, 2010

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Fundamental Properties of Graphs

On k-dimensional Balanced Binary Trees*

A more efficient algorithm for perfect sorting by reversals

Stores a collection of elements each with an associated key value

Ma/CS 6b Class 26: Art Galleries and Politicians

Programming II (CS300)

The Encoding Complexity of Network Coding

Bipartite Roots of Graphs

Binary Heaps in Dynamic Arrays

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

2 Eulerian digraphs and oriented trees.

Search Trees. Undirected graph Directed graph Tree Binary search tree

Decreasing the Diameter of Bounded Degree Graphs

Ecient Implementation of Sorting Algorithms on Asynchronous Distributed-Memory Machines

CH 8. HEAPS AND PRIORITY QUEUES

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

Graph Theory Questions from Past Papers

EE 368. Week 6 (Notes)

Finger Search Trees with Constant Insertion Time. Gerth Stlting Brodal. Max-Planck-Institut fur Informatik. Im Stadtwald, D Saarbrucken, Germany

Lecture 3: Art Gallery Problems and Polygon Triangulation

Pebble Sets in Convex Polygons

Graph Algorithms. Tours in Graphs. Graph Algorithms

CS2 Algorithms and Data Structures Note 6

Applied Mathematics Letters. Graph triangulations and the compatibility of unrooted phylogenetic trees

Transcription:

Reconstructing a Binary Tree from its Traversals in Doubly-Logarithmic CREW Time Stephan Olariu Michael Overstreet Department of Computer Science, Old Dominion University, Norfolk, VA 23529 Zhaofang Wen y Department of Computer Science, University of Minnesota, Duluth, MN55812 Abstract We consider the following problem. For a binary tree T = (V; E) where V = f1; 2; :::; ng, given its inorder traversal and either its preorder or its postorder traversal, reconstruct the binary tree. We present a new parallel algorithm for this problem. Our algorithm requires O(n) space. The main idea of our algorithm is to reduce the reconstruction process to merging two sorted sequences. With the best parallel merging algorithms, our algorithm can be implemented n in O(log log n) time using O( log log n ) processors on the CREW PRAM (or in O(log n) time using O( n log n ) processors on the EREW PRAM). Our result provides one more example of a fundamental problem which can be solved by optimal parallel algorithms in O(log log n) time on the CREW PRAM. 1 Introduction We consider the problem of reconstructing a binary tree T = (V; E) with vertices f1; 2; :::; ng given its inorder traversal and either its preorder or its postorder traversal. It is well-known that a binary tree can be reconstructed from its inorder traversal along with either its preorder or its postorder traversals [8]. Recently, sequential solutions to this classical problem have reported in [1, 4]. Especially, the algorithm in [1] requires O(n) time and space. Parallel solutions to this problem can be found in [3, 10]. In particular, the algorithm in [10] runs in O(log n) time using O(n) processors on the CREW PRAM; and the solution in [3] takes in O(log log n) time using O( n log log n ) processors on the CRCW PRAM. Most recently, parallel algorithms running in O(log n) time with O( n log n ) processors on the EREW PRAM were also reported in [7, 5] 1. In this paper, we present a new parallel algorithm for this problem. Our algorithm requires O(n) space. The main idea of our algorithm is to reduce the reconstruction process to merging two sorted sequences. With the best results for parallel merging, our algorithm can be implemented in O(log log n) time using O( n log log n ) processors on the CREW PRAM or in O(log n) This author was supported in part by the National Science Foundation under grant CCR-8909996 y Part of the work was done when the author was with Dept. of Computer Science, Old Dominion University 1 We learned about these algorithms after submission of this paper 1

time using O( n log n ) processors on the EREW PRAM. Thus, our algorithm improves on the previous results, either in time complexity or in the model of computation. As pointed out in [3], optimal parallel algorithms that run in O(log log n) time usually need to be implemented on the CRCW PRAM. For example, any optimal O(log log n) time parallel algorithm for the All Nearest Smaller Values (ANSV) problem [3] needs the CRCW PRAM. It is worth noting that in [3] the solution for the ANSV problem is used to solve the binary tree reconstruction problem in O(log log n) time, with O( n log log n ) processors on the CRCW PRAM. The only known doubly logarithmic optimal CREW algorithm is Kruskal's parallel merging algorithm [9], which runs in O(log log n) time using O( n log log n ) processors on the CREW PRAM [9]. The main contribution of this paper is to provide another example of designing doubly logarithmic time parallel algorithm on the CREW PRAM. 2 Preliminaries Many methods can be used to generate traversals for a binary tree. Here, we are interested in one of them, known as the Euler Tour technique. This powerful technique was proposed by Tarjan and Vishkin [11] for designing ecient parallel algorithms on trees. Specically, this technique reduces the computation of various kinds of information about the tree structure to the computation on a linked list. To make this paper self-contained, the Euler tour technique for binary trees is described next. The Euler tour technique: Let T be a binary tree. Every node v of T is split into three copies v 1, v 2, and v 3, all having the same node label as v. For each of the resulting nodes, we dene a next eld as follows: If v has no left child then v 1 :next = v 2. If v has no right child then v 2 :next = v 3. If w is the left child of v then v 1 :next = w 1, and w 3 :next = v 2. If w is the right child of v then v 2 :next = w 1 and w 3 :next = v 3. What results is a linked list, called an Euler path, which starts at root 1 and ends at root 3 and traverses each edge of T exactly once in each direction. Letting T be a binary tree tree with left subtree T 1 and right subtree T 2. The Euler path (T ) of T can be expressed as root 1 (T 1 )root 2 (T 2 )root 3. When no confusion is possible, we let the Euler path denote the sequence of node labels contained in the corresponding linked list. Obviously, the Euler path of a binary tree contains 2

1 AA A A A 2 3 B B B B B B 4 5 6 C C C 7 8 Euler path: 1 2 4 4 4 2 5 7 7 7 5 8 8 8 5 2 1 3 3 6 6 6 3 1 Preorder-inorder Euler path: 1 2 4 4 2 5 7 7 5 8 8 1 3 3 6 6 Preorder traversal: 1 2 4 5 7 8 3 6 Inorder traversal: 4 2 7 5 8 1 3 6 Figure 1: a binary tree and its various (Euler) paths and traversals three copies of each node label in the tree. An interesting property of the Euler path of a binary tree T is that keeping only the rst copy of each label results in a preorder traversal of T; keeping only the second copy of each label gives an inorder traversal of T ; and keeping only the third copy of each label yields a postorder traversal of T [11]. For convenience, we dene a preorder-inorder Euler path to be a sequence of labels obtained by deleting the third copy of each label in an Euler path. Similarly, we can dene an inorderpostorder Euler path as a sequence of labels obtained by deleting the rst copy of each label in an Euler path. It is well-known that a binary tree can be reconstructed from its inorder traversal along with either its preorder or postorder traversal [8]. It follows that a binary tree is completely determined by its preorder-inorder Euler path or its inorder-postorder Euler path. For example, Figure 1 features a binary tree along with the associate Euler path, preorderinorder Euler path, preorder traversal, and inorder traversal. Lemma 1 A sequence of labels b 1, b 2,...,b 2n represents a preorder-inorder Euler path (respectively, the inorder-postorder Euler path) of an n-node binary tree T if and only if, the following two conditions hold: (1) there are exactly two copies of each label in the sequence; and (2) there exist no integers i; j; k; m with 1 i < j < k < m 2n such that b i = b k and b j = b m. Proof We prove the statement for the case of a preorder-inorder Euler path (the case of an 3

inorder-postorder Euler path follows by a mirror argument). Let (T ) denote the preorder-inorder Euler path of a tree T. Note that the preorderinorder Euler path of a tree with left subtree T 1 and right subtree T 2 can be expressed as root 1 (T 1 )root 2 (T 2 ). Thus, the \only if" part of the lemma follows by an obvious inductive argument. The \if" part will be proved by induction on n. When n = 1 the lemma is obviously true. Assume the lemma true for 1 < n < k. When n = k, let b t be the second copy of b 1, i.e. b t = b 1. By condition (2) and the induction hypothesis, b 2,..., b t?1 and b t+1,..., b 2n can both be seen as a preorder-inorder Euler paths. Let T 1 and T 2 be the binary trees determined by b 2,..., b t?1 and b t+1,..., b 2n, respectively. The tree rooted at b 1 with left subtree T 1 and right subtree T 2 is the binary tree determined by b 1,..., b 2n. 2 Intuitively, Lemma 1 states that any preorder-inorder Euler path containing distinct labels u and v must be of the form \:::u:::v:::v:::u:::" or \:::u:::u:::v:::v:::"; it is impossible for a preorderinorder Euler path to be of the form \:::u:::v:::u:::v:::", which we refer to as overlapping of labels u and v. Let b 1, b 2,..., b 2n be a preorder-inorder Euler path with 2n labels. For any pair of duplicate labels b i and b j (1 i < j n), we call b i the rst copy of its duplicate label and b j the second copy of its duplicate label. The following corollary of Lemma 1 will be used in the Appendix. Corollary 1 Let c 1, c 2,..., c n and d 1, d 2,..., d n be the preorder and inorder traversals of a binary tree, respectively. There do not exist integers i 1, i 2, j 1, j 2, k 1, k 2 such that (1 i 1 < j 1 < k 1 n) and (1 k 2 < i 2 < j 2 n), and (c i1 = d i2 ) ^ (c j1 = d j2 ) ^ (c k1 = d k2 ). Proof. (by contradiction) Assume there exist integers i 1, i 2, j 1, j 2, k 1, k 2 such that (1 i 1 < j 1 < k 1 n) and (1 k 2 < i 2 < j 2 n), and (c i1 = d i2 ) ^ (c j1 = d j2 ) ^ (c k1 = d k2 ). Then, in the corresponding preorder-inorder Euler path, d k2 is the second copy of the node label c k1, and we can nd :::c i1 :::c j1 :::c k1 :::d k2 :::d i2 :::d j2 ::: contradicting condition (2) of Lemma 1 (due to :::c i1 :::c j1 ::::::d i2 :::d j2 :::). 2 Intuitively, Corollary 1 states that if the preorder traversal of a binary tree is of the form \:::u:::v:::w:::", then the inorder traversal can never be of form \:::w:::u:::v:::"; where u, v, and 4

w are three distinct node labels. 3 The Algorithm To reconstruct a binary tree from its preorder and inorder traversals, our algorithm has two steps. The rst step is to construct the preorder-inorder Euler path of the binary tree determined by the traversals. Once the preorder-inorder Euler path is available, it is straightforward to reconstruct the corresponding binary tree. Hence, we would like to give rst the following procedure to implement the second step: Procedure EulerPath-to-Tree; Input: b 1, b 2,...,b 2n, a preorder-inorder Euler path in which every label remembers the position of its duplicate. Output: A binary tree with root r and set of nodes S = f1; :::; ng; begin r b 1 ; for each label b i (1 i 2n) do if (b i is the rst copy of its duplicate) then if (b i+1 is the rst copy of its duplicate label) then leftchild(b i ) b i+1 ; else fcomment: b i is the second copy of its duplicateg if (b i+1 is the rst copy of its duplicate label) then rightchild(b i ) b i+1 ; end. Lemma 2 Given a preorder-inorder Euler path with 2n labels in which each label remembers the position of its duplicate, procedure EulerPath-to-Tree correctly reconstructs the corresponding binary tree in O(1) time using O(n) processors on the EREW PRAM. Proof. The correctness of the algorithm follows directly from the proof of Lemma 1. It is obvious that the procedure above can be implemented in O(1) time using O(n) processors on the EREW PRAM. 2 5

We now turn to the rst step of our tree reconstruction algorithm, that is, the construction of the preorder-inorder Euler path from the traversals. The main idea of this step is suggested by Lemma 1, which tells that overlapping of labels is impossible in a preorder-inorder Euler path. In addition, we know that keeping only the rst copy of each label in a preorder-inorder Euler path gives the preorder traversal of the binary tree, and that keeping only the second copy of each label in an preorder-inorder Euler path yields the inorder traversal of the binary tree. The idea here is to \merge" the preorder and inorder traversals into a sequence that satises the above properties. To give the reader some intuition about this idea, we show that this \merging" process can actually be carried out with the help of a stack, as in the following procedure. ProcedureTraversals-to-EulerPath; Input:c 1, c 2,...,c n, the preorder traversal of a binary tree, and d 1, d 2,...,d n, the inorder traversal of the same binary tree. Output: b 1, b 2,...,b 2n, the preorder-inorder Euler path of the binary determined by the traversals. begin Stack ; j k 1; for i 1 to 2n do if (d k matches the label on the top of Stack) then b i d k ; pop Stack; and d k remembers each other's position in the sequence b 1, b 2,...,b 2n ; k k + 1; else b i c j ; push c j onto Stack; j j + 1; return(b 1, b 2,...,b 2n ); end. 6

Lemma 3 Given the preorder and the inorder traversals of an n-node binary tree T, Procedure Traversals-to-EulerPath correctly construct in O(n) time the preorder-inorder Euler path of T, such that every label remembers the position of its duplicate in the path. Proof. We prove the correctness of the procedure by induction on n. When n = 1, the correctness is obvious. Assume the procedure correct for 1 < n < k. Consider the case when n = k. Without loss of generality, assume d q = c 1. By the denitions of the inorder and preorder traversals, c 1 is the root of T, and the left subtree of T has preorder traversal c 2,..., c q and inorder traversal d 1,..., d q?1 ; the right subtree of T has preorder traversal c q+1,..., c k and inorder traversal d q+1,..., d k. By the induction hypothesis, consuming subsequences c 2,..., c q and d 1,..., d q?1 the procedure computes b 2, b 3,..., b 2q?2 as the preorder-inorder Euler path of the left subtree, with c 1 left on the top of Stack. After matching d q with the top of Stack (i.e. c 1 ), the algorithm proceeds to computes b 2q, b 2q+1,..., b 2k as the preorder-inorder Euler path of the right subtree by consuming subsequences c q+1,..., c k and d q+1,..., d k. From the discussion above, we see that the output sequence b 1, b 2,...,b 2n satises both conditions of Lemma 1. Thus, the output sequence represents the preorder-inorder path of some tree. Furthermore, keeping only the rst copies of the labels in b 1,...,b 2n results in c 1,...,c n, while keeping only the the second copies of the labels gives d 1,...,d n. It follows that b 1, b 2,...,b 2n is the preorder-inorder Euler path of T. According to the way the Stack is used in the algorithm, we ensure that each label in the output sequence remembers the position of its duplicate. The procedure takes O(n) sequential time, since each iteration of the for loop runs in O(1) time. 2 Combining Lemma 2 and Lemma 3, we have the following result. Theorem 1 An n-node binary tree can be reconstructed from its preorder and inorder traversal in O(n) time with O(n) extra space. 2 Although it is easy to implement procedure Traversals-to-EulerPath sequentially, it is not obvious how the procedure can be implemented in parallel. In the following, we will show that the idea behind the Stack being used in the procedure can lead to the denition of a linear order. With this linear order, both the preorder and the inorder traversals are sorted sequences; 7

and the construction of the preorder-inorder path can be done by merging these two sorted sequences according to the linear order. Let c 1 ; c 2 ; :::c n and d 1 ; d 2 ; :::; d n be the preorder and inorder traversals of T. For simplicity, we assume that c 1 ; c 2 ; :::c n is 1; 2; :::; n. (the case where c 1 ; c 2 ; :::c n is a permutation of 1, 2,..., n can be reduced to this case easily). Construct two sequences of triples: a sequence (1; j 1 ; c 1 ); (1; j 2 ; c 2 ); :::; (1; j n ; c n ) such that d ji = c i ; (i = 1; 2; :::; n) (i.e. j i is the position of c i in sequence d 1 ; d 2 ; :::; d n ), and a sequence (2; 1; d 1 ); (2; 2; d 2 ); :::; (2; n; d n ). Denote Q = f(1; j 1 ; c 1 ); (1; j 2 ; c 2 ); :::; (1; j n ; c n ); (2; 1; d 1 ); (2; 2; d 2 ); :::; (2; n; d n )g. Dene a binary relation on Q as follows: for arbitrary triples (; ; ) and ( 0 ; 0 ; 0 ) in Q we have: Rule 1. (( = 1) ^ ( 0 = 1))! (((; ; ) ( 0 ; 0 ; 0 )) $ ( < 0 )); Rule 2. (( = 2) ^ ( 0 = 2))! (((; ; ) ( 0 ; 0 ; 0 )) $ ( < 0 )); Rule 3. (( = 1) ^ ( 0 = 2))! (((; ; ) ( 0 ; 0 ; 0 )) $ (( 0 ) _ ( 0 ))) The motivation behind Rules 1{2 is the fact that keeping only the rst (second) copies of the labels in the preorder-inorder Euler path gives the preorder (inorder) traversal of the binary tree. Therefore, when combining the traversals the original relative order between labels from the same traversal should be maintained. (For the case of Rule 1, notice the assumption that c 1 ; c 2 ; :::c n is 1; 2; :::; n.) The motivation for Rule 3 is the following: For any c i and d j, their relative order in the merged sequence should be determined in a way consistent with the fact that overlapping of labels is not allowed in the preorder-inorder Euler path (see Lemma 1), and that the copy of a label from the preorder traversal should always come before the copy of the same label from the inorder traversal. It is easy to conrm that the binary relation is total on Q. In addition, it turns out that it is also transitive (refer to the Appendix for the proof), and so is a linear order on Q. Rules 1{2 guarantee that is consistent with both sequences (1; j 1 ; c 1 ), (1; j 2 ; c 2 ),..., (1; j n ; c n ) and (2; 1; d 1 ), (2; 2; d 2 ),..., (2; n; d n ). By merging these two sequences according to we obtain a sorted sequence of 2n triples, ( 1 ; 1 ; 1 ), ( 2 ; 2 ; 2 ),..., ( 2n ; 2n ; 2n ). We claim that 1, 2,..., 2n is the preorder-inorder Euler path of the tree determined by the traversals. By Lemma 1, the correctness of the claim relies on the following facts: 8

(a) There are exactly two copies of each label in 1, 2,..., 2n ; the rst copy is from the preorder traversal and the second copy is from the inorder traversal. (b) There do not exist 1 i < j < k < m 2n such that i = k and j = m (c) Deleting the second copies of the duplicate labels in 1, 2,..., 2n results in c 1 ; c 2 ; :::c n ; and deleting the rst copies of the duplicate labels in 1, 2,..., 2n gives d 1 ; d 2 ; :::d n. Fact (a) follows from Rule 3 and the construction of the set Q. By the denition of both sequences (1; j 1 ; c 1 ), (1; j 2 ; c 2 ),..., (1; j n ; c n ) and (2; 1; d 1 ), (2; 2; d 2 ),..., (2; n; d n ) are already sorted, so fact (c) is also true. The proof of fact (b) is given below: Proof of (b). (by contradiction) (1)( i = k ) ^ ( j = m ) [Assumption] where 1 i < j < k < m 2n (2)( i ; i ; i ) ( j ; j ; j ) ( k ; k ; k ) [by 1 i < j < k < m 2n ( m ; m ; m ) in (1)] (3)( i = 1) ^ ( j = 1) ^ ( k = 2) ^ ( m = 2) [by (1), (2), Rule 3 and the construction of the triples] (4)( i = k ) ^ ( j = m ) [ by (1) and the construction of the triples ] (5)( j k ) _ ( j k ) [by ( j = 1) ^ ( k = 2) in (3), ( j ; j ; j ) ( k ; k ; k ) in (2) and Rule 3] (6)( m k ) _ ( m k ) [replace j ; j in (5) with m ; m respectively, by (1) and (4)] (7)( k < m ) [by ( k ; k ; k ) ( m ; m ; m ) in (2), ( k = 2) ^ ( m = 2) in (3) and Rule 2] (8)( m k ) [by (6) and (7)] (9)( i < j ) [by ( i ; i ; i ) ( j ; j ; j ) in (2), ( i = 1) ^ ( j = 1) in (3) and Rule 1] (10)( k < m ) [by (1) and (9) ] (11)Contradiction [(8) and (10)] 2 Up to this point, we have successfully reduced computing the preorder-inorder path to parallel merging. We now discuss the complexity of this reduction. First, we consider the complexity to construct Q. For this purpose, let us see how to construct from the given traversals, sequences (1; j 1 ; c 1 ), (1; j 2 ; c 2 ),..., (1; j n ; c n ) and (2; 1; d 1 ), (2; 2; d 2 ),..., (2; n; d n ) such that c i = d ji (i = 1; 2; :::; n). We note that this can be done easily with an auxiliary array A[1::n]. Since c 1 ; c 2 ; :::; c n is 1; 2; :::; n, and d 1 ; d 2 ; :::; d n is a permutation of 1; 2; :::; n, we can compute the entries of A[1::n] as follow: A[d i ] = i (1; 2; :::; n) in O(1) time on an EREW PRAM with n processors. To determine the subscript j i satisfying c i = d ji (1; 2; :::; n), we simply take j i = A[c i ] 9

(1; 2; :::; n). This again can be computed in O(1) time on an EREW PRAM with n processors. Consequently, Q can be constructed in O(1) time using n processors on an EREW PRAM. Next, we consider the complexity to merge (1; j 1 ; c 1 ), (1; j 2 ; c 2 ),..., (1; j n ; c n ) and (2; 1; d 1 ), (2; 2; d 2 ),..., (2; n; d n ) according. Optimal parallel algorithms are proposed in [2, 6, 9]. With their results, we have, Theorem 2 A binary tree T = (V; E) where V = f1; 2; :::; ng, can be reconstructed from its preorder and inorder traversals in O(log log n) time using O( n log log n ) processors on the CREW PRAM, or in O(log n) time using O( n log n ) processors on the EREW PRAM, using O(n) extra space, 2 In case the inorder traversal and the postorder traversal is given, the binary tree can be reconstructed similarly. 4 Conclusion We have shown how to reconstruct a binary tree from its inorder traversal along with either its preorder traversal or its postorder traversal by reducing the problem to parallel merging. With the best known results for parallel merging, our reconstruction algorithm can be implemented in O(log log n) time using O( n log log n ) processors on the CREW PRAM (or in O(log n) time using O( n log n ) processors on the EREW PRAM). We have thus found one more example in the class of problems that can be solved in doubly logarithmic time using optimal number of processors on the CREW PRAM. Acknowledgement: The authors would like to thank two anonymous referees for a very thorough review that resulted in a much better presentation. References [1] A. Anderson and S. Carlsson. Construction of a tree from its traversals in optimal time and space. Information Processing Letters, 34:21{25, 1990. [2] R. J. Anderson, E. W. Mayr, and M. K. Warmuth. Parallel approximation algorithms for bin backing. Information and Computation, 82:262{277, October 1989. 10

[3] O. Berkman, D. Breslauer, Z. Galil, B. Schieber, and U. Vishkin. Highly parallelizable problems. In Proceedings of the Annual IEEE Symposium on Foundation of Computer Science, pages 309{319, 1989. [4] G. H. Chen, M. S. Yu, and L. T. Liu. Two algorithms for constructing a binary tree from its traversals. Information Processing Letters, 28:297{299, 1988. [5] N. Gabrani and P. Shankar. A note on the reconstruction of a binary tree from its traversals. Information Processing Letters, 42:117{119, 1992. [6] T. Hagerup and C. Rub. Optimal merging and sorting on the EREW PRAM. Information Processing Letters, 33:181{185, 1989. [7] V. Kamakoti and C. Pandu Rangan. An optimal algorithm for reconstructing a binary tree. Information Processing Letters, 42:113{115, 1992. [8] D. E. Knuth. The Art of Computer Programming, Vol. 1, Fundamental Algorithms. Second Edition, Addison-Wesley, Reading, Mass., 1973. [9] C. P. Kruskal. Searching, merging, and sorting in parallel computation. IEEE Transactions on Computers, 32:924{946, 1983. [10] F. Springsteel and I. Stojmenovic. Parallel general prex computations with geometric, algebraic and other applications. In Proceedings of the International Conference on on Fundamentals of Computation Theory, Szeged, Hungary, August, 1989, Springer-Verlag, pages 424{433, 1989. [11] R. E. Tarjan and U. Vishkin. An ecient parallel biconnectivity algorithm. SIAM J. Comput., pages 862{874, 1985. 11

Appendix The purpose of this appendix is to show that the binary relation dened in Section 3 is a linear order on the set Q. For completeness, we repeat the denition of before we give the proof. Let c 1 ; c 2 ; :::; c n and d 1 ; d 2 ; :::; d n be the preorder and inorder traversals of a binary tree T, where c 1 ; c 2 ; :::c n is 1; 2; :::; n. Construct two sequences of triples: a sequence (1; j 1 ; c 1 ), (1; j 2 ; c 2 ),..., (1; j n ; c n ) such that d ji = c i, (i = 1; 2; :::; n) (i.e. j i is the position of c i in sequence d 1, d 2,..., d n, and a sequence (2; 1; d 1 ), (2; 2; d 2 ),..., (2; n; d n ). Write Q = f(1; j 1 ; c 1 ); (1; j 2 ; c 2 ); :::; (1; j n ; c n ); (2; 1; d 1 ); (2; 2; d 2 ); :::; (2; n; d n )g and dene a binary relation on Q as follows: for arbitrary triples (; ; ) and ( 0 ; 0 ; 0 ) in Q we have: Rule 1. (( = 1) ^ ( 0 = 1))! (((; ; ) ( 0 ; 0 ; 0 )) $ ( < 0 )); Rule 2. (( = 2) ^ ( 0 = 2))! (((; ; ) ( 0 ; 0 ; 0 )) $ ( < 0 )); Rule 3. (( = 1) ^ ( 0 = 2))! (((; ; ) ( 0 ; 0 ; 0 )) $ (( 0 ) _ ( 0 ))) Quite often, we shall nd it convenient to use the following equivalent formulation of Rule 3. Rule 3. (( = 2) ^ ( 0 = 1))! (((; ; ) ( 0 ; 0 ; 0 )) $ (( < 0 ) ^ ( < 0 ))) We note that Rules 1{3 above imply that the binary relation is total on Q. We propose to show that, in fact, is a linear order. For this purpose, we only need to justify that is transitive. We shall present our arguments in the form of a case-by-case analysis. Let (; ; ), ( 0 ; 0 ; 0 ) and ("; "; ") be arbitrary triples in Q satisfying: (; ; ) ( 0 ; 0 ; 0 ) and ( 0 ; 0 ; 0 ) ("; "; ") We need prove that (; ; ) ("; "; "). Our proof procceds in a case by case fashion. Case 1. = " Subcase 1.1 = 0 = " = 1. By Rule 1 and the assumption of this subcase, we have < 0 and 0 < " and therefore < ". The conclusion follows by Rule 1. Subcase 1.2 = " = 1 and 0 = 2 By Rule 3, (a) ( 0 ) W ( 0 ) By Rule 3, again, 12

(b) ( 0 < ") V ( 0 < ") If < " then conclusion follows immediately from Rule 1. Therefore, we assume (c) " < Note that (a), (b) and (c) combined imply that ( 0 < " < ) V ( < 0 < ") which is contradicting Corollary 1. (The contradiction is derived as follows: Because = 00 = 1 and 0 = 2, and 00 are from c 1, c 2,..., c n ; and 0 is from d 1, d 2,..., d n. Assume = c i, 00 = c k, 0 = d j 0 Further assume c i = d i 0, c j = d j 0, and c k = d k 0 We have (c j < c k < c i ) ^ (i 0 < j 0 < k 0 ). Since c 1, c 2,..., c n is 1, 2,..., n, we have (j < k < i) ^ (i 0 < j 0 < k 0 ) and c i = d i 0, c j = d j 0, and c k = d k 0, which contradict Corollary 1. ) Subcase 1.3 = 0 = " = 2 By Rule 2, we have < 0 and 0 < " and therefore < ". The conclusion follows by Rule 2. Subcase 1.4 = 2, 0 = 1 and " = 2 By Rule 3, (d) ( < 0 ) V ( < 0 ) By Rule 3, again, (e) ( 0 ") W ( 0 ") If ( < ") then conclusion follows instantly from Rule 2. We may assume, therefore, (f) (" < ) By now, (d), (e) and (f) combined imply that ( < 0 < ") V (" < < 0 ) which contradicts Corollary 1. Case 2. 6= " Subcase 2.1 = 0 = 1 and " = 2. By Rule 1, (g) ( < 0 ) By Rule 3, (h) ( 0 ") V ( 0 ") 13

Note that if ( ") W ( ") then by Rule 3, we have (; ; ) ("; "; "). Therefore, we may assume that (i) (" < ) V (" ) But now, (g), (h) and (i) combined imply (" < < 0 ) V ( 0 < " < ) which contradicts Corollary 1. Subcase 2.2 = 1 and 0 = " = 2. By Rule 3, (j) ( 0 ) W ( ") By Rule 2, (k) ( 0 < ") Note that if ( ") W ( ") then the conclusion follows by Rule 3, Therefore, we may assume that (l) (" < ) V (" < ) But now, (j), (k) and (l) combined imply (" < < 0 ) V ( 0 < " < ) which contradicts Corollary 1. Subcase 2.3 = 2 and 0 = " = 1. By Rule 3, (m) ( < 0 ) V ( < 0 ) By Rule 1, (n) ( 0 < ") Note that if ( ") V ( ") then the conclusion follows by Rule 3, Therefore, we may assume that (o) (" < ) V (" < ) But now, (m), (n) and (o) combined imply ( < 0 < ") V (" < < ") which contradicts Corollary 1. Subcase 2.4 = 0 = 2 and " = 1. By Rule 2, (p) ( < 0 ) By Rule 3, 14

(q) ( 0 ") V ( 0 < ") Note that if ( < ") then (p) and (q) combined give (; ; ) ("; "; ") by Rule 3. Thus, we may assume (" < ). But now (p) and (q) imply ( 0 < " < ) V ( < 0 < ) which contradicts Corollary 1. 2 15