c++二叉树问题 运行输入“ab#d##c##”后层序遍历有错误,麻烦各位高手帮改改最好在改的地方注明,原因,谢

#include<iostream>#include<string>using namespace std;template<class T>struct BiNode{ T data; BiNode<T> *lchild, *rchild;}; template<class T>class BiTree{public: BiTree(); ~BiTree(); void PreOrder(BiNode<T> *root); void InOrder(BiNode<T> *root); void PostOrder(BiNode<T> *root); void LevelOrder(BiNode<T> *root); BiNode<T> *getroot() { return root; }private: BiNode<T> *root; BiNode<T> *lchild, *rchild; void Creat(BiNode<T> *root); void Release(BiNode<T> *root);};template<class T>void BiTree<T>::PreOrder(BiNode<T> *root) //前序遍历{ if(root->data==✀#✀) return; else { cout<<root->data; PreOrder(root->lchild); PreOrder(root->rchild); }}template<class T> //中序遍历void BiTree<T>::InOrder(BiNode<T> *root){ if(root->data==✀#✀) return; else { InOrder(root->lchild); cout<<root->data; InOrder(root->rchild); }}template<class T> //后序遍历void BiTree<T>::PostOrder(BiNode<T> *root){ if(root->data==✀#✀) return; else { PostOrder(root->lchild); PostOrder(root->rchild); cout<<root->data; }}template<class T> //层序遍历void BiTree<T>::LevelOrder(BiNode<T> *root){ int front=0; int rear=0; BiNode<T> *Q[100]; BiNode<T> *q; if(root->data==✀#✀) return; Q[++rear]=root; while(front!=rear) { q=Q[++front]; cout<<q->data; if(q->lchild!=NULL) Q[++rear]=q->lchild; if(q->rchild!=NULL) Q[++rear]=q->rchild; }}template<class T> //二叉树构建BiTree<T>::BiTree(){root =new BiNode<T>; Creat(root);}template<class T>void BiTree<T>::Creat(BiNode<T> *root){ char ch; cin>>ch; if(ch==✀#✀)root->data=✀#✀; else { root->data=ch; root->lchild=new BiNode<T>; Creat(root->lchild); root->rchild=new BiNode<T>; Creat(root->rchild); }}template<class T>BiTree<T>::~BiTree(){ Release(root);}template<class T>void BiTree<T>::Release(BiNode<T> *root){ if(root==NULL) { Release(root->lchild); Release(root->rchild); delete root; }}void main(){ cout<<"输入要遍历的字符:"<<endl; BiTree<char> h; cout<<"前序遍历:"<<endl; h.PreOrder(h.getroot()); cout<<endl; cout<<"中序遍历:"<<endl; h.InOrder(h.getroot()); cout<<endl; cout<<"后序遍历:"<<endl; h.PostOrder(h.getroot()); cout<<endl; cout<<"层序遍历:"<<endl; h.LevelOrder(h.getroot()); cout<<endl;}
2026年09月25日 05:32
有1个网友回答
网友(1):

修改的地方加了★,楼主看看。

#include
#include
using namespace std;
template
struct BiNode
{
T data;
BiNode *lchild, *rchild;
};
template
class BiTree
{
public:

BiTree();
~BiTree();
void PreOrder(BiNode *root);
void InOrder(BiNode *root);
void PostOrder(BiNode *root);
void LevelOrder(BiNode *root);
BiNode *getroot()
{
return root;
}
private:
BiNode *root;
BiNode *lchild, *rchild;
void Creat(BiNode *root);
void Release(BiNode *root);
};
template
void BiTree::PreOrder(BiNode *root) //前序遍历
{
if(root->data=='#') return;
else
{
cout<data;
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
template //中序遍历
void BiTree::InOrder(BiNode *root)
{
if(root->data=='#') return;
else
{
InOrder(root->lchild);
cout<data;
InOrder(root->rchild);
}
}
template //后序遍历
void BiTree::PostOrder(BiNode *root)
{
if(root->data=='#') return;
else
{
PostOrder(root->lchild);
PostOrder(root->rchild);
cout<data;
}
}
template //层序遍历
void BiTree::LevelOrder(BiNode *root)
{
int front=0;
int rear=0;
BiNode *Q[100];
BiNode *q;
if(root->data=='#') return;
Q[++rear]=root;
while(front!=rear)
{

q=Q[++front];
cout<data;
if(q->lchild->data != '#') Q[++rear]=q->lchild; //★尾部的节点的特征是值为 # ,不是左右为 NULL 指针
if(q->rchild->data != '#') Q[++rear]=q->rchild; //★尾部的节点的特征是值为 # ,不是左右为 NULL 指针
}
}
template //二叉树构建
BiTree::BiTree()
{
root =new BiNode;
Creat(root);
}
template
void BiTree::Creat(BiNode *root)
{
char ch;
cin>>ch;
if(ch=='#')root->data='#';
else
{

root->data=ch;
root->lchild=new BiNode;
Creat(root->lchild);
root->rchild=new BiNode;
Creat(root->rchild);
}
}
template
BiTree::~BiTree()
{
Release(root);
}
template
void BiTree::Release(BiNode *root)
{
if(root==NULL)
{
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
void main()
{ cout<<"输入要遍历的字符:"< BiTree h;
cout<<"前序遍历:"< h.PreOrder(h.getroot());
cout< cout<<"中序遍历:"< h.InOrder(h.getroot());
cout< cout<<"后序遍历:"< h.PostOrder(h.getroot());
cout< cout<<"层序遍历:"< h.LevelOrder(h.getroot());
cout<}
调试通过。