Skip to content
Related Articles

Related Articles

Difference between graph and tree

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 Aug, 2022
View Discussion
Improve Article
Save Article

Graph :

A graph is a collection of two sets V and E where V is a finite non-empty set of vertices and E is a finite non-empty set of edges.

  • Vertices are nothing but the nodes in the graph.
  • Two adjacent vertices are joined by edges.
  • Any graph is denoted as G = {V, E}.

For Example:


G = {{V1, V2, V3, V4, V5, V6}, {E1, E2, E3, E4, E5, E6, E7}} Tree :

A tree is a finite set of one or more nodes such that –

  1. There is a specially designated node called root.
  2. The remaining nodes are partitioned into n>=0 disjoint sets T1, T2, T3, …, Tn 
    where T1, T2, T3, …, Tn are called the subtrees of the root.

The concept of a tree is represented by following Fig.

Graph vs Tree

The basis of ComparisonGraphTree
DefinitionGraph is a non-linear data structure. Tree is a non-linear data structure.
StructureIt is a collection of vertices/nodes and edges.It is a collection of nodes and edges.
EdgesEach node can have any number of edges.If there is n nodes then there would be n-1 number of edges
Types of EdgesThey can be directed or undirectedThey are always directed
Root nodeThere is no unique node called root in graph.There is a unique node called root(parent) node in trees.
Loop FormationA cycle can be formed.There will not be any cycle.
TraversalFor graph traversal, we use Breadth-First Search (BFS), and Depth-First Search (DFS).We traverse a tree using in-order, pre-order, or post-order traversal methods.
ApplicationsFor finding shortest path in networking graph is used.For game trees, decision trees, the tree is used.
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!