Difference
Difference
For example, the posts of employees are arranged in a tree data structure
like managers, officers, clerk. In the above figure, A represents a
manager, B and C represent the officers, and other nodes represent the
clerks.
o Graph
A graph is a non-linear data structure that has a finite number of vertices
and edges, and these edges are used to connect the vertices. The vertices
are used to store the data elements, while the edges represent the
relationship between the vertices. A graph is used in various real-world
problems like telephone networks, circuit networks, social networks like
LinkedIn, Facebook. In the case of facebook, a single user can be considered
as a node, and the connection of a user with others is known as edges.
Types Arrays, linked list, stack, Trees and graphs are the types of a
queue are the types of a non-linear data structure.
linear data structure.
Levels This data structure does In this, the data elements are
not contain any arranged in multiple levels.
hierarchy, and all the
data elements are
organized in a single
level.
In the linked list, the elements are not stored in a contiguous manner. It
consists of multiple blocks, and each block is represented as a node. Each
node has two fields, i.e., one is for the data field, and another one stores the
address of the next node. To find any node in the linked list, we first need to
determine the first node known as the head node. If we have to find the
second node in the list, then we need to traverse from the first node, and in
the worst case, to find the last node, we will be traversing all the nodes. The
average case for accessing the element is O(n).
We conclude that the cost of accessing an element in array is less than the
linked list. Therefore, if we have any requirement for accessing the elements,
then array is a better choice.
2. Cost of inserting an element
There can be three scenarios in the insertion:
o Inserting the element at the beginning: To insert the new element at the
beginning, we first need to shift the element towards the right to create a
space in the first position. So, the time complexity will be proportional to the
size of the list. If n is the size of the array, the time complexity would be O(n).
In the case of a linked list, to insert an element at the starting of the linked
list, we will create a new node, and the address of the first node is added to
the new node. In this way, the new node becomes the first node. So, the time
complexity is not proportional to the size of the list. The time complexity
would be constant, i.e., O(1).
In the case of linked list, we have to traverse to that position where we have
to insert the new element. Even though, we do not have to perform any kind
of shifting, but we have to traverse to n/2 position. The time taken is
proportional to the n number of elements, and the time complexity for the
average case would be O(n).
o Ease of use
The implementation of an array is easy as compared to the linked list. While
creating a program using a linked list, the program is more prone to errors
like segmentation fault or memory leak. So, lots of care need to be taken
while creating a program in the linked list.
o Dynamic in size
The linked list is dynamic in size whereas the array is static. Here, static
doesn't mean that we cannot decide the size at the run time, but we cannot
change it once the size is decided.
3. Memory requirements
As the elements in an array store in one contiguous block of memory, so
array is of fixed size. Suppose we have an array of size 7, and the array
consists of 4 elements then the rest of the space is unused. The memory
occupied by the 7 elements:
Array works with a static memory. Here The Linked list works with dynamic
static memory means that the memory memory. Here, dynamic memory means
size is fixed and cannot be changed at that the memory size can be changed at
the run time. the run time according to our
requirements.
Array elements are independent of each Linked list elements are dependent on
other. each other. As each node contains the
address of the next node so to access the
next node, we need to access its previous
node.
Array takes more time while performing Linked list takes less time while performing
any operation like insertion, deletion, etc. any operation like insertion, deletion, etc.
Principle It follows the principle LIFO It follows the principle FIFO (First In -
(Last In- First Out), which First Out), which implies that the
implies that the element element which is added first would
which is inserted last would be the first element to be removed
be the first one to be from the list.
deleted.
Structure It has only one end from It has two ends, i.e., front and rear
which both the insertion and end. The front end is used for the
deletion take place, and that deletion while the rear end is used
end is known as a top. for the insertion.
Number of It contains only one pointer It contains two pointers front and
pointers used known as a top pointer. The rear pointer. The front pointer holds
top pointer holds the address the address of the first element,
of the last inserted or the whereas the rear pointer holds the
topmost element of the address of the last element in a
stack. queue.
Variants It does not have any types. It is of three types like priority
queue, circular queue and double
ended queue.
Meaning The linear queue is a type of The circular queue is also a linear data
linear data structure that structure in which the last element of
contains the elements in a the Queue is connected to the first
sequential manner. element, thus creating a circle.
Order of It follows the FIFO principle in It has no specific order for execution.
execution order to perform the tasks.
Let's look at the differences in a tabular form.
The differences between the singly-linked list and doubly linked list are given
below:
o Definition
The singly-linked is a linear data structure that consists of a collection of
nodes in which one node consists of two parts, i.e., one is the data part, and
another one is the address part. In contrast, a doubly-linked list is also a
linear data structure in which the node consists of three parts, i.e., one is the
data part, and the other two are the address parts.
o Direction
As we know that in a singly linked list, a node contains the address of the
next node, so the elements can be traversed in only one direction, i.e.,
forward direction. In contrast, in a doubly-linked list, the node contains two
pointers (previous pointer and next pointer) that hold the address of the
next node and the address of the previous node, respectively so
elements can be traversed in both directions.
o Memory space
The singly linked list occupies less memory space as it contains a single
address. We know that the pointer variable stores the address, and the
pointer variable occupies 4 bytes; therefore, the memory space occupied by
the pointer variable in the singly linked list is also 4 bytes. The doubly linked
list holds two addresses in a node, one is of the next node and the other one
is of the previous node; therefore, the space occupied by the two pointer
variables is 8 bytes.
o Insertion and Deletion
The insertion and deletion in a singly-linked list are less complex than a
doubly linked list. If we insert an element in a singly linked list then we need
to update the address of only next node. On the other hand, in the doubly
linked list, we need to update the address of both the next and the previous
node.
Let's look at the differences in a tabular form.
Access The singly linked list can The doubly linked list can be accessed
be traversed only in the in both directions.
forward direction.
In the above figure, we can observe that each node contains utmost 2
children. If any node does not contain left or right child then the value of the
pointer with respect to that child would be NULL.
Basic terminologies used in a Binary tree are:
o Root node: The root node is the first or the topmost node in a binary tree.
o Parent node: When a node is connected to another node through edges,
then that node is known as a parent node. In a binary tree, parent node can
have a maximum of 2 children.
o Child node: If a node has its predecessor, then that node is known as
a child node.
o Leaf node: The node which does not contain any child known as a leaf
node.
o Internal node: The node that has atleast 2 children known as an internal
node.
o Depth of a node: The distance from the root node to the given node is
known as a depth of a node. We provide labels to all the nodes like root
node is labeled with 0 as it has no depth, children of the root nodes are
labeled with 1, children of the root child are labeled with 2.
o Height: The longest distance from the root node to the leaf node is
the height of the node.
In a binary tree, there is one tree known as a perfect binary tree. It is
a tree in which all the internal nodes must contain two nodes, and all the leaf
nodes must be at the same depth. In the case of a perfect binary tree, the
total number of nodes exist in a binary tree can be calculated by using the
following equation:
n = 2m+1-1
where n is the number of nodes, m is the depth of a node.
What is a Binary Search tree?
A Binary search tree is a tree that follows some order to arrange the
elements, whereas the binary tree does not follow any order. In a Binary
search tree, the value of the left node must be smaller than the parent node,
and the value of the right node must be greater than the parent node.
Let's understand the concept of a binary search tree through
examples.
In the above figure, we can observe that the value of the root node is 15,
which is greater than the value of all the nodes in the left subtree. The value
of root node is less than the values of all the nodes in a right-subtree. Now,
we move to the left-child of the root node. 10 is greater than 8 and lesser
than 12; it also satisfies the property of the Binary search tree. Now, we
move to the right-child of the root node; the value 20 is greater than 17 and
lesser than 25; it also satisfies the property of binary search tree. Therefore,
we can say that the tree shown above is the binary search tree.
Now, if we change the value of 12 to 16 in the above binary tree, we have to
find whether it is still a binary search tree or not.
The value of the root node is 15 which is greater than 10 but lesser than 16,
so it does not satisfy the property of the Binary search tree. Therefore, it is
not a binary search tree.
Operations on Binary search tree
We can perform insert, delete and search operations on the binary search
tree. Let's understand how a search is performed on a binary search. The
binary tree is shown below on which we have to perform the search
operation:
First, we will calculate the middle element and compare the middle element
with the element, which is to be searched. The middle element is calculated
by using n/2. The value of n is 7; therefore, the middle element is 15. The
middle element is not equal to the searched element, i.e., 10.
Note: If the element is being searched is lesser than the mid element, then the
searching will be performed in the left half; else, searching will be done on the right
half. In the case of equality, the element is found.
As the element to be searched is lesser than the mid element, so searching
will be performed on the left array. Now the search is reduced to half, as
shown below:
The mid element in the left array is 10, which is equal to the searched
element.
Time complexity
In a binary search, there are n elements. If the middle element is not equal
to the searched element, then the search space is reduced to n/2, and we
will keep on reducing the search space by n/2 until we found the element. In
the whole reduction, if we move from n to n/2 to n/4 and so on, then it will
take log2n steps.
Differences between Binary tree and Binary search tree
Structure The structure of the binary tree is that The binary search tree is one of
the first node or the topmost node is the types of binary tree that has
known as the root node. Each node in the value of all the nodes in the
a binary tree contains the left pointer left subtree lesser or equal to
and the right pointer. The left pointer the root node, and the value of
contains the address of the left all the nodes in a right subtree
subtree, whereas right pointer are greater than or equal to the
contains the address of right subtree. value of the root node.
Operations The operations that can be Binary search trees are the
implemented on a binary tree are sorted binary trees that provide
insertion, deletion, and traversal. fast insertion, deletion and
search. Lookups mainly
implement binary search as all
the keys are arranged in sorted
order.
types Four types of binary trees are Full There are different types of
Binary Tree, Complete Binary Tree, binary search trees such as AVL
Perfect Binary Tree, and Extended trees, Splay tree, Tango trees,
Binary Tree. etc.
What is Tree?
A tree is a non-linear data structure that represents the hierarchy. A tree is a
collection of nodes that are linked together to form a hierarchy.
17.7M
367
HTML Tutorial
Let's look at some terminologies used in a tree data structure.
o Root node: The topmost node in a tree data structure is known as a root
node. A root node is a node that does not have any parent.
o Parent of a node: The immediate predecessor of a node is known as a
parent of a node. Here predecessor means the previous node of that
particular node.
o Child of a node: The immediate successor of a node is known as a child of
a node.
o Leaf node: The leaf node is a node that does not have any child node. It is
also known as an external node.
o Non-leaf node: The non-leaf node is a node that has atleast one child node.
It is also known as an internal node.
o Path: It is a sequence of the consecutive edges from a source node to the
destination node. Here edge is a link between two nodes.
o Ancestor: The predecessor nodes that occur in the path from the root to that
node is known as an ancestor.
o Descendant: The successor nodes that exist in the path from that node to
the leaf node.
o Sibling: All the children that have the same parent node are known as
siblings.
o Degree: The number of children of a particular node is known as a degree.
o Depth of node: The length of the path from the root to that node is known
as a depth of a node.
o Height of a node: The number of edges that occur in the longest path from
that node to the leaf node is known as the height of a node.
o Level of node: The number of edges that exist from the root node to the
given node is known as a level of a node.
Note: If there are n number of nodes in the tree, then there would be (n-1) number of
edges.
How is a tree represented in the memory?
Each node will contain three parts, data part, address of the left subtree, and
address of the right subtree. If any node does not have the child, then both
link parts will have NULL values.
What is a Graph?
A graph is like a tree data structure is a collection of objects or entities
known as nodes that are connected to each other through a set of edges. A
tree follows some rule that determines the relationship between the nodes,
whereas graph does not follow any rule that defines the relationship among
the nodes. A graph contains a set of edges and nodes, and edges can
connect the nodes in any possible way.
Mathematically, it can be defined as an ordered pair of a set of vertices, and
a set of nodes where vertices are represented by 'V' and edges are
represented by 'E'.
G= (V , E)
Here we are referring to an ordered pair because the first object must be the
set of vertices, and the second object must be a set of edges.
In Graph, each node has a different name or index to uniquely identify each
node in the graph. The graph shown below has eight vertices named as v1,
v2, v3, v4, v5, v6, v7, and v8. There is no first node, a second node, a third
node and so on. There is no ordering of the nodes. Now, we will see how can
we represent the edges in a graph?. An edge can be represented by the two
endpoints in the graph. We can write the name of the two endpoints as a
pair, that represents the edge in a graph.
There are two types of edges:
o Directed edge: The directed edge represents one endpoint as an origin and
another point as a destination. The directed edge is one-way. For example,
there are two vertices U and V; then directed edge would represent the link
or path from U to V, but no path exists from V to U. If we want to create a
path from V to U, then we need to have one more directed edge from V to U.
The directed edge can be represented as an ordered pair in which the first
element is the origin, whereas the second element is the destination.
o Undirected edge: The undirected edge is two-way means that there is
no origin and destination. For example, there are two vertices U and V,
then undirected would represent two paths, i.e., from U to V as well as from V
to U. An undirected edge can be represented as an unordered pair because
the edge is bi-directional.
The tree data structure contains only directed edges, whereas the graph can
have both types of edges, i.e., directed as well as undirected. But, we
consider the graph in which all the edges are either directed edges or
undirected edges.
There are two types of graphs:
Directed graph: The graph with the directed edges known as a directed
graph.
Root node In tree data structure, there is a In graph data structure, there is
unique node known as a parent node. no unique node.
It represents the topmost node in the
tree data structure.
Loop It does not create any loop or cycle. In graph, loop or cycle can be
formation formed.
Edges If there are n nodes then there would The number of edges depends
be n-1 number of edges. on the graph.
Type of Tree data structure will always have In graph data structure, all the
edge directed edges. edges can either be directed
edges, undirected edges, or
both.
Applications It is used for inserting, deleting or It is mainly used for finding the
searching any element in tree. shortest path in the network.
Step 2: In the above figure, we can observe that tree is unbalanced because
the balance factor of node 30 is 2. In order to make it an AVL tree, we need
to perform some rotations. If we perform the right rotation on node 20 then
the node 30 will move downwards, whereas the node 20 will move upwards,
as shown below:
As we can observe, the final tree follows the property of the Binary Search
tree and a balanced tree; therefore, it is an AVL tree.
In the case, the tree was left unbalanced tree, so we perform the right
rotation on the node.
o If the order of insertion is 10, 20, 30.
Step 1: First we create a Binary search tree as shown below:
Step 2: In the above figure, we can observe that the tree is unbalanced
because the balance factor of node 10 is -2. In order to make it an AVL tree,
we need to perform some rotations. It is a right unbalanced tree, so we will
perform left rotation. If we perform left rotation on node 20, then the node
20 will move upwards, and node 10 will move downwards, as shown below:
As we can observe, the final tree follows the property of the Binary Search
tree and a balanced tree; therefore, it is an AVL tree.
o If the order of insertion is 30, 10, 20
Step 1: First we create the Binary Search tree as shown below:
Step 2: In the above figure, we can observe that the tree is unbalanced
because the balance factor of the root node is 2. In order to make it an AVL
tree, we need to perform some rotations. The above scenario is left-right
unbalanced as one node is the left of its parent node and another is the right
of its parent node. First, we will perform the left rotation, and rotation
happens between nodes 10 and 20. After left rotation, 20 will move upwards,
and 10 will move downwards as shown below:
Still, the tree is unbalanced, so we perform the right rotation on the tree.
Once the right rotation is performed on the tree, then the tree would like, as
shown below:
As we can observe in the above tree, the tree follows the property of the
Binary Search tree and a balanced tree; therefore, it is an AVL tree.
o If the order of the insertion is 10, 30, 20
Step 1: First, we create the Binary Search tree, as shown below:
Step 2: In the above figure, we can observe that tree is unbalanced because
the balance factor of the root node is 2. In order to make it an AVL tree, we
need to perform some rotations. The above scenario is right-left unbalanced
as one node is right of its parent node, and another node is left of its parent
node. First, we will perform the right rotation that happens between nodes
30 and 20. After right rotation, 20 will move upwards, and 30 will move
downwards as shown below:
Still, the above tree is unbalanced, so we need to perform left rotation on the
node. Once the left rotation is performed, the node 20 will move upwards,
and node 10 will move downwards as shown below:
As we can observe in the above tree, the tree follows the property of the
Binary Search tree and a balanced tree; therefore, it is an AVL tree.
Differences between Binary Search tree and AVL tree
Every binary search tree is a binary tree Every AVL tree is also a binary tree because
because both the trees contain the utmost AVL tree also has the utmost two children.
two children.
In BST, there is no term exists, such as In the AVL tree, each node contains a
balance factor. balance factor, and the value of the
balance factor must be either -1, 0, or 1.
Every Binary Search tree is not an AVL tree Every AVL tree is a binary search tree
because BST could be either a balanced or because the AVL tree follows the property
an unbalanced tree. of the BST.
Each node in the Binary Search tree Each node in the AVL tree consists of four
consists of three fields, i.e., left subtree, fields, i.e., left subtree, node value, right
node value, and the right subtree. subtree, and the balance factor.
In the case of Binary Search tree, if we want In the case of AVL tree, first, we will find the
to insert any node in the tree then we suitable place to insert the node. Once the
compare the node value with the root value; node is inserted, we will calculate the
if the value of node is greater than the root balance factor of each node. If the balance
node value then the node is inserted to the factor of each node is satisfied, the
right subtree otherwise the node is inserted insertion is completed. If the balance factor
to the left subtree. Once the node is is greater than 1, then we need to perform
inserted, there is no need of checking the some rotations to balance the tree.
height balance factor for the insertion to be
completed.
In Binary Search tree, the height or depth of In AVL tree, the height or depth of the tree
the tree is O(n) where n is the number of is O(logn).
nodes in the Binary Search tree.
BST is not a balanced tree because it does AVL tree is a height balanced tree because
not follow the concept of the balance factor. it follows the concept of the balance factor.
Searching is inefficient in BST when there Searching is efficient in AVL tree even when
are large number of nodes available in the there are large number of nodes in the tree
tree because the height is not balanced. because the height is balanced.
The Red-Black tree is a binary search tree, and the AVL tree is also a binary
search tree.
o Rules
The following rules are applied in a Red-Black Tree:
1. The node in a Red-Black tree is either red or black in color.
2. The color of the root node should be black.
3. The adjacent nodes should not be red. In other words, we can say that
the red node cannot have red children, but the black node can have
black children.
4. There should be the same number of black nodes in every path; then,
only a tree can be considered a Red-Black tree.
5. The external nodes are the nil nodes, which are always black in color.
Rule of the AVL tree:
Every node should have the balance factor either as -1, 0 or 1.
o Example
In the above figure, we need to check whether the tree is a Red-Black tree or
not. In order to check this, first, we need to check whether the tree is a
binary search tree or not. As we can observe in the above figure that it
satisfies all the properties of the binary search tree; therefore, it is a binary
search tree. Secondly, we have to verify whether it satisfies the above-said
rules or not. The above tree satisfies all the above five rules; therefore, it
concludes that the above tree is a Red-Black tree.
In the above figure, we need to check whether the tree is an AVL tree or not.
As each node has a value of balance factor either as -1, 0, or 1, so it is an
AVL tree.
o How can the tree be considered as a balanced tree or not?
In the case of a Red-Black tree, if all the above rules are satisfied, provided
that a tree is a binary search tree, then the tree is said to be a Red-black
tree.
In the case of the AVL tree, if the balance factor is -1, 0, or 1, then the above
tree is said to be an AVL tree.
o Tools used for balancing
If the tree is not balanced, then two tools are used for balancing the tree in a
Red-Black tree:
1. Recoloring
2. Rotation
If the tree is not balanced, then one tool is used for balancing the tree in the
AVL tree:
1. Rotation
o Efficient for which operation
In the case of the Red-Black tree, the insertion and deletion operations are
efficient. If the tree gets balanced through the recoloring, then insertion and
deletion operations are efficient in the Red-Black tree.
In the case of the AVL tree, the searching operation is efficient as it requires
only one tool to balance the tree.
o Time complexity
In the Red-Black tree case, the time complexity for all the operations, i.e.,
insertion, deletion, and searching is O(logn).
In the case of AVL tree, the time complexity for all the operations, i.e.,
insertion, deletion, and searching is O(logn).
Let's understand the differences in the tabular form.
Searching Red Black tree does not AVL trees provide efficient
provide efficient searching as searching as it is strictly
Red Black Trees are roughly balanced tree.
balanced.
Color of In the Red-Black tree, the color In the case of AVL trees, there
the node of the node is either Red or is no color of the node.
Black.
Balance It does not contain any balance Each node has a balance
factor factor. It stores only one bit of factor in AVL tree whose value
information that denotes either can be 1, 0, or -1. It requires
Red or Black color of the node. extra space to store the
balance factor per node.
Strictly Red-black trees are not strictly AVL trees are strictly balanced,
balanced balanced. i.e., the left subtree's height
and the height of the right
subtree differ by at most 1.
B tree vs B+ tree
Before understanding B tree and B+ tree differences, we should know the B
tree and B+ tree separately.
What is the B tree?
B tree is a self-balancing tree, and it is a m-way tree where m defines the
order of the tree. Btree is a generalization of the Binary Search tree in which
a node can have more than one key and more than two children depending
upon the value of m. In the B tree, the data is specified in a sorted order
having lower values on the left subtree and higher values in the right
subtree.
Properties of B tree
The following are the properties of the B tree:
OPs Concepts in Java
o In the B tree, all the leaf nodes must be at the same level, whereas, in the
case of a binary tree, the leaf nodes can be at different levels.
Let's understand this property through an example.
In the above tree, all the leaf nodes are not at the same level, but they have
the utmost two children. Therefore, we can say that the above tree is
a binary tree but not a B tree.
o If the Btree has an order of m, then each node can have a maximum of m In
the case of minimum children, the leaf nodes have zero children, the root
node has two children, and the internal nodes have a ceiling of m/2.
o Each node can have maximum (m-1) keys. For example, if the value of m is 5
then the maximum value of keys is 4.
o The root node has minimum one key, whereas all the other nodes except the
root node have (ceiling of m/2 minus - 1) minimum keys.
o If we perform insertion in the B tree, then the node is always inserted in the
leaf node.
Suppose we want to create a B tree of order 3 by inserting values
from 1 to 10.
Step 1: First, we create a node with 1 value as shown below:
Step 2: The next element is 2, which comes after 1 as shown below:
As we know that each node can have 2 maximum keys, so we will split this
node through the middle element. The middle element is 2, so it moves to its
parent. The node 2 does not have any parent, so it will become the root node
as shown below:
As we know that each node can have 2 maximum keys, so we will split this
node through the middle element. The middle element is 4, so it moves to its
parent. The parent is node 2; therefore, 4 will be added after 2 as shown
below:
Step 6: The next element is 6. Since 6 is greater than 2, 4 and 5, so 6 will
come after 5 as shown below:
As we know that each node can have 2 maximum keys, so we will split this
node through the middle element. The middle element is 6, so it moves to its
parent as shown below:
But, 6 cannot be added after 4 because the node can have 2 maximum keys,
so we will split this node through the middle element. The middle element is
4, so it moves to its parent. As node 4 does not have any parent, node 4 will
become a root node as shown below:
What is a B+ tree?
The B+ tree is also known as an advanced self-balanced tree because every
path from the root of the tree to the leaf of the tree has the same length.
Here, the same length means that all the leaf nodes occur at the same level.
It will not happen that some of the leaf nodes occur at the third level and
some of them at the second level.
A B+ tree index is considered a multi-level index, but the B+ tree structure is
not similar to the multi-level index sequential files.
Why is the B+ tree used?
A B+ tree is used to store the records very efficiently by storing the records
in an indexed manner using the B+ tree indexed structure. Due to the multi-
level indexing, the data accessing becomes faster and easier.
B+ tree Node Structure
The node structure of the B+ tree contains pointers and key values shown in
the below figure:
As we can observe in the above B+ tree node structure that it contains n-1
key values (k1 to kn-1) and n pointers (p1 to pn).
The search key values which are placed in the node are kept in sorted order.
Thus, if i<j then ki<kj.
Constraint on various types of nodes
Let 'b' be the order of the B+ tree.
Non-Leaf node
Let 'm' represents the number of children of a node, then the relation
between the order of the tree and the number of children can be represented
as:
Let k represents the search key values. The relation between the order of the
tree and search key can be represented as:
As we know that the number of pointers is equal to the search key values
plus 1, so mathematically, it can be written as:
Number of Pointers (or children) = Number of Search keys + 1
Therefore, the maximum number of pointers would be 'b', and the minimum
number of pointers would be the ceiling function of b/2.
Leaf Node
A leaf node is a node that occurs at the last level of the B+ tree, and each
leaf node uses only one pointer to connect with each other to provide the
sequential access at the leaf level.
In leaf node, the maximum number of children is:
Root Node
The maximum number of children in the case of the root node is: b
The minimum number of children is: 2
Special cases in B+ tree
Case 1: If the root node is the only node in the tree. In this case, the root
node becomes the leaf node.
In this case, the maximum number of children is 1, i.e., the root node itself,
whereas, the minimum number of children is b-1, which is the same as that
of a leaf node.
Representation of a leaf node in B+ tree
In the above figure, '.' represents the pointer, whereas the 10, 20 and 30 are
the key values. The pointer contains the address at which the key value is
stored, as shown in the above figure.
Example of B+ tree
In the above figure, the node contains three key values, i.e., 9, 16, and 25.
The pointer that appears before 9, contains the key values less than 9
represented by ki. The pointer that appears before 16, contains the key
values greater than or equal to 9 but less than 16 represented by kj. The
pointer that appears before 25, contains the key values greater than or equal
to 16 but less than 25 represented by kn.
The following are the differences between the B tree and B+ tree:
B tree B+ tree
In the B tree, all the keys and In the B+ tree, keys are the indexes stored in the
records are stored in both internal internal nodes and records are stored in the leaf
as well as leaf nodes. nodes.
In the Btree, leaf nodes are not In B+ tree, the leaf nodes are linked to each
linked to each other. other to provide the sequential access.
Deletion of internal nodes is very Deletion in B+ tree is very fast because all the
slow and a time-consuming records are stored in the leaf nodes so we do not
process as we need to consider have to consider the child of the node.
the child of the deleted key also.
In Btree, sequential access is not In the B+ tree, all the leaf nodes are connected
possible. to each other through a pointer, so sequential
access is possible.
In Btree, the more number of B+ tree has more width as compared to height.
splitting operations are performed
due to which height increases
compared to width,
In Btree, each node has atleast In B+ tree, internal nodes contain only pointers
two branches and each node and leaf nodes contain records. All the leaf nodes
contains some records, so we do are at the same level, so we need to traverse till
not need to traverse till the leaf the leaf nodes to get the data.
nodes to get the data.
The root node contains atleast 2 The root node contains atleast 2 to m children
to m children where m is the order where m is the order of the tree.
of the tree.
3. Partition of In quick sort, the array can Merge sort partition an array into
elements be divide into any ratio. two sub array (N/2).
6 Time Its worst time complexity Whereas, it's worst time complexity
complexity is O (n2). is O (n log n).
10. Functionali Compare each element Whereas, the merge sort splits the
ty with the pivot until all array into two parts (N/2) and it
elements are arranged in continuously divides the array until
ascending order. an element is left.
What is BFS?
BFS stands for Breadth First Search. It is also known as level order
traversal. The Queue data structure is used for the Breadth First Search
traversal. When we use the BFS algorithm for the traversal in a graph, we
can consider any node as a root node.
Let's consider the below graph for the breadth first search traversal.
Once node 0 is removed from the Queue, it gets printed and marked as
a visited node.
Once node 0 gets removed from the Queue, then the adjacent nodes of node
0 would be inserted in a Queue as shown below:
Now the node 1 will be removed from the Queue; it gets printed and marked
as a visited node
Once node 1 gets removed from the Queue, then all the adjacent nodes of a
node 1 will be added in a Queue. The adjacent nodes of node 1 are 0, 3, 2, 6,
and 5. But we have to insert only unvisited nodes in a Queue. Since nodes 3,
2, 6, and 5 are unvisited; therefore, these nodes will be added in a Queue as
shown below:
The next node is 3 in a Queue. So, node 3 will be removed from the Queue, it
gets printed and marked as visited as shown below:
Once node 3 gets removed from the Queue, then all the adjacent nodes of
node 3 except the visited nodes will be added in a Queue. The adjacent
nodes of node 3 are 0, 1, 2, and 4. Since nodes 0, 1 are already visited, and
node 2 is present in a Queue; therefore, we need to insert only node 4 in a
Queue.
Now, the next node in the Queue is 2. So, 2 would be deleted from the
Queue. It gets printed and marked as visited as shown below:
Once node 2 gets removed from the Queue, then all the adjacent nodes of
node 2 except the visited nodes will be added in a Queue. The adjacent
nodes of node 2 are 1, 3, 5, 6, and 4. Since the nodes 1 and 3 have already
been visited, and 4, 5, 6 are already added in the Queue; therefore, we do
not need to insert any node in the Queue.
The next element is 5. So, 5 would be deleted from the Queue. It gets printed
and marked as visited as shown below:
Once node 5 gets removed from the Queue, then all the adjacent nodes of
node 5 except the visited nodes will be added in the Queue. The adjacent
nodes of node 5 are 1 and 2. Since both the nodes have already been visited;
therefore, there is no vertex to be inserted in a Queue.
The next node is 6. So, 6 would be deleted from the Queue. It gets printed
and marked as visited as shown below:
Once the node 6 gets removed from the Queue, then all the adjacent nodes
of node 6 except the visited nodes will be added in the Queue. The adjacent
nodes of node 6 are 1 and 4. Since the node 1 has already been visited and
node 4 is already added in the Queue; therefore, there is not vertex to be
inserted in the Queue.
The next element in the Queue is 4. So, 4 would be deleted from the Queue.
It gets printed and marked as visited.
Once the node 4 gets removed from the Queue, then all the adjacent nodes
of node 4 except the visited nodes will be added in the Queue. The adjacent
nodes of node 4 are 3, 2, and 6. Since all the adjacent nodes have already
been visited; so, there is no vertex to be inserted in the Queue.
What is DFS?
DFS stands for Depth First Search. In DFS traversal, the stack data structure
is used, which works on the LIFO (Last In First Out) principle. In DFS,
traversing can be started from any node, or we can say that any node can be
considered as a root node until the root node is not mentioned in the
problem.
In the case of BFS, the element which is deleted from the Queue, the
adjacent nodes of the deleted node are added to the Queue. In contrast, in
DFS, the element which is removed from the stack, then only one adjacent
node of a deleted node is added in the stack.
Let's consider the below graph for the Depth First Search traversal.
Consider node 0 as a root node.
First, we insert the element 0 in the stack as shown below:
The node 0 has two adjacent nodes, i.e., 1 and 3. Now we can take only one
adjacent node, either 1 or 3, for traversing. Suppose we consider node 1;
therefore, 1 is inserted in a stack and gets printed as shown below:
Now we will look at the adjacent vertices of node 1. The unvisited adjacent
vertices of node 1 are 3, 2, 5 and 6. We can consider any of these four
vertices. Suppose we take node 3 and insert it in the stack as shown below:
Now we will consider the unvisited adjacent vertices of node 4. The unvisited
adjacent vertex of node 4 is node 6. Therefore, element 6 is inserted into the
stack as shown below:
After inserting element 6 in the stack, we will look at the unvisited adjacent
vertices of node 6. As there is no unvisited adjacent vertices of node 6, so we
cannot move beyond node 6. In this case, we will perform backtracking.
The topmost element, i.e., 6 would popped out from the stack as shown
below:
The topmost element in the stack is 4. Since there are no unvisited adjacent
vertices left of node 4; therefore, node 4 is popped out from the stack as
shown below:
The next topmost element in the stack is 2. Now, we will look at the unvisited
adjacent vertices of node 2. Since only one unvisited node, i.e., 5 is left, so
node 5 would be pushed into the stack above 2 and gets printed as shown
below:
Now we will check the adjacent vertices of node 5, which are still unvisited.
Since there is no vertex left to be visited, so we pop the element 5 from the
stack as shown below:
We will check the adjacent vertices of node 0, which are still unvisited. As
there is no adjacent vertex left to be visited, so we perform backtracking. In
this, only one element, i.e., 0 left in the stack, would be popped out from the
stack as shown below:
As we can observe in the above figure that the stack is empty. So, we have
to stop the DFS traversal here, and the elements which are printed is the
result of the DFS traversal.
Differences between BFS and DFS
The following are the differences between the BFS and DFS:
BFS DFS
Full form BFS stands for Breadth First DFS stands for Depth First Search.
Search.
Data Queue data structure is used Stack data structure is used for the
Structure for the BFS traversal. BFS traversal.
Backtracking BFS does not use the DFS uses backtracking to traverse all
backtracking concept. the unvisited nodes.
Number of BFS finds the shortest path In DFS, a greater number of edges are
edges having a minimum number of required to traverse from the source
edges to traverse from the vertex to the destination vertex.
source to the destination
vertex.
Optimality BFS traversal is optimal for DFS traversal is optimal for those
those vertices which are to be graphs in which solutions are away
searched closer to the source from the source vertex.
vertex.
Suitability It is not suitable for the It is suitable for the decision tree.
for decision decision tree because it Based on the decision, it explores all
tree requires exploring all the the paths. When the goal is found, it
neighboring nodes first. stops its traversal.