×

Tree vs Graph: Data Structure

Difference Between Tree and Graph

What is Tree?

A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in hierarchical order basically a tree, shows the hierarchy among the data items. It organizes the data items into branches and a tree does not contain any cycle.

If we talk about the types of tree, so there are many types of tree like; Binary tree, Binary search tree, AVL tree, Red-black tree, B-tree, B+ tree, Splay tree etc. According to the requirement of the application, we can use appropriate tree.

Now we are discussing the properties of the tree:

  • A tree contains root node which is designated as top node of the tree.
  • A tree is divided into sub-trees which contain remaining elements of the tree.
  • A tree is expanded itself top to bottom.
  • A tree always contains a path which means the root node must be connected to all others node of the tree.
  • A tree doesn’t contain any cycle or loop.
  • A tree contains n-1 edges in it.

What is Graph?

 A graph is also a non-linear data structure. It contains a group of nodes called vertices of the graph and set of lines called edges of the graph which are used to connect the two nodes or vertices. In graph, we represent the vertices or nodes by the circle or point and for the edges, we use the arc or line segments. An edge is mathematically represented by E(v,w) where the v and w are pairs of the nodes or vertices. The graph can contain cycle or loops.

There are many categories of graph like; directed graph, un-directed graph, connected graph, non-connected graph, simple graph and multi graph etc. The graph is used in computer network, electrical circuits, social networking etc.

Now we are discussing the properties of the graph:

  • In graph, a vertex or node can be connected to more than one vertex using edges.
  • In graph, an edge could be directed or bi-directed.
  • In graph, we can provide the weighted on the edges of the graph
  • A graph can contain loops.

Difference Between Tree and Graph

parametersTREEGRAPH
PathIn tree, there is only one path between two verticesIn graph, more than one path is allowed between two vertices
Root nodeA tree contains one root called top of the tree.Graph doesn't have a root node.
LoopsIn tree, there is no cycle or loops allowedThe graph can contain cycle or loops
ImplementationA tree is easy to implement as compared to graph.The graph is more difficult to implement as compared to tree.
ComplexityA tree takes less time to perform operationsThe graph takes more time to perform operations as compared to tree.
Traversal techniquesA tree can be traversed by Pre-order, In-order, Post-order traversalsA tree can be traversed by Breadth-first search and depth-first search.
Number of edgesA tree has n-1edges (where n is the number of nodes)Not defined
Model typeA tree follows Hierarchical modelThe graph follows network model.

Related Topics

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 minutes read.

Linked List Data Structure

Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each...

3 minutes read.

Given a Binary Tree Return All Root-to-Leaf Paths

Implementation #include <bits/stdc++.h> using namespace std; // A binary tree node generally consists of data, a pointer to the left and right child, and a pointer to the right child.  class __nod { public: int record; __nod* Lft; __nod*...

9 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it...

4 minutes read.

Finding the Minimum and Maximum Value of a Binary Tree

Implementation // Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.  #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new class tree node. class...

5 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.