主题:关于二叉树的遍历 谁帮我看看为什么一运行就出错
能运行
但是输入完成后就出错了
哪位大哥帮我看看出了什么问题啊
#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);
BiNode<T>* Getroot();
void PreOrder(BiNode<T> *root);
void InOrder(BiNode<T> *root);
void PostOrder(BiNode<T> *root);
void LeverOrder(BiNode<T> *root);
private:
BiNode<T> *root;
void Creat(BiNode<T> *root);
void Release(BiNode<T> *root);
};
template<class T>
BiTree<T>::BiTree( )
{
Creat(root);
}
template<class T>
void BiTree<T>::Creat(BiNode<T> *root)
{
char ch;
cin>>ch;
if(ch=='#')root=NULL;
else
{
root=new BiNode<T>;
root->data=ch;
Creat(root->lchild);
Creat(root->rchild);
}
}
template<class T>
BiTree<T>::~BiTree(void)
{
Release(root);
}
template<class T>
void BiTree<T>::Release(BiNode<T> *root)
{
if(root!=NULL)
{
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
template<class T>
BiNode<T>* BiTree<T>::Getroot( )
{
return root;
}
template<class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
if(root==NULL) return;
else{
cout<<root->data<<" ";
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
template <class T>
void BiTree<T>::InOrder (BiNode<T> *root)
{
if (root==NULL) return;
else{
InOrder(root->lchild);
cout<<root->data<<" ";
InOrder(root->rchild);
}
}
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
if (root==NULL) return;
else{
PostOrder(root->lchild);
PostOrder(root->rchild);
cout<<root->data<<" ";
}
}
template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
const int MaxSize = 100;
int front = 0;
int rear = 0;
BiNode<T>* Q[MaxSize];
BiNode<T>* q;
if (root==NULL) return;
else{
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;
}
}
}
void main()
{
BiTree<char> bt;
BiNode<char>* root = bt.Getroot( );
cout<<"------前序遍历------ "<<endl;
bt.PreOrder(root);
cout<<endl;
cout<<"------中序遍历------ "<<endl;
bt.InOrder(root);
cout<<endl;
cout<<"------后序遍历------ "<<endl;
bt.PostOrder(root);
cout<<endl;
cout<<"------层序遍历------ "<<endl;
bt.LeverOrder(root);
cout<<endl;
}
但是输入完成后就出错了
哪位大哥帮我看看出了什么问题啊
#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);
BiNode<T>* Getroot();
void PreOrder(BiNode<T> *root);
void InOrder(BiNode<T> *root);
void PostOrder(BiNode<T> *root);
void LeverOrder(BiNode<T> *root);
private:
BiNode<T> *root;
void Creat(BiNode<T> *root);
void Release(BiNode<T> *root);
};
template<class T>
BiTree<T>::BiTree( )
{
Creat(root);
}
template<class T>
void BiTree<T>::Creat(BiNode<T> *root)
{
char ch;
cin>>ch;
if(ch=='#')root=NULL;
else
{
root=new BiNode<T>;
root->data=ch;
Creat(root->lchild);
Creat(root->rchild);
}
}
template<class T>
BiTree<T>::~BiTree(void)
{
Release(root);
}
template<class T>
void BiTree<T>::Release(BiNode<T> *root)
{
if(root!=NULL)
{
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
template<class T>
BiNode<T>* BiTree<T>::Getroot( )
{
return root;
}
template<class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
if(root==NULL) return;
else{
cout<<root->data<<" ";
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
template <class T>
void BiTree<T>::InOrder (BiNode<T> *root)
{
if (root==NULL) return;
else{
InOrder(root->lchild);
cout<<root->data<<" ";
InOrder(root->rchild);
}
}
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
if (root==NULL) return;
else{
PostOrder(root->lchild);
PostOrder(root->rchild);
cout<<root->data<<" ";
}
}
template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
const int MaxSize = 100;
int front = 0;
int rear = 0;
BiNode<T>* Q[MaxSize];
BiNode<T>* q;
if (root==NULL) return;
else{
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;
}
}
}
void main()
{
BiTree<char> bt;
BiNode<char>* root = bt.Getroot( );
cout<<"------前序遍历------ "<<endl;
bt.PreOrder(root);
cout<<endl;
cout<<"------中序遍历------ "<<endl;
bt.InOrder(root);
cout<<endl;
cout<<"------后序遍历------ "<<endl;
bt.PostOrder(root);
cout<<endl;
cout<<"------层序遍历------ "<<endl;
bt.LeverOrder(root);
cout<<endl;
}