Page 113 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 113
Advanced Data Structure and Algorithms
Notes
A
B C
D E
A Unique Binary Constructed using the Inorder and Postorder
5.5.2 Procedure for Inorder Traversal
void inorder(tnode *p)
{
if(p != NULL)
{
inorder(p->lchild);
printf(p->data);
inorder(p->rchild);
}
}
A non-recursive/iterative procedure for traversing a binary tree in inorder is given below for the
purpose of doing the analysis.
void inorder(tnode *p)
{
tnode *stack[100];
int top;
{
top = 0;
if(p != NULL)
{
top = top + 1;
stack[top] = p;
p = p->lchild;
while(top > 0)
{
while(p != NULL)
/*push the left child onto the stack*/
{
top = top + 1;
stack[top] = p;
p = p->lchild;
108 LOVELY PROFESSIONAL UNIVERSITY