官术网_书友最值得收藏!

Recursive structures

The find command on Linux (and the dir /s command on Windows) recursively descends into a directory; if there are a few subdirectories within command, then it descends into each subdirectory, one by one. If the subdirectories, in turn, have subdirectories, command goes into each one and repeats the process all over again till all the directories are traversed. Let's have a look at the following directory:

Figure 3.1: A directory tree is a recursive structure

Given this directory, try the following command:

 % find ./tmp -type f -exec wc -c {} \;

The find command starts at the tmp directory and applies the wc command to each regular file (so for this example, skip directories).

The command enters in tmp and finds a and c. As these are directories, the flow enters a first, and finds b and one.txt. As directory b is empty, it looks at one.txt for which the predicate type f is true. So, the characters are counted for one.txt, and then the flow comes back to a and recurs into c. The process continues till every node in the directory tree is visited; also, every node is visited once and only once. Now, if you look carefully, when we come to node a, we have the same problem to solve as when we started with tmp. This problem is inherently recursive. This is the essence of recursion—we keep reducing the problem by dividing the dataset into smaller and smaller pieces. At some point though, we need to look at solving the problem (counting characters in regular files in our case). In our case, when we don't have any more directories to recur into, forms a base case with following cases:

  • A subdirectory is empty—the flow just returns in this case.
  • The note is not a directory at all but a regular file (one.txt). We perform the operation (count characters) and return.

The sub-problems that are solved directly without dividing it any further. Such base cases allow the algorithm to terminate eventually.

In the previous chapter, we looked at the binary tree traversal method. When the traversal flow hits the Null object, it terminates the traversal. This is a base case. Similarly, when the insertion flow hits the Null object, it adds the new node, forming another base case. When the traversal hits an intermediate node, we have a recursive case.

主站蜘蛛池模板: 黄平县| 蒙自县| 克东县| 临安市| 宿迁市| 黑水县| 渭南市| 福安市| 南川市| 双牌县| 灌南县| 习水县| 五大连池市| 定边县| 高台县| 通城县| 墨玉县| 格尔木市| 五莲县| 海阳市| 增城市| 龙里县| 富源县| 天等县| 鹤山市| 湘阴县| 广饶县| 涟水县| 淳安县| 林芝县| 隆安县| 开鲁县| 济阳县| 溧水县| 阿克陶县| 炉霍县| 汪清县| 和平区| 皮山县| 利辛县| 洛扎县|