What a tree is
After this: Find the root, parents and children, and say what makes a tree binary.
On this page · 6 sections · 3 checks
Open the folders on your computer. A folder holds files, and it can also hold other folders. Those folders hold more files. Here is a small example.
Documents├── Photos│ ├── cat.jpg│ └── beach.jpg└── Homework └── maths.pdfNow suppose your program has to store this. You could put the six names in a list.
["Documents", "Photos", "Homework", "cat.jpg", "beach.jpg", "maths.pdf"]
All the names are there, but something important is lost. Which folder is cat.jpg in? The list does not say. A list knows which item comes next. It does not know which item sits under which.
We need a shape where each item can have other items under it. That shape is called a tree. This tutorial is about trees: what they are, and how a program goes through one.
Why anyone wants a tree
Many things in computing have this shape.
- Folders. A folder holds files and other folders.
- Web pages. A page holds a header and a list, and the list holds items.
- Sums. In (2 + 3) × 4, the × joins two parts: the group (2 + 3) and the 4. The 2 and the 3 sit under the +.
- Fast searching. A well-shaped tree that keeps its values in a careful order can find one value among a million in about 20 steps. A plain list can need a million steps. The binary search trees tutorial builds this.
You do not need to remember this list. It is here to show that trees are worth learning.
The smallest tree
Programmers draw a tree with circles and lines. Here is a very small one.
Each circle is one item of the tree. An item in a tree is called a . This tree has three nodes, and they hold the values 1, 2 and 3.
The node at the top is called the . Here the root is node 1. All the other nodes sit below it.
A real tree has its root at the bottom. In computing we draw the tree the other way up, with the root at the top. It takes a little time to get used to this.
Parents and children
Look at the same tree again. Node 1 sits directly above nodes 2 and 3. We describe this with two words from family life.
- Node 1 is the of node 2 and of node 3.
- Nodes 2 and 3 are the of node 1.
A parent can have more than one child, but a child has only one parent. This is different from real families, where a person has two parents. Folders are a better guide: a file sits in one folder and never in two.
The root is the only node with no parent.
A binary tree
A folder can hold any number of things. This tutorial uses a smaller kind of tree, where each node has room for two children: one on the left and one on the right. Either one can be missing, so a node has zero, one or two children.
This kind of tree is called a . Here, "binary" means "two": two places for children. It says nothing about the values, which can be anything.
Here is a binary tree with six nodes. We use it all through this tutorial, so we call it the example tree.
It has the same shape as the folders at the top of this page. The numbers stand for the names.
| Number in the drawing | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Name in the folders | Documents | Photos | Homework | cat.jpg | beach.jpg | maths.pdf |
Read the tree from the top.
- Node 1 is the root. Its children are 2 and 3.
- Node 2 has two children, 4 and 5.
- Node 3 has one child, 6, on its right. Its left side is empty.
- Nodes 4, 5 and 6 have no children.
Why stop at two children? Two are enough to show every idea, and the code stays short because each node has only a left and a right. The same ideas work for folders too, and the last lesson shows how.
Check yourself
The first two checks use the example tree above.
Which node of the example tree is the root?
In the example tree, what is the parent of node 5?
In a tree, can one node have two parents?
What comes next
You can now read a drawing of a binary tree. The next lesson adds a few more words, shows how to measure a tree, and writes one in code. After that we learn how a program visits every node.
- Say what a tree is: items with other items under them, like files and folders on a computer.
- Find the root, and any node's parent and children, on a drawing.
- Say what makes a tree binary: every node has at most two children, one on the left and one on the right.