C++ Logic Error (boolean) -
template <class t> bool bst<t>::printsubtree(t item) { int id; bool result; if (root==null) { cout << "empty tree, please insert data" << endl; return false; } cout << "enter id print sub-tree" << endl; cin >> id; result=searchsubtree(id, item); if(result == false) { cout << "record not found" << endl << endl; } //preorderprint(); return true; } template <class t> bool bst<t>::searchsubtree(int target, t &item) { btnode<t> *cur, *curtemp; bool flag = false; if (root==null) { cout << "empty tree, please insert data" << endl; return false; } cur = root; curtemp = root; while(!flag && cur!= null) { if(curtemp->item.id == target) { flag = true; root = curtemp; //promote desired node become root temporarily inorderprint(); flag = true; } else if (curtemp->item.id < target) //target value greater current value, search right sub-tree { curtemp = curtemp->right; } else if (curtemp->item.id > target) //search left sub-tree { curtemp = curtemp -> left; } root = cur; //set root position ori place } return flag; }
in code above, running fine. however, why tried input value doesn't exist(id input) , program crash, can't proceed if(result == false)
. may know what's problem on here?
thanks guides!
your while statement shall use curtemp instead of cur.
while(!flag && curtemp!= null)
Comments
Post a Comment