Introduction to Trees
Table of Contents + β
So far you have seen data stored in a straight line. Arrays, linked lists, stacks, queues. One item, then the next, then the next. But not all data lives in a line. Some data is shaped like a family. One thing owns many things below it. That is where trees come in.
π³ The Problem: Some Data Is Not Flat
Think about the folders on your computer. You open Documents, and inside it there are more folders. Inside those, even more folders. And then finally some files. A folder can hold many folders. Each of those can hold many more.
Now try to store that in a plain list:
Documents, Photos, Resume.pdf, Trip2024, Beach.jpg, Mountains.jpg, Work, Report.docxSee the problem? The list shows the names. But it loses the shape. You cannot tell that Beach.jpg lives inside Trip2024, which lives inside Photos. A flat list has no idea who is inside whom. The relationship is gone.
This kind of data is hierarchical. That means items are arranged in levels. One item sits above and owns the items below it. A list cannot show levels. A tree can.
π What Is a Tree?
A tree is a data structure that stores data in nodes connected in a hierarchy. A node is just one box that holds a value. The tree starts from one top node and spreads downward into more nodes, like branches.
Here is the key idea. There is one node at the very top. Every other node sits below some other node. Nothing floats on its own. So the whole thing stays connected and organised, from the top going down.
Quick way to picture it:
- one box at the top
- that box points down to a few more boxes
- each of those can point down to even more
- it keeps branching until you reach boxes with nothing below them
A real family tree works the same way. One grandparent at the top. Their children below. Then grandchildren below that.
Note
A tree in computers is usually drawn upside down compared to a real tree. The root is at the top and the branches grow downward. Do not let that confuse you.
π The Words You Need: Root, Parent, Child, Leaf
A tree has a few simple names for its parts. Let us define each one in plain words.
- Root β the single node at the very top. It has nothing above it. Every tree has exactly one root.
- Parent β a node that has one or more nodes directly below it. The node above is the parent of the nodes below.
- Child β a node that sits directly below another node. That node below is the child. One parent can have many children.
- Leaf β a node with no children. It is the end of a branch. Nothing hangs below a leaf.
The root is where everything begins. From the root you walk down to children, then their children, until you hit the leaves at the bottom.
One small note. A node can be a parent and a child at the same time. A middle folder has a folder above it, so it is a child. And it has folders inside it, so it is a parent. Only the root is never a child. And only a leaf is never a parent.
ποΈ A Tree You Can See
Let us draw that folder example as a real tree. Read it from the top down.
Now the shape is clear. Documents is the root. Photos is a child of Documents and also a parent of Trip2024. Beach.jpg and Mountains.jpg are leaves because nothing lives inside them.
Walk through it once:
Documentsis the root. Nothing is above it.PhotosandResume.pdfare children ofDocuments.Trip2024is a child ofPhotos, and a parent of the two images.Beach.jpg,Mountains.jpg, andResume.pdfare leaves.
π Where Trees Show Up in Real Life
Trees are not just a textbook idea. You use them every day without noticing.
- File system β folders inside folders, exactly like the example above. Your phone and laptop both store files as a tree.
- Company org chart β the CEO sits at the top. Managers report to the CEO. Team members report to managers. That is a tree, and the CEO is the root.
- HTML page structure β every web page is a tree. The
htmltag is the root. Inside it sitheadandbody. Insidebodysit headings, paragraphs, images. The browser actually builds this tree to draw the page.
Notice the pattern in all of these. One thing at the top, and a clear βwho is inside whomβ going down. Whenever your data has that shape, a tree is the natural way to store it.
π» What One Node Looks Like in Code
You do not need heavy code to understand trees yet. But it helps to see what a single node is made of. A node holds a value, and it holds links to its children.
Here we make one node that stores a value and keeps a list of its children. Then we attach two children to it and print them.
# A node holds a value and a list of its children.class Node: def __init__(self, value): self.value = value self.children = []
# Attach a child under a parent node.def add_child(parent, child): parent.children.append(child)
root = Node("Documents")add_child(root, Node("Photos"))add_child(root, Node("Resume.pdf"))
print("Root:", root.value)for child in root.children: print("Child:", child.value)The output of the above code will be:
Root: DocumentsChild: PhotosChild: Resume.pdfSee how small that is? A node is just a value plus a way to reach its children. Stack many of these together and you have a tree.
β οΈ Common Mistakes Beginners Make
A few things trip people up when they first meet trees. Keep these in mind.
- Thinking a tree can have many roots. It cannot. A tree has exactly one root at the top.
- Mixing up leaf and root. The root is at the top with nothing above it. A leaf is at the bottom with nothing below it.
- Forgetting that one node can be both a parent and a child. A middle node is a child of the node above and a parent of the nodes below.
- Drawing the root at the bottom. In computer science the root goes on top and the tree grows downward.
- Connecting a child to two parents. In a tree, each node has only one parent (except the root, which has none).
Tip
When you look at any tree, find the root first. Once you know the top node, everything else is just βwhat hangs below itβ, step by step.
π§© What Youβve Learned
- β A tree stores data in a hierarchy of nodes, not in a flat line.
- β It is the natural fit for data where one item owns many items below it.
- β The root is the single top node, and every other node sits below it.
- β A parent has nodes below it, a child sits below a parent, and a leaf has no children.
- β One node can be both a parent and a child at the same time.
- β File systems, org charts, and HTML pages are all real trees.
- β A node in code is just a value plus links to its children.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the topmost node of a tree called?
Why: The root is the single node at the very top of a tree, with nothing above it.
- 2
A node that has no children is called a:
Why: A leaf is a node at the end of a branch, with no nodes hanging below it.
- 3
Why is a flat list a poor fit for folders inside folders?
Why: A flat list keeps the names but loses the hierarchy, so you cannot tell which item lives inside which.
- 4
Can a single node in a tree be both a parent and a child?
Why: A middle node has a parent above it and children below it, so it plays both roles at once.