标题:将二叉树叶子链成一个链表,求指教哪出错
只看楼主
正海其
Rank: 1
等 级:新手上路
帖 子:10
专家分:5
注 册:2013-10-21
结帖率:100%
 问题点数:0 回复次数:1 
将二叉树叶子链成一个链表,求指教哪出错
#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&&current->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;
    }
}
搜索更多相关主题的帖子: include public 二叉树 叶子 
2013-12-21 14:24
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
得分:0 
报错信息贴出来。


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2013-12-25 19:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-425489-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.643336 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved