转载自:http://wenku.baidu.com/view/b9c8c4115f0e7cd1842536da.html
#include "stdafx.h"
#include<iostream>
using namespace std;
int i=1;
typedef struct bnode
{
char data;
struct bnode*lchild,*rchild;
}bnode;
class bitree
{
public:
void preorder(bnode*t);
void inorder(bnode*t);
void postorder(bnode*t);
bnode * get_root(){return root;}
void creat(bnode*&t);
void inorder_printjie(bnode*t,int i);
private:
bnode * root;
};
void bitree::preorder (bnode*t)
{
if(t!=NULL)
{
cout<<t->data<<" ";
preorder(t->lchild );
preorder(t->rchild );
}
}
void bitree::inorder (bnode*t)
{
if(t!=NULL)
{ inorder(t->lchild );
cout<<t->data<<" " ;
inorder(t->rchild );
}
}
void bitree::creat (bnode*&t)
{
char x;
cin>>x;
if(x=='.')t=NULL;
else
{
t=new bnode;
t->data =x;
creat(t->lchild );
creat(t->rchild );
}
}
void bitree::inorder_printjie (bnode*t,int i)
{
if(t!=NULL)
{
inorder_printjie(t->lchild ,i+1);
cout<<" ("<<t->data <<","<<i<<")";
inorder_printjie(t->rchild ,i+1);
}
}
void main()
{
bitree a;
bnode*t=a.get_root ();
cout<<"请输入A二叉树,以.表示没有孩子数目:"<<endl;
a.creat (t);
cout<<endl;
cout<<"以中序的方式输出A二叉树:"<<endl;
a.inorder(t);
cout<<endl;
cout<<"以中序输出二叉树各结点的值及对应的层次:"<<endl;
a.inorder_printjie (t,i);
getchar();
}
<span style="font-size:24px;">虽然能够求得每个节点对应的层次,但还是不能求得每一层的节点,还需要好好研究才行。</span>