user12340167
user12340167

Reputation:

Search for an element in a n-ary tree recursively

I have a Tree class that has a nested private Node class.
I wrote a method (Search(T elem)) that searches if the element given as parameter exists in the tree and sends a boolean.
However my problem is that this method only sends true if the element exists in the first branch of my tree and if not, it sends false. I must have mistaken somewhere in recursive calls.

for example, here in main i call search('C') and I get false although I should get true because C is in the second branch of my tree.
P.S. : Also I should mention that in this class, copy operator and constructor must be disabled and i can not use anything but raw pointer (no vector or smart pointers).
Thank you in advanced.

Upvotes: 0

Views: 152

Answers (1)

n314159
n314159

Reputation: 5075

You shouldn't return children[i]->search(elem); directly. So it will return false if elem is not found in the first branch, it will never get past the first child. Only return there directly if it is found. Otherwise try the other children.

Upvotes: 1

Related Questions