#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 _PerTraverse(BTnode *T); void PerTraverse();};Tree::~Tree(){ delete m_root;}Tree::Tree(){ m_root=NULL;}//先序遍历void Tree::PerTraverse(){ _PerTraverse(m_root);}void Tree::_PerTraverse(BTnode *T){ if(T) { cout<<T->data<<" "; _PerTraverse(T->Lchild); _PerTraverse(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=NULL; else { T=new BTnode; T->data=ch[i]; _creat(T->Lchild,++i,ch,c); _creat(T->Rchild,++i,ch,c); }}void main(){ int a[100]={1,2,4,0,0,5,7,0,8,0,0,0,3,6,0,0,0}; int c=0; Tree t; t.creat(a,c); t.PerTraverse();}
目前暂无任何回答
- 0 回答
- 0 关注
- 1141 浏览
添加回答
举报
0/150
提交
取消