00:00

Graph Vs Tree in Data Structure

Last updated:

Graphs and trees are both made of nodes connected by edges, and in fact every tree is a graph: a tree is a connected graph with no cycles. The extra restrictions give trees a clear hierarchy that graphs do not have. This page reviews both structures and then lists their differences.

What is Graph Data Structure?

A graph is a non-linear data structure made of vertices (nodes) and edges that connect pairs of vertices. It is written G = (V, E), where V is the set of vertices and E the set of edges. Graphs model networks of any shape: social connections, roads, circuits, computer networks.

For example, consider this graph:

Undirected graph with vertices A, B, C, D, E and edges A-B, A-C, B-D, B-E, C-D, D-E

It has 5 vertices and 6 edges: V = {A, B, C, D, E} and E = {(A,B), (A,C), (B,D), (B,E), (C,D), (D,E)}. It also contains cycles, for example A → B → D → C → A, which a tree could not have.

Terminology of Graph Data Structure

  • Vertex: a point in the graph that represents an object; vertices are usually drawn as labelled circles.

  • Edge: a connection between two vertices, drawn as a line (or an arrow in a directed graph).

  • Weight: a number attached to an edge, such as the distance between two cities.

  • Path: a sequence of vertices in which each consecutive pair is joined by an edge.

What is Tree Data Structure?

A tree is a connected, acyclic graph that is usually drawn as a hierarchy: one node is the root, every other node has exactly one parent, and nodes without children are leaves. Trees represent hierarchical data such as file systems, organisation charts and HTML documents, and search trees keep data ordered for fast lookups.

Tree with root A, parent and child nodes, leaf nodes, siblings, a subtree and levels 0 to 3

Terminologies in Tree Data Structure

  • Node: an element of the tree, holding a value and references to its children.

  • Edge: the link from a parent to a child; a tree with n nodes has n − 1 edges.

  • Root Node: the topmost node, the only node without a parent.

  • Parent Node: a node that has at least one child.

  • Child Node: a node directly below another node.

  • Sibling: nodes with the same parent.

  • Leaf Node: a node with no children (also called an external node). In the tree above, E, F, H, I, J and K are leaves.

  • Ancestor of a Node: every node on the path from that node up to the root.

  • Descendant: x is a descendant of y if y is an ancestor of x.

  • Level of a node: the root is at level 0, its children at level 1, and so on.

  • Internal node: a node with at least one child.

  • Neighbour of a Node: its parent and its children.

  • Subtree: a node together with all of its descendants.

What is the difference between trees and graphs?

The main differences between a graph and a tree are:

FeatureGraphTree
DefinitionA set of vertices and a set of edges connecting pairs of vertices, with no further restrictions.A connected graph with no cycles, usually drawn as a hierarchy from a chosen root.
CyclesAllowed.Not allowed: there is exactly one path between any two nodes.
ConnectivityMay be disconnected (several components).Always connected.
RootNone; no vertex is special.One node is chosen as the root (in a rooted tree).
RelationshipsAny vertex can be adjacent to any other; there is no parent or child.Every node except the root has exactly one parent; a node can have many children.
Number of edgesAnything from 0 up to V(V − 1)/2 in a simple undirected graph.Exactly n − 1 edges for n nodes.
DirectionEdges may be directed or undirected.Edges are usually understood as directed from parent to child.
TraversalBFS or DFS with a visited set, because cycles could otherwise cause infinite loops; O(V + E).Preorder, inorder, postorder or level order; no visited set needed in a rooted tree; O(n).
ExamplesSocial networks, road and flight networks, the web, dependency graphs.File systems, organisation charts, HTML/XML documents, search trees.