程序很简单,请执行以下步骤:
运行时没有报告错误,但函数removeNode保持printf("Remove bug1!\n");
我找不到任何逻辑错误,所以我不明白为什么会这样。该函数的结构是:
min<root->key
和&#39;离开不是NULL,则向左移动bug
树定义如下,语言为c ++
typedef struct myNode* LPNode;
typedef struct myNode Node;
struct myNode
{
double key;
LPNode Left; //left subtree
LPNode Right; //right subtree
};
程序的主要部分如下:
compareDouble(a,b)将返回1 if a < b
,返回2 if a > b
,如果相等则返回3
//删除root
void removeRootMatch(LPNode Root)
{
LPNode tmp = MakeNewNode(Root-&gt; key);
tmp-&gt;左= Root-&gt;左;
tmp-&gt; Right = Root-&gt; Right;
//没有孩子
if(Root-&gt; Left == NULL&amp;&amp; Root-&gt; Right == NULL){
Root = NULL;
删除Root;
} else if(Root-&gt; Left == NULL&amp;&amp; Root-&gt; Right!= NULL){//一个正确的孩子
Root = Root-&gt; Right;
tmp-&gt; Right = NULL;
删除tmp;
} else {
printf(&#34;删除root bug!\ n&#34;);
}
}
//remove a node
void removeMatch(LPNode Root,LPNode match,bool left)
{
//no child
if(match->Left==NULL && match->Right == NULL){
left==true?
Root->Left=NULL:
Root->Right=NULL;
delete match;
}
else if(match->Left==NULL && match->Right!=NULL){//one right child
left==true?
Root->Left=match->Right:
Root->Right=match->Right;
delete match;
} else {
printf("Remove root bug!\n");
}
}
//delete a node
void removeNode(LPNode Root,double min)
{
if(compareDouble(min,Root->key)==3){
removeRootMatch(Root);
}else if(compareDouble(min,Root->key)==1 && Root->Left != NULL) {
compareDouble(min,Root->key)==3 ?
removeMatch(Root,Root->Left,true):
removeNode(Root->Left,min);
}else{
printf("Remove bug1!\n");
}
}
这是函数调用removeNode函数。
//call minValue to find the min key
//record the min key in a vector
//call removeNode to delete the Node
//repeat till the tree is empty
void problem1(LPNode Root,double* sortedvector,int& nmax)
{
double min = MAX;
while(Root!=NULL)
{
sortedvector[nmax] = minValue(Root,min) ;
nmax++;
removeNode(Root,min);
}
printf("The tree is empty");
}
答案 0 :(得分:1)
sortedvector[nmax] = minValue(Root,min) ;
removeNode(Root,sortedvector[nmax]);//change here
nmax++;
你有通过分钟的问题。我在这里回答,不只是阅读标题和投票。