呵呵 原来打印语句位置的不同就是不同的访问方式~
// btree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
int a[10]={1,2,4,5,3,6,7,8,9,10};
struct tree
{
tree *left;
tree *right;
int value;
};
void btree(tree *node,tree *tr)
{
tree *list = tr;
if (node->value >list->value)
{
if (list->right !=NULL)
{
btree(node,list->right);
}
else
{
list->right = node;
}
}
else
{
if (list->left !=NULL)
{
btree(node,list->left);
}
else
{
list->left = node;
}
}
}
void ptree(tree *tr)
{
tree *list = tr;
if (list->left)
{
ptree(list->left);
}
printf("%d\n",list->value);//中序输出
if (list->right)
{
ptree(list->right);
}
return;
}
int main(int argc, char* argv[])
{
tree *binetree = (tree*)malloc(sizeof(tree));
binetree->value =a[0];
binetree->left =NULL;
binetree->right =NULL;
tree *temp = binetree;
for (int i=1;i<10;i++)
{
tree *node =(tree*)malloc(sizeof(tree));
node->value =a[i];
node->left =NULL;
node->right =NULL;
btree(node,temp);
}
ptree(binetree);
return 0;
}