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 −

Parameter Graph Tree
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.

Conclusion

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.


Advertisements