Iterative Method For Recreating A Binary Tree From Its Traversals
Iterative Method For Recreating A Binary Tree From Its Traversals
ABSTRACT
Many reconstruction algorithms for binary tree have been
discussed in this paper. A particular focus of this paper is on
“A new Non-Recursive Algorithm for Reconstructing a Binary
Tree from its Traversals”. The computation time required for
executing the reconstruction algorithm are O(N) and space
complexity is O(NlogN) where N is the number of nodes in
the binary tree. This algorithm works well for most of the
input cases, but it has some drawbacks. There are some
sequences of pre-order and in-order for which no legitimate
tree can be constructed but the algorithm didn’t take these
cases into consideration and constructed a wrong tree for
these cases.
In this paper, we have proposed a solution to the problem in
the previous algorithm and designed an algorithm which is the
modified version of the previous algorithm for generating a
correct binary tree. The new modified algorithm is
implemented in C language and tested in GCC Compiler in
Linux, for all types of input cases. The New modified
algorithm works well for all types of input cases. We have
calculated the best case time complexity of modified
algorithm and show that a correct tree can be reported in O(N) Figure 1: Binary Tree containing 9 nodes
time in best case and O(NlogN) space where N is the number
of nodes in the tree. We have discussed some applications of Commonly there are three traversing methods: in-order, pre-
the new modified algorithm in Huffman Coding, compiler order and post-order traversal. In an in-order traversal, first the
design, text processing and searching algorithms. left child is processed recursively, and then process the current
node followed by the right child. The output of an in-order
Key words traversal algorithm of the binary tree shown in figure 1 is: B,
Non-recursive; tree traversals; binary tree. D, A, I, G, E, H, C, F and that of the pre-order is: A, B, D, C,
E, G, I. H, F.
1. INTRODUCTION
It is well known that given the in-order traverse of a binary
A tree is a fundamental structure in computer science. Almost
tree, along with one of its pre-order or post-order traversals,
all operating systems store files in trees or tree like structures.
the original binary tree can be uniquely identified. It is not
It is well known that given the in-order traverse of a binary
difficult to write a recursive algorithm to reconstruct the
tree, along with one of its pre-order or post-order traversals,
binary tree. Most text books and reference books present the
the original binary tree can be uniquely identified. It is not
recursive and non-recursive algorithms for traversing a binary
difficult to write a recursive algorithm to reconstruct the
tree in in-order, post-order and pre-order. The computation
binary tree. Most text books and reference books present the
time required is O(N2) where N is the number of nodes in the
recursive [1, 2, 3, 4, 5] and non-recursive algorithms [6, 7, 8,
tree. Many iterative methods for reconstructing a binary tree
9, 10] for traversing a binary tree in in-order, post-order and
has been proposed till date for which computation time
pre-order.
required is O(N). In this paper one of the proposed algorithms
Many iterative methods [11, 12, 13, 14, 15] for reconstructing
has been examined. The proposed algorithm works well for
a binary tree from its traversals have been proposed till date
most of the input cases, but it has a drawback. There are some
for which computation time required is O(N) where N is the
sequences of pre-order and in-order traversals for which no
number of nodes in the tree.
legitimate tree can be constructed but the proposed algorithm
A binary tree is different recursively as either empty or
didn’t take this case into consideration and constructed a
consists of a root, a left tree and a right tree. The left and
wrong tree for these cases. We have modified this algorithm
right trees may themselves be empty, thus a node with one
so that if for an in-order and pre-order sequence a correct tree
child could have a left or right child.
6
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
In-order traversal: 4, 7, 2, 8, 5, 1, 6, 9, 3
Pre-order traversal: 1, 2, 4, 7, 5, 8, 3, 6, 9
7
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
We are now left with only one value 7 in both the pre-order
and in-order form we simply represent it with a node as
shown in figure 2(d).
In the same way one by one all the data are picked from the
Figure 2(f): Reconstruction of a binary tree (contd.) pre-order list and are placed and their respective sub-trees are
constructed and the whole tree is constructed. Figure 2(e) to
2(i) shows each step of construction of nodes one by one.
8
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
9
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
address will be returned for any further use. This non- if(root == NULL)
recursive algorithm works on the following lemma [11]: root = NewNode
while (I[count2] is present in the LL)
Lemma: Let P[j] be the parent of P[i], (where j < i) then Lp[j] {
be the corresponding index in the in-order sequence of node PresentNode= address of
P[j] and Lp[i] be the corresponding index in the in-order I[count2];
sequence of node P[i]. If Lp[j] is greater than Lp[i] then P[i] is remove the I[count2] from the
the left child of P[j] otherwise it is the right child. The binary LL;
tree can be reconstructed by exploiting the following three right = 1;Left=0;count2++;
properties of the above lemma [11]. }
1. First node in the pre-order sequence will be the root of if( data != I[count2] )
the binary tree. {
2. The node(s) which comes to the left of root node in the Add the “data” into the linear list LL
in-order sequence will be the nodes in the left sub tree If( Left == 1)
and node(s) in the right are the nodes in the right sub tree Place the NewNode as a left child of
of the reconstructed root node. PresentNode
3. Apply the second property recursively to the left and else if(Right == 1)
right sub tree of the root node to obtain the complete Place the NewNode as a right child of
binary tree. PresentNode
For example in Figure 3, the first node of the pre-order Left=1; Right=0;
sequence is A, which is a root node of the reconstructed tree. }
The nodes B and D are the only two nodes to the left of A in else
in-order sequence, and they are the nodes in the left sub tree {
of the root node A. If( Left == 1)
Place the NewNode as a left child of
PresentNode
else if(Right == 1)
Place the NewNode as a right child of
PresentNode
Left=0; Right=1; count2++;
}
data = P[count1];
PresentNode=NewNode;
}
The computation time required for executing the
reconstruction algorithm are O(N) and space complexity is
O(NlogN) [11].
10
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
unvisited nodes in in-order sequence. Whenever a node is The best case time complexity of our modified algorithm
visited in in-order it will be removed from the LL. is O(N) where N is the number of nodes.
PresentNode is a pointer variable that holds the address of the
present node. 3.1 Complexity analysis
An extra variable m is used in our algorithm which keeps a By the performance analysis of a program, we mean the
count of the number of nodes having two children. amount of computer memory and time needed to run a
program.
Following is the modified Non-Recursive algorithm for
generating a tree from its in-order and pre-order traversals: On analysis of the algorithm we find out the recurrence
relation that can be formed is
/*Variable Declarations*/ (3.1)
left=0; right=0; count1=0; count2=0; data=P[0]; root=NULL;
m=0; /* New variable in our program which keeps a count of We can solve this recurrence relation by iterative method as
the number of nodes having two children */ follows:
for ( count1 = 0 ; count1<n ; count1++ )
{
/* create a new node*/ On adding the subsequent terms of (3.1), we get
newnode->info = data;
newnode->lchild = NULL;
newnode->rchild = NULL;
If (root == NULL)
… … …
root=newnode;
… … …
/*Intially the linked list is empty*/
while ( I[count2] is present in the linked list )
Summing up all these terms we get
{
____________________________________
presentnode = address of I[count2];
remove the I[count2] from the linked list;
right=1;left=0;count2++;
/*Here a small modification is done*/
m++;
}
(3.2)
If( data != I[count2] )
{
Add the data into the linked list; From the Sterling approximation [1, 19]
If (left == 1) (3.3)
Place the newnode as left child of
presentnode Taking log at base e on both the sides of (3.3)
If( right == 1)
Place the newnode as right child of (3.4)
presentnode
left=1;right=0;
From (3.2) and (3.4) we get,
}
else
{
If( left == 1)
So the time complexity of the algorithm is of order of
Place the newnode as a left child of
.
presentnode
elseif( right == 1)
For the best case scenario the loop runs for n times
Place the newnode as right child of
only, it does not have to search the link list for a parent
presentnode
address. Hence the time complexity will be n.
left=0;right=1;count2++;
}
data=P[count1+1];
presentnode = newnode;
} /*for loop end*/
… … …
… … …
If (m is greater than zero or check if the in-order traversal of
the tree formed matches with the given in-order sequence)
______________________________
{
Print the tree;
}
else
{ Hence is of the order of .
Print that a legitimate tree cannot be constructed with the
sequences;
}
11
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
Fig. 4: The generated tree for input sequence Pre-order: 1, 2, Some other inputs supplied to our modified algorithm.
4, 8, 9, 5, 3, 6, 7 and In-order: 8, 4, 9, 2, 5, 1, 6, 3, 7
3. For the input
The tree generated in figure 4 for the input sequence Pre-order
traversal: 1, 2, 4, 8, 9, 5, 3, 6, 7 and In-order traversal: 8, 4, 9, Pre-order Traversal: 1, 2, 3, 4, 5, 6
2, 5, 1, 6, 3, 7 is the correct binary tree because we can In-order Traversal: 1, 2, 3, 4, 5, 6
generate the same pre-order and in-order traversals as The correct generated binary tree is shown in figure 7.
supplied.
12
International Journal of Computer Applications (0975 – 8887)
Volume 57– No.11, November 2012
4. For the input sequence compression may go wrong. The modified algorithm
prevents any wrong tree from forming.
Pre-order Traversal: 1, 2, 3, 4, 5, 6, 7
In-order Traversal: 7, 6, 5, 4, 3, 2, 1 6. REFERENCES
The correct generated binary tree is shown in figure 8. [1] Vinu V Das, “Principles of Data Structures Using C and
C++”, New Age International Publishers, Reading,
Mass., 2005.
[2] M. Weiss, “Data Structures & Problem Solving Using
Java”, 2nd ed., Addison Wesley, 2002.
[3] D. E. Knuth, “The Art of Computer Programming”, Vol.
3 (2nd ed.): Sorting and Searching, Addison Wesley,
1998.
[4] R. Sedgewick, “Algorithms in Java”, 3d edition,
Addison Wesley, 2003.
[5] D. E. Kunth, “The Art of Computer Programming”, Vol.
1: Fundamental Algorithm, Addison-Wesley, Reading,
Fig. 8: The generated output for input sequence Pre-order: 1,
Mass., 1973.
2, 3, 4, 5, 6, 7 and In-order: 7, 6, 5, 4, 3, 2, 1 by our
new modified the algorithm [6] Mark Allen Weiss, “Data Structures and Algorithm
Analysis in C”, Vol. 3 (2nd ed.), Addison-Wesley, 1997.
5. CONCLUSION AND FUTURE WORK [7] Yedidyah Langsam, Moshe J. Augenstein, Aaron M.
A tree is a fundamental structure in computer science. Almost Tanenbaum: “Data Structures using C and C++”, 2nd
all operating systems store files in trees or tree like structures. Ed., Prentice-Hall India, July 2002.
It is well known that given the in-order traverse of a binary [8] Sartaj Sahni: “Data Structures, Algorithms and
tree, along with one of its pre-order or post-order traversals, Applications in JAVA”, 2nd Ed., University Press.
the original binary tree can be uniquely identified. It is not [9] A. Anderson and S. Carlsson: “Construction of a tree
difficult to write a recursive algorithm to reconstruct the from its traversals in optimal time and space”,
binary tree. Most text books and reference books present the Information Processing Letters, 34:21-25, 1990.
recursive and non-recursive algorithms for traversing a binary
tree in in-order, post-order and pre-order. The computation [10] N. Gabrani and P. Shankar: “A note on the reconstruction
time required is O(N2) where N is the number of nodes in the of a binary tree from its traversals”, Information
tree. Many iterative methods for reconstructing a binary tree Processing Letters, 42:117-119, 1992.
has been proposed till date for which computation time [11] Vinu V Das, “A new Non-Recursive Algorithm for
required is O(N). The Non- Recursive algorithm presented by Reconstructing a Binary Tree from its Traversals”, IEEE
Vinu V Das [11] is considered in this paper. This algorithm Comm., pp. 261-263, 2010.
works well for most of the input in-order and pre-order [12] H. A. Burgdorff, S. Jojodia, F.N. Springsteel, and
traversals, but generates wrong binary tree for some another Y.Zalcstein, “Alternative Methods for the Reconstruction
in-order and pre-order sequences. In this paper a new modified of Tree from their Traversals”, BIT, Vol. 27, No. 2, p.
Non Recursive algorithm has been proposed which works well 134, 1987.
for all the input cases.
[13] G. H. Chen, M. S. Yu, and L.T. Liu: “Non-recursive
Also in the future better algorithm can be found out than
Algorithms for Reconstructing a Binary Tree from its
the above one which would further reduce the complexity of
Traversals”, IEEE Comm., Vol. 88, pp. 490-492, 1988.
the algorithm. The algorithm can be implemented using
suitable simulation tools. [14] C. C. Lee, D. T. Lee, C. K. Wong: “Generating Binary
Trees of bounded height”, Acta Inf., 23, 529-544, 1986.
5.1 Applications [15] Seymour Lipschutz: “Theory and problem of Data
Structures”, International Edition, McGRAW-HILL,
The tree is fundamental structure in computer science. The 1986.
applications of new proposed algorithm in general are: [16] Glenn W. Rowe: “Introduction to Data Structure and
Algorithms with C++”, PHI, ISBN: 81-203-1277-5.
1. The optimality with the simplicity of the algorithm [17] Robert Sedgewick: “Algorithms in C++”, Ed. 3rd, 2001,
makes this method an alternative to other methods for ISBN: 81-7808-249-7.
temporary storage of a tree in secondary memory.
Almost all operating systems store files in trees or tree [18] C. P. Kruskal: “Searching, merging, and sorting in
like structures. parallel computation”, IEEE Transactions on Computers,
2. It may for instance be used on tree structures that are 32:924-946, 1983.
expensive to construct, such as optimum search trees and [19] Lindstrom, G. Scanning: “List structures without stacks
multidimensional trees. or tag bits”. Inform. Proc. Letters 2, 1973, pp. 47-51.
3. Trees are also used in compiler design, text processing [20] Arora N., Tamta V. and Kumar S: “Modified Non-
and searching algorithms. Recursive Algorithm for Reconstructing a Binary Tree
4. In Huffman Coding instead of sending the whole tree in from its Traversals”, IJCA, volume 43-No.10, April
transmission, redundant sequences of in-order and pre- 2012.
order are send from the sender to the receiver. If a wrong
tree is accidently formed then the whole process of
13