我想要进行线索二叉树的中序遍历,为什么我的这个程序编译通过却无法运行?
//binthr.h
template
class binthrnode
{
public:
binthrnode() //创建一个空结点
{
lchild=rchild=NULL;
}
binthrnode(T e)
{
data=e;
lchild=rchild=NULL;
}
binthrnode<T> *makebthrtree(binthrnode *bt); //生成二叉链表
void inorderthread(binthrnode *bt); //建立中序线索二叉链表
binthrnode<T> *next(binthrnode *p); //访问后继结点
void inorder(binthrnode *bt); //遍历线索二叉链表
private:
T data;
int ltag,rtag;
binthrnode<T> *lchild,*rchild;
};
typedef binthrnode *binthrtree;
template
void binthrnode::inorderthread(binthrnode *bt)
{
static binthrnode *pre=NULL;
if(bt!=NULL)
{
bt->inorderthread(bt->lchild);
bt->ltag=(bt->lchild==NULL)?1:0;
bt->rtag=(bt->rchild==NULL)?1:0;
if(pre)
{
if(pre->rtag==1)
pre->rchild=bt;
if(bt->ltag==1)
bt->lchild=pre;
}
pre=bt;
bt->inorderthread(bt->rchild);
}
}
template
binthrnode *binthrnode::next(binthrnode *p)
{
if(p->rtag==1)
return p->rchild;
else
{
p=p->rchild;
while(p->rtag==0)
p=p->lchild;
return p;
}
}
template
void binthrnode::inorder(binthrnode *bt)
{
if(bt!=NULL)
{
binthrnode *p=bt;
while(p->ltag==0)
p=p->lchild;
do{
cout<data<<" ";
p=bt->next(p);
} while(p!=NULL);
}
cout<<endl;
}
template
binthrnode *binthrnode::makebthrtree(binthrnode *bt)
{
char ch;
cin>>ch;
if(ch==' ')
bt=NULL;
else
{
binthrnode *bt;
bt->data=ch;
bt->ltag=0;
bt->rtag=0;
bt->lchild=makebthrtree(bt->lchild);
bt->rchild=makebthrtree(bt->rchild);
}
return bt;
}
//main.cpp
#include
using namespace std;
#include"binthr.h"
void main()
{
binthrnode BT;
binthrtree bt=&BT;
bt=bt->makebthrtree(bt);
bt->inorder(bt);
cout<
bt->inorderthread(bt);
bt->inorder(bt);
}