不知道为什么,关于一棵树!!!寻求帮助
#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&¤t->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&¤t->PrevSibling!=NULL)
{
current=current->PrevSibling;
}
else
{
current=NULL;
}
}
void Tree::Srh_NextSibling()
{
if(current!=NULL&¤t->NextSibling!=NULL)
{
current=current->NextSibling;
}
else
{
current=NULL;
}
}
int Tree::Parent(const int&value)
{
int i=0;
Find(value);
if(current==NULL&¤t==root) return i;
else
{
current=current->Father;
i=1;
return i;
}
}
int Tree::Parent()
{
int i=0;
if(current==NULL&¤t==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))出了问题,不让它变成一棵树.
请各位帮我查下错在哪?