0% found this document useful (0 votes)
22 views86 pages

UNIT 3 DSA

Dsa

Uploaded by

jaisingh2744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views86 pages

UNIT 3 DSA

Dsa

Uploaded by

jaisingh2744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 86

UNIT -3

Subject Name:- DSA

Linked List
o Linked List can be defined as collection of objects called nodes that
are randomly stored in the memory.
o A node contains two fields i.e. data stored at that particular address
and the pointer which contains the address of the next node in the
memory.
o The last node of the list contains pointer to the null.

One way chain or singly linked list can be traversed only in one direction. In
other words, we can say that each node contains only next pointer, therefore
we can not traverse the list in the reverse direction.

Consider an example where the marks obtained by the student in three


subjects are stored in a linked list as shown in the figure.
In the above figure, the arrow represents the links. The data part of every
node contains the marks obtained by the student in the different subject.
The last node in the list is identified by the null pointer which is present in
the address part of the last node. We can have as many elements we
require, in the data part of the list.

Complexity
Data Time Complexity Space
Structure Compleity

Average Worst Worst

Acces Searc Insertio Deletio Acces Searc Insertio Deletio


s h n n s h n n

Singly Linked θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
List

Uses of Linked List


o The list is not required to be contiguously present in the memory. The node
can reside any where in the memory and linked together to make a list. This
achieves optimized utilization of space.
o list size is limited to the memory size and doesn't need to be declared in
advance.
o Empty node can not be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.

Why use linked list over array?


Till now, we were using array data structure to organize the group of
elements that are to be stored individually in the memory. However, Array
has several advantages and disadvantages which must be known in order to
decide the data structure which will be used throughout the program.

Array contains following limitations:


1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to
expand the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory.
Inserting any element in the array needs shifting of all its predecessors.

Linked list is the data structure which can overcome all the limitations of an
array. Using linked list is useful because,

1. It allocates the memory dynamically. All the nodes of linked list are non-
contiguously stored in the memory and linked together with the help of
pointers.
2. Sizing is no longer a problem since we do not need to define its size at the
time of declaration. List grows as per the program's demand and limited to
the available memory space.

Common Singly Linked List Operations:


The following operations are performed on a Single Linked List

 Insertion
 Deletion
 Display

Before we implement actual operations, first we need to set up an empty list. First, perform the
following steps before implementing actual operations.

 Step 1 - Include all the header files which are used in the program.
 Step 2 - Declare all the user defined functions.
 Step 3 - Define a Node structure with two members data and next
 Step 4 - Define a Node pointer 'head' and set it to NULL.
 Step 5 - Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.

Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting at Beginning of the list


We can use the following steps to insert a new node at beginning of the single linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set newNode→next = NULL and head = newNode.
 Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.

Inserting at End of the list


We can use the following steps to insert a new node at end of the single linked list...

 Step 1 - Create a newNode with given value and newNode → next as NULL.
 Step 2 - Check whether list is Empty (head == NULL).
 Step 3 - If it is Empty then, set head = newNode.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
 Step 6 - Set temp → next = newNode.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the single linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last
node then display 'Given node is not found in the list!!! Insertion not possible!!!' and
terminate the function. Otherwise move the temp to next node.
 Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the single linked list...
 Step 1 - Check whether list is Empty (head == NULL)
 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list is having only one node (temp → next == NULL)
 Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
 Step 6 - If it is FALSE then set head = temp → next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the single linked list...
 Step 1 - Check whether list is Empty (head == NULL)
 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4 - Check whether list has only one Node (temp1 → next == NULL)
 Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
(Setting Empty list condition)
 Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list. (until temp1 → next == NULL)
 Step 7 - Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the single linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last
node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
 Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not
 Step 7 - If list has only one node and that is the node to be deleted, then
set head = NULL and delete temp1 (free(temp1)).
 Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in the list
(temp1 == head).
 Step 9 - If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.
 Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1 →
next == NULL).
 Step 11 - If temp1 is last node then set temp2 → next = NULL and
delete temp1 (free(temp1)).
 Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).

Search

Finding an element is similar to a traversal operation. Instead of displaying data, we have to


check whether the data matches with the item to find.

 Initialize PTR with the address of HEAD. Now the PTR points to the first node of the linked
list.
 A while loop is executed which will compare data of every node with item.
 If item has been found then control goes to last step.
Algorithm: Search

Step 1: [INITIALIZE] SET PTR = HEAD

Step 2: Repeat Steps 3 and 4 while PTR != NULL

Step 3: If ITEM = PTR -> DATA

SET POS = PTR

Go To Step 5

ELSE

SET PTR = PTR -> NEXT

[END OF IF]

[END OF LOOP]

Step 4: SET POS = NULL

Step 5: EXIT
Traversing a singly linked list

Traversing a singly linked list simply means, Accessing or printing the values
(i.e., data items stored inside each node) of a singly linked list exactly once
until the end node is reached and, this accessing and printing is sometimes
called “visiting” the linked list. It’s only because, Let’s suppose you wish to
check the data items (values) stored at the node of a given linked list or you
wish to use the node (i.e, either the value or the address stored inside the
node) as part of some other operation or process then traversing is the best
suited way to do that.

Algorithm to traverse a singly linked list:

 Step 01: Start


 Step 02: [Declare a pointer named temp of node type. ]
 Step 03: Set temp = head

 Step 04: If head == NULL

o Step 05: Display “List is empty”


o Step 06: Go to step 14
 Step 07: [End of If ]
 Step 08: Else
o Step 09: Repeat Step 10 and 11 Until temp != NULL

 Step 10: Display temp -> data

 Step 11: Set temp = temp -> next

o Step 12: [End of step 09 loop. ]


 Step 13: [End of Else block. ]
 Step 14: Stop
Linked list implementation of stack
Instead of using array, we can also use linked list to implement stack. Linked
list allocates the memory dynamically. However, time complexity in both the
scenario is same for all the operations i.e. push, pop and peek.

In linked list implementation of stack, the nodes are maintained non-


contiguously in the memory. Each node contains a pointer to its immediate
successor node in the stack. Stack is said to be overflown if the space left in
the memory heap is not enough to create a node.

The top most node in the stack always contains null in its address field. Lets
discuss the way in which, each operation is performed in linked list
implementation of stack.
Adding a node to the stack (Push operation)
Adding a node to the stack is referred to as push operation. Pushing an
element to a stack in linked list implementation is different from that of an
array implementation. In order to push an element onto the stack, the
following steps are involved.

1. Create a node first and allocate memory to it.


2. If the list is empty then the item is to be pushed as the start node of
the list. This includes assigning value to the data part of the node and
assign null to the address part of the node.
3. If there are some nodes in the list already, then we have to add the
new element in the beginning of the list (to not violate the property of
the stack). For this purpose, assign the address of the starting element
to the address field of the new node and make the new node, the
starting node of the list.
4. Time Complexity : o(1)
Linked List implementation of Queue
Due to the drawbacks discussed in the previous section of this tutorial, the
array implementation can not be used for the large scale applications where
the queues are implemented. One of the alternative of array implementation
is linked list implementation of queue.

The storage requirement of linked representation of a queue with n elements


is o(n) while the time requirement for operations is o(1).

In a linked queue, each node of the queue consists of two parts i.e. data part
and the link part. Each element of the queue points to its immediate next
element in the memory.
In the linked queue, there are two pointers maintained in the memory i.e.
front pointer and rear pointer. The front pointer contains the address of the
starting element of the queue while the rear pointer contains the address of
the last element of the queue.

Insertion and deletions are performed at rear and front end respectively. If
front and rear both are NULL, it indicates that the queue is empty.

The linked representation of queue is shown in the following figure.

Operation on Linked Queue


There are two basic operations which can be implemented on the linked queues. The operations
are Insertion and Deletion.

Insert operation
The insert operation append the queue by adding an element to the end of the queue. The new
element will be the last element of the queue.

Firstly, allocate the memory for the new node ptr by using the following statement.

1. Ptr = (struct node *) malloc (sizeof(struct node));

There can be the two scenario of inserting this new node ptr into the linked queue.

In the first scenario, we insert element into an empty queue. In this case, the condition front =
NULL becomes true. Now, the new element will be added as the only element of the queue and
the next pointer of front and rear pointer both, will point to NULL.

In the second case, the queue contains more than one element. The condition front
= NULL becomes false. In this scenario, we need to update the end pointer rear so
that the next pointer of rear will point to the new node ptr. Since, this is a linked
queue, hence we also need to make the rear pointer point to the newly added
node ptr. We also need to make the next pointer of rear point to NULL.
In this way, the element is inserted into the queue. The algorithm and the C
implementation is given as follows.

Algorithm
o Step 1: Allocate the space for the new node PTR
o Step 2: SET PTR -> DATA = VAL
o Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
o Step 4: END

Header Node in Linked List

A header node is a special node that is found at the beginning of the list. A list
that contains this type of node, is called the header-linked list. This type of list is
useful when information other than that found in each node is needed.
For example, suppose there is an application in which the number of items in a
list is often calculated. Usually, a list is always traversed to find the length of the
list. However, if the current length is maintained in an additional header node
that information can be easily obtained.

Types of Header Linked List

1. Grounded Header Linked List


It is a list whose last node contains the NULL pointer. In the header linked
list the start pointer always points to the header node. start -> next =
NULL indicates that the grounded header linked list is empty. The operations
that are possible on this type of linked list are Insertion, Deletion, and
Traversing.

2. Circular Header Linked List


A list in which last node points back to the header node is called circular
linked list. The chains do not indicate first or last nodes. In this case,
external pointers provide a frame of reference because last node of a
circular linked list does not contain the NULL pointer. The possible
operations on this type of linked list are Insertion, Deletion and
Traversing.
Applications of Header Linked List
Polynomials
 The header linked lists are frequently used to maintain the polynomials in
memory. The header node is used to represent the zero polynomial.
 Suppose we have
F(x) = 5x5 – 3x3 + 2x2 + x1 +10x0
 From the polynomial represented by F(x) it is clear that this polynomial has
two parts, coefficient and exponent, where, x is formal parameter. Hence,
we can say that a polynomial is sum of terms, each of which consists of a
coefficient and an exponent.
 The computer implementation requires implementing polynomials as a list of
pair of coefficient and exponent. Each of these pairs will constitute a
structure, so a polynomial will be represented as a list of structures.
 If one wants to represent F(x) with help of linked list then the list will contain
5 nodes. When we link each node we get a linked list structure that
represents polynomial F(x).
Double Linked List?
In a single linked list, every node has a link to its next node in the sequence. So, we can traverse
from one node to another node only in one direction and we can not traverse back. We can solve
this kind of problem by using a double linked list. A double linked list can be defined as follows...

Double linked list is a sequence of elements in which every element has links to its previous
element and next element in the sequence.

In a double linked list, every node has a link to its previous node and next node. So, we can traverse
forward by using the next field and can traverse backward by using the previous field. Every node in
a double linked list contains three fields and they are shown in the following figure...

Here, 'link1' field is used to store the address of the previous node in the sequence, 'link2' field is
used to store the address of the next node in the sequence and 'data' field is used to store the
actual value of that node.

EXAMPLE:-

Importent Points to be Remembered

In double linked list, the first node must be always pointed by head.

Always the previous field of the first node must be NULL.


Always the next field of the last node must be NULL.

Operations on Double Linked List


In a double linked list, we perform the following operations...

1. Insertion
2. Deletion
3. Display

Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the double linked list...

 Step 1 - Create a newNode with given value and newNode → previous as NULL.
 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
 Step 4 - If it is not Empty then, assign head to newNode → next and newNode to head.

Time Complexity: O(1)


Auxiliary Space: O(1)
Inserting At End of the list
We can use the following steps to insert a new node at end of the double linked list...

 Step 1 - Create a newNode with given value and newNode → next as NULL.
 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty, then assign NULL to newNode → previous and newNode to head.
 Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
 Step 6 - Assign newNode to temp → next and temp to newNode → previous.

Time Complexity: O(n)


Auxiliary Space: O(1)

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the double linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, assign NULL to both newNode → previous & newNode →
next and set newNode to head.
 Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
 Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp1 to next node.
 Step 7 - Assign temp1 → next to temp2, newNode to temp1 → next, temp1 to newNode
→ previous, temp2 to newNode → next and newNode to temp2 → previous.

Time Complexity: O(1)


Auxiliary Space: O(1)

Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the double linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list is having only one node (temp → previous is equal to temp →
next)
 Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list
conditions)
 Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and
delete temp.
Deleting from End of the list
We can use the following steps to delete a node from end of the double linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list has only one Node (temp → previous and temp → next both
are NULL)

 Step 5 - If it is TRUE, then assign NULL to head and condition)


 Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list.
(until temp → next is equal to NULL)
 Step 7 - Assign NULL to temp → previous → next and delete temp.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the double linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
 Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last
node.
 Step 5 - If it is reached to the last node, then display 'Given node not found in the list!
Deletion not possible!!!' and terminate the fuction.
 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not
 Step 7 - If list has only one node and that is the node which is to be deleted then
set head to NULL and delete temp (free(temp)).
 Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list
(temp == head).
 Step 9 - If temp is the first node, then move the head to the next node (head = head →
next), set head of previous to NULL (head → previous = NULL) and delete temp.
 Step 10 - If temp is not the first node, then check whether it is the last node in the list ( temp
→ next == NULL).
 Step 11 - If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp (free(temp)).
 Step 12 - If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp →
previous) and delete temp (free(temp)).

Doubly Linked List Complexity

Doubly Linked List Complexity Time Complexity Space Complexity

Insertion Operation O(1) or O(n) O(1)

Deletion Operation O(1) O(1)

Advantages of DLL over the singly linked list:


 A DLL can be traversed in both forward and backward directions.
 The delete operation in DLL is more efficient if a pointer to the node to be
deleted is given.
 We can quickly insert a new node before a given node.
 In a singly linked list, to delete a node, a pointer to the previous node is
needed. To get this previous node, sometimes the list is traversed. In DLL,
we can get the previous node using the previous pointer.
Disadvantages of DLL over the singly linked list:
 Every node of DLL Requires extra space for a previous pointer. It is possible
to implement DLL with a single pointer though (See this and this).
 All operations require an extra pointer previous to be maintained. For
example, in insertion, we need to modify previous pointers together with the
next pointers. For example in the following functions for insertions at
different positions, we need 1 or 2 extra steps to set the previous pointer.

Circular Singly Linked List


In a circular Singly linked list, the last node of the list contains a pointer to
the first node of the list. We can have circular singly linked list as well as
circular doubly linked list.

We traverse a circular singly linked list until we reach the same node where
we started. The circular singly liked list has no beginning and no ending.
There is no null value present in the next part of any of the nodes.

The following image shows a circular singly linked list.

Circular linked list are mostly used in task maintenance in operating systems. There
are many examples where circular linked list are being used in computer science
including browser surfing where a record of pages visited in the past by the user, is
maintained in the form of circular linked lists and can be accessed again on clicking
the previous button.
Memory Representation of circular linked list:
In the following image, memory representation of a circular linked list
containing marks of a student in 4 subjects. However, the image shows a
glimpse of how the circular list is being stored in the memory. The start or
head of the list is pointing to the element with the index 1 and containing 13
marks in the data part and 4 in the next part. Which means that it is linked
with the node that is being stored at 4th index of the list.

However, due to the fact that we are considering circular linked list in the
memory therefore the last node of the list contains the address of the first
node of the list.
Insertion on a Circular Linked List
We can insert elements at 3 different positions of a circular linked list:

1. Insertion at the beginning


2. Insertion in-between nodes
3. Insertion at the end
Suppose we have a circular linked list with elements 1, 2, and 3.

1. Insertion at the Beginning

 store the address of the current first node in the newNode (i.e. pointing
the newNode to the current first node)
 point the last node to newNode (i.e making newNode as head)

**Algorithm to insert a new node at the beginning**


Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR -> NEXT != START
Step 7: PTR = PTR -> NEXT
[END OF LOOP]
Step 8: SET NEW_NODE -> NEXT = START
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: SET START = NEW_NODE
Step 11: EXIT

2. Insertion in between two nodes

Let's insert newNode after the first node.

 travel to the node given (let this node be p )


 point the next of newNode to the node next to p

 store the address of newNode at next of p

3. Insertion at the end

 store the address of the head node to next of newNode (making newNode the
last node)
 point the current last node to newNode

 make newNode as the last node

**Algorithm to insert a new node at the end**


Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT != START
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT

Deletion on a Circular Linked List

1. If the node to be deleted is the only node

 free the memory occupied by the node

 store NULL in last

**Algorithm to delete the first node**


Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR -> NEXT != START
Step 4: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 5: SET PTR -> NEXT = START -> NEXT
Step 6: FREE START
Step 7: SET START = PTR -> NEXT
Step 8: EXIT

If last node is to be deleted

 find the node before the last node (let it be temp )

 store the address of the node next to the last node in temp

 free the memory of last

 make temp as the last node

**Algorithm to delete the last node**


Step 1: IF START = NULL

Write UNDERFLOW

Go to Step 8

[END OF IF]

Step 2: SET PTR = START

Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != START

Step 4: SET PREPTR = PTR

Step 5: SET PTR = PTR -> NEXT


[END OF LOOP]

Step 6: SET PREPTR -> NEXT = START

Step 7: FREE PTR

Step 8: EXIT

Tree Data Structure-


Tree data structure may be defined as-

Tree is a non-linear data structure which organizes data in a hierarchical structure and
this is a recursive definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between every pair of vertices, then graph is
called as a tree.
Properties-

The important properties of tree data structure are-


 There is one and only one path between every pair of vertices in a tree.
 A tree with n vertices has exactly (n-1) edges.
 A graph is a tree if and only if it is minimally connected.
 Any connected graph with n vertices and (n-1) edges is a tree.

Tree Terminology-

The important terms related to tree data structure are-

1. Root-

 The first node from where the tree originates is called as a root node.
 In any tree, there must be only one root node.
 We can never have multiple root nodes in a tree data structure.
Example-

2. Edge-

 The connecting link between any two nodes is called as an edge.


 In a tree with n number of nodes, there are exactly (n-1) number of edges.

Example-
3. Parent-

 The node which has a branch from it to any other node is called as a parent node.
 In other words, the node which has one or more children is called as a parent node.
 In a tree, a parent node can have any number of child nodes.

Example-
Here,
 Node A is the parent of nodes B and C
 Node B is the parent of nodes D, E and F
 Node C is the parent of nodes G and H
 Node E is the parent of nodes I and J
 Node G is the parent of node K

4. Child-

 The node which is a descendant of some node is called as a child node.


 All the nodes except root node are child nodes.

Example-

Here,
 Nodes B and C are the children of node A
 Nodes D, E and F are the children of node B
 Nodes G and H are the children of node C
 Nodes I and J are the children of node E
 Node K is the child of node G
5. Siblings-

 Nodes which belong to the same parent are called as siblings.


 In other words, nodes with the same parent are sibling nodes.

Example-

Here,
 Nodes B and C are siblings
 Nodes D, E and F are siblings
 Nodes G and H are siblings
 Nodes I and J are siblings

6. Degree-

 Degree of a node is the total number of children of that node.


 Degree of a tree is the highest degree of a node among all the nodes in the tree.
Example-

Here,
 Degree of node A = 2
 Degree of node B = 3
 Degree of node C = 2
 Degree of node D = 0
 Degree of node E = 2
 Degree of node F = 0
 Degree of node G = 1
 Degree of node H = 0
 Degree of node I = 0
 Degree of node J = 0
 Degree of node K = 0

7. Internal Node-

 The node which has at least one child is called as an internal node.
 Internal nodes are also called as non-terminal nodes.
 Every non-leaf node is an internal node.
Example-

Here, nodes A, B, C, E and G are internal nodes.

8. Leaf Node-

 The node which does not have any child is called as a leaf node.
 Leaf nodes are also called as external nodes or terminal nodes.

Example-
Here, nodes D, I, J, F, K and H are leaf nodes.

9. Level-

 In a tree, each step from top to bottom is called as level of a tree.


 The level count starts with 0 and increments by 1 at each level or step.

Example-
10. Height-

 Total number of edges that lies on the longest path from any leaf node to a particular
node is called as height of that node.
 Height of a tree is the height of root node.
 Height of all leaf nodes = 0

Example-
Here,
 Height of node A = 3
 Height of node B = 2
 Height of node C = 2
 Height of node D = 0
 Height of node E = 1
 Height of node F = 0
 Height of node G = 1
 Height of node H = 0
 Height of node I = 0
 Height of node J = 0
 Height of node K = 0

11. Depth-

 Total number of edges from root node to a particular node is called as depth of that
node.
 Depth of a tree is the total number of edges from root node to a leaf node in the longest
path.
 Depth of the root node = 0
 The terms “level” and “depth” are used interchangeably.

Example-
Here,
 Depth of node A = 0
 Depth of node B = 1
 Depth of node C = 1
 Depth of node D = 2
 Depth of node E = 2
 Depth of node F = 2
 Depth of node G = 2
 Depth of node H = 2
 Depth of node I = 3
 Depth of node J = 3
 Depth of node K = 3

12. Subtree-

 In a tree, each child from a node forms a subtree recursively.


 Every child node forms a subtree on its parent node.

Example-
13. Forest-

A forest is a set of disjoint trees.

Example-

Applications of trees
The following are the applications of trees:

o Storing naturally hierarchical data: Trees are used to store the data in
the hierarchical structure. For example, the file system. The file system
stored on the disc drive, the file and folder are in the form of the naturally
hierarchical data and stored in the form of trees.
o Organize data: It is used to organize data for efficient insertion, deletion
and searching. For example, a binary tree has a logN time for searching an
element.
o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast
and efficient way for dynamic spell checking.
o Heap: It is also a tree data structure implemented using arrays. It is used to
implement priority queues.
o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
o Routing table: The tree data structure is also used to store the data in
routing tables in the routers.

Types of Tree data structure


The following are the types of a tree data structure:

o General tree: The general tree is one of the types of tree data structure. In
the general tree, a node can have either 0 or maximum n number of nodes.
There is no restriction imposed on the degree of the node (the number of
nodes that a node can contain). The topmost node in a general tree is known
as a root node. The children of the parent node are known as subtrees.

There can be n number of subtrees in a general tree. In the general tree, the
subtrees are unordered as the nodes in the subtree cannot be ordered.
Every non-empty tree has a downward edge, and these edges are connected
to the nodes known as child nodes. The root node is labeled with level 0.
The nodes that have the same parent are known as siblings.
o
Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In
a binary tree, each node in a tree can have utmost two child nodes. Here,
utmost means whether the node has 0 nodes, 1 node or 2 nodes.
o

o
Binary Search tree: Binary search tree is a non-linear data structure in
which one node is connected to n number of nodes. It is a node-based data
structure. A node can be represented in a binary search tree with three fields,
i.e., data part, left-child, and right-child. A node can be connected to the
utmost two child nodes in a binary search tree, so the node contains two
pointers (left child and right child pointer).
Every node in the left subtree must contain a value less than the value of the
root node, and the value of each node in the right subtree must be bigger
than the value of the root node.
o
AVL tree

It is one of the types of the binary tree, or we can say that it is a variant of
the binary search tree. AVL tree satisfies the property of the binary tree as
well as of the binary search tree. It is a self-balancing binary search tree
that was invented by Adelson Velsky Lindas. Here, self-balancing means
that balancing the heights of left subtree and right subtree. This balancing is
measured in terms of the balancing factor.

We can consider a tree as an AVL tree if the tree obeys the binary search tree as
well as a balancing factor. The balancing factor can be defined as
the difference between the height of the left subtree and the height of
the right subtree. The balancing factor's value must be either 0, -1, or 1;
therefore, each node in the AVL tree should have the value of the balancing
factor either as 0, -1, or 1.

Strictly Binary Tree


In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none. That means every internal node must have exactly
two children. A strictly Binary Tree can be defined as follows...

A binary tree in which every node has either two or zero number of children is called Strictly
Binary Tree

Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree

Strictly binary tree data structure is used to represent mathematical expressions.


Example

2. Complete Binary Tree


In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none and in complete binary tree all the nodes must have
exactly two children and at every level of complete binary tree there must be 2 level number of nodes.
For example at level 2 there must be 22 = 4 nodes and at level 3 there must be 23 = 8 nodes.

A binary tree in which every internal node has exactly two children and all leaf nodes are at
same level is called Complete Binary Tree.

Complete binary tree is also called as Perfect Binary Tree


3. Extended Binary Tree
A binary tree can be converted into Full Binary tree by adding dummy nodes to existing nodes
wherever required.

The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended
Binary Tree.
In above figure, a normal binary tree is converted into full binary tree by adding dummy nodes (In
pink colour).

o B-tree
B-tree is a balanced m-way tree where m defines the order of the tree. Till
now, we read that the node contains only one key but b-tree can have more
than one key, and more than 2 children. It always maintains the sorted data.
In binary tree, it is possible that leaf nodes can be at different levels, but in
b-tree, all the leaf nodes must be at the same level.

If order is m then node has the following properties:

o Each node in a b-tree can have maximum m children


o For minimum children, a leaf node has 0 children, root node has minimum 2
children and internal node has minimum ceiling of m/2 children. For example,
the value of m is 5 which means that a node can have 5 children and internal
nodes can contain maximum 3 children.
o Each node has maximum (m-1) keys.

The root node must contain minimum 1 key and all other nodes must contain
atleast ceiling of m/2 minus 1 keys.

Threaded Binary Tree


In the linked representation of binary trees, more than one half of the link
fields contain NULL values which results in wastage of storage space. If a
binary tree consists of n nodes then n+1 link fields contain NULL values. So in
order to effectively manage the space, a method was devised by Perlis and
Thornton in which the NULL links are replaced with special links known as
threads. Such binary trees with threads are known as threaded binary trees.
Each node in a threaded binary tree either contains a link to its child node or
thread to other nodes in the tree.
Types of Threaded Binary Tree
There are two types of threaded Binary Tree:

o One-way threaded Binary Tree


o Two-way threaded Binary Tree

One-way threaded Binary trees:


In one-way threaded binary trees, a thread will appear either in the right or
left link field of a node. If it appears in the right link field of a node then it will
point to the next node that will appear on performing in order traversal. Such
trees are called Right threaded binary trees. If thread appears in the left
field of a node then it will point to the nodes inorder predecessor. Such trees
are called Left threaded binary trees. Left threaded binary trees are used
less often as they don't yield the last advantages of right threaded binary
trees. In one-way threaded binary trees, the right link field of last node and
left link field of first node contains a NULL. In order to distinguish threads
from normal links they are represented by dotted lines.

The above figure shows the inorder traversal of this binary tree yields D, B,
E, A, C, F. When this tree is represented as a right threaded binary tree, the
right link field of leaf node D which contains a NULL value is replaced with a
thread that points to node B which is the inorder successor of a node D. In
the same way other nodes containing values in the right link field will contain
NULL value.

Two-way threaded Binary Trees:


The above figure shows the inorder traversal of this binary tree yields D, B,
E, A, C, F. When this tree is represented as a right threaded binary tree, the
right link field of leaf node D which contains a NULL value is replaced with a
thread that points to node B which is the inorder successor of a node D. In
the same way other nodes containing values in the right link field will contain
NULL value.

Two-way threaded Binary Trees:


Application of threaded binary tree

 Threaded trees are a important data structure where trees are created
once with very less of insertions and deletions operations there on and
more of traversal operations (specifically depth first traversals) are
more in number. In such situations the threaded trees act as a boon for
the system performance by reducing the space required for stacks.
 It is also possible to discover the parent of a node from a threaded
binary tree, without explicit use of parent pointers or a stack, albeit
slowly. This can be useful where stack space is limited, or where a stack
of parent pointers is unavailable (for finding the parent pointer via DFS).

Binary Search Tree


A binary search tree follows some order to arrange the elements. In a Binary
search tree, the value of left node must be smaller than the parent node, and
the value of right node must be greater than the parent node. This rule is
applied recursively to the left and right subtrees of the root.

Let's understand the concept of Binary search tree with an example.


In the above figure, we can observe that the root node is 40, and all the
nodes of the left subtree are smaller than the root node, and all the nodes of
the right subtree are greater than the root node.

Similarly, we can see the left child of root node is greater than its left child
and smaller than its right child. So, it also satisfies the property of binary
search tree. Therefore, we can say that the tree in the above image is a
binary search tree.

Suppose if we change the value of node 35 to 55 in the above tree, check


whether the tree will be binary search tree or not.

In the above tree, the value of root node is 40, which is greater than its left
child 30 but smaller than right child of 30, i.e., 55. So, the above tree does
not satisfy the property of Binary search tree. Therefore, the above tree is
not a binary search tree.

Advantages of Binary search tree


o Searching an element in the Binary search tree is easy as we always have a
hint that which subtree has the desired element.
o As compared to array and linked lists, insertion and deletion operations are
faster in BST.

Example of creating a binary search tree


Now, let's see the creation of binary search tree using an example.

Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

o First, we have to insert 45 into the tree as the root of the tree.

o Then, read the next element; if it is smaller than the root node, insert it
as the root of the left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as
the root of the right subtree.

Now, let's see the process of creating the Binary search tree using the given
data element. The process of creating the BST is shown below -

Step 1 - Insert 45.

Step 2 - Insert 15.

As 15 is smaller than 45, so insert it as the root node of the left subtree.
Step 3 - Insert 79.

As 79 is greater than 45, so insert it as the root node of the right subtree.

Step 4 - Insert 90.

90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
Step 5 - Insert 10.

10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.

10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.

Step 6 - Insert 55.

55 is larger than 45 and smaller than 79, so it will be inserted as the left
subtree of 79.
Step 7 - Insert 12.

12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the
right subtree of 10.

Step 8 - Insert 20.

20 is smaller than 45 but greater than 15, so it will be inserted as the right
subtree of 15.
Step 9 - Insert 50.

50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a
left subtree of 55.
Now, the creation of binary search tree is completed. After that, let's move
towards the operations that can be performed on Binary search tree.

We can perform insert, delete and search operations on the binary search
tree.

Operations on a Binary Search Tree


The following operations are performed on a binary search tree...

1. Search
2. Insertion
3. Deletion

Search Operation in BST


In a binary search tree, the search operation is performed with O(log n) time complexity. The search
operation is performed as follows...

 Step 1 - Read the search element from the user.


 Step 2 - Compare the search element with the value of root node in the tree.
 Step 3 - If both are matched, then display "Given node is found!!!" and terminate the function
 Step 4 - If both are not matched, then check whether search element is smaller or larger
than that node value.
 Step 5 - If search element is smaller, then continue the search process in left subtree.
 Step 6- If search element is larger, then continue the search process in right subtree.
 Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node
 Step 8 - If we reach to the node having the value equal to the search value then display
"Element is found" and terminate the function.
 Step 9 - If we reach to the leaf node and if it is also not matched with the search element,
then display "Element is not found" and terminate the function.

Insertion Operation in BST


In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary
search tree, new node is always inserted as a leaf node. The insertion operation is performed as
follows...

 Step 1 - Create a new Node with given value and set its left and right to NULL.
 Step 2 - Check whether tree is Empty.
 Step 3 - If the tree is Empty, then set root to new Node.
 Step 4 - If the tree is Not Empty, then check whether the value of new Node
is smaller or larger than the node (here it is root node).
 Step 5 - If new Node is smaller than or equal to the node then move to its left child. If new
Node is larger than the node then move to its right child.
 Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL).
 Step 7 - After reaching the leaf node, insert the new Node as left child if the new Node
is smaller or equal to that leaf node or else insert it as right child.

Deletion Operation in BST


In a binary search tree, the deletion operation is performed with O(log n) time complexity. Deleting a
node from Binary search tree includes following three cases...

 Case 1: Deleting a Leaf node (A node with no children)


 Case 2: Deleting a node with one child
 Case 3: Deleting a node with two children

Case 1: Deleting a leaf node


We use the following steps to delete a leaf node from BST...

 Step 1 - Find the node to be deleted using search operation


 Step 2 - Delete the node using free function (If it is a leaf) and terminate the function.
Case 2: Deleting a node with one child
We use the following steps to delete a node with one child from BST...

 Step 1 - Find the node to be deleted using search operation


 Step 2 - If it has only one child then create a link between its parent node and child node.
 Step 3 - Delete the node using free function and terminate the function.

Case 3: Deleting a node with two children


We use the following steps to delete a node with two children from BST...

 Step 1 - Find the node to be deleted using search operation


 Step 2 - If it has two children, then find the largest node in its left subtree (OR)
the smallest node in its right subtree.
 Step 3 - Swap both deleting node and node which is found in the above step.
 Step 4 - Then check whether deleting node came to case 1 or case 2 or else goto step 2
 Step 5 - If it comes to case 1, then delete using case 1 logic.
 Step 6- If it comes to case 2, then delete using case 2 logic.
 Step 7 - Repeat the same process until the node is deleted from the tree.

Example
Construct a Binary Search Tree by inserting the following sequence of numbers...

10,12,5,4,20,8,7,15 and 13
Above elements are inserted into a Binary Search Tree as follows...
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree
is named AVL in honour of its inventors.

AVL Tree can be defined as height balanced binary search tree in which each
node is associated with a balance factor which is calculated by subtracting
the height of its right sub-tree from that of its left sub-tree.

Tree is said to be balanced if balance factor of each node is in between -1 to


1, otherwise, the tree will be unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height


(right(k))
If balance factor of any node is 1, it means that the left sub-tree is one level
higher than the right sub-tree.

If balance factor of any node is 0, it means that the left sub-tree and right
sub-tree contain equal height.

If balance factor of any node is -1, it means that the left sub-tree is one level
lower than the right sub-tree.

An AVL tree is given in the following figure. We can see that, balance factor
associated with each node is in between -1 and +1. therefore, it is an
example of AVL tree.
Complexity
Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)


Operations on AVL tree
Due to the fact that, AVL tree is also a binary search tree therefore, all the
operations are performed in the same way as they are performed in a binary
search tree. Searching and traversing do not lead to the violation in property
of AVL tree. However, insertion and deletion are the operations which can
violate this property and therefore, they need to be revisited.

S Operation Description
N

1 Insertion Insertion in AVL tree is performed in the same way as it is performed in


a binary search tree. However, it may lead to violation in the AVL tree
property and therefore the tree may need balancing. The tree can be
balanced by applying rotations.

2 Deletion Deletion can also be performed in the same way as it is performed in a


binary search tree. Deletion may also disturb the balance of the tree
therefore, various types of rotations are used to rebalance the tree.

VL tree controls the height of the binary search tree by not letting it to be skewed. The time
taken for all operations in a binary search tree of height h is O(h). However, it can be extended
to O(n) if the BST becomes skewed (i.e. worst case). By limiting this height to log n, AVL tree
imposes an upper bound on each operation to be O(log n) where n is the number of nodes.

AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1.
There are basically four types of rotations which are as follows:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A

Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations. For a tree to be unbalanced,
minimum height must be at least 2, Let us understand each rotation

. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right
subtree of the right subtree of A, then we perform RR rotation, RR rotation is
an anticlockwise rotation, which is applied on the edge below a node having
balance factor -2

In above example, node A has balance factor -2 because a node C is inserted


in the right subtree of A right subtree. We perform the RR rotation on the
edge below A.

2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left
subtree of the left subtree of C, then we perform LL rotation, LL rotation is
clockwise rotation, which is applied on the edge below a node having
balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted
in the left subtree of C left subtree. We perform the LL rotation on the edge
below A.

3.LR Rotation
Double rotations are bit tougher than single rotation which has already
explained above. LR rotation = RR rotation + LL rotation, i.e., first RR
rotation is performed on subtree and then LL rotation is performed on full
tree, by full tree we mean the first node from the path of inserted node
whose balance factor is other than -1, 0, or 1.

4. RL Rotation
As already discussed, that double rotations are bit tougher than single
rotation which has already explained above. R L rotation = LL rotation + RR
rotation, i.e., first LL rotation is performed on subtree and then RR rotation is
performed on full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.

Q: Construct an AVL tree having the following elements


H, I, J, B, A, E, C, F, D, G, K, L

1. Insert H, I, J
On inserting the above elements, especially in the case of H, the BST
becomes unbalanced as the Balance Factor of H is -2. Since the BST is right-
skewed, we will perform RR Rotation on node H.

The resultant balance tree is:

2. Insert B, A
On inserting the above elements, especially in case of A, the BST becomes
unbalanced as the Balance Factor of H and I is 2, we consider the first node
from the last inserted node i.e. H. Since the BST from H is left-skewed, we
will perform LL Rotation on node H.

The resultant balance tree is:

3. Insert E
On inserting E, BST becomes unbalanced as the Balance Factor of I is 2,
since if we travel from E to I we find that it is inserted in the left subtree of
right subtree of I, we will perform LR Rotation on node I. LR = RR + LL
rotation

3 a) We first perform RR rotation on node B

The resultant tree after RR rotation is:


3b) We first perform LL rotation on the node I

The resultant balanced tree after LL rotation is:


4. Insert C, F, D

On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B


and H is -2, since if we travel from D to B we find that it is inserted in the
right subtree of left subtree of B, we will perform RL Rotation on node I. RL =
LL + RR rotation.

4a) We first perform LL rotation on node E

The resultant tree after LL rotation is:


4b) We then perform RR rotation on node B

The resultant balanced tree after RR rotation is:

5. Insert G
On inserting G, BST become unbalanced as the Balance Factor of H is 2,
since if we travel from G to H, we find that it is inserted in the left subtree of
right subtree of H, we will perform LR Rotation on node I. LR = RR + LL
rotation.

5 a) We first perform RR rotation on node C

The resultant tree after RR rotation is:


5 b) We then perform LL rotation on node H

The resultant balanced tree after LL rotation is:

6. Insert K
On inserting K, BST becomes unbalanced as the Balance Factor of I is -2.
Since the BST is right-skewed from I to K, hence we will perform RR Rotation
on the node I.

The resultant balanced tree after RR rotation is:

7. Insert L

On inserting the L tree is still balanced as the Balance Factor of each node is
now either, -1, 0, +1. Hence the tree is a Balanced AVL tree
B Tree
B Tree is a specialized m-way tree that can be widely used for disk access. A
B-Tree of order m can have at most m-1 keys and m children. One of the
main reason of using B tree is its capability to store large number of keys in
a single node and large key values by keeping the height of the tree
relatively small.

A B tree of order m contains all the properties of an M way tree. In addition,


it contains the following properties.

1. Every node in a B-Tree contains at most m children.


2. Every node in a B-Tree except the root node and the leaf node contain
at least m/2 children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.

It is not necessary that, all the nodes contain the same number of children
but, each node must have m/2 number of nodes.

A B tree of order 4 is shown in the following image.

While performing some operations on B Tree, any property of B Tree may


violate such as number of minimum children a node can have. To maintain
the properties of B Tree, the tree may split or join.
Operations
Searching :
Searching in B Trees is similar to that in Binary search tree. For example, if
we search for an item 49 in the following B Tree. The process will something
like following :

1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left
sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.

Searching in a B tree depends upon the height of the tree. The search
algorithm takes O(log n) time to search any element in a B tree.

Inserting
Insertions are done at the leaf node level. The following algorithm needs to
be followed in order to insert an item into B Tree.

1. Traverse the B Tree in order to find the appropriate leaf node at which the
node can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it too by
following the same steps.

Example:

Insert the node 8 into the B Tree of order 5 shown in the following image.

8 will be inserted to the right of 5, therefore insert 8.


The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys.
Therefore split the node from the median i.e. 8 and push it up to its parent
node shown as follows.

Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted
can either be a leaf node or an internal node. Following algorithm needs to
be followed in order to delete a node from a B tree.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key
from the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking
the element from eight or left sibling.
o If the left sibling contains more than m/2 elements then push its
largest element up to its parent and move the intervening element
down to the node where the key is deleted.
o If the right sibling contains more than m/2 elements then push its
smallest element up to the parent and move intervening element down
to the node where the key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new
leaf node by joining two leaf nodes and the intervening element of the parent
node.
5. If parent is left with less than m/2 nodes then, apply the above process on the
parent too.

If the the node which is to be deleted is an internal node, then replace the
node with its in-order successor or predecessor. Since, successor or
predecessor will always be on the leaf node hence, the process will be similar
as the node is being deleted from the leaf node.

Example 1

Delete the node 53 from the B Tree of order 5 shown in the following figure.
53 is present in the right child of element 49. Delete it.

Now, 57 is the only element which is left in the node, the minimum number
of elements that must be present in a B tree of order 5, is 2. it is less than
that, the elements in its left and right sub-tree are also not sufficient
therefore, merge it with the left sibling and intervening element of parent i.e.
49.

The final B tree is shown as follows.


Steps to search an element in B Tree

1. The search starts from the root node. Compare the search element k with the root.
1.1. If root node contains k, search completes.
1.2. If k < the first value in the root, we move to the leftmost chld and search the child
recursively.
1.3.1. If the root has just 2 children, then move to the rightmost child and search the
child recursively.
1.3.2. If the root has more than 2 keys, then search the rest.
2. If k is not found after traversing the whole tree, then the search element is not present in
the B Tree.

Application of B tree
B tree is used to index the data and provides fast access to the actual data
stored on the disks since, the access to value stored in a large database that
is stored on a disk is a very time consuming process.

Searching an un-indexed and unsorted database containing n key values


needs O(n) running time in worst case. However, if we use B Tree to index
this database, it will be searched in O(log n) time in worst case.
Applications Of B Trees
 B trees are used to index the data especially in large databases as access to data
stored in large databases on disks is very time-consuming.
 Searching of data in larger unsorted data sets takes a lot of time but this can be
improved significantly with indexing using B tree.

B+ Tree
B+ tree is an extension of the B tree. The difference in B+ tree and B tree is that in B tree
the keys and records can be stored as internal as well as leaf nodes whereas in B+ trees,
the records are stored as leaf nodes and the keys are stored only in internal nodes.

The records are linked to each other in a linked list fashion. This arrangement makes the
searches of B+ trees faster and efficient. Internal nodes of the B+ tree are called index
nodes.

The B+ trees have two orders i.e. one for internal nodes and other for leaf or external
nodes.

An Example of B+ Tree is shown below.

As B+ tree is an extension of B-tree, the basic operations that we discussed under the topic
B-tree still holds.
While inserting as well as deleting, we should maintain the basic properties of B+ Trees
intact. However, deletion operation in the B+ tree is comparatively easier as the data is
stored only in the leaf nodes and it will be deleted from the leaf nodes always.

Advantages of B+ Trees
 We can fetch records in an equal number of disk accesses.
 Compared to the B tree, the height of the B+ tree is less and remains balanced.
 We use keys for indexing.
 Data in the B+ tree can be accessed sequentially or directly as the leaf nodes are
arranged in a linked list.
 Search is faster as data is stored in leaf nodes only and as a linked list.

Difference between B-Tree and B+ Tree


B-Tree B+ Tree

Data is stored in leaf nodes as well as internal nodes. Data is stored only in leaf nodes.

Searching is a bit slower as data is stored in internal as well Searching is faster as the data is stored only in the leaf nodes.
as leaf nodes.

No redundant search keys are present. Redundant search keys may be present.

Deletion operation is complex. Deletion operation is easy as data can be directly deleted from
the leaf nodes.

Leaf nodes cannot be linked together. Leaf nodes are linked together to form a linked list.

You might also like