标签:des blog io os sp for 数据 div log
1 2 2 1 20
2 1 20
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef struct node
{
int data;
struct node *ll;
struct node *rr;
}Binode, *Bitree;
void Creat_Sort_Bitree(Bitree &root, int key )
{
if(root==NULL)
{
root=new Binode;
root->ll=NULL;
root->rr=NULL;
root->data=key;
return ;
}
else
{
if(key < root->data)
{
Creat_Sort_Bitree(root->ll, key );
}
else
{
Creat_Sort_Bitree(root->rr, key );
}
}
}
int s[10000],e=0;
void In_order(Bitree p)
{
if(p)
{
In_order(p->ll);
s[e++]=p->data; //将该要被访问的数据 存进数组了。 一开始的时候写的直接输出, 但格式总是弄不对,只好放弃了那种写法!
In_order(p->rr);
}
}
int main()
{
int n, dd;
int i;
Bitree root;
while(cin>>n)
{
root=NULL;
for(i=0; i<n; i++)
{
cin>>dd;
Creat_Sort_Bitree(root, dd );
}
e=0;
In_order(root);
for(i=0; i<e; i++)
{
if(i==0)
cout<<s[i];
else
cout<<" "<<s[i];
}
cout<<endl;
}
return 0;
}
看到一篇博客里,一个人的中序访问数据的写法:
void visit(BiTree T)
{
if(T->data!=NULL)
{
if(f==1)
{
cout<<" "<<T->data;
}
else
{
cout<<T->data;
f=1;
}
}
}
void InOrder(BiTree T)
{
if(T)
{
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
数据结构之二叉树 树结构练习——排序二叉树的中序遍历 (排序建树+中序遍历)
标签:des blog io os sp for 数据 div log
原文地址:http://www.cnblogs.com/yspworld/p/4120160.html