OK.......right now i'm learning about binary tree traversal and this problem looks like a total killer to me. Here's it is:

Write a program that takes as input the preorder and inorder traversals of a binary tree, and produces as output the level-order traversal of the tree.

All i gotta say to that is :eek:

I've built a skeleton of the program and so far I've been able to take the preorder traversal of a tree as input and generate the original tree. As for inorder, I really have no clue what to do :sad:

``````#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define SIZE 50

struct node {
int item;
};

{
x->item = data;
return x;
}

{
if (root == NULL)
root = make_node(data);
if (root->item == data)
return root;
else
{
int dir = root->item < data;
}
return root;
}

{
int i;

if ( tree == NULL ) {
for ( i = 0; i < level; i++ )
printf ( "\t" );
printf ( "~\n" );

return;
}

disp_tree ( tree->link, level + 1 );

for ( i = 0; i < level; i++ )
printf ( "\t" );
printf ( "%d\n", tree->item );

disp_tree ( tree->link, level + 1 );
}

/*
struct node *q;

{
q[tail++] = item;
tail = tail % N;
}

{
}
*/
// preorder
{
while (*a != 0)
{
make_tree(root, *a);
a++;
}

disp_tree(root, 1);
}
// inorder
void inorder()
{}

int main(void)
{
int num;
puts("1: Preorder Traversal");
puts("2: Inorder Traversal");
scanf("%d", &num);
fflush(stdin);

int i = 0, j = 0, val[SIZE] = {0};
char trav[SIZE];
puts("Enter the traversal order");
fgets(trav, SIZE, stdin);
while (isdigit(trav[i]))
{
val[j++] = 10*val[i] + (trav[i++]-'0');
while (trav[i] == ' ')
i++;
}

if (num == 1)
{
preorder(root, val);
}
else
inorder();

return 0;
}``````

## All 5 Replies >As for inorder, I really have no clue what to do

You mean you don't know how inorder traversal would look or you don't know how to code it?

Inorder
1. Traverse the left subtree
2. Visit the root
3. Traverse the right subtree

That's the basic idea...

No that's not what I meant. I'm supposed to change an inorder traversal into the original binary tree and then produce a level order traversal. Sorry for any misunderstanding. Well if the user explicitly gives you the preorder and inorder traversals of a binary tree, then recovery of the original binary can be done like thus:

Clearly the first element in the preorder is the root. We can then search for this element in the inorder traversal. The elements of the tree are now uniquely partitioned into left and right subtrees by this element. (ie., the subsequence of elements preceding the root in the inorder sequence is the inorder sequence for the left sub-tree and similarly the subsequence of elements succeeding the root is the inorder sequence for the right sub-tree) We can then recursively construct the left and right sub-trees from the preorder and inorder subsequences.

And Once we have the original binary tree, then we can use the Al-Gore-it-him for level-traversal which is well documented on the web me thinks.

I Agree With Iamthwee

Well if the user explicitly gives you the preorder and inorder traversals of a binary tree, then recovery of the original binary can be done like thus:

And Once we have the original binary tree, then we can use the Al-Gore-it-him for level-traversal which is well documented on the web me thinks.

Oo that seems to make a bit more sense. Where did you get that quote btw?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.