algoritmauzmanı
Sign in
TREN
6Trees
Lesson 6.0

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 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 you will learn
  • 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
7 min

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 .

A tree is made of and the edges that connect them. The definition rests on two rules:

  • There is a single node at the top, called the . Nothing is above the root.
  • Every node except the root has exactly one . 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.

ABCDEFGHI
A tree with nine nodes. A is the root (orange). The green nodes are leaves: C, E, F, H and I have no children.

2Family terms

The relations between nodes take their names from a family tree:

TermMeaningIn the example
ParentThe node directly above a nodeB's parent is A, I's parent is G
ChildThe nodes directly below a nodeD's children are G and H
SiblingChildren of the same parentB, C and D are siblings
AncestorThe nodes on the path from the root to that nodeI's ancestors are G, D and A
DescendantEvery node below a nodeD's descendants are G, H and I
A node with no childrenC, E, F, H, I
Internal nodeA node with at least one childA, 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 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.

depth 0depth 1depth 2depth 3ABCDEFGHI
The numbers on the left are the depth of each level. Level 1 holds B, C and D, level 2 holds E, F, G and H. The deepest node is I (depth 3).

The 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 ; 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 . Below, D's subtree is marked: a small tree with root D, made of G, H and I.

ABCDEFGHI
D's subtree: D, G, H and I. It is a tree on its own; its root is D and its height is 2.

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 , 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

9Try it

In the example tree, what are the depth and the height of node G?

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?

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.