Hello to all.
There is a non-binary tree. It is necessary to make the search function of the element in the subtree. Those. find out in which subtree this element lies: in the left or in the right. Here is the bypass function (symmetric, recursive).
int Obxod(ElemT * pCur, int a) { if (pCur != NULL) { if (pCur->pRight != NULL) Obxod(pCur->pRight, a); if (pCur->Sod == a) return 1; if (pCur->pLeft != NULL) Obxod(pCur->pLeft, a); } return -1; }
Help me find a bug. Why the function does not return 1 if it finds an element in the subtree. It always returns -1 in any case. Those. it does not enter the body of the conditional operator at all. / I checked the main key pointers for left / right for NULL - everything is fine. Here is how this function is called for the subtree. A pointer to the subtree and the value to be found are passed to the function.
cout << "nЛевое поддерево = " << Obxod(pHead->pLeft, ii); cout << "nПравое поддерево = " << Obxod(pHead->pRight, ii);