Register to get access to free programming courses with interactive exercises

Definitions Python: Trees

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
├── # File
├── __tests__ # Directory
│   └── # File
├── setup.cfg # File
├── pyproject.toml # File
└── .venv # Directory
    └── lib # Directory
        └── python3.6 # Directory
            └── site-packages # Directory
                └── pip # Directory
                    └── # 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.


Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.