Computer >> Computer tutorials >  >> Programming >> Programming

Difference between Graph and Tree


In programming data type denotes the type and nature of data which is intended to be use by the user. It is the data type which compiler or interpreter going to deal with and provides corresponding storing location in main memory. Now for storing data we have introduced different data structure depending on the nature of data. As data is mainly classified as Linear and Nonlinear class so specifically for Non Linear data there is concept of Graph and Tree to represent such data for better understanding.

Now as both Graph and Tree used to represents the Nonlinear data so share some common characteristics as both comprises of Nodes and Edges but still there are some differences between both of them which are listed below.

Following are the important differences between Graph and Tree.

Sr. No.KeyGraphTree
1DefinitionGraph is the graphical representation of nonlinear data where data is denoted by nodes and the relation between them is denoted by connecting path which is known as Edge.On other hand Tree is also used to represents the nonlinear data but in context of hierarchy where data is again denoted by node and its successive data is denoted by node just below it termed as child nodes/s.
2ImplementationFor representation of nonlinear data Graph is implemented in such a way that the nodes may or may not be connected or even there may be chances of self-loops between nodes to represent the connection between data.On other hand Tree is implemented in such a manner that each node must have its parent except Parent or first node and must be connected to some other node i.e no node could exist without other node. Also there is no chance of loop or self-loop in case of Tree as data representation is in hierarchical nature.
3Data SearchingAs Graph may contain self-loop hence it is difficult to search the data on traversal approach. User has to connect the dots to reach out the desired data.On other hand in case of Tree the data is represented as nodes which are connected in hierarchical manner so traversal search could be possible for user to search the desired data at particular level of the tree.
4Parent Child relationAs Graph did not represent the data is hierarchical manner so there is no parent child relation between data representation hence no such parent node or child node do exist in case of Graph.On other hand in case of Tree data is represented in hierarchical manner so parent to child relation exists between the nodes and yes there exist parent node as well as child node in case of Tree.
5Vice e VersaIf cases of Graph we can say that all Graphs are not Trees.On other hand on case of Tree we can say that All trees are Graphs.
6UsageMain use of graphs is colouring and job scheduling.On other hand Main use of trees is for sorting and traversing.