Nodes, edges, trees and graphs
A node holds a value. An edge links two nodes. A graph is a collection of nodes and edges, and the edges may be directed (one-way) or undirected (two-way), and may carry a weight such as a distance or cost.
A tree is a special graph that is connected and has no cycles. A cycle is a path that returns to its starting node without reusing an edge. In a rooted tree, one node is the root, and every other node has exactly one parent. A node with no children is a leaf.
How trees and graphs are stored and traversed
A tree node usually stores a value and references to its children. A binary tree allows at most two children per node. A binary search tree keeps smaller values in the left subtree and larger values in the right subtree, so a search can discard about half of a balanced tree at each step.
A graph is commonly stored as an adjacency list, where each node keeps a list of its neighbours, or as an adjacency matrix, a table with one row and column per node. Lists suit graphs with few edges. Matrices give fast edge lookup but use space proportional to the square of the node count.
Traversal means visiting every reachable node. Depth-first search follows one path as far as it can before backing up, often using a stack or recursion. Breadth-first search visits nodes level by level using a queue.
Worked example: counting edges and searching a small tree
A tree with n nodes has n minus 1 edges. A tree with 7 nodes therefore has 7 minus 1 = 6 edges. A connected graph with 7 nodes and 8 edges must contain at least one cycle, because 8 is more than 6.
Take a binary search tree built by inserting 50, 30, 70, 20, 40 in that order. Root 50 has left child 30 and right child 70. Node 30 has children 20 and 40. To find 40, compare with 50 (smaller, go left), compare with 30 (larger, go right), then find 40. That takes 3 comparisons, against up to 5 in a plain list of five values.
Common misconceptions
A tree is not a different kind of object from a graph. It is a graph with extra conditions. Another frequent mistake is to assume a binary search tree is always fast. If values are inserted in sorted order, the tree becomes a chain and a search can take as many steps as there are nodes, unless the tree is kept balanced.
Breadth-first search is also described as finding shortest routes in every graph. That holds when each edge counts equally. For weighted edges, algorithms such as Dijkstra's algorithm are used, and the usual version requires non-negative weights.
Where trees and graphs show up
Trees describe file system folders, the structure of HTML documents, and the parse of an expression in a compiler. Database indexes commonly use balanced tree variants such as B-trees.
Graphs model road maps, computer networks, social connections and task dependencies. Route planning, link analysis and build ordering all reduce to graph problems.
Key terms
- Node
- An element of a tree or graph that holds a value.
- Edge
- A link between two nodes, which may be directed or weighted.
- Cycle
- A path that leads back to its starting node.
- Root
- The top node of a rooted tree, which has no parent.
- Binary search tree
- A binary tree where left descendants are smaller and right descendants are larger than a node.
- Adjacency list
- A representation where each node stores a list of its neighbours.
- Breadth-first search
- A traversal that visits nodes level by level using a queue.
