问题描述
- 程序崩溃问题,系统不报错。
- #include
using namespace std;
template
struct TreeNode{
T data;
TreeNodeFirstChild*NextBrother;
public:
TreeNode(T value=0TreeNode L=NULLTreeNode* R=NULL){
data=value;
FirstChild=L;
NextBrother=R;
}
TreeNodeGetFirstChild()const{return FirstChild;}
TreeNode*GetNextBrother()const{return NextBrother;}
TreeNode*SetFirstChild(TreeNode t){FirstChild=t;}
TreeNodeSetNextBrother(TreeNode t){NextBrother=t;}
T& GetData(){return data;}
void SetData(const T& item){data=item;}
};
template
class Tree{
private:
TreeNode* root;
T stop;
public:
Tree(){root=Creat();}
TreeNodeFindTarget(TreeNode tT target);
TreeNodeFindFather(TreeNode tTreeNode* p);
TreeNodeFirstChild(TreeNode t);
TreeNodeNextBrother(TreeNode t);
void DelSubtree(TreeNode* tTreeNode* p);//在t为根的树中删除p为根的子树
void Del(TreeNode* p);
void PreOrder(TreeNode* t);//递归先根遍历
void NotPreOrder(TreeNode* t);//非递归先根遍历
void AftOrder(TreeNode* t);//递归后根遍历
void NotAftOrder(TreeNode* t);//非递归后根遍历
T GetStop(){return stop;}
void SetStop(T stop1){stop=stop1;}
TreeNodeGetRoot(){return root;}
void SetRoot(TreeNode t){root=t;}
TreeNodeCreat();
};
template
TreeNode Tree::Creat(){
int item;
cin>>item;
TreeNode *root1;
if(item=='#')
root1=NULL;
else{
root1=new TreeNode;
root1->data=item;
root1->FirstChild=Creat();
root1->NextBrother=Creat();
}
return root1;
}
int main(){
Tree my_Tree;
//my_Tree.PreOrder(my_Tree.GetRoot());
return 0;
}
解决方案
if(item=='#')这个有问题,你输入不了#字符,所以死循环了。
你试试改成if(item==-1)呢?
时间: 2024-10-28 18:09:36