efficient algorithms for tree reconstruction

3
BIT29 (1989), 361-363 SCIENTIFIC NOTES EFFICIENT ALGORITHMS FOR TREE RECONSTRUCTION Department of Computer Science, University of Missouri-Columbia, Columbia, MO 65211 U.S.A. W. SLOUGH and K. EFE Centerfor Advanced Computer Studies, University of Southwestern Louisiana, Lafayette, LA 70504, U.S.A. Abstract. In this note, we show that O(n log n) operations are sufficient to reconstruct an ordered binary tree given its inorder traversal and either its preorder or postorder traversal. An alternative linear representa- tion allows reconstruction using O(n) operations. CR Class!fication: E.t. 1: Preliminaries. A problem, apparently due to Knuth ([4]~ p. 329, problem 7), concerning the reconstruction of an ordered binary tree given a pair of its traversals has recently been discussed by Burgdorff et al. [1]. They offer a simple algorithm which builds a tree given its inorder and preorder traversals as an alternative to a "'well-kmown" recursive solution. Although their algorithm is conceptually simple, it requires O(n 2) operations to construct a tree with n nodes. By contrast, we describe an algorithm which requires O(n log n) operations. Our algorithm uses the notion of an "'inorder-preorder" sequence [2] (also known as a "tree permutation" [3]), which we define here in order to make this note self-contained. An inorder-preorder sequence (or i-p sequence, for short) is a permutation n of 1,2 ..... n with the property that there is a binary tree with inorder traversal given by 1,2 ..... n and preorder traversal given by n. A g-trans- lated i-p sequence is a sequence which can be obtained from an i-p sequence by adding 6 to each element. Received June 1988. RevisedJanuary 1989.

Upload: w-slough

Post on 10-Jul-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

BIT29 (1989), 361-363

S C I E N T I F I C N O T E S

EFFICIENT ALGORITHMS FOR TREE RECONSTRUCTION

Department of Computer Science, University of Missouri-Columbia, Columbia, MO 65211 U.S.A.

W. SLOUGH and K. EFE

Center for Advanced Computer Studies, University of Southwestern Louisiana,

Lafayette, LA 70504, U.S.A.

Abstract .

In this note, we show that O(n log n) operations are sufficient to reconstruct an ordered binary tree given its inorder traversal and either its preorder or postorder traversal. An alternative linear representa- tion allows reconstruction using O(n) operations.

CR Class!fication: E.t.

1: Pre l iminar ies .

A problem, apparently due to Knuth ([4]~ p. 329, problem 7), concerning the reconstruction of an ordered binary tree given a pair of its traversals has recently been discussed by Burgdorff et al. [1]. They offer a simple algori thm which builds a tree given its inorder and preorder traversals as an alternative to a " 'well-kmown" recursive solution. Although their a lgori thm is conceptually simple, it requires O(n 2) operat ions to construct a tree with n nodes. By contrast , we describe an algori thm which requires O(n log n) operations.

Our algori thm uses the notion of an " ' inorder-preorder" sequence [2] (also known as a "tree permutat ion" [3]), which we define here in order to make this note self-contained. An inorder-preorder sequence (or i-p sequence, for short) is a permutat ion n of 1,2 . . . . . n with the property that there is a binary tree with inorder traversal given by 1,2 . . . . . n and preorder traversal given by n. A g-trans-

lated i-p sequence is a sequence which can be obtained from an i-p sequence by adding 6 to each element.

Received June 1988. Revised January 1989.

362

2. The algorithm.

V¢. S L O U G H A N D K . EFE

We focus" our attention on the problem of tree reconstruction given inorder and preorder traversals; simple modifications provide a solution when the traversals are inorder and postorder.

Our algorithm operates in two stages. In the first stage, an i-p sequence IP is computed from the inorder and preorder traversals provided. This sequence unique- ly determines the shape of the tree. The second stage constructs the tree using IP and a preorder traversal P, which supplies the content of the nodes. If the i-p and preorder sequences are given as the representation of the tree, then the reconstruc- tion involves only the second stage, which we show can be performed in O(n)

operations. The computation of IP involves locating the index within the inorder sequence for

each of the n elements from P. This can be done using O(n log n) operations if we associate an index with each element of the inorder sequence, sort the inorder sequence using any O(n log n) sorting method, then perform the n searches employ- ing binary search. The second stage, discussed below, requires O(n) operations; hence the first stage dominates the reconstruction process. Of course, one may use some hashing scheme to perform these n search operations, yielding O(n) expected operations.

The second stage of our algorithm takes advantage of the recursive structure ofi-p sequences, first pointed out in I-3] and [5]. We use a recursive characterization ofi-p sequences given in the following lemma.

LEMMA 1: A nonempty 6-translated i-p sequence a o f length n can be written a = j~fl,

where ~ is a 6-translated i-p sequence o f length j - 6 - 1 and 13 is a j-translated i-p

sequence of length n - j + 6.

Lemma 1 is the basis for the algorithm shown in Figure 1. To understand this algorithm, it helps to know that BuildTree (f, 6) returns a pointer to the binary tree whose shape is determined by a 6-translated i-p sequence of length E; hence, BuildTree (n, 0) returns a pointer to the tree determined by IP and P.

A linked representation of binary trees commonly used is assumed here. Each node of the tree has three components - Info, Left, and Right. The statement " p : = GetCell" causes p to point to a newly-allocated node of this type. Info(p) denotes the Info component of the node pointed to by p; Left(p) and Right(p) have similar meanings.

IP is accessed strictly in a left-to-right fashion, so it does not nead to be explicitly stored provided there is a mechanism to generate successive values in the sequence. GetNextlPvalue is a primitive providing this mechanism. Similarly, the preorder sequence is accessed in one left-to-right scan; successive calls to GetNextPreorder- Value yield the preorder sequence.

EFFICIENT ALGORITHMS FOR TREE RECONSTRUCTION

f u n c t i o n B u i l d T r e e (n, ~) r e tu rns P o i n t e r

363

loca l

p: P o i n t e r

j : i ndex

begin

i f n = 0 then

r e tu rn nil

else

p : = G e t C e l l

j : = G e t N e x t I P v a l u e

Info(p): = G e t N e x t P r e o r d e r V a l u e

Left(p): = Bui ldTree ( j - 6 -- 1, 6)

R i g h t ( p ) : = Bui ldTree(n - j + 6,j)

r e tu rn p

end B u i l d T r e e

Fig. l. Tree reconstruction algorithm.

LEMMA 2: Let a be the sequence defined by f successive calls to Ge tNex t l Pvalue and

let z be the sequence defined by t ~ successive calls to GetNextPreorderValue. I f a is

a b-translated i-p sequence then BuildTree((, 3) returns a pointer to the binary tree

determined by a and z.

PROOF, I n d u c t i o n on {, us ing L e m m a 1.

LEMMA 3: The number o f operations performed by BuildTree(n, O) is O(n).

REFERENCES

[1] H. A. Burgdorff, S. Jajodia, F. N. Springsteel, and Y. Zalcstein, Alternative methods Jbr the reeonstruetion of trees Jhom their traversals, BIT, vol. 27, pp. 134-140, 1987.

[2] T. Hikita, Listinq and eountinq subtrees of equal size of a binary tree, Info. Processing Letters, vol. 17, pp. 225-229, 1983.

[3] G. D. Knott, A numberinq system for binary trees, Comm. of ACM, voL 20, pp. 113 115, 1977. [4] D. E. Knuth, The Art of Computer Pro qrammin9, VoL 1, Fundamental Alyorithms, Second Edition,

Addison-Wesley, Reading, Mass., 1973. [5] M. Solomon and R. A. Finkel, A note on enumeratin 9 binary trees, Jour. of ACM, vol. 27, pp. 3-5,

1980.