Graph : a diagram showing the relation between variable quantities, typically of two variables, each measured along one of a pair of axes at right angles.
Tree : a woody perennial plant, typically having a single stem or trunk growing to a considerable height and bearing lateral branches at some distance from the ground.
But how can we relate Tree in Computer Science !
Tree and graphs are datastructures having representation like trees(with root and leaf nodes)and graph (nodes and links) respectively.
Tree example :
Graph example :
A Tree is just a restricted form of a Graph.
Difference between Trees and Graphs
||Tree is special form of graph i.e.
minimally connected graph and
having only one path between any two vertices.
|In graph there can be more
than one path i.e. graph can have
uni-directional or bi-directional paths (edges)
||Tree is a special case of graph having no loops, no circuitsand no self-loops.
||Graph can have loops,
circuits as well as can have self-loops.
||In tree there is exactly one root node and
every child have only one parent.
|In graph there is no such concept of
|Parent Child relationship
||In trees, there is parent child relationship
so flow can be there with direction
top to bottom or vice versa.
|In Graph there is no such parent
||Trees are less complex then graphs as
having no cycles, no self-loops and still connected.
|Graphs are more complex in
compare to trees as it can have cycles, loops etc
|Types of Traversal
||Tree traversal is a kind of special case of
traversal of graph. Tree is traversed in Pre-Order, In-Order and Post-Order(all three in DFS or in BFS algorithm)
|Graph is traversed by
DFS: Depth First Search and in
BFS : Breadth First Search algorithm
||In trees, there are many rules / restrictions for making connections between nodes through edges.
||In graphs no such rules/ restrictions
are there for connecting the nodes
||Trees come in the category of DAG :
Directed Acyclic Graphs is a kind of directed graph that have no cycles.
|Graph can be Cyclic or Acyclic.|
||Different types of trees are :
Binary Tree , Binary Search Tree, AVL tree, Heaps.
|There are mainly two types of Graphs :
Directed and Undirected graphs.
||Tree applications : sorting and searching like Tree Traversal & Binary Search.
||Graph applications :
Coloring of maps, in OR (PERT & CPM), algorithms, Graph coloring, job scheduling, etc.
|No. of edges
||Tree always has n-1 edges.
||In Graph, no. of edges depend on the graph.|
||Tree is a hierarchical model.
||Graph is a network model.|
Free Courses on Datastructure at k.online : http://enggforum.com/online/course/index.php?categoryid=10
Difference in Coding Formats ?
There is no change in coding format but there is change in logic the way programs are written.
The thing is first understand what actually a tree is or what actually a graph is
For coding practices below are the references you should check out :