| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖
标题:递归建立二叉树问题
只看楼主 加入收藏
回到原點
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2012-10-29
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:6 
递归建立二叉树问题
我的代码如下:
程序代码:
template <class Elem>
void CreateBinTree(BinNode<Elem> *subroot)
{
    BinNode<Elem>* T= subroot;
    Elem nodeval;
    cin>>nodeval;
    if(nodeval=='#')
    {
        subroot=NULL;
        return ;
    }
    else
    {
        subroot=new BinNodePtr<Elem>();
        if(subroot!=NULL)
        {
        subroot->setVal(nodeval);
        CreateBinTree<Elem>(subroot->left());   

        CreateBinTree<Elem>(subroot->right()); 

        }
    }
}
运行的结果是退不出递归,即使输“#”进去了,还是在不断递归,请问有什么问题?
搜索更多相关主题的帖子: 二叉树 
2012-11-10 21:02
回到原點
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2012-10-29
收藏
得分:0 
以下是我改进后的,但当我输入'#'后,会异常停止:
程序代码:
template <class Elem>
void  CreateBinTree(BinNode<Elem> *subroot)
{
    BinNode<Elem>* T= subroot;
    Elem nodeval;
    cin>>nodeval;
    if(nodeval!='#')
    {

        subroot=new BinNodePtr<Elem>();
        if(subroot!=NULL)
        {   

        subroot->setVal(nodeval);
        CreateBinTree<Elem>(subroot->left());   

        CreateBinTree<Elem>(subroot->right()); 

        }
        else
            exit(0);
    }
    else
        subroot=NULL;

}
求高手解答,谢谢了!
2012-11-11 10:57
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:10 
空间没有释放

成功贵在坚持
2012-11-11 16:26
回到原點
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2012-10-29
收藏
得分:0 
回复 3楼 mayuebo
请问那里是需要释放空间的?我是要建立一颗二叉树,不用释放空间吧?
2012-11-11 19:53
孀倪
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-8-21
收藏
得分:0 
貌似看上去好像没有错。。
我以前也写过类似的代码我这里还有呢 。原因找出来了但是我不记得了。
2012-11-15 19:13
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
回复 楼主 回到原點
BinNode<Elem>把 它的详细信息 贴出来 观摩下
2012-11-15 19:45
回到原點
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2012-10-29
收藏
得分:0 
回复 6楼 寒风中的细雨
程序代码:
#ifndef _BINNODE_H
#define _BINNODE_H
//Binary tree node abstract class
template <class Elem> class BinNode{
public:
    //return the node's element
    virtual Elem& val() = 0;
    //set the node's element
    virtual void setVal(const Elem &) = 0;
    //return the node's left child
    virtual BinNode* left() const = 0;
    //set the node's left child
    virtual void setLeft(BinNode*) = 0;
    //return the node's right child
    virtual BinNode* right() const = 0;
    //set the node's right child
    virtual void setRight(BinNode*) = 0;
    //return true if the node is a leaf
    virtual bool isLeaf() = 0;
};
#endif
#ifndef _BINNODEPTR_H
#define _BINNODEPTR_H
#include "BinNode.h"
// Binary tree node class
class KEComp{
public:

 static bool lt(int x,int y){return x<y;}

 static bool eq(int x,int y){return x==y;}

 static bool gt(int x,int y){return x>y;}
};
template <class Elem>
class BinNodePtr : public BinNode<Elem> {
private:
    Elem it;         // The node's value
    BinNodePtr* lc;  // Pointer to left child
    BinNodePtr* rc;  // Pointer to right child

public:
    BinNodePtr() { lc = rc = NULL; }
    BinNodePtr(Elem e, BinNodePtr* l =NULL,
        BinNodePtr* r =NULL)
    { it = e; lc = l; rc = r; }
    Elem& val() { return it; }
    void setVal(const Elem& e) { it = e; }
    inline BinNode<Elem>* left() const
    { return lc; }
    void setLeft(BinNode<Elem>* b)
    { lc = (BinNodePtr*)b; }
    inline BinNode<Elem>* right() const
    { return rc; }
    void setRight(BinNode<Elem>* b)
    { rc = (BinNodePtr*)b; }
    bool isLeaf()
    { return (lc == NULL) && (rc == NULL); }
   

};
代码如上,请问那里出了问题?
2012-11-16 22:23
快速回复:递归建立二叉树问题
数据加载中...
 
   



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

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