up
This commit is contained in:
parent
85e67ebb5b
commit
19cd4ab574
12 changed files with 56 additions and 59 deletions
|
@ -326,18 +326,18 @@ In other words, a company has departments.
|
|||
|
||||
Now let's say we want a function to get the sum of all salaries. How can we do that?
|
||||
|
||||
An iterative approach is not easy, because the structure is not simple. The first idea may be to make a `for` loop over `company` with nested subloop over 1st level departments. But then we need more nested subloops to iterate over the staff in 2nd level departments like `sites`. ...And then another subloop inside those for 3rd level departments that might appear in the future? Should we stop on level 3 or make 4 levels of loops? If we put 3-4 nested subloops in the code to traverse a single object, it becomes rather ugly.
|
||||
An iterative approach is not easy, because the structure is not simple. The first idea may be to make a `for` loop over `company` with nested subloop over 1st level departments. But then we need more nested subloops to iterate over the staff in 2nd level departments like `sites`... And then another subloop inside those for 3rd level departments that might appear in the future? If we put 3-4 nested subloops in the code to traverse a single object, it becomes rather ugly.
|
||||
|
||||
Let's try recursion.
|
||||
|
||||
As we can see, when our function gets a department to sum, there are two possible cases:
|
||||
|
||||
1. Either it's a "simple" department with an *array of people* -- then we can sum the salaries in a simple loop.
|
||||
2. Or it's *an object with `N` subdepartments* -- then we can make `N` recursive calls to get the sum for each of the subdeps and combine the results.
|
||||
1. Either it's a "simple" department with an *array* of people -- then we can sum the salaries in a simple loop.
|
||||
2. Or it's *an object* with `N` subdepartments -- then we can make `N` recursive calls to get the sum for each of the subdeps and combine the results.
|
||||
|
||||
The (1) is the base of recursion, the trivial case.
|
||||
The 1st case is the base of recursion, the trivial case, when we get an array.
|
||||
|
||||
The (2) is the recursive step. A complex task is split into subtasks for smaller departments. They may in turn split again, but sooner or later the split will finish at (1).
|
||||
The 2nd case when we gen an object is the recursive step. A complex task is split into subtasks for smaller departments. They may in turn split again, but sooner or later the split will finish at (1).
|
||||
|
||||
The algorithm is probably even easier to read from the code:
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue