The parts of a tree
After this: Find leaves and subtrees, count depth and height, and build a tree in code.
On this page · 9 sections · 3 checks
In What a tree is you met nodes, the root, parents and children. This lesson adds the words for the rest of a tree, and then writes a tree in code. We use the same six-node tree all the way through.
Edges and leaves
The lines in the drawing have a name. A line from a parent down to a child is called an . This tree has five edges, one above each node except the root.
Some nodes have nothing below them. In this tree they are nodes 4, 5 and 6. A node with no children is called a . The name fits the picture: leaves are where the branches of a tree end.
Being a leaf is only about having no children. Node 6 is a leaf, and so are 4 and 5, even though they are on the other side of the tree.
Left and right are different places. Node 3 has one child, and it is on the right. If 6 were on the left of 3, that would be a different binary tree, even with the same numbers.
Subtrees
Pick any node, and take that node together with everything below it. What you are holding is a smaller tree. It is called a .
Try it with node 2.
- Start with node 2.
- Add its children, 4 and 5.
- Nodes 4 and 5 have no children, so we stop.
The result is a tree of three nodes, with node 2 as its root. We call it "the subtree at 2". In the folder picture, it is the Photos folder with everything inside it.
Here are some more.
- The subtree at 3 has two nodes: 3 and 6.
- The subtree at 6 has one node: 6 alone.
- The subtree at 1 is the whole tree.
This is the most useful word in the lesson. Below any node, the left side is a tree and the right side is a tree. Every recursive walk in this tutorial works on a tree by doing the same work on its smaller subtrees.
Depth: how far down is a node?
The of a node is the number of edges between the root and that node. Here is how to find the depth of node 6.
- Start at the root, node 1. No edges so far.
- Go down to node 3. That is one edge.
- Go down to node 6. That is two edges.
So node 6 has depth 2. Notice that we count edges, not nodes. Three nodes lie on that path, but only two edges. Here is every node of the tree.
| Node | Path from the root | Depth |
|---|---|---|
| 1 | 1 | 0 |
| 2 | 1, 2 | 1 |
| 3 | 1, 3 | 1 |
| 4 | 1, 2, 4 | 2 |
| 5 | 1, 2, 5 | 2 |
| 6 | 1, 3, 6 | 2 |
The root has depth 0, because there are no edges between the root and itself.
Height: how tall is the tree?
The of a tree is the greatest depth of any node. Look down the last column of the table. The biggest number is 2, so the height of this tree is 2.
Here are two more small cases. The tree with nodes 1, 2 and 3 from the last lesson has height 1. A tree with a single node has height 0, because it has no edges at all.
A node in code
Now that you have the words, here is the code. A node needs three things: its value, its left child and its right child.
class Node: def __init__(self, value, left=None, right=None): self.value = value self.left = left # another Node, or None self.right = rightpublic class Node { int value; Node left, right; // another Node, or null
Node(int value) { this.value = value; }
Node(int value, Node left, Node right) { this.value = value; this.left = left; this.right = right; }}A class is a template for making objects, and each object gets its own copy of the fields. Here is how to read this one.
valueis the value the node holds.leftandrightare the links to the children. These links are the edges of the drawing. Each one holds anotherNode.- If there is no child on that side, the link holds Nonenull.
There is no "tree" class. A tree is only nodes joined by their links. Your program keeps one variable that holds the root, and it can reach every other node from there.
Building the example tree
Here is the example tree, built by hand one node at a time.
root = Node(1)root.left = Node(2)root.right = Node(3)root.left.left = Node(4)root.left.right = Node(5)root.right.right = Node(6) # 3 has no left childprint(root.value, root.left.value, root.right.right.value)print(root.right.left) # the missing child1 2 6 None
Node root = new Node(1);root.left = new Node(2);root.right = new Node(3);root.left.left = new Node(4);root.left.right = new Node(5);root.right.right = new Node(6); // 3 has no left childSystem.out.println(root.value + " " + root.left.value + " " + root.right.right.value);System.out.println(root.right.left); // the missing child1 2 6 null
Here is how to read it.
- The first line creates the root.
root.leftandroot.rightare the two children of the root, so the next two lines create nodes 2 and 3.root.left.leftmeans "go to the root, go to its left child, and then use that node's left link". This line creates node 4 under node 2.- Node 5 and node 6 are added in the same way.
- The last line prints
root.right.left, the left child of node 3. That child does not exist, so the line prints Nonenull.
A missing child is not an error. It is how every path down the tree ends. Every walk in the next lessons starts by checking for it.
Watch it happen
Count the nodes, then measure the height. Look at the final number in each run. The steps before it use a method that a later lesson teaches, so you can skip past them for now.
- Count the nodesCount the nodes. Go to the last step of the run, which shows the answer: 6 nodes.
- Measure the height
Run an operation to begin.
Check yourself
In the example tree, which nodes are leaves?
What makes a tree a binary tree?
What is the depth of node 6, and what is the height of the whole tree?
Connect
- Find the edges, the leaves and the subtree at any node on a drawing.
- Count depth and height in edges, so that the example tree has height 2 and a single node has height 0.
- Write a node as a value, a left child and a right child, where a missing child is the empty marker (None in Python, null in Java).