Ch 7. Trees
7.1 Search, Insertion, Deletion, and Sorting
In discrete mathematics and computer science, a tree is a fundamental data structure for representing hierarchical relationships. We refer to a tree as a connected, acyclic graph. This means it is a set of nodes (also called vertices) connected by edges. There are no cycles (i.e., no paths that start and end at the same node). It is connected, meaning there is a path between any two nodes. Formally, a tree with [latex]n[/latex] nodes has exactly [latex]n - 1[/latex] edges.
A rooted tree is a tree in which one node is designated as the root. From this root, every other node is reachable by a unique path. This structure introduces a natural hierarchy:
- Root: The topmost node.
- Parent: A node that has children.
- Child: A node that descends from a parent.
- Leaf: A node with no children.
- Subtree: A tree formed by a node and all its descendants.
Rooted trees are directional, meaning that their edges implicitly point away from the root toward the leaves.
Example 7.1
Consider the following tree structure:

The node [latex]A[/latex] is the root. Nodes [latex]B[/latex] and [latex]C[/latex] are children of node [latex]A[/latex]. Nodes [latex]D[/latex] and [latex]E[/latex] are children of node [latex]B[/latex]. Node [latex]F[/latex] is a child of node [latex]C[/latex]. Nodes [latex]D[/latex], [latex]E[/latex], and [latex]F[/latex] are leaf nodes. This tree has [latex]6[/latex] nodes, [latex]5[/latex] edges, and a clear hierarchical structure.
The level of a vertex (or node) in a rooted tree is defined as the number of edges from the root to that vertex. The root is at level 0. Its immediate children are at level 1. Their children are at level 2, and so on. This concept helps describe the depth of nodes in a hierarchical structure.
The height of a tree is the maximum level of any vertex in the tree. It represents the longest path from the root to a leaf. A tree with only the root has a height of 0. A tree where the deepest leaf is at level 3 has height 3. Height is a key measure of a tree’s efficiency, especially in search operations, where time complexity often depends on tree height.
Example 7.2
Consider the following rooted tree:

The level of [latex]A[/latex] is [latex]0[/latex], the level of [latex]B[/latex] and [latex]C[/latex] is [latex]1[/latex], the level of [latex]D[/latex], [latex]E[/latex], and [latex]F[/latex] is [latex]2[/latex], the level of [latex]G[/latex] is [latex]3[/latex], and the height of the tree is [latex]3[/latex] (maximum level).
A hierarchical tree is a rooted tree that represents relationships in a top-down structure, where each node may have one parent and zero or more children.
Example 7.3
Consider a company structure:

The CEO is the root. The roles of Manager 1 and Manager 2 are at level 1. The roles of Dev 1, Dev 2, and Analyst are at level 2. This tree has a height of 2 and clearly shows reporting relationships.
In a rooted tree, nodes are connected hierarchically. Understanding the relationships between nodes is essential for analyzing and manipulating tree structures. The following definitions are used throughout this chapter:
- A parent is a node that has one or more children.
- A child is a node that descends directly from another node.
- Ancestors are all nodes along the path from a given node to the root.
- Descendants are all nodes that descend from a given node.
- Siblings are nodes that share the same parent.
- A terminal vertex (leaf) is a node with no children.
- An internal vertex (branch) is a node that has at least one child.
- A subtree of [latex]T[/latex] rooted at [latex]x[/latex] is a tree consisting of node [latex]x[/latex] and all its descendants.
Example 7.4
Consider the tree in Example 7.2:
- The node [latex]B[/latex] is the parent of nodes [latex]D[/latex] and [latex]E[/latex].
- Nodes [latex]D[/latex] and [latex]E[/latex] are the children of node [latex]B[/latex].
- The ancestors of node [latex]G[/latex] are nodes [latex]E[/latex], [latex]B[/latex], and [latex]A[/latex].
- The descendants of node [latex]B[/latex] are nodes [latex]D[/latex], [latex]E[/latex], and [latex]G[/latex].
- Nodes [latex]D[/latex] and [latex]E[/latex] are siblings.
- Nodes [latex]D[/latex], [latex]F[/latex], and [latex]G[/latex] are leaf nodes.
- Nodes [latex]B[/latex] and [latex]E[/latex] are internal vertices.
- The subtree rooted at node [latex]B[/latex] includes nodes [latex]B[/latex], [latex]D[/latex], [latex]E[/latex], and [latex]G[/latex].

A graph that contains no cycles is called acyclic. This means no path starts and ends at the same vertex while traversing each edge exactly once. In the context of undirected graphs (see Section 9.2), this means the graph does not loop back on itself. A tree is a special kind of acyclic graph that is also connected, meaning there is a path between every pair of vertices.
The following theorem provides several methods for determining whether a graph is a tree. These properties are foundational in graph theory and are often used in algorithm design.
Theorem 7.1: Equivalent Characterizations of Trees
Let [latex]T[/latex] be a graph with [latex]n[/latex] vertices. The following statements are equivalent characterizations of a tree:
- If [latex]T[/latex] is connected and has no cycles, then [latex]T[/latex] is a tree.
- If [latex]T[/latex] is connected and has exactly [latex]n - 1[/latex] edges, then [latex]T[/latex] is a tree.
- If [latex]T[/latex] is acyclic and has exactly [latex]n - 1[/latex] edges, then [latex]T[/latex] is connected, and therefore a tree.
Proof
- By definition, a tree is a connected, acyclic graph. Thus, “connected and has no cycles” implies a tree.
[latex]\square[/latex]
- If [latex]T[/latex] is connected and has [latex]n - 1[/latex] edges, and if there were a cycle, removing one edge from the cycle would keep the graph connected but reduce the edge count below [latex]n - 1[/latex], a contradiction. Then [latex]T[/latex] must be acyclic. Hence [latex]T[/latex] is connected and acyclic, i.e., a tree.
[latex]\square[/latex]
- If [latex]T[/latex] is acyclic and has [latex]n - 1[/latex] edges but is not connected, then it breaks into at least [latex]r \geq 2[/latex] connected pieces. Each piece is acyclic, and a piece with [latex]k[/latex] vertices has at most [latex]k - 1[/latex] edges. Summing over pieces with vertex counts [latex]n_{1}, \dots, n_{r}[/latex]:
[latex]\text{edges} \leq (n_{1} - 1) + \cdots + (n_{r} - 1) = (n_{1} + \cdots + n_{r}) - r = n - r < n - 1[/latex]
a contradiction. Therefore, [latex]T[/latex] is connected, and hence a tree.
[latex]\square[/latex]
A binary tree is a type of rooted tree in which each node has at most two children. These children are typically referred to as the left and right children. Binary trees are widely used in IT for searching and sorting.
The left child is the child node that appears on the left side of a parent node. The right child is the child node that appears on the right side of a parent node. Each node in a binary tree may have no children (a leaf), one child (left or right), or two children (left and right)
Example 7.5

The node [latex]A[/latex] is the root, node [latex]B[/latex] is the left child of node [latex]A[/latex], node [latex]C[/latex] is the right child of node [latex]A[/latex], node [latex]D[/latex] is the left child of node [latex]B[/latex], node [latex]E[/latex] is the right child of node [latex]C[/latex], and nodes [latex]D[/latex] and [latex]E[/latex] are leaf nodes. This binary tree has 5 nodes, a height of 2, and a clear left-right structure.
A full binary tree is a binary tree in which every internal vertex has exactly two children. This means each node is either a leaf (no children) or an internal node with two children. No node has only one child. Full binary trees are important in both theoretical and applied computer science.
The following theorem provides a simple way to determine the size and shape of a full binary tree given the number of internal nodes.
Theorem 7.2: Structure of a Full Binary Tree
Let [latex]T[/latex] be a full binary tree with [latex]i[/latex] internal vertices. Then
- [latex]T[/latex] has exactly [latex]i + 1[/latex] terminal vertices.
- [latex]T[/latex] has exactly [latex]2i + 1[/latex] total vertices.
Proof
a. Let [latex]t[/latex] be the number of terminal vertices and [latex]n[/latex] be the total number of vertices. Each internal vertex contributes [latex]2[/latex] edges, so the total number of edges is [latex]2i[/latex]. But in any tree with [latex]n[/latex] vertices, the number of edges is [latex]n - 1[/latex]. So, we equate
[latex]2i = n - 1 \Rightarrow n = 2i + 1[/latex].
Now, since the total number of vertices is the sum of internal and terminal vertices
[latex]n = i + t[/latex].
Substituting [latex]n = 2i + 1[/latex] gives
[latex]2i + 1 = i + t \Rightarrow t = i + 1 \quad[/latex]
[latex]\square[/latex]
b. From the above derivation
[latex]n = i + t = i + (i + 1) = 2i + 1 \quad[/latex]
[latex]\square[/latex]
Example 7.6
Consider the full binary tree:

The internal vertices are [latex]A[/latex], [latex]B[/latex], and [latex]C[/latex] and [latex]i = 3[/latex]. The terminal vertices are [latex]D[/latex], [latex]E[/latex], [latex]F[/latex], and [latex]G[/latex] and [latex]i + 1 = 4[/latex]. The total vertices are [latex]A[/latex], [latex]B[/latex], [latex]C[/latex], [latex]D[/latex], [latex]E[/latex], [latex]F[/latex], and [latex]G[/latex] and [latex]2i + 1 = 7[/latex]. This tree satisfies the conditions of Theorem 7.2.
A binary tree in which all internal nodes have two children and all leaves are at the same level is called a perfect binary tree.
The next theorem is an inequality that expresses a fundamental limit: the number of leaves in a binary tree is bounded by the height. In other words, to accommodate more leaves, the tree must be taller. This theorem is useful in analyzing the efficiency of tree-based algorithms, especially in search and decision trees.
Theorem 7.3: Height Bound in Binary Trees
Let [latex]T[/latex] be a binary tree of height [latex]h[/latex] with [latex]t[/latex] terminal vertices. Then
[latex]\text{log}_{2}t \leq h[/latex]
Proof
In a perfect binary tree of height [latex]h[/latex], the number of leaves is exactly [latex]2^{h}[/latex]. This is the maximum possible number of leaves for any binary tree of height [latex]h[/latex].
Let [latex]T[/latex] be any binary tree of height [latex]h[/latex] with [latex]t[/latex] leaves. Since [latex]T[/latex] may not be perfect, it must have fewer than or equal to [latex]2^{h}[/latex] leaves, that is,
[latex]t \leq 2^{h}[/latex]
Take base 2 logarithms of both sides to obtain
[latex]\text{log}_{2}t \leq \text{log}_{2}(2^{h}) = h[/latex]
Thus,
[latex]\text{log}_{2}t \leq h[/latex].
[latex]\square[/latex]
Example 7.7
Suppose a binary tree has [latex]8[/latex] terminal vertices. Then [latex]\text{log}_{2} 8 = 3[/latex]. So, the tree must be at least [latex]3[/latex] units tall. If the tree has height exactly [latex]3[/latex], it must be a perfect binary tree with [latex]2^{3} = 8[/latex] leaves and [latex]2^{4} - 1 = 15[/latex] total nodes. If the tree has a height greater than [latex]3[/latex], it may be unbalanced or incomplete.
A binary search tree (BST) is a type of binary tree in which each node contains a key (or value), and the tree satisfies the following ordering property:
- The left child of a node contains a key less than the node’s key.
- The right child of a node contains a key greater than the node’s key.
- This property holds recursively for all nodes in the tree.
BSTs are widely used in searching, sorting, and maintaining ordered data structures. They allow efficient operations such as search, insertion, and deletion.
Example 7.8
Consider inserting the following values into a BST in this order: 50, 30, 70, 20, 40, 60, 80. The resulting BST is

Note the following:
- 30 is the left child of 50 because 30 < 50.
- 70 is the right child of 50 because 70 > 50.
- 20 and 40 are children of 30, maintaining the BST property.
- 60 and 80 are children of 70, also maintaining the BST property.
This tree is balanced, and searching for any value (e.g., 60) requires at most 3 comparisons.
To search for a value in a BST, follow these steps:
- Start at the root node.
- Compare the target value with the current node’s key.
- If the target equals the current key, the search is successful.
- If the target is less than the current key, move to the left child.
- If the target is greater than the current key, move to the right child.
- Repeat the comparison at each node until the value is found or a leaf is reached (i.e., the search fails).
This process leverages the BST’s ordering property and is efficient, especially in balanced trees.
Example 7.9
Given the BST from Example 7.8, let’s search for the value 60.
Comparison 1: Start at 50. Since 60 > 50, move to the right child (70).
Comparison 2: At 70. Since 60 < 70, move to the left child (60).
Comparison 3: At 60. Since 60 = 60, value found.
The search completes in 3 comparisons.
To insert a new value into a BST, follow these steps:
- Start at the root node.
- Compare the value to be inserted with the current node’s key.
- If the value is less than the current key, move to the left child.
- If the value is greater than the current key, move to the right child.
- Repeat the comparison until you reach a null (empty) child position.
- Insert the new value at that position as a new leaf node.
This process maintains the BST property: left children are smaller, right children are larger.
Example 7.10
Start with the BST from Example 7.8. Let’s insert the value 65.
Comparison 1: Start at 50. Since 65 > 50, move to the right child (70).
Comparison 2: At 70. Since 65 < 70, move to the left child (60).
Comparison 3: At 60. Since 65 > 60, move to the right child (currently empty)
Insert 65 as the right child of 60.
Updated tree:

Now the BST still satisfies its ordering property, and the new value has been successfully inserted.
Deleting a node from a BST must preserve the binary search tree property. There are three cases to consider:
Case 1: Node to be deleted is a leaf (no children)
Remove the node.
Case 2: Node has one child
Remove the node and connect its parent directly to its child.
Case 3: Node has two children
Replace the node with either:
-
-
- Its in-order predecessor (maximum value in the left subtree), or
- Its in-order successor (minimum value in the right subtree)
-
Then delete the predecessor/successor node (which will now fall under Case 1 or 2).
Example 7.11
Start with the BST from Example 7.8 and delete node 70.
Case 3: 70 has two children: 60 and 80.
- Find the in-order successor of 70, which is 80 (the smallest in the right subtree).
Replace 70 with 80.
Case 1: Delete the original node 80 (a leaf).
Updated tree:

The BST property is preserved, since all nodes in the left subtree of 50 are less than 50 and all nodes in the right subtree of 50 are greater than 50.
A binary search tree can be used to sort a list of values by traversing it in order. In-order traversal visits nodes in the following order:
- Traverse the left subtree
- Visit the current node
- Traverse the right subtree
When applied to a BST, this traversal yields the values in ascending order.
The steps to sort using a BST are as follows:
- Insert all values into a binary search tree.
- Perform in-order traversal to retrieve the values in sorted order.
This method is known as tree sort.
Example 7.12
Suppose we want to sort the list: [40, 20, 50, 10, 30].
Step 1: Insert into BST

Step 2: In-Order Traversal
Traverse left subtree of 40: [10, 20, 30]
Then visit 40: [10, 20, 30, 40]
Finally, traverse right subtree :[10, 20, 30, 40, 50]
The sorted output is [10, 20, 30, 40, 50].
While we have demonstrated in-order traversal, which retrieves values from a binary search tree in sorted order, two other common traversal methods are pre-order traversal and post-order traversal. Pre-order traversal visits the nodes in the order: node, left subtree, then right subtree, and is commonly used when copying a tree. Post-order traversal visits the nodes in the order left subtree, right subtree, and then the node and is typically used when deleting trees.
Real-World Example 7.1: File System Hierarchy
One of the most common applications of tree structures is in computer file systems. Most operating systems organize files and directories using a rooted tree structure.
The root of the tree is the main directory (e.g., C:\ in Windows or / in Unix/Linux). Each internal vertex represents a folder (directory). Each leaf represents a file. Parent-child relationships represent containment: a folder contains subfolders and files.
An example of a tree is shown below:

Note the following:
/is the root directory.- home and
etcare children of/. simonis a subdirectory underhome.docsandphotosare subdirectories undersimon.img1.jpgis a file (leaf) underphotos.