标签:算法
#include <cstdio>
#include <iostream>
using namespace std;
#ifdef ONLINE_JUDGE
#define FINPUT(file) 0
#define FOUTPUT(file) 0
#else
#define FINPUT(file) freopen(file,"r",stdin)
#define FOUTPUT(file) freopen(file,"w",stdout)
#endif
struct binTree{
int data;
binTree *left;
binTree *right;
};
void insertTree(binTree **root,binTree *node)
{
if(*root==NULL)
{
*root = node;
return ;
}
if(node->data<(*root)->data)
insertTree(&(*root)->left,node);
else if(node->data>(*root)->data)
insertTree(&(*root)->right,node);
else if(node->data = (*root)->data)
return;
}
void preTvs(binTree *root)
{
if(root)
{
cout<<root->data<<" ";
preTvs(root->left);
preTvs(root->right);
}
}
void orTvs(binTree *root)
{
if(root)
{
orTvs(root->left);
cout<<root->data<<" ";
orTvs(root->right);
}
}
void postTvs(binTree *root)
{
if(root)
{
postTvs(root->left);
postTvs(root->right);
cout<<root->data<<" ";
}
}
int main()
{
FINPUT("in.txt");
FOUTPUT("out.txt");
int n;
while(cin>>n && n)
{
binTree *bt = new binTree[n];
int i = 0;
binTree *root = NULL;
while(i<n)
{
int t;
cin>>t;
bt[i].data = t;
bt[i].right = bt[i].left = NULL;
insertTree(&root,&bt[i]);
i++;
}
/* */
preTvs(root);
cout<<endl;
orTvs(root);
cout<<endl;
postTvs(root);
cout<<endl;
}
return 0;
}标签:算法
原文地址:http://blog.csdn.net/daringpig/article/details/25548275