| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 788 人关注过本帖
标题:不知道为什么,关于一棵树!!!寻求帮助
只看楼主 加入收藏
zhanghj
Rank: 1
来 自:江苏盐城
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-11-24
结帖率:100%
收藏
 问题点数:0 回复次数:2 
不知道为什么,关于一棵树!!!寻求帮助
#include<iostream.h>
class Tree;
class TreeNode
{
    friend class Tree;
public:
    TreeNode();
    TreeNode(int value,TreeNode* fc=NULL,TreeNode* lc=NULL,TreeNode* ps=NULL,
        TreeNode* ns=NULL,TreeNode* fa=NULL);
private:
    int data;
    TreeNode* FirstChild;
    TreeNode* LastChild;
    TreeNode* PrevSibling;
    TreeNode* NextSibling;
    TreeNode* Father;
};
TreeNode::TreeNode()
{
    FirstChild=NextSibling=Father=NULL;
}
TreeNode::TreeNode(int value,TreeNode* fc,TreeNode* lc,TreeNode* ps,TreeNode* ns,TreeNode* fa)
{
    data=value;
    FirstChild=fc;
    LastChild=lc;
    PrevSibling=ps;
    NextSibling=ns;
    Father=fa;
}

class Tree
{
public:
    Tree();
    ~Tree();
    int Root();
    void BuildRoot(const int&value);
    void Srh_FirstChild();
    void Srh_LastChild();
    void Srh_PrevSibling();
    void Srh_NextSibling();
    int Parent(const int&value);
    int Parent();
    int Find(TreeNode* p,const int&value);
    int Find(const int&value);
    void InsertChild(const int&value);
    int DeleteChild(int i);
    void DeleteSubTree(TreeNode* p);
    void DeleteSubTree();
    int GetData();
    int IsEmpty(){if(root==NULL) return 1;else return 0;}
private:
    TreeNode* root;
    TreeNode* current;
};
Tree::Tree()
{
    root=current=NULL;
}
Tree::~Tree()
{
    if(root!=NULL) DeleteSubTree(root);
    root=NULL;
}
int Tree::Root()
{
    if(root==NULL){current=NULL; return 0;}
    else {current=root; return 1;}
}
void Tree::BuildRoot(const int&value)
{
    root=current=new TreeNode(value);
}
void Tree::Srh_FirstChild()
{
    if(current!=NULL&&current->FirstChild!=NULL)
    {
        current=current->FirstChild;
    }
    else
    {
        current=NULL;
    }
}
void Tree::Srh_LastChild()
{
    TreeNode* p=current->FirstChild;
    if(p==NULL)
    {
        current=p;
    }
    else
    {
        while(p->NextSibling!=NULL)
        {
            p=p->NextSibling;
        }
        current=p;
    }
}
void Tree::Srh_PrevSibling()
{
    if(current!=NULL&&current->PrevSibling!=NULL)
    {
        current=current->PrevSibling;
    }
    else
    {
        current=NULL;
    }
}
void Tree::Srh_NextSibling()
{
    if(current!=NULL&&current->NextSibling!=NULL)
    {
        current=current->NextSibling;
    }
    else
    {
        current=NULL;
    }
}
int Tree::Parent(const int&value)
{
    int i=0;
    Find(value);
    if(current==NULL&&current==root) return i;
    else
    {
        current=current->Father;
        i=1;
        return i;
    }
}
int Tree::Parent()
{
    int i=0;
    if(current==NULL&&current==root) return i;
    else
    {
        current=current->Father;
        i=1;
        return i;
    }
}
int Tree::Find(TreeNode*p,const int&value)
{
    int s=0;
    if(p->data==value)
    {
        s=1;
        current=p;
        return s;
    }
    else
    {
        TreeNode*q=p->FirstChild;
        while(q!=NULL&&!(s=Find(q,value))) q=q->NextSibling;
    }
    return s;
}
int Tree::Find(const int&value)
{
    if(IsEmpty()) return 0;
    return Find(root,value);
}
void Tree::InsertChild(const int& value)
{
    TreeNode* newNode=new TreeNode(value);
    if(current->FirstChild==NULL)
    {    
        current->FirstChild=newNode;
        newNode->Father=current;
        newNode->PrevSibling=NULL;
        newNode->NextSibling=NULL;
    }    
    else
    {
        TreeNode* p=current->FirstChild;
        while(p->NextSibling!=NULL) p=p->NextSibling;
        p->NextSibling=newNode;
        newNode->PrevSibling=p;
        newNode->Father=current;
        newNode->NextSibling=NULL;
    }
}
int Tree::DeleteChild(int i)
{
    if(i==1)
    {
        TreeNode* t=current->FirstChild;        
                  if(t!=NULL) current->FirstChild=t->NextSibling;
        else return 0;
    }
    else
    {
        TreeNode*q=current->FirstChild;
        int k=1;
        while(q!=NULL&&k<i-1){q=q->NextSibling;k++;}
        if(q!=NULL)
        {
            TreeNode*t=q->NextSibling;
            if(t!=NULL) q->NextSibling=t->NextSibling;
            else return 0;
        }
        else return 0;
    }
    return 1;
}
void Tree::DeleteSubTree(TreeNode*p)
{
    TreeNode*q=p->FirstChild,*next;
    while(q!=NULL)
    {
        next=q->NextSibling;
        DeleteSubTree(q);
        q=next;
    }
    delete p;
}
void Tree::DeleteSubTree()
{
    if(current!=NULL)
    {
        if(current=root) root=NULL;
        DeleteSubTree(current);
        current=NULL;
    }
}
int Tree::GetData()
{
    if(current==NULL)
    {
        cout<<"空!!";
        return 0;
    }
    else
    {
        return current->data;
    }    
}

void main()
{
    Tree x;
    x.BuildRoot(10);
    cout<<"树根:";
    cout<<x.GetData()<<endl;
    x.InsertChild(11);
    x.InsertChild(12);
    x.InsertChild(13);
    x.InsertChild(14);
    x.InsertChild(15);
    if(x.Find(12)!=0)
    {
        x.InsertChild(16);
        x.InsertChild(17);
        x.InsertChild(18);
    }
    x.Find(10);
    x.Srh_FirstChild();
    cout<<x.GetData()<<endl;
    x.Srh_LastChild();
    cout<<x.GetData()<<endl;

    x.Find(12);
    x.Parent();
    cout<<x.GetData()<<endl;

    x.Srh_PrevSibling();
    cout<<x.GetData()<<endl;
    x.Srh_NextSibling();
    cout<<x.GetData()<<endl;
    x.Srh_FirstChild();
    cout<<x.GetData()<<endl;
//    x.Srh_LastChild();
//    cout<<x.GetData()<<endl;
}
这是棵树,自我感觉应该没问题了,可是测试功能函数时没有效果,
可能是插入结点函数(void Tree::InsertChild(const int& value))出了问题,不让它变成一棵树.
请各位帮我查下错在哪?
2008-08-01 17:39
zhanghj
Rank: 1
来 自:江苏盐城
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-11-24
收藏
得分:0 
各位粘贴回去运行试试,找出问题在哪,
2008-08-03 18:02
zhanghj
Rank: 1
来 自:江苏盐城
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-11-24
收藏
得分:0 
Help~
2008-08-04 10:39
快速回复:不知道为什么,关于一棵树!!!寻求帮助
数据加载中...
 
   



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

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