The file structure is an example of a tree familiar to everyone who uses a computer. It consists of directories and different types of files:
python-package # Root directory ├── Makefile # File ├── README.md # File ├── __tests__ # Directory │ └── test_fs.py # File ├── setup.cfg # File ├── pyproject.toml # File └── .venv # Directory └── lib # Directory └── python3.6 # Directory └── site-packages # Directory └── pip # Directory └── __init__.py # File
It's called a tree because of its structure. We build all elements of the file system in a hierarchy. If we're talking about Windows, there's a root directory or disk at the top level, and then there are files and directories, which themselves can contain files and directories.
The key feature of the tree structure is that it is recursive. In other words, a tree consists of subtrees, which in turn consists of subtrees, and so on. This feature defines the main ways of working with trees in the code. They all work recursively. Trees consist of nodes and edges between them. The edges don't exist, we only need them to visualize the connection and describe it if necessary.
Nodes can be of two types:
- Internal nodes with descendants
- Leaf nodes without descendants
In the case of a file system, leaf nodes are files, and internal nodes are directories.
Each node in the tree has a parent. The only exception is the root node — it has no parents, and that's where the tree starts. Internal nodes can have any number of descendants. In addition, you also need to understand the concept of depth, which determines how many steps you need to go along the nodes from the root node to reach the current one. Nodes located at the same depth and having a common parent are called sibling nodes.