Skip to content
Educora
IntermediateGrade 922 min21 / 59

Tree information models

A tree is a graphic model of a hierarchy: root, node, leaf, level; family, classification and file-system trees, the full name of a file; a tree as a graph without cycles (edges = nodes − 1), counting leaves and paths, writing a tree as a nested list.

Check yourself
In this lesson you will learn
  • Recognise and count the parts of a tree: root, node, parent, child, leaf, level.
  • Read family, classification and file-system trees and write the full name of a file.
  • Explain a tree as a connected graph without cycles and use the rule “edges = nodes − 1”.
  • Turn a tree into a nested list or bracket notation and back.

A family tree in a photo album, the folders on a computer, the contents page of a book, the draw of a sports tournament — they all have the same shape: branches grow from one starting point, and more branches grow from those. Such a structure is called a hierarchy, and its graphic model is a tree. The name is no accident: the picture looks like an upside-down tree, with the root at the top and the leaves at the bottom.

In the lesson “Models and modelling” we met the tree among the graphic information models, and in “Table information models: solving logic puzzles with tables” we worked with tables. A table places objects side by side, while a tree shows who is under whom and what is part of what.

The parts of a tree

Definition
Tree

A graphic information model that shows hierarchical (“belongs to”) relations between objects. A tree has one root, and every node except the root has exactly one parent.

  • Root — the top node; it has no parent.
  • Node (vertex) — any element of the tree; the lines joining nodes are edges (branches).
  • Parent and child — an upper and a lower node joined directly by an edge. A node may have any number of children.
  • Leaf — a node with no children; a node with at least one child is an internal node.
  • Level — the number of edges from the node up to the root: the root is on level 0, its children on level 1, and so on. The largest level is the height of the tree. (Some books count the root as level 1 — then every number is one larger; read the task carefully.)
Level0123ABCDEFGHKLMNrootinternal nodeleaf
Node C is a leaf even though it is on level 1: a leaf is not the lowest node but a node without children.
Reading a tree

For the tree in the picture find: 1) the number of nodes, leaves and internal nodes; 2) the nodes on level 2 and the height of the tree; 3) the parent of G and all descendants of B (children, their children …).

Show solution
1) Nodes: A, B, C, D, E, F, G, H, K, L, M, N — 12. Leaves: C, F, H, K, L, M, N — 7. Internal nodes: 12 − 7 = 5 (A, B, D, E, G).
2) Level 2: E, F, G. The lowest level is 3 → the height is 3.
3) The parent of G is D. Descendants of B: E, F, H, K — 4 nodes.
m = n − 1
where:
  • nthe number of nodes of the tree
  • mthe number of edges (branches)

Every node except the root is joined to its parent by exactly one edge, so there is one edge fewer than nodes.

Edges and nodes

1) How many edges does the tree in the picture have?
2) A tree has 20 edges. How many nodes does it have?
3) Can a connected graph with 7 vertices and 7 edges be a tree?
4) A tree has 15 nodes, 9 of them leaves. How many children do the non-leaf nodes have in total?

Show solution
1) m = 12 − 1 = 11 (count them in the picture to check).
2) n = m + 1 = 21.
3) No: a tree with 7 nodes has 6 edges. The extra edge always makes a cycle.
4) Every child has exactly one parent and the root has none → the total number of children is 15 − 1 = 14 (as many as edges). The number of leaves is not needed here.

Family trees and classification trees

A family tree shows the descendants of one person: the root is the ancestor and every level is a generation (children, grandchildren, great-grandchildren). Careful: if both parents of every child are drawn, the picture is no longer a tree, because a node would have two parents. You get a tree when it shows the descendants of one person.

TahirKamalSamirLeylaElvinAyselMuradNigarRaufFidanTuralZaurSevda
Every level is a generation: level 1 — children, level 2 — grandchildren, level 3 — great-grandchildren.
Questions on the family tree

Using the family tree, answer: 1) How many grandchildren does Tahir have? 2) How many great-grandchildren? 3) How many people have no children? 4) Who is Zaur’s grandfather? 5) How many edges does the tree have?

Show solution
1) Grandchildren are on level 2: Elvin, Aysel, Murad, Nigar, Rauf, Fidan — 6.
2) Great-grandchildren are on level 3: Tural, Zaur, Sevda — 3.
3) Leaves: Aysel, Nigar, Rauf, Fidan, Tural, Zaur, Sevda — 7.
4) Zaur’s parent is Murad, and Murad’s parent is Samir → the grandfather is Samir.
5) Nodes: 1 + 3 + 6 + 3 = 13 → edges: 13 − 1 = 12.

A classification tree divides a notion into kinds: the general notion is at the root and more specific kinds go lower down. A tree can also be written without drawing — as a nested list, where every child is written one step further in than its parent. The contents page of a book (1, 1.1, 1.2, 2 …) is such a list. Below, the classification of software is given as a nested list.

Text
Software
├── System software
│   ├── Operating systems
│   ├── Utilities
│   └── Drivers
├── Application software
│   ├── Text editors
│   ├── Spreadsheets
│   ├── Graphics editors
│   ├── Desktop publishing systems
│   └── Database management systems
└── Programming tools
The classification of software: 12 nodes, 11 edges, 9 leaves. The branch “Programming tools” is not divided here, so it is a leaf.

The file-system tree and the full name of a file

Files on a disk are arranged as a tree too. The root is the disk’s root folder (C:\), internal nodes are folders, and leaves are files and empty folders. The path from the root to any file is unique, because every node of a tree has one parent. This path is the path of the file, and the path together with the file name is the full name of the file. In Windows folders are separated by a backslash \. For working with files and folders see the lesson “Software, operating systems and files”.

C:\LessonsInformaticsmodel.docxtree.pyMathstest.xlsxPictures2025sea.jpg2026← root folder of the disk← a file is always a leaf← an empty folder is a leaf
The full name of tree.py: C:\Lessons\Informatics\tree.py
disk:\folder₁\folder₂\…\name.extension
where:
  • disk:\the root folder of the disk, e.g. C:\
  • folder₁ … folderₖthe folders on the way from the root to the file, from top to bottom
  • name.extensionthe name and extension of the file

The full name of a file: the unique path from the root to the file.

Full names and moving between folders

Using the tree in the picture: 1) Write the full name of test.xlsx. 2) A user was in the folder C:\Lessons\Informatics, went up one level and then opened the folder Maths. Which folder is the user in now? 3) Apart from the root folder, how many folders and how many files are there? How many leaves and edges does the tree have? 4) On which level is sea.jpg?

Show solution
1) C:\Lessons\Maths\test.xlsx
2) One level up → C:\Lessons; then Maths → C:\Lessons\Maths.
3) Folders: Lessons, Informatics, Maths, Pictures, 2025, 2026 — 6; files: 4. Leaves: the 4 files and the empty folder 2026 — 5. Nodes: 1 + 6 + 4 = 11 → edges: 10.
4) C:\ — 0, Pictures — 1, 2025 — 2, sea.jpg — level 3.
How does the full name change when a folder is moved?

The full name of a file was C:\School\9A\Physics\test.docx. A student moved the folder Physics with all its contents into the folder School. What is the new full name of the file?
A) C:\School\9A\test.docx B) C:\Physics\test.docx C) C:\School\Physics\test.docx D) C:\School\Physics\9A\test.docx E) C:\9A\Physics\test.docx

Show solution
In the tree, the branch Physics is cut off from 9A and “grafted” onto the folder School: its parent is now School. Everything inside the branch (test.docx too) moves with it.
Path: C:\ → School → Physics → test.docx.
Answer: C.

A tree is a graph without cycles

In the language of graphs, a tree is a connected graph without cycles: any two vertices are joined by a path, but there is no closed loop (cycle). Three useful facts follow: the path between any two nodes is unique; removing any edge splits the tree into two parts; adding any new edge creates a cycle. More about graphs in the next lesson, “Graph information models: adjacency matrices and counting paths”.

A tree can also be written with brackets: after a node, its children are written in brackets, separated by commas. The tree in the picture is written as A(B(E(H, K), F), C, D(G(L, M, N))). The letters without brackets after them are the leaves.

Paths, distances and bracket notation

1) In the tree in the picture, how many edges are on the paths from H to M and from F to K?
2) How many different paths lead from the root to the leaves?
3) A tree is given as K(A(X, Y), B, C(Z)). How many nodes and leaves does it have, what is its height, and which nodes are the children of the root?

Show solution
1) Climb from both nodes up to their common ancestor. H → E → B → A → D → G → M: 6 edges. F → B → E → K: 3 edges (common ancestor B).
2) Exactly one path leads from the root to each leaf → the number of paths equals the number of leaves: 7.
3) Nodes: K, A, X, Y, B, C, Z — 7; leaves X, Y, B, Z — 4; the children of the root are A, B, C; height 2 (X, Y, Z are on level 2); edges 7 − 1 = 6.
n = 1 + k + k² + … + kʰ, yarpaqlar = kʰ
where:
  • kthe number of children of every internal node (the same for all)
  • hthe height of the tree; all leaves are on level h
  • nthe total number of nodes

A full tree: on every level the number of nodes grows k times. For k = 2, n = 2ʰ⁺¹ − 1.

Full trees: news and a tournament

1) How many leaves and nodes does a full binary tree (k = 2) of height 3 have?
2) Aysel told some news to 3 friends, each of them told 3 new people, and they each told 3 more. After 3 rounds, how many people know the news (Aysel included)?
3) A knockout tournament has 16 teams: the loser of every game drops out. How many games will be played?

Show solution
1) Leaves 2³ = 8, nodes 1 + 2 + 4 + 8 = 15 = 2⁴ − 1.
2) By levels: 1 + 3 + 9 + 27 = 40 people.
3) The games are the internal nodes of a tree whose leaves are the teams: 8 + 4 + 2 + 1 = 15. Shortcut: every game knocks out one team, and 16 − 1 = 15 teams must drop out before one winner is left.
Python
tree = {
    "A": ["B", "C", "D"],
    "B": ["E", "F"],
    "D": ["G"],
    "E": ["H", "K"],
    "G": ["L", "M", "N"],
}

def show(node, level):
    print("    " * level + node)
    for child in tree.get(node, []):
        show(child, level + 1)

show("A", 0)
nodes = {"A"} | {c for kids in tree.values() for c in kids}
leaves = [n for n in sorted(nodes) if n not in tree]
edges = sum(len(kids) for kids in tree.values())
print("nodes:", len(nodes), "edges:", edges)
print("leaves:", leaves)
▸ Expected output
A
    B
        E
            H
            K
        F
    C
    D
        G
            L
            M
            N
nodes: 12 edges: 11
leaves: ['C', 'F', 'H', 'K', 'L', 'M', 'N']
A computer model of the tree: the dictionary lists the children of every node. The function show prints the tree as a nested list, 4 spaces further in on each level. Nodes without children (no key in the dictionary) are the leaves.
Interactive
Loading simulation…
A graph has cycles, but a tree can be “cut out” of it. BFS (breadth-first search) starts at A and opens the vertices level by level. At the end the chosen edges form a tree with 9 vertices and 8 edges: root A; level 1 B, C; level 2 D, I, H; level 3 E, F; level 4 G. The other 13 − 8 = 5 edges of the graph are not in the tree — each of them would create a cycle.

Key points

  • A tree is a graphic model of a hierarchy: one root, and every other node has one parent.
  • A leaf is a node without children; the level is the number of edges up to the root.
  • A tree with n nodes has n − 1 edges; a tree is a connected graph without cycles.
  • The path between two nodes is unique; there are as many root-to-leaf paths as leaves.
  • The full name of a file is the path from the root folder to the file: C:\folder\…\name.extension.
  • A tree can also be given as a nested list or in bracket notation (A(B, C)).

Check yourself

12 questions. Every correct answer earns XP.

1 / 12
What is a node without children called?