程序没错 ,输出不出来结果,调试了好长时间 。。。。。 谢谢

#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");
}


慕田峪7551596
浏览 1247回答 1
1回答

慕移动6052691

1、如果想改变指针所指向的内容,采用单重指针就行了,就是普通的真正2、如果想改变指针所指向的地址,则需要采用双重指针 BTnode** T,_creat(m_root,i,ch,c);如果想改变m_root的指向,所以要使用双重指针,传入&m_roor;修改后代码:遍历出的内容是否正确我没有做检查#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");}
打开App,查看更多内容
随时随地看视频慕课网APP