c++ - double free or corruption when implement binary tree -
update:
add more code asked.pasted end of question. stops.
current code fine delete leaf, come root, can not delete.
==== update: revise code, change removerootmatch to
tmp = root; root = tmp->right; tmp->right = null; delete tmp;
and no error not delete node.
=====
the program simple following step:
- find min value of binary tree;
- record min value in vector;
- delete node min value in tree;
- repeat 1-3 till tree empty.
i have removenode()function, call removeroot function(check code below),if 1 needed removed root. have trouble function. doing debug , found wrong removerootmatch function. give error when run. error got error got *** glibc detected *** ./bintree: double free or corruption (fasttop): 0x0000000000727060 ***
, 1 can me?
the tree defined following, language c++
typedef struct mynode* lpnode; typedef struct mynode node; struct mynode { double key; lpnode left; //left subtree lpnode right; //right subtree };
main part of program following:
- nmax initialed 0,
- sortedvector alloacted vector space large total nodes in tree,
- min initialed 99999.
- minvalue return min value of tree.
- comparedouble(a,b) return 1
if < b
,return 2if > b
,return 3 if equal
code following
void removerootmatch(lpnode root) { lpnode tmp = makenewnode(root->key); tmp->left = root->left; tmp->right = root->right; //no child if(root->left==null && root->right == null) { root=null; delete root; } else if(root->left==null && root->right!=null){ //one right child //delete root; root = tmp->right; tmp->right = null; delete tmp; } else { printf("remove root bug!\n"); } }
this function call removenode function.
//compare double int comparedouble(double a,double b) { if(a-b<-epsilon) //a<b return 1; else if(a-b>epsilon)//a>b return 2; else return 3; } //find min key in tree double minvalue(lpnode root,double min) { if(root == null) return min; if(comparedouble(root->key,min)==1) min = root->key; min = minvalue(root->left, min); min = minvalue(root->right, min); return min; } //remove root void removerootmatch(lpnode& root) { lpnode tmp = makenewnode(root->key); tmp->left = root->left; tmp->right = root->right; //no child if(root->left==null && root->right == null) { root=null; delete root; } else if(root->left==null && root->right!=null){ //one right child double k = root->key; root = tmp->right; tmp->right = null; delete tmp; //tmp=tmp->right; //root->right = null; //delete root; //root = tmp; } else { printf("remove root bug!\n"); } } //remove node void removematch(lpnode& root,lpnode match,bool left) { //no child if(match->left==null && match->right == null){ double k = match->key; left==true? root->left=null: root->right=null; delete match; if(!root->left)printf("%f ",k); } else if(match->left==null && match->right!=null){//one right child double k = match->key; left==true? root->left=match->right: root->right=match->right; delete match; if(!root->left)printf("%f ",k); } else { printf("remove root bug!\n"); } } //delete 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->left->key)==3 ? removematch(root,root->left,true): removenode(root->left,min); }else if(comparedouble(min,root->key)==2 && root->right != null){ comparedouble(min,root->right->key)==3 ? removematch(root,root->right,false): removenode(root->right,min); }else{ printf("remove bug1!\n"); } } //call minvalue find min key //record min key in vector //call removenode delete node //repeat till tree empty void problem1(lpnode root,double* sortedvector,int& nmax) { double min; //while(root!=null) for(int i=0;i<3;i++) { min = max; sortedvector[nmax] = minvalue(root,min) ; printf("inv%f\n",sortedvector[nmax]); removenode(root,sortedvector[nmax]); nmax++; } printf("the tree empty"); }
if function removenode
may adjust root, function not declared properly.
void removerootmatch(lpnode root)
you passing pointer root. inside removerootmatch
function, working on copy of pointer. code inside of removerootmatch
function:
root = tmp->right; tmp->right = null; delete tmp;
does not change root node when function returns.
to address issue, should pass root pointer reference:
void removerootmatch(lpnode& root)
Comments
Post a Comment