473,379 Members | 1,344 Online

# Question on binary tree

1
/ \
2 3
/ / \
4 5 6
\ / \
7 8 9

Given the binary trees above, determine the order in which the
nodes will be visited in the mixed order given by invoking
function A:

typedef struct treenode TreeNode;
typedef struct treenode {
TreeNode *left;
int entry;
TreeNode *right;
}TreeNode;

void A(TreeNode *root)
{
if(root) {
printf("%d\n",root->entry);
B(root->left,Visit);
B(root->right,Visit);
}
}

void B(TreeNode *root)
{
if(root) {
A(root->left,Visit);
printf("%d\n",root->entry);
A(root->right,Visit);
}
}
Nov 14 '05 #1
1 1799

"sugaray" <ru****@sohu.com> wrote in message

Given the binary trees above, determine the order in which the
nodes will be visited in the mixed order given by invoking
function A:

Hi. Unfortunately we're not a homework-doing service.

Personally I find code very difficult to dry-run, so what I suggest you do
is write some code to read in a binary tree (and draw it, to test you have
got the tree correct), and then just call the function on the root of the
tree and see what the output is.

One way to specify a tree is to define a text format

name, parentsname, left

To add a node, search the whole tree until you find the parent. (This is
really wasteful, but easy, and Ok unless the tree is huge).
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.