#include<iostream>
using namespace std;
struct BTnode
{
int data;
BTnode *Lchild,*Rchild;
};
class Tree
{
private:
BTnode *m_root;
public:
Tree();
~Tree();
void creat(int *ch,const int c);
void _creat(BTnode *T,int &i,int *ch,const int c);
void _InorderTraverse(BTnode *T);
void InorderTraverse();
};
Tree::~Tree()
{
}
Tree::Tree()
{
m_root=NULL;
}
//中序遍历
void Tree::InorderTraverse()
{
_InorderTraverse(m_root);
}
void Tree::_InorderTraverse(BTnode *T)
{
if(T)
{
_InorderTraverse(T->Lchild);
cout<<T->data;
_InorderTraverse(T->Rchild);
}
}
//先序
void Tree::creat(int *ch,const int c)
{
int i=0;
_creat(m_root,i,ch,c);
}
void Tree::_creat(BTnode *T,int &i,int *ch,const int c)
{
if(ch[i]!=c)
{
T=new BTnode;
T->data=ch[i];
_creat(T->Lchild,++i,ch,c);
_creat(T->Rchild,++i,ch,c);
}
}
void main()
{
int a[]={1,5,3,0,0,4,0,0,6,7,0,0,2,0,0};
const int c=0;
Tree t;
t.creat(a,c);
t.InorderTraverse();
//system("pause");
}
慕移动6052691
相关分类