HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Data aggregation is the most essential operation when working with trees. Examples of data aggregation are:</p>
1 <p>Data aggregation is the most essential operation when working with trees. Examples of data aggregation are:</p>
2 <ul><li>Calculating the total number of files in the directory or the overall size of all files</li>
2 <ul><li>Calculating the total number of files in the directory or the overall size of all files</li>
3 <li>Getting a list of all files</li>
3 <li>Getting a list of all files</li>
4 <li>Finding all files by template</li>
4 <li>Finding all files by template</li>
5 </ul><p>The point in aggregating operations is accumulating the result. Traversing the tree in depth using a recursive process, which we discussed in detail in the previous lesson, is well-suited for this. Using it, we can go through all the tree nodes and collect the result, starting from the lowest level.</p>
5 </ul><p>The point in aggregating operations is accumulating the result. Traversing the tree in depth using a recursive process, which we discussed in detail in the previous lesson, is well-suited for this. Using it, we can go through all the tree nodes and collect the result, starting from the lowest level.</p>
6 <p>Let's look at aggregation using a recursive process by counting the total number of nodes in a tree as an example. Essentially, we want to find out how many files and directories are in our file tree:</p>
6 <p>Let's look at aggregation using a recursive process by counting the total number of nodes in a tree as an example. Essentially, we want to find out how many files and directories are in our file tree:</p>
7 <p>There's not much code here, but it's pretty tricky. There are several key points:</p>
7 <p>There's not much code here, but it's pretty tricky. There are several key points:</p>
8 <ol><li>The function checks the node type:<ul><li>If it is a file, the function returns the node</li>
8 <ol><li>The function checks the node type:<ul><li>If it is a file, the function returns the node</li>
9 <li>If the node is a directory, the function returns the child nodes, so we call our function again for each child node and repeat the algorithm</li>
9 <li>If the node is a directory, the function returns the child nodes, so we call our function again for each child node and repeat the algorithm</li>
10 </ul></li>
10 </ul></li>
11 <li>Calls to this function on children return the number of their descendants, so we get a list of the numbers that we need to combine</li>
11 <li>Calls to this function on children return the number of their descendants, so we get a list of the numbers that we need to combine</li>
12 <li>At the end, we get the total number of all descendants of the node plus one calculated as the current node itself</li>
12 <li>At the end, we get the total number of all descendants of the node plus one calculated as the current node itself</li>
13 </ol><p>Before moving on, have a play around with this code. It's the only way to get to know how it works.</p>
13 </ol><p>Before moving on, have a play around with this code. It's the only way to get to know how it works.</p>