将二叉树叶子链成一个链表,求指教哪出错
#include<iostream>using namespace std;
template <class T>
struct BinTreeNode{
T data;
BinTreeNode<T> *leftChild,*rightChild, *link;
BinTreeNode():leftChild(0),rightChild(0),link(0){}
BinTreeNode(T x,BinTreeNode<T> *l=0,BinTreeNode<T> *r=0,BinTreeNode<T> *lk=0):data(x),leftChild(l),rightChild(r),link(lk){}
};
template <class T>
struct node
{
BinTreeNode <T> *tp;
bool f;
};
template <class T>
class BinaryTree {
public:
void LianBiao()//叶子结点转换为链表
{LianBiao(root);}
BinaryTree ( ) : root (0) {}
BinaryTree ( T value ) :root (0) { }
~BinaryTree(){destroy(root); }
int size(){return size(root);}
void creatBinTree(istream& in,BinTreeNode<T> * & current);
friend istream &operator >> ( istream& in, BinaryTree <T> &Tree );
friend ostream &operator << ( ostream& out, BinaryTree <T> &Tree );
void Traverse ( BinTreeNode<T> *current,ostream& out );
BinTreeNode <T> *root;
protected:
void LianBiao(BinTreeNode <T> * current );//叶子结点转换为链表
};
//建立二叉树表
template <class T>
void BinaryTree<T>::creatBinTree(istream & in,BinTreeNode<T> *& bt)
{ stack<BinTreeNode<T> * > s;//新建栈
bt=0;
BinTreeNode<T> *p,*t;int k;
T ch;
in>>ch;
while(ch!=RefValue)
{
switch(ch)
{
case '(':s.push(p);k=1;break;//进入子树
case ')':s.pop();break;//退出子树
case ',':k=2;break;//以','作为左右子树分割点
default:p=new BinTreeNode<char>(ch);//为p申请新空间并赋值
if(bt==0) bt=p;
else
if(k==1)
{
t=s.top();t->leftChild=p;//链入*t的左子女
}
else
{
t=s.top();t->rightChild=p;//链入*t的右子女
}
};
in>>ch;
}
};
template <class T>
istream & operator >> ( istream & in, BinaryTree <T> &Tree )
{
Tree.creatBinTree(in,Tree.root);
return in;
};
template <class T>
ostream & operator <<( ostream & out, BinaryTree <T> &Tree )
{
out<<"二叉树的前序遍历"<<endl;
Tree.Traverse(Tree.root,out);
out<<endl;
return out;
};
//叶子结点转换为链表
template<class T>
void BinaryTree<T>::LianBiao(BinTreeNode<T> *current)
{
BinTreeNode<T> *p = new BinTreeNode;
BinTreeNode<T> *q;
while(current!=0)
{
if(current->leftChild==0&¤t->rightChild==0)
{
p=current;
q = new BinTreeNode;
q=p;
p = new BinTreeNode;
p->link=q;
}
LianBiao(current->leftChild);
LianBiao(current->rightChild);
}
while(p!=0)
{
cout<<p->data<<" ";
p=p->link;
}
}