Skip to main content

Section 3.1 Trees and Graphs

The search methods we will be dealing with in this chapter are defined on trees and graphs, so we must first go over what trees are and how they work. Throughout this section, we will use the following tree as a guiding example for visualizing the definitions.
A directed tree diagram consisting of six empty circles connected by downward-pointing arrows.
A simple tree diagram showing a hierarchical structure. The diagram is composed of six empty, unlabeled circles. At the top is a single circle with two arrows branching left and right into two more circles. The left circle then branches further into three more circles arranged side-by-side at the bottom. All arrows connecting these circles are pointing strictly downward, indicating the direction of the hierarchy.
Figure 3.1.1. Unlabeled tree structure.

Subsection 3.1.1 Tree Definitions

Node β€” the general term for a structure which contains an item, such as a character or even another data structure.
Edge β€” the connection between two nodes. In a tree, the edge will be pointing in a downward direction.
The empty tree diagram with every circle labeled as "node" and every connecting arrow labeled as "edge".
The same hierarchical tree diagram from the previous figure, with all structural elements now labeled. Every one of the six circles in the diagram is labeled with the word "node." Every arrow connecting these circles is labeled with the word "edge." This provides a clear visual distinction between the two fundamental components of the tree: the points of data storage (nodes) and the directional connections between them (edges).
Figure 3.1.2. Tree diagram with nodes and edges explicitly labeled.
This tree has five edges and six nodes. There is no limit to the number of nodes in a tree. The only stipulation is that the tree is fully connected. This means that there cannot be disjoint portions of the tree.
Parent node β€” a node that is a direct predecessor to one or more nodes.
Child node β€” a node that is a direct successor of another node. In a general tree, the children of a node are an unordered set. There is not a fixed or defined order for generic trees. Child nodes must have exactly one parent node. However, there is no limit to the number of child nodes a parent node can have, but to be a parent node, the node must have at least one child node.
Two side-by-side tree diagrams showing how different nodes can be labeled as parent or child depending on the chosen perspective.
Two identical hierarchical tree diagrams placed side-by-side, demonstrating parent and child relationships using different reference points. In the tree on the left, the topmost node is labeled "parent," with two edges branching down to two nodes, both of which are labeled "child." In the tree on the right, the topmost node is empty. The left node, which receives an edge from the top, is labeled "parent." This node branches down into three nodes at the bottom, all of which are labeled "child." Together, these two diagrams show that a "parent" node is simply one that has direct successors, while a "child" node is a direct successor of another node.
Figure 3.1.3. Two tree diagrams illustrating the parent-child relationship from different starting points.
Root node β€” the topmost node of the tree that has no parent. To be a tree, there MUST be only one root. Also, a tree consisting of only a single node is still considered a tree and that single node would be the root.
The empty tree diagram with the single node at the very top labeled "root".
The same hierarchical tree diagram from the previous figures, with one label applied. The single node positioned at the very top of the structureβ€”which has no incoming arrowsβ€”is labeled "root." This node acts as the entry point for the entire tree. The rest of the nodes remain empty, with the root node branching down into the rest of the hierarchy, illustrating that a tree must have exactly one root node.
Figure 3.1.4. Tree diagram with root node explicitly labeled.
A rule of thumb for discerning trees is this: if you imagine holding the tree up by the root and gravity took effect, then all edges must be pointing downward.
Sibling nodes β€” nodes with the same parent.
The tree diagram with the top node labeled "Parent" and its two direct children labeled "Sibling".
The same hierarchical tree diagram, now used to demonstrate the concept of siblings. The root node is labeled "Parent." From this parent node, two edges branch downward to two separate nodes. Both of these direct child nodes are labeled "Sibling." This visual shows that siblings are nodes that share the exact same parent.
Figure 3.1.5. Tree diagram showing two nodes that share the same direct parent labeled as sibings.
Leaf β€” a node with no children. This tree has four leaves. There is no limit to how many leaves can be in a tree.
The tree diagram with the four nodes that have no outgoing arrows labeled "leaf".
The same hierarchical tree diagram, illustrating leaf nodes. The four nodes located at the very ends of their respective branchesβ€”meaning they have no outgoing arrows pointing to any other nodesβ€”are each labeled with the word "leaf." All the parent nodes remain unlabeled, demonstrating that leaves are strictly nodes at the bottom of a tree.
Figure 3.1.6. Tree diagram showing nodes with no children labeled as leaves.

Subsection 3.1.2 Real World Application of Tree Structure

Many operating systems use a tree structure to organize your directories, folders, and files. Some of you may have a folder, within afolder, within a folder, that leads to your assignments files. The files you reach from a folder would be considered the leaf nodes while the folder(s) that got you to those files would be the parent nodes. Here is a visual of how the files and folders may look as a tree.
A tree diagram with icon-based nodes illustrating a hierarchy of folders and files on a computer.
A tree diagram demonstrating a directory structure commonly used in operating systems. At the top, a root folder icon labeled "Student_Files" branches down into four separate subfolder nodes: "Math," "Physics," "Literature," and "Biology." Each of these subfolders acts as a parent node to various file nodes listed beneath them. The files consist of homework, projects and assignments. A box at the bottom of the image clarifies that blue folder icons represent directories, while white document icons represent files. All files are positioned as leaf nodes at the bottom of their branches.
Figure 3.1.7. A visual representation of a computer file system organized as a tree structure.

Subsection 3.1.3 Graphs

Graph β€” a set of nodes connected by edges.
The difference between trees and graphs is that trees are connected (every pair of nodes has a path between them), and all nodes in a tree must descend from the root in a hierarchy where each node only has one parent. This also means that trees are a subset of graphs (all trees are graphs but all graphs are not trees).
Here is a graph that cannot be a tree because one node has two parents.
A graph with two nodes labeled "Parent", that both point to the same node, making the node have two parents
The same graph stucture with specific nodes labeled to demonstrate a parent-child relationship. The upmost node is labeled "Parent", and branches down left and right to two separate nodes. The left node is also labeled "Parent," and a bolded arrow points horizontally from this left "Parent" node to the right node, which is labeled "Child." This bolded arrow highlights that the node can be a direct descendant of more than one node.
Figure 3.1.8. A graph with a node having two parents
Disconnected Graph - A graph where not all nodes are connected by edges.
This graph has two separate pieces that are not connected by an edge, making it disconnected.
A graph diagram consisting of two entirely separate, unconnected groups of nodes.
A graph diagram showing two completely disjoint structures. On the right, a single vertical pair of nodes is connected by a downward arrow. On the left, a larger five-node structure branches into a hierarchy. There is no arrow or edge connecting the two components, illustrating that the graph is disconnected.
Figure 3.1.9. A disconnected graph with two separate components.

Subsection 3.1.4 Real World Examples of Graphs

Graphs are used to model relationships in many real-world domains. Here are some common examples:

Social Networks.

Nodes: Users
Edges: Friendships, follows, or connections
Example: Facebook’s friend graph or LinkedIn’s professional network.

Transportation Networks.

Nodes: Locations (cities, stations, airports)
Edges: Roads, railways, or flight routes
Example: Airline route maps where edges may have weights like distance or cost.

Web Page Linking (Internet Graph).

Nodes: Web pages
Edges: Hyperlinks between pages
Example: Google’s PageRank algorithm models the web as a directed graph.

Supply Chain and Logistics.

Nodes: Warehouses, suppliers, distribution centers
Edges: Shipping routes or supply relationships
Example: Optimizing delivery paths for e-commerce.

Reading Questions 3.1.5 Reading Question

1.

Which of the following are true about trees?
  • siblings share the same parent node
  • correct
  • nodes can have 2 parents
  • nodes can only have one parent
  • each node has children
  • leaf nodes have no children
  • hierarchy
  • correct
  • trees must have at least 2 nodes
  • trees only need one node (the root)
  • trees must be connected
  • correct
You have attempted of activities on this page.