ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A-1998-5



Similar documents
Binary Search Trees (BST)

2. FINDING A SOLUTION

Binary Search Trees CMPSC 122

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Full and Complete Binary Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

DATA STRUCTURES USING C

Ordered Lists and Binary Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Converting a Number from Decimal to Binary

Binary Trees and Huffman Encoding Binary Search Trees

Data Structure [Question Bank]

Data Structure with C

Algorithms Chapter 12 Binary Search Trees

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

How To Create A Tree From A Tree In Runtime (For A Tree)

The Goldberg Rao Algorithm for the Maximum Flow Problem

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Analysis of Algorithms I: Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

TREE BASIC TERMINOLOGIES

Data Structures and Algorithms

Chapter 14 The Binary Search Tree

Symbol Tables. Introduction

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Data Structures. Level 6 C Module Descriptor

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Algorithms and Data Structures

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

OPTIMAL BINARY SEARCH TREES

Fast nondeterministic recognition of context-free languages using two queues

1/1 7/4 2/2 12/7 10/30 12/25

Binary Search Trees 3/20/14

Analysis of Algorithms I: Optimal Binary Search Trees

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

GRAPH THEORY LECTURE 4: TREES

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Parallelization: Binary Tree Traversal

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

International Journal of Advanced Research in Computer Science and Software Engineering

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

Data Structures Fibonacci Heaps, Amortized Analysis

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Topology-based network security

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Optimal Binary Search Trees Meet Object Oriented Programming

How Asymmetry Helps Load Balancing

Rotation Operation for Binary Search Trees Idea:

Data Structures and Algorithms(5)

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Persistent Binary Search Trees

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Analysis of Algorithms, I

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

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

The ADT Binary Search Tree

Chapter 7: Termination Detection

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Shortest Inspection-Path. Queries in Simple Polygons

Binary Heap Algorithms

From Last Time: Remove (Delete) Operation

Classification/Decision Trees (II)

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Lecture 10: Regression Trees

1.2 Solving a System of Linear Equations

Classifying Large Data Sets Using SVMs with Hierarchical Clusters. Presented by :Limou Wang

Hash-based Digital Signature Schemes

Sequential Data Structures

Less naive Bayes spam detection

On line construction of suffix trees 1

Binary Heaps. CSE 373 Data Structures

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Introduction to Data Structures and Algorithms

Lecture 4: Balanced Binary Search Trees

Lecture 1: Course overview, circuits, and formulas

Performance Analysis of Naive Bayes and J48 Classification Algorithm for Data Classification

Chapter 4: Computer Codes

THE SECURITY AND PRIVACY ISSUES OF RFID SYSTEM

Cpt S 223. School of EECS, WSU

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Lecture 5 - CPA security, Pseudorandom functions

CSE 326: Data Structures B-Trees and B+ Trees

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Binary Coded Web Access Pattern Tree in Education Domain

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Transcription:

S I N S UN I ER E P S I T VER M A TA S CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A-1998-5

UNIVERSITY OF TAMPERE DEPARTMENT OF COMPUTER SCIENCE SERIES OF PUBLICATIONS A A-1998-5, APRIL 1998 CONSTRUCTING A BINARY TREE EFFICIENTLY FROM ITS TRAVERSALS ERKKI M AKINEN University oftampere Department of Computer Science P.O.Box 607 FIN-33101 Tampere, Finland ISBN 951-44-4351-9 ISSN 0783-6910

Constructing a binary tree eciently from its traversals Erkki Makinen Department of Computer Science, University of Tampere, P.O. Box 607, FIN-33101 Tampere, Finland Abstract In this note we streamline an earlier algorithm for constructing a binary tree from its inorder and preorder traversals. The new algorithm is conceptually simpler than the earlier algorithms and its time complexity has a smaller constant factor. Keywords: Algorithms; Binary trees; Tree traversals. 1 Introduction Given the preorder and inorder traversals (or postorder and inorder traversals) of the nodes of a binary tree, the binary tree structure can be constructed. In the matter of fact, the construction is possible in linear time [1,4]. Recently, Xiang and Ushijima [5] have determined the constant factors of these algorithms by counting the numbers of comparision operations needed. They found out that the linear coecient of Makinen's algorithm [4] is 3 in the best case and 5 in the worst case, and that the corresponding coecients in the algorithm of Andersson and Carlsson [1] are 4 and 7. Moreover, Xiang and Ushijima presented a new algorithm with linear coecient 3 both in the best and in the worst case. In this note we streamline our earlier algorithm [4], and show that the resulting algorithm can be implemented to run with linear coecient 2 in the best case and 3 in the worst case. We use the standard tree terminology [3]. In order to simplify our presentation, we rst recall a binary tree coding system introduced by Johnsen [2]. A unique coding system for binary trees can be established by inserting the nodes into the tree in preorder and recording the positions to which the insertions are made. As an example, consider the tree in Figure 1. There are three possibilities to insert the next node in preorder. These are numbered from left to right by 0, 1, and 2. Johnsen [2] uses the phrase trailing leaf for

2 0 1 Fig. 1. A sample binary tree with three trailing leaves. the positions where the next insertion can take place. So far, the insertions in the sample tree are done into the positions 0, 2, and 0. The next insertion will augment the code word to be (0,2,0,0), (0,2,0,1), or (0,2,0,2) depending on our choice for the place of the next insertion. Notice that a binary tree with n nodes has a code word with n, 1 code items. Johnsen has proved that for each item x i inacodeword (x1;x2;:::;x n,1 )we have 0 x i i, (x1 + x2 + :::+ x i,1 ). This implies the following fact. Proposition 1 If (x1;x2;:::;x n,1 ) isacode word of a binary tree with n nodes in Johnsen's coding scheme, then P n,1 i=1 x i n, 1. 2 The algorithm We suppose that trees are labeled with symbols from an ordered alphabet. Moreover, we suppose that inorder sequences always obey this order, i.e., if I1I2 :::I n is an inorder sequence, then we have I1 <I2 < ::: < I n according to the order dened in the alphabet. These assumptions make it possible to compare labels when the tree is constructed by studying the preorder sequence. Notice that these assumptions can be done without loss of generality. Namely, if the inorder sequences do not obey the order in the alphabet, we can store the inorder positions in a table and compare the table entries. Instead of using such a table we simply directly compare the labels. As can be expected, our algorithm needs a stack. It is advantageous to use a bottom marker in the stack. We dene the bottom marker X to be the last element in the order relation of the label alphabet used. As an example, consider the tree shown in Figure 2. Its nodes are labeled in alphabetical order according to inorder. The corresponding preorder sequence is DACBF EG. The algorithm begins with creating the root labeled with D. The lable D is also pushed to the stack. The second label in the preorder sequence is A. Since A < D, where D is the top element of the stack, we 2

D A F C E G B Fig. 2. A sample binary tree with nodes alphabetically labeled in inorder. know thattheleftchild of the root should be labeled with A. Moreover, A is pushed to the stack. Next, we notice that A<Cbut C<D.Now, A is popped from the stack, and C is pushed to its place. C is the right child of A. Using the terminology of Johnsen's coding scheme, popping an element from the stack means that the leftmost trailing leaf does not actually exist in the tree. Or equivalently, the corresponding code item is greater that 0. Next, we compare B and the top element C. Since B<C,we insert B as the left child of C. Now the stack contains XDCB (, where the top of the stack isonthe right). The next label in the preorder sequence is F.Wehave B<F, C<F, D<F,andF<X. It follows, that B, C, and D are popped from the stack, and F is pushed to the stack. We now know that the left subtree of the root is completed. Notice that each label comparision was made for checking to which trailing leaf the next node can be inserted. The whole construction is completed by noticing that E is the label of the left child of F, and that G is the lable of the corresponding right child. Notice that after pushing an element to the stack we create a left child, and after popping an element from the stack a right child is created. More importantly, notice the interesting fact that the number of label comparisions needed to nd the correct place for a node, and the corresponding code item in Johnsen's coding scheme are in a very close connection with each others. The code word for the tree in Figure 2 is (0,1,0,3,0,2). It is easy to check that the number of label comparisions done for placing a node is always one greater than the corresponding code item. For example, when the code item is 0, the only comparision is done between the next label in the preorder sequence and the top of the stack. Before formulating this as a theorem, we give the algorithm itself in a more formal notation. 3

Algorithm 1 (Binary Tree Construction) f the preorder sequence is indexed aspreorder[i], where 1 i n; the phrase 'current node' refers to the node labeled with the top element of the stack g begin initialize the stack with the bottom marker X; create the root and label it with preorder[1]; index := 2; while (index n) do if preorder[index] <top(stack) then begin create the left child of the current node and label it with preorder[index]; push preorder[index] to the stack; index:= index + 1 end else begin pop top(stack); while preorder[index] < top(stack) do pop top(stack); create the right child of the previous current node and label it with preorder[index]; push preorder[index] to the stack; index:= index + 1 end end falgorithmg Consider now again the number of label comparisions done by the algorithm. The best case occurs when the tree to be constructed is a left list, i.e. a tree containing the root and left children only. The corresponding code word is (0; 0;:::;0), and we need only n, 1 label comparisions. On the other hand, the worst occurs when the sum of the code items in the code word is n, 1. Then we need 2n, 2 label comparisions. In order to make the number of comparisions comparable with those given by Xiang and Ushijima [5], we also count comparisions other than label comparisions made by the algorithm. The algorithm has a while-loop condition (index n), which causes n, 1 comparisions when constructing a tree with n nodes. Hence, by Proposition 1 and the discussion above, we have Theorem 2 When constructing a binary tree with n nodes, Algorithm 1 makes n, 1 label comparisions in the best case and 2n, 2 label comparisions in the worst case. The total number of comparisions is 2n, 2 in the best case and 3n, 3 in the worst case. 4

3 Conclusions Our algorithm requires fewer comparisions than the previously known algorithms for constructing a binary tree from its traversals. Moreover, our algorithm is conceptually simple imitating a known binary tree coding scheme. References [1] Arne Andersson and Svante Carlsson, Construction of a tree from its traversals in optimal time and space. Inform. Process. Lett. 34 (1990), 21{25. [2] Ben Johnsen, Generating binary trees with uniform probability. BIT 31 (1991), 15{31. [3] Donald E. Knuth, The Art of Computer Programming. Vol. 1, Fundamental Algorithms. Third Edition. Addison-Wesley, 1997. [4] Erkki Makinen, Constructing a binary tree from its traversals. BIT 29 (1989), 572{575. [5] Limin Xiang and Kazuo Ushijima, On constructing a bianry tree from its traversals. Manuscript, 1998. 5