Tree Terminology
Table of Contents + β
When you first start reading about trees, the words get in the way. People say βrootβ, βleafβ, βparentβ, βchildβ, βheightβ, and βdegreeβ like you already know them. So the tree itself feels harder than it really is. The fix is simple. Once you learn this small set of words, every tree explanation after this starts to make sense.
So let us slow down and learn the vocabulary first. We will draw one tree. We will give every part a name. And we will keep pointing back at the same picture.
π³ One Tree We Will Keep Pointing At
Here is the tree we will use for the whole tutorial. Look at it once. Then we will name every piece.
Read it from top to bottom. A sits at the very top. B and C sit below A. Then B has D and E under it. C has F under it. And E has G under it. That is the full family. Now let us name everyone.
π± Root: the topmost node
The root is the single node at the very top of the tree. It is the one with nothing above it. In our picture that is A. Every tree has exactly one root. And you start every walk through the tree from there. Think of it like the home screen of an app. You always begin from there and move down into the rest.
πͺ Parent and Child
A parent is a node that sits directly above another node and connects down to it. The node directly below it is its child.
So in our tree:
Ais the parent ofBandC.Bis the parent ofDandE.Cis the parent ofF.Eis the parent ofG.
The other way around works too. B and C are children of A. G is a child of E. And so on. It is just like a real family. Every node except the root has one parent. The root has no parent, because nobody sits above it.
π― Siblings
Siblings are nodes that share the same parent. They sit on the same line under one node.
In our tree B and C are siblings, because both have A as their parent. D and E are siblings too, since both come from B. But D and F are not siblings. They look like they sit on the same level. But they have different parents. So the rule is about the parent. It is not about the line they sit on.
Tip
Same level does not mean siblings. Only nodes with the same parent are siblings.
π Leaf and Internal Node
A leaf is a node with no children. It is the end of a branch. Nothing grows below it.
In our tree the leaves are D, F, and G. Nothing hangs below them.
An internal node is the opposite. It has at least one child. So A, B, C, and E are internal nodes. Sometimes people call the root plus the internal nodes the βnon-leafβ nodes. Same idea, just a different name.
β Edge
An edge is the line that connects a parent to a child. It is one single link between two nodes.
Each arrow you see in our picture is one edge. A to B is an edge. B to E is an edge. E to G is an edge. Here is a small fact that helps later. A tree with n nodes always has exactly n - 1 edges. That is because every node except the root is joined to its parent by one edge.
π€οΈ Path
A path is the sequence of nodes you step through to get from one node to another. You move along edges as you go.
To go from A down to G, you step A to B to E to G. That whole chain is the path. In a tree there is only one path between any two nodes. There is never a second route. So you never get lost.
π Level
The level tells you which row a node sits in. The root is at level 0. Its children are at level 1. Their children are at level 2, and so on. Each step down adds one.
So in our tree:
| Level | Nodes on that level |
|---|---|
| 0 | A |
| 1 | B, C |
| 2 | D, E, F |
| 3 | G |
Note
Some books start counting levels from 1 instead of 0. The idea is the same. Just check which one your course uses, then stay with it.
π Depth of a Node
The depth of a node is how many edges you cross to reach it from the root. The root has depth 0, because you are already there.
Count the edges from the top:
Ahas depth 0.BandChave depth 1.D,E, andFhave depth 2.Ghas depth 3.
See how the depth matches the level here? That is normal. Depth counts the edges down from the root. Level names the row. So they line up.
π Height of a Tree
The height is the number of edges on the longest path from the root all the way down to the deepest leaf.
In our tree the longest path is A to B to E to G. That is three edges. So the height of this tree is 3. You can also talk about the height of a single node. That is the longest path from that node down to a leaf below it. For example, the height of B is 2, because B to E to G is two edges.
Caution
Depth is measured from the root going down. Height is measured from a node going down to its deepest leaf. People mix these two up all the time, so read the question slowly.
πΏ Subtree
A subtree is any node together with everything that hangs below it. Pick any node. That node plus all the nodes under it form a smaller tree of their own.
Take B. On its own, B with D, E, and G under it is a subtree. It looks and behaves like a full little tree, with B as its own root. Every node in a tree is the root of its own subtree. This idea is why so many tree problems get solved by repeating the same steps on smaller and smaller subtrees.
π’ Degree
The degree of a node is simply how many children it has.
In our tree:
Ahas degree 2 (childrenBandC).Bhas degree 2 (childrenDandE).Chas degree 1 (childF).Ehas degree 1 (childG).D,F, andGhave degree 0, since they are leaves.
The degree of the whole tree is the largest degree of any single node in it. Here the biggest is 2. So this tree has degree 2.
π All the Terms in One Place
Keep this table near you while you practice. Every term points back to the same tree we drew.
| Term | Meaning |
|---|---|
| Root | The topmost node, with no parent (A). |
| Parent | A node directly above another node. |
| Child | A node directly below its parent. |
| Sibling | Nodes that share the same parent. |
| Leaf | A node with no children (D, F, G). |
| Internal node | A node with at least one child (A, B, C, E). |
| Edge | The link between a parent and a child. |
| Path | The sequence of nodes from one node to another. |
| Level | The row a node sits in, starting at 0 for the root. |
| Depth of a node | Number of edges from the root down to that node. |
| Height of a tree | Edges on the longest path from root to the deepest leaf. |
| Subtree | A node plus everything below it. |
| Degree | The number of children a node has. |
β οΈ Common Mix-ups
A few of these terms trip up almost everyone at first. Watch out for these:
- Calling two nodes siblings just because they sit on the same level. They are only siblings if they have the same parent.
- Mixing up depth and height. Depth goes from the root down to a node. Height goes from a node down to its deepest leaf.
- Thinking degree means total connections. Here degree means the count of children only.
- Forgetting that the root has no parent, and a leaf has no children.
π§© What Youβve Learned
β The root is the single topmost node, and every node except the root has exactly one parent.
β Parent, child, and sibling describe the family links, and siblings must share the same parent.
β A leaf has no children, an internal node has at least one child, and they are joined by edges.
β Level and depth are counted from the root downward, and height is the longest path from a node down to its deepest leaf.
β A subtree is any node plus everything below it, and the degree of a node is just its number of children.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
In a tree, what makes two nodes siblings?
Why: Siblings are defined by sharing the same parent, not by sitting on the same level.
- 2
What is the depth of the root node?
Why: Depth counts edges from the root down, so the root itself has depth 0.
- 3
Which statement best describes a leaf node?
Why: A leaf is a node that has no children, so nothing grows below it.
- 4
What is the degree of a node?
Why: The degree of a node is simply how many children it has.
π Whatβs Next?
Now that the words make sense, go back and read the big picture. Then move on to the most common tree of all.