×

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

Red Black Tree vs AVL Tree: Data Structure

Difference Between Red Black Tree vs AVL Tree Red Black Tree: A red-black tree is referred as self-balancing binary search tree. In red-black, each node stores an extra bit that determines...

4 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation #include <stdio.h> #include <stdlib.h>   /* Creating a binary tree node that consists of some data along with the pointer to the left and right child.  */ struct __nod {     int record;     struct...

4 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 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.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Operations on 1D-Arrays

One Dimensional Array Operations Basic Methods The fundamental operations enabled by an array are listed below. Traverse prints each element of the array one by one.Insert a new element at the specified index.Delete...

8 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

3 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.