Tree vs Graph
A tree is a graph with rules. It has one root and no loops, so there is one path to each node. A graph may link any node to any other, loops and all.
Tree (Data Structure)
Nodes joined in a branching shape, with one root at the top and no loops.
A tree is a data structure made of nodes joined in a branching shape. It has one node at the top, called the root. Each node can have child nodes below it, and nodes with no children are called leaves. There are no loops: you can never walk back to where you started. Folders on a computer and the HTML of a web page are both trees.
Graph (Data Structure)
Nodes joined by edges, where any node can link to any other.
A graph is a set of nodes joined by lines called edges. Unlike a tree, any node can link to any other, and the links can form loops. Edges can have a direction, like a one-way street, and a weight, like a distance. Road maps, friend networks and links between web pages are all graphs. Many hard problems become easier once you see them as one.
In one line
- Tree (Data Structure)
- Nodes joined in a branching shape, with one root at the top and no loops.
- Graph (Data Structure)
- Nodes joined by edges, where any node can link to any other.