简体   繁体   中英

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.
PS : 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM