3 ms·
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 emplo
by mulmen 9y ago
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.
- Kiro 9y agoTake 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.
- poxrud 9y agoI 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.
- hyperpape 9y agoCorrect.
- hyperpape 9y agoAn 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.