- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Tree and Graph
Both Trees and Graphs are types of non−linear data structures. They are different from each other in the context of their types of connections and loop formation. That means, a tree structure is connected such that it can never have loops, whereas a graph structure follows a network model and may have loops.
Read this article to find out more about Tress and Graphs and how they are different from each other.
What is Tree?
A Tree is a non−linear data structure that is used to represent hierarchy. It is a set of nodes that are joined together to form a hierarchy. In a tree structure, only one path is allowed between two nodes or vertices. The tree structure has an exactly one root node, where the root node is the topmost node in the structure and it does not have any parent node.
Loops are not allowed in a tree structure, therefore it has (n−1) edges, where is the number of nodes. Since a tree structure does not form any loops, it is a hierarchical type model.
There are three traversal techniques used in the tree structure which are pre−order, in−order, and post−order. The tree structure is comparatively a less complex type of non−linear data structure.
What is Graph?
A Graph is also a non−linear data structure used in software engineering. Graphs are used to represent various types of physical structures.
A graph consists of a group of nodes (or vertices) and set of edges. Each edge connect the two nodes. On the graph, the nodes are represented by a point or a circle, and the edges are represented by line segments or arcs.
In a graph structure, more than one paths are allowed between vertices. Graphs can also have loops, therefore, they do not have a root node. Graphs follow the network model.
There are two traversal techniques used in the graph that are breadth−first search and depth−first search. Another important point about graphs is that we can defined number of edges in the graph. Graph structure has relatively more complex structure.
Difference between Tree and Graph
The following table highlights the important differences between a graph and a tree data structure −
|Description||Graph is a non−linear data structure that can have more than one path between vertices.||Tree is also a non−linear data structure, but it has only one path between two vertices.|
|Loops||Graphs can have loops.||Loops are not allowed in a tree structure.|
|Root Node||Graphs do not have a root node.||Trees have exactly one root node.|
|Traversal Techniques||Graphs have two traversal techniques namely, breadth−first search and depth−first search.||Trees have three traversal techniques namely, pre−order, in−order, and post−order.|
|Number of edges||In a graph structure, the number of edges are not defined.||A tree structure has number of edges, where is the number of nodes.|
|Model Type||Graphs follow the network model.||Trees follow the hierarchical model.|
|Complexity||Graphs are relatively more complex.||Trees are less complex structure.|
|Applications||The applications of graph include finding shortest path in a networking graph.||The applications of tree structures include game trees and decision trees.|
From the above discussion, it is clear that both graphs and trees are types of non−linear data structures, but they are quite different from each other in many aspects. The most significant difference between a tree and a graph is that the formation of loop or cycle is not allowed in the case of a tree structure, whereas a graph can have loops or cycles.
- Related Articles
- Difference between Graph and Tree
- Difference Between B-tree and Binary tree
- Difference between Binary Tree and Binary Search Tree
- What is the difference between Parse Tree and the Syntax Tree?
- Difference between Tree Set and Hash Set in Java
- Tree or Connected acyclic graph
- Graph Valid Tree in C++
- Check if a given graph is tree or not
- Difference between sums of odd level and even level nodes of a Binary Tree in Java
- Difference between sums of odd position and even position nodes of a Binary Tree in Java
- Difference Between & and &&
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++
- Check if a given tree graph is linear or not in C++
- Check whether given degrees of vertices represent a Graph or Tree in Python
- Difference between Voltage Drop and Potential Difference