Graph and tree, both are the types of representations use to draw the relationship between two or many objects. Both are usually used to represent different algorithms in a logical structure. Both are consist of nodes or objects and edges, used to draw the relationship between nodes. Edges may be weighted, the weight of the edge is defined, or un-weighted, weight of the edge is undefined.
The difference between tree and graph is explained below.
It is used for the representation of non-linear data structure. There are different types of graphs used for various purposes, such as non-directed, directed, non-connected, connected, multi-graph, and simple graph.
A graph has a unidirectional or bidirectional path between two nodes. There is no concept of the root node in graphs and has the ability to facilitate loops and self-loops. It contains self-loops and loops, so the graph is considered difficult to manage. The traversing in graphs can be done through the techniques, such as DFS (depth-first search) and BFS (breadth-first search). There is no method to predefine the number of edges that a graph will contain, as it totally depends on the structure of the graph. The structure of the graph looks like a network model. In the graph, a node has the ability to possess any number of edges.
Some of the real-life applications of graph include graphs in chemistry (where it is used to represent bonds between atoms), defining network security, route maps of airlines, scheduling of timetable, modelling of sensor networks and circuits, and to find the shortest path in the network model.
It is also a type of representation that uses vertices and edges to define the relationship between objects, but the main difference is in the structure of the tree. In tree representation, the nodes are arranged in a sorted manner. There are different types of tree used for the representation of nodes, in which some includes threaded binary tree, binary tree, AVL tree, binary search tree, and B-tree.
A tree has an only unidirectional path between two nodes, parent to child. There is a concept of the root node in a tree, from which all the nodes are inherited, and every child has only one parent. There is no concept of loops, between nodes and also self-loops. Trees are simple to manage as compared to the graph, due to not having any loops which can make it quite difficult.
The tree can be traversed through pre-order (PLR, parent left-right), in-order (LPR, left parent right), and post-order (LRP, left-right parent) techniques. The total number of edges in trees can find out through the formula n-1, where n is the total number of nodes in that tree. The structure of the tree is in a hierarchal manner, parent-child. In general trees, there may have a parent with many children, but in the case of binary trees, there are only two children, left and right, attached to one parent.
The real-life application of trees includes various games, compression of data, storage of file, computer chess games, areas that require fast searching, indexing in databases, use in compilers, and use in routers to store routing information.