6Trees
What is a tree? Basic terms
The structures so far were linear: in an array, a linked list, a stack and a queue, every element had at most one before and one after. A tree splits that order into branches: an element can have more than one "after". This lesson explains what a tree is and the terms every later lesson uses, all on one example tree.
- What sets a tree apart from linear structures, and how a tree is defined
- The terms root, parent, child, sibling, leaf, edge and path
- The ideas of depth, level, height, degree and subtree
1Not linear but hierarchical
Think of the folders on your computer. A folder holds a few folders, and those hold more folders. You cannot line this up as a list, because one folder can have several subfolders. The chapters of a book, a company's org chart and the matches of a tournament look the same way. This shape is called a tree.
A tree is made of nodes and the edges that connect them. The definition rests on two rules:
- There is a single node at the top, called the root. Nothing is above the root.
- Every node except the root has exactly one parent. So a tree has no cycles: going down from a node you can never get back to it.
In computer science trees are drawn upside down: the root at the top, the branches below. The tree below is used for every term in this lesson.
2Family terms
The relations between nodes take their names from a family tree:
| Term | Meaning | In the example |
|---|---|---|
| Parent | The node directly above a node | B's parent is A, I's parent is G |
| Child | The nodes directly below a node | D's children are G and H |
| Sibling | Children of the same parent | B, C and D are siblings |
| Ancestor | The nodes on the path from the root to that node | I's ancestors are G, D and A |
| Descendant | Every node below a node | D's descendants are G, H and I |
| Leaf | A node with no children | C, E, F, H, I |
| Internal node | A node with at least one child | A, B, D, G |
The root is also an internal node, but it gets its own name because it is the only node without a parent. In a tree with one node, the root is also a leaf.
3Edges and paths
The line that joins a parent to its child is called an edge. The example has 9 nodes and 8 edges. That is no accident: every node except the root is joined to its parent by exactly one edge. So every tree with n nodes has n - 1 edges.
The sequence of nodes you pass through when you go from one node to another along edges is called a path. The length of a path is its number of edges. The path from A to I is A, D, G, I, and its length is 3. In a tree there is only one path between any two nodes.
4Depth, level and height
The depth of a node is the length of the path from the root to that node. The root has depth 0. Nodes with the same depth are on the same level. In the drawing below the depth of each level is written on the left.
The height of a node is the length of the path from that node down to its farthest leaf. Leaves have height 0. In the example G has height 1, D has height 2 and A has height 3. The height of a tree is the height of its root, which is also the depth of its deepest node: 3 here.
On this site height is counted in edges: a tree with one node has height 0 and an empty tree has height -1. Some books and teachers count height in levels; with that definition a one-node tree has height 1 and our example tree has height 4. In an exam, check which definition is used; the two always differ by 1.
Depth is counted from the top down, height from the bottom up. To find a depth you walk down from the root to the node; to find a height you walk down from the node to its farthest leaf.
5Degree
The degree of a node is its number of children. A has degree 3, B has 2, G has 1 and the leaves have 0. The degree of a tree is the largest degree among its nodes: 3 in the example. A tree where every node has at most two children is called a binary tree; most of the next lessons are about it.
6Subtrees and forests
A node together with every node below it is a tree again. It is called that node's subtree. Below, D's subtree is marked: a small tree with root D, made of G, H and I.
This idea is the basis of tree code. An operation on a tree is usually written like this: handle the root, then apply the same operation to every subtree. Since a subtree is a tree too, the same function calls itself. That is recursion, and you will meet it all through the tree lessons.
If you remove the root, you are left with several separate trees: in the example, the subtrees of B, C and D. A set of separate trees is called a forest.
7Where are trees used?
- File systems: folders and files form a tree. The root directory is the root and files are leaves.
- Web pages: a browser keeps an HTML page as a tree (the DOM). Every tag is a node.
- Fast search: the binary search tree and its balanced kinds make searching, inserting and deleting in sorted data fast. Database indexes are trees too.
- Compilers: an expression or a program is first turned into a tree. See Expression trees.
- Priority queues: a special tree called a heap gives the smallest element quickly.
8What is in this part
- Binary trees: their kinds and how node count and height relate.
- Binary tree with an array: the index formulas, insertion and the four traversals.
- Binary tree with linked nodes: the left and right pointers, the same operations in the same order.
- Binary search tree: first with an array, then search, insertion and deletion with linked nodes.
- Applications and exams: expression trees, rebuilding a tree from traversals, general trees and exam questions.
9Try it
In the example tree, what are the depth and the height of node G?
The answer is B. The path from the root to G is A, D, G: two edges, depth 2. The farthest leaf below G is I, one edge away: height 1.
Show the answer
The answer is B. The path from the root to G is A, D, G: two edges, depth 2. The farthest leaf below G is I, one edge away: height 1.
How many edges does a tree with 20 nodes have?
The answer is A. Every node except the root is joined to its parent by exactly one edge. 19 nodes are not the root, so there are 19 edges. The shape of the tree does not change that.
Show the answer
The answer is A. Every node except the root is joined to its parent by exactly one edge. 19 nodes are not the root, so there are 19 edges. The shape of the tree does not change that.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.