Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I have a problem with the second example. An attribute to track the title of an employee would solve the problem of finding the CEO and any other type of employee, this should not be done with recursion. If recursion is such an important concept we should have real-life examples of it being employed properly to lean on.

Also am I missing something or does your findCeo function return the employee when they have a boss and recurse when they have no boss? Seems like this just returns the employee in most cases and infinitely recurses for the CEO who presumably has no boss.



Take another example then. Like a random chain of people to find the last member. It's just an example to teach the concept.

I agree with your second comment though. Looks like a typo.


I think the author made a typo/bug. The function will crash on a null.getBoss call as soon as it found an employee without a boss.


Correct.


An attribute will only work if you are given a list of employees or other iterable structure.

My real example is that in my day job different business units of our customers are represented by a hierarchy, and you frequently traverse that hierarchy looking for some feature that's derived from the ancestor.

But really this is any algorithm that looks up a tree.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: