Question on binary tree

S

sugaray

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);
}
}
 
M

Malcolm

sugaray said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top