TutorChase logo
Login
AQA A-Level Computer Science

11.5.1 Introduction to trees

Trees are a fundamental structure in computing, used to represent hierarchical data without cycles, forming the backbone of many algorithms and real-world systems.

What is a tree?

A tree is a special type of graph used in computer science and mathematics. Specifically, it is a connected, undirected graph that contains no cycles.

To break this definition down:

  • Connected means there is a path between every pair of nodes in the graph. No node is isolated.

  • Undirected means the edges between nodes do not have a direction. The relationship is mutual — if node A is connected to node B, then B is also connected to A.

  • No cycles means that there is no way to start at a node, travel along a sequence of distinct edges, and return to the starting node.

A tree is a non-linear data structure, unlike arrays or linked lists. It consists of elements (called nodes) connected by edges in a way that forms a branching structure. Trees are used extensively to model and organise information, especially when the data has a hierarchical nature.

Key structural properties

  • A tree with n nodes always has n - 1 edges.

  • Any two nodes are connected by exactly one unique path.

  • If any edge is removed from a tree, it becomes disconnected.

  • Adding one extra edge to a tree introduces a cycle, which breaks its definition.

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

In formal tree structures, especially in computer science, a tree must have exactly one root when considered as a rooted tree. This root serves as the origin point from which all nodes descend. While the general mathematical definition of a tree as an undirected, connected, acyclic graph doesn’t inherently mention a root, any such tree can have only one node that can act as the root in a hierarchical interpretation. If a structure has multiple nodes that could be considered roots without any clear top-level node, it is not a valid rooted tree. Trees are meant to model hierarchies, and a hierarchy cannot have multiple origins. In computing, especially in applications like file systems, HTML DOMs, and organisational charts, there is always one root that defines the top of the structure. If a system has multiple such top-level nodes, it would either be considered a forest (a collection of trees) or incorrectly structured.

Yes, a tree can absolutely have only one node, and this is considered a valid tree. In such cases, the single node is both the root and a leaf since it has no children. This is the smallest possible tree, and it still satisfies the formal definition: it is connected (trivially, as there are no other nodes), has zero edges (which matches the rule of n - 1 edges for n nodes), and contains no cycles. These types of trees are important in theory and practice. For example, when constructing a data structure recursively, the base case often involves a tree of one node. In parsing or search algorithms, a tree may temporarily consist of one node during initialisation. Although it might seem trivial, recognising that a one-node tree is valid is essential for understanding edge cases in tree algorithms and recursive tree functions, where such small trees often represent terminating conditions.

Yes, a node in a tree can be both a parent and a child simultaneously, and this is very common in tree structures. Every node, except the root, must have one parent. At the same time, if that node has one or more children, then it also acts as a parent. This dual role is essential in forming the hierarchical structure of trees. For example, in the following layout:

css

A
       / \
      B   C
     /
    D

Node B is a child of A and also a parent to D. This ability to serve both roles enables the branching nature of trees. Only the root node is not a child, as it has no parent, and leaf nodes are not parents, as they have no children. All internal nodes — nodes with children — are both children (of their parent) and parents (to their children), making this a central concept in understanding tree traversal and recursive operations.

There is always exactly one unique path between any two nodes in a tree because a tree is, by definition, a connected, acyclic graph. Being connected ensures that a path exists between any two nodes. Being acyclic guarantees that no node is revisited, so there are no alternative paths. If there were two distinct paths between any two nodes, this would create a cycle, since one could traverse one path to a node and return using the other, forming a loop — which violates the definition of a tree. This uniqueness is what makes trees predictable and efficient for operations like searching, pathfinding, and traversal. Algorithms that work on trees, such as depth-first search (DFS) and breadth-first search (BFS), rely on the fact that there’s a clear and singular route between any two points, which avoids unnecessary complexity and the need for cycle detection mechanisms, unlike in general graphs.

If you remove one edge from a tree, the structure becomes disconnected, meaning it is no longer a valid tree. This is because a tree with n nodes must have n - 1 edges to maintain connectivity without cycles. If you remove one edge, you end up with n - 2 edges, and the graph splits into two separate components. These components each become a smaller tree, and together they form what is called a forest — a collection of disjoint trees. This property of trees is critical in many algorithms, especially minimum spanning tree algorithms like Kruskal’s, which work by removing or avoiding cycles to create a spanning tree from a general graph. In hierarchical data models, removing an edge might mean breaking a relationship — such as a child node no longer being associated with its parent — leading to data fragmentation or inaccessibility. Hence, edge integrity is essential in maintaining a valid and useful tree.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email