What is a tree data structure? Explain its basic terminology (root, parent, child, leaf, edge).
Updated Apr 28, 2026
Short answer
A tree is a non-linear data structure that organizes data in a hierarchical relationship using nodes connected by edges. It starts with a single top node called the root, and each node can have zero or more child nodes. Trees are used to represent relationships such as file systems, organization charts, and search structures.
Deep explanation
A tree data structure consists of nodes and edges. Unlike linear data structures such as arrays or linked lists, where elements are arranged in a sequence, a tree represents a hierarchy where one element can connect to multiple other elements.
A tree has the following characteristics:
- It has exactly one root node, which is the starting point of the tree.
- Every node except the root has exactly one parent node.
- A node can have zero or more children.
- Nodes with no children are called leaf nodes.
- Nodes are connected using edges, which represent relationships between nodes.
- A tree does not contain cycles, meaning you cannot start at a node and follow connections to return to the same node.
A simple tree structure can be visualized as:
A / \ B C / \ \ D E FIn this example:
Ais the root because it is the topmost node.BandCare children ofA.Ais the parent ofBandC.D,E, andFare leaf nodes because they do not have any children.- The connections between
A-B,A-C,B-D,B-E, andC-Fare edges.
Basic Terminology
Root
The root is the topmost node in a tree. It is the only node that does not have a parent.
Example:
Root A / \ B CA is the root node.
Parent
A parent is a node that has one or more child nodes connected below it.
Example:
A / \ B CA is the parent of B and C.
Child
A child is a node that is directly connected below another node.
Example:
A / BB is a child of A.
Leaf
A leaf is a node that has no children. It is the end point of a branch in a tree.
Example:
A / \ B CB and C are leaf nodes because they do not have any children.
Edge
An edge is the connection between two nodes in a tree. It shows the relationship between a parent and a child.
Example:
A | BThe line connecting A and B is an edge.
Why Trees Are Useful
Trees are useful when data naturally has hierarchical relationships. They allow efficient searching, insertion, deletion, and organization of information.
Common types of trees include:
- Binary Tree: Each node has at most two children.
- Binary Search Tree (BST): A binary tree where left children contain smaller values and right children contain larger values.
- Heap: A tree structure commonly used for priority queues.
- Trie: A tree used for storing and searching strings efficiently.
The performance of tree operations depends on the type of tree and how balanced it is. For example, searching in a balanced binary search tree can take O(log n) time, while searching in a badly unbalanced tree may degrade to O(n).
Real-world example
A computer's file system is a common example of a tree structure.
C: / \ Users Program Files | Alice / \ Documents PicturesIn this example:
C:is the root directory.UsersandProgram Filesare children of the root.Aliceis a child ofUsers.DocumentsandPicturesare leaf nodes.
A simplified representation in code might look like:
class TreeNode: def __init__(self, value): self.value = value self.children = []
root = TreeNode("C:")users = TreeNode("Users")programs = TreeNode("Program Files")
root.children.append(users)root.children.append(programs)
users.children.append(TreeNode("Alice"))This structure allows a program to easily navigate folders by moving from parent directories to child directories.
Common mistakes
- * Confusing a tree with a graph
- a tree is a special type of graph with no cycles and a single root.
- * Assuming every tree node must have exactly two children
- only binary trees have a maximum of two children.
- * Forgetting that the root node has no parent.
- * Calling every node without children a root
- only the topmost node is the root.
- * Assuming trees must always be displayed vertically
- the layout is only a visual representation of the relationships.
- * Ignoring the effect of tree balance on performance, especially in search trees.
Follow-up questions
- What is the difference between a tree and a graph?
- What is a binary tree?
- How is a binary search tree different from a normal binary tree?
- What is tree traversal?
- Why does tree balance matter in a binary search tree?
- Where are trees used in software systems?