HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In some situations, we need additional information while traversing the tree, depending on the node location. It can't be obtained from the node definition itself - this information should be accumulated directly during the traversal.</p>
1 <p>In some situations, we need additional information while traversing the tree, depending on the node location. It can't be obtained from the node definition itself - this information should be accumulated directly during the traversal.</p>
2 <p>This information includes, among other things, the full path to the file or the depth of the current node. The node itself doesn't actually know where it's located. The file location in the file structure is determined by the nodes that lead to the specific file.</p>
2 <p>This information includes, among other things, the full path to the file or the depth of the current node. The node itself doesn't actually know where it's located. The file location in the file structure is determined by the nodes that lead to the specific file.</p>
3 <p>In this lesson, we'll get to know the concept of<strong>accumulator</strong>, a special parameter that collects the necessary data while traversing the tree. It makes the code more complicated but allows one to handle more tricky tasks.</p>
3 <p>In this lesson, we'll get to know the concept of<strong>accumulator</strong>, a special parameter that collects the necessary data while traversing the tree. It makes the code more complicated but allows one to handle more tricky tasks.</p>
4 <p>Let's take the following task: we want to find all empty directories in our file system. First, we'll implement a basic solution, and then we'll complicate it and introduce an accumulator. Below is an example of a file system:</p>
4 <p>Let's take the following task: we want to find all empty directories in our file system. First, we'll implement a basic solution, and then we'll complicate it and introduce an accumulator. Below is an example of a file system:</p>
5 <p>There are three empty directories in this structure:<em>/logs</em>,<em>/etc/apache</em>and<em>/etc/consul/data</em>. The code that does this task looks like this:</p>
5 <p>There are three empty directories in this structure:<em>/logs</em>,<em>/etc/apache</em>and<em>/etc/consul/data</em>. The code that does this task looks like this:</p>
6 <p>The most unusual thing about this implementation is the flatMap() function. What is it for? If you leave only map(), the result may be surprising:</p>
6 <p>The most unusual thing about this implementation is the flatMap() function. What is it for? If you leave only map(), the result may be surprising:</p>
7 <p>This happens because an array is returned at each nesting level. The output is an array of arrays, similar in structure to the original file tree. To avoid this, you need to fix the code with flat() or use flatMap() straight away.</p>
7 <p>This happens because an array is returned at each nesting level. The output is an array of arrays, similar in structure to the original file tree. To avoid this, you need to fix the code with flat() or use flatMap() straight away.</p>
8 <p>Let's try to make it more difficult. Find all empty directories, but with a maximum search depth of 2 levels. Basically,<em>/logs</em>and<em>/etc/apache</em>fit this condition, but<em>/etc/consul/data</em>- does not.</p>
8 <p>Let's try to make it more difficult. Find all empty directories, but with a maximum search depth of 2 levels. Basically,<em>/logs</em>and<em>/etc/apache</em>fit this condition, but<em>/etc/consul/data</em>- does not.</p>
9 <p>To begin with, you need to understand where to get the depth from. With trees, depth is counted as the number of edges from the root to the desired node. It's easy to calculate visually, but what about the code? The depth of a particular node can be represented as the depth of the previous node plus one.</p>
9 <p>To begin with, you need to understand where to get the depth from. With trees, depth is counted as the number of edges from the root to the desired node. It's easy to calculate visually, but what about the code? The depth of a particular node can be represented as the depth of the previous node plus one.</p>
10 <p>The next step is to add a variable that is passed in each recursive call (falling into the directory). In our task, this value contains the current depth. I.e., at each level (within each directory) one is added to it. This variable is called<em>accumulator</em>because it<em>accumulates</em>data.</p>
10 <p>The next step is to add a variable that is passed in each recursive call (falling into the directory). In our task, this value contains the current depth. I.e., at each level (within each directory) one is added to it. This variable is called<em>accumulator</em>because it<em>accumulates</em>data.</p>
11 <p>The only problem is that the original findEmptyDirPaths() function has exactly one parameter: the node. It can't save information about the current depth level for all nested directories and files. Therefore, we have to introduce an internal function that will be able to pass the accumulator further down the tree:</p>
11 <p>The only problem is that the original findEmptyDirPaths() function has exactly one parameter: the node. It can't save information about the current depth level for all nested directories and files. Therefore, we have to introduce an internal function that will be able to pass the accumulator further down the tree:</p>
12 <p>You can go even further and allow the maximum depth to be specified outside:</p>
12 <p>You can go even further and allow the maximum depth to be specified outside:</p>
13 <p>But one question arises, how do we make it so that by default, the entire tree is viewed? For example, you can take a clearly large number and make it the default value. This approach will work, but it's a bit of a hack job. The proper way to do this is to use<em>Infinity</em>as the default value:</p>
13 <p>But one question arises, how do we make it so that by default, the entire tree is viewed? For example, you can take a clearly large number and make it the default value. This approach will work, but it's a bit of a hack job. The proper way to do this is to use<em>Infinity</em>as the default value:</p>
14  
14