0% found this document useful (0 votes)
2 views33 pages

Unit-1 Complete CSP

The document covers the fundamentals of algorithm analysis, including time and space complexity, and asymptotic notations such as Big-O, Omega, and Theta. It also discusses binary search trees, their operations (search, insertion, deletion), and their applications and advantages. Additionally, it highlights the limitations of binary search trees and the importance of algorithm analysis in predicting performance and efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views33 pages

Unit-1 Complete CSP

The document covers the fundamentals of algorithm analysis, including time and space complexity, and asymptotic notations such as Big-O, Omega, and Theta. It also discusses binary search trees, their operations (search, insertion, deletion), and their applications and advantages. Additionally, it highlights the limitations of binary search trees and the importance of algorithm analysis in predicting performance and efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Unit-1

Introduction to Algorithm Analysis, Space and Time Complexity analysis, Asymptotic


Notations.
AVL Trees – Creation, Insertion, Deletion operations and Applications
B-Trees – Creation, Insertion, Deletion operations and Applications

Introduction to Algorithm Analysis:

Algorithm analysis is an important part of computational complexity theory, which


provides theoretical estimation for the required resources of an algorithm to solve a specific
computational problem. Analysis of algorithms is the determination of the amount of time
and space resources required to execute it.

Why Analysis of Algorithms is important?

 To predict the behaviour of an algorithm without implementing it on a specific


computer.
 It is much more convenient to have simple measures for the efficiency of an
algorithm than to implement the algorithm and test the efficiency every time a
certain parameter in the underlying computer system changes.
 It is impossible to predict the exact behaviour of an algorithm. There are too many
influencing factors.
 The analysis is thus only an approximation; it is not perfect.
 More importantly, by analysing different algorithms, we can compare them to
determine the best one for our purpose.
Types of Algorithm Analysis:
1. Best case
2. Worst case
3. Average case
 Best case: Define the input for which algorithm takes less time or minimum time.
In the best case calculate the lower bound of an algorithm. Example: In the linear
search when search data is present at the first location of large data then the best
case occurs.
 Worst Case: Define the input for which algorithm takes a long time or maximum
time. In the worst calculate the upper bound of an algorithm. Example: In the
linear search when search data is not present at all then the worst case occurs.
 Average case: In the average case take all random inputs and calculate the
computation time for all inputs.

Types of Asymptotic Notations in Complexity Analysis of Algorithms


We have discussed Asymptotic Analysis, and Worst, Average, and Best Cases of
Algorithms. The main idea of asymptotic analysis is to have a measure of the efficiency of
algorithms that don’t depend on machine-specific constants and don’t require algorithms to
be implemented and time taken by programs to be compared. Asymptotic notations are
mathematical tools to represent the time complexity of algorithms for asymptotic analysis.

Asymptotic Notations:
 Asymptotic Notations are mathematical tools used to analyze the performance of
algorithms by understanding how their efficiency changes as the input size grows.
 These notations provide a concise way to express the behavior of an algorithm’s
time or space complexity as the input size approaches infinity.
 Rather than comparing algorithms directly, asymptotic analysis focuses on
understanding the relative growth rates of algorithms’ complexities.
 It enables comparisons of algorithms’ efficiency by abstracting away machine-
specific constants and implementation details, focusing instead on fundamental
trends.
 Asymptotic analysis allows for the comparison of algorithms’ space and time
complexities by examining their performance characteristics as the input size
varies.
 By using asymptotic notations, such as Big O, Big Omega, and Big Theta, we can
categorize algorithms based on their worst-case, best-case, or average-case time
or space complexities, providing valuable insights into their efficiency.

There are mainly three asymptotic notations:


1. Big-O Notation (O-notation)
2. Omega Notation (Ω-notation)
3. Theta Notation (Θ-notation)
1. Theta Notation (Θ-Notation):
Theta notation encloses the function from above and below. Since it represents the
upper and the lower bound of the running time of an algorithm, it is used for analyzing
the average-case complexity of an algorithm.
Theta (Average Case) You add the running times for each possible input combination and
take the average in the average case.
Let g and f be the function from the set of natural numbers to itself. The function f is said to
be Θ(g), if there are constants c1, c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤
c2 * g(n) for all n ≥ n0

Theta notation

Mathematical Representation of Theta notation:


Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1 * g(n) ≤ f(n) ≤
c2 * g(n) for all n ≥ n0}
Note: Θ(g) is a set
The above expression can be described as if f(n) is theta of g(n), then the value f(n) is always
between c1 * g(n) and c2 * g(n) for large values of n (n ≥ n0). The definition of theta also
requires that f(n) must be non-negative for values of n greater than n0.
The execution time serves as both a lower and upper bound on the algorithm’s time
complexity.
It exist as both, most, and least boundaries for a given input value.

2. Big-O Notation (O-notation):


Big-O notation represents the upper bound of the running time of an algorithm. Therefore,
it gives the worst-case complexity of an algorithm.
.It is the most widely used notation for Asymptotic analysis.
.It specifies the upper bound of a function.
.The maximum time required by an algorithm or the worst-case time complexity.
.It returns the highest possible output value(big-O) for a given input.
.Big-Oh(Worst Case) It is defined as the condition that allows an algorithm to complete
statement execution in the longest amount of time possible.

If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive
constant C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
It returns the highest possible output value (big-O)for a given input.
The execution time serves as an upper bound on the algorithm’s time complexity.

3. Omega Notation (Ω-Notation):


Omega notation represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
The execution time serves as a lower bound on the algorithm’s time complexity.
It is defined as the condition that allows an algorithm to complete statement execution
in the shortest amount of time.
Let g and f be the function from the set of natural numbers to itself. The function f is said to
be Ω(g), if there is a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n
≥ n0
Time and Space complexity:
What Is Time Complexity?
Time complexity is defined in terms of how many times it takes to run a given
algorithm, based on the length of the input. Time complexity is not a measurement of how
much time it takes to execute a particular algorithm because such factors as programming
language, operating system, and processing power are also considered.

Time complexity is a type of computational complexity that describes the time required
to execute an algorithm. The time complexity of an algorithm is the amount of time it takes for
each statement to complete. As a result, it is highly dependent on the size of the processed data.
It also aids in defining an algorithm's effectiveness and evaluating its performance.

What Is Space Complexity?

When an algorithm is run on a computer, it necessitates a certain amount of memory


space. The amount of memory used by a program to execute it is represented by its space
complexity. Because a program requires memory to store input data and temporal values while
running, the space complexity is auxiliary and input space.
Time Complexity vs. Space Complexity

You now understand space and time complexity fundamentals and how to calculate it for an
algorithm or program. In this section, you will summarise all previous discussions and list the
key differences in a table.

Time Complexity Space Complexity

Estimates the space memory


Calculates the time required
required

Memory space is counted for all


Time is counted for all statements
variables, inputs, and outputs.

The size of the input data is the primary Primarily determined by the
determinant. auxiliary variable size

More essential in terms of


More crucial in terms of solution optimization
solution optimization
GATES INSTITUTE OF TECHNOLOGY, GOOTY

UNIT - 2
Trees Part-I
Trees Part-I: Binary Search Trees: Definition and Operations, AVL Trees: Definition and
Operations, Applications. B Trees: Definition and Operations.

1. Binary Search Trees


1.1.What is a Binary Search Tree:
A binary search tree is a binary tree. It may be empty. If it is not empty then it
satisfies the following properties:
1. The elements in the left sub-tree are smaller than the root.
2. The elements in the right sub-tree are larger than the root.
3. The left and right sub-trees are also binary search trees.
Example

Every binary search tree is a binary tree but every binary tree need not to be
binary search tree.
1.2.Operations on Binary Search Tree:
The following operations are performed on a binary search tree:
1. Search
2. Insertion
3. Deletion
1.2.1. Search Operation:
 In a binary search tree, the search operation is performed with O(log n) average
time complexity.

GIT-CSE-ADSA PAGE NO.26


GATES INSTITUTE OF TECHNOLOGY, GOOTY

 The search operation is performed as follows:

Algorithm:

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 " Element 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 sub-tree.

Step 6- If search element is larger, then continue the search process in right sub-tree.

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.

Example:

GIT-CSE-ADSA PAGE NO.27


GATES INSTITUTE OF TECHNOLOGY, GOOTY

A. The element to be searched is 10


B. Compare the element with the root node 12, 10 < 12, hence you move to the left
sub-tree. No need to analyze the right sub-tree
C. Now compare 10 with node 7, 10 > 7, so move to the right sub-tree
D. Then compare 10 with the next node, which is 9, 10 > 9, look in the right sub-
tree child
E. 10 matches with the value in the node, 10 = 10, return the value to the user.

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

Step 1 - Create a newNode 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 newNode.

Step 4 - If the tree is Not Empty, then check whether the value of newNode
is smaller or larger than the node (here it is root node).

Step 5 - If newNode is smaller than or equal to the node then move to its left child.
If newNode 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 newNode as left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.

GIT-CSE-ADSA PAGE NO.28


GATES INSTITUTE OF TECHNOLOGY, GOOTY

Example:
Construct a Binary Search Tree by inserting the following sequence of
numbers...
10, 12, 5, 4, 20, 8, 7, 15 and 13

Solution:
10, 12, 5, 4, 20, 8, 7, 15 and 13
• Above elements are inserted into a Binary Search Tree as follows:

GIT-CSE-ADSA PAGE NO.29


GATES INSTITUTE OF TECHNOLOGY, GOOTY

1.2.3. Deletion:
 In a binary search tree, the deletion operation is performed with O(log n) average
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 (A node with no children):
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.

Example:

GIT-CSE-ADSA PAGE NO.30


GATES INSTITUTE OF TECHNOLOGY, GOOTY

Case 2- Deleting a node with one child:


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 - If it has only one child then replace the node with its child.

Example:

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 sub-tree (OR)
the smallest node in its right sub-tree.

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.

GIT-CSE-ADSA PAGE NO.31


GATES INSTITUTE OF TECHNOLOGY, GOOTY

Example:

1.3.Applications of Binary Search Tree:


1. BSTs are used for indexing and multi-level indexing.
2. They are also helpful to implement various searching algorithms.
3. It is helpful in maintaining a sorted stream of data.
4. Tree Map and Tree Set data structures are internally implemented using self-balancing
BSTs

1.4.Advantages of Binary Search Tree:


1. Time Complexity of insert, delete, search operations is to O(logN) where N is the number
of nodes in the tree.
2. We have an ordering of keys stored in the tree. Any time we need to traverse the
increasing (or decreasing) order of keys, we just need to do the in-order (and reverse in-
order) traversal on the tree.
3. We can implement order statistics with binary search tree - Nth smallest, Nth largest
element. This is because it is possible to look at the data structure as a sorted array.
4. We can also do range queries - find keys between N and M (N <= M).
5. BST can also be used in the design of memory allocators to speed up the search of free
blocks (chunks of memory), and to implement best fit algorithms where we are interested

GIT-CSE-ADSA PAGE NO.32


GATES INSTITUTE OF TECHNOLOGY, GOOTY

in finding the smallest free chunk with size greater than or equal to size specified in
allocation request.

1.5.Limitations of Binary Search Tree:


1. BST’s have an average time complexity of ϴ(log(n)) for inserting, deleting, and finding,
but in the worst case, all of these operations have a time complexity of O(n).
2. BST’s forms skewed trees as follows:

3. In Skewed BST’s, all operations are performed in O(n) time

1.6.Skewed Binary Search Trees:


• A skewed binary search tree is a type of binary search tree in which all the nodes have
only either one child or no child.
• In Skewed BST’s, all operations are performed in O(n) time.
• There are 2 types of skewed trees:
1. Left Skewed Tree:
In this all the nodes are having a left child or no child at all.
2. Right Skewed Tree:
In this all the nodes are having a right child or no child at all.

GIT-CSE-ADSA PAGE NO.33


GATES INSTITUTE OF TECHNOLOGY, GOOTY

1.7.Time Complexity of BST:


Operation Best-Case Average-Case Worst-Case
Searching O( h ) O( h ) O( n )
Insertion O( h ) O( h ) O( n )
Deletion O( h ) O( h ) O( n )
Where, h is height of the BST and n is number of nodes in BST

2. AVL Trees:
2.1.What is Height Balanced Trees?
 A tree which performs any operation in O(log n) in worst case, then it is called Height
Balanced Tree.
 Example:
AVL Tree, Red-Black Tree, Splay tree, B- Tree etc...,

2.2.What an AVL Tree?


 AVL Tree is height balanced Binary Search Tree which performs all operations in

𝑂(log 𝑛).
 AVL Tree is inverted by G.M. Adelson-Velski and E.M. Landis.

GIT-CSE-ADSA PAGE NO.34


GATES INSTITUTE OF TECHNOLOGY, GOOTY

 Definition:
An AVL Tree is a height balanced binary search tree, it may be empty. If
it is not empty then every node has balance factor is either -1, 0, or +1.
 Balance Factor is calculated as:
𝐵𝑎𝑙𝑎𝑛𝑐𝑒 𝐹𝑎𝑐𝑡𝑜𝑟 = ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑙𝑒𝑓𝑡 𝑠𝑢𝑏 𝑡𝑟𝑒𝑒 − ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑟𝑜𝑔ℎ𝑡 𝑠𝑢𝑏 𝑡𝑟𝑒𝑒
 Example:

2.3.What is a rotation? Why rotations are required in AVL Tress?


 Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
 In AVL tree, after performing operations like insertion and deletion we need to check
the balance factor of every node in the tree.
 If every node satisfies the balance factor condition then we conclude the tree is in
balance, otherwise the tree is imbalance.
 Whenever the tree becomes imbalanced due to any operation we
use rotation operations to make the tree balanced.

GIT-CSE-ADSA PAGE NO.35


GATES INSTITUTE OF TECHNOLOGY, GOOTY

2.4.List and Explain Different types of rotations in AVL Trees?


 There are four rotations and they are classified into two types.

1. LL Rotation:
 Unbalance occurs when a new node is inserted in the left subtree of the left child of
pivot node, then we perform LL rotation to balance the tree.
 Below figure illustrates the LL rotation:

 Example:

GIT-CSE-ADSA PAGE NO.36


GATES INSTITUTE OF TECHNOLOGY, GOOTY

2. RR Rotation:
 Unbalance occurs when a new node is inserted in the right sub-tree of the right child
of pivot node, then we perform RR rotation to balance the tree.
 Below figure illustrates the RR rotation:

 Example:

3. LR Rotation:
 Unbalance occurs when a new node is inserted in the right subtree of the left child of
pivot node, then we perform LR rotation to balance the tree.
 Below figure illustrates the LR rotation:

GIT-CSE-ADSA PAGE NO.37


GATES INSTITUTE OF TECHNOLOGY, GOOTY

 Example:

4. RL Rotation:
 Unbalance occurs when a new node is inserted in the left sub-tree of the right child of
pivot node, then we perform RL rotation to balance the tree.
 Below figure illustrates the RL rotation:

GIT-CSE-ADSA PAGE NO.38


GATES INSTITUTE OF TECHNOLOGY, GOOTY

 Example:

2.5.Operations on AVL Trees:


 The following operations are performed on AVL tree:
1. Search
2. Insertion
3. Deletion
2.5.1. Search Operation:
 The Search Operation in AVL Trees is same as in BST.
 The search operation is performed as follows:
Algorithm:

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 " Element 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 sub-tree.

Step 6- If search element is larger, then continue the search process in right sub-tree.

GIT-CSE-ADSA PAGE NO.39


GATES INSTITUTE OF TECHNOLOGY, GOOTY

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.

2.5.2. Insert Operation:


 In AVL tree, new node is always inserted as a leaf node.
 The insertion operation is performed as follows:
Algorithm:
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.

Step 2 - After insertion, check the Balance Factor of every node.

Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next


operation.

Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform suitable Rotation to make it balanced and
go for next operation.

2.5.3. Delete Operation:


 The deletion operation in AVL Tree is similar to deletion operation in BST.
 But after every deletion operation, we need to check with the Balance Factor
condition.
 If the tree is balanced after deletion go for next operation otherwise perform suitable
rotation to make the tree Balanced.

GIT-CSE-ADSA PAGE NO.40


GATES INSTITUTE OF TECHNOLOGY, GOOTY

2.6.Applications of AVL Trees:


1. AVL trees are mostly used for in-memory sorts of sets and dictionaries.
2. AVL trees are also used extensively in database applications in which insertions and
deletions are fewer but there are frequent lookups for data required.
3. It is used in applications that require improved searching apart from the database
applications.

2.7.Properties of AVL Trees:


1. Maximum possible number of nodes in AVL tree of height H = 2 H+1 – 1.
2. Minimum number of nodes in AVL Tree of height H is given by a recursive relation:
N(H) = N(H-1) + N(H-2) + 1
Where, N(0) = 1 and N(1) = 2.
3. Minimum possible height of AVL Tree using N nodes = ⌊log2 𝑁⌋
4. Maximum height of AVL Tree using N nodes is calculated using recursive relation:
N(H) = N(H-1) + N(H-2) + 1
Where, N(0) = 1 and N(1) = 2.

3. M-Way Trees
3.1.What an M-Way Tree?
• An M-way search tree of order m is a search tree in which any node can have at most m
children.
• Definition:
An M-way search tree may be empty. If it is not empty, then it satisfies the below
properties:
1. Each node can hold maximum m-1 keys and can have maximum m children.
2. The keys in a node are in ascending order.
3. If keys in a node exceeds m-1, then keys are arranged in left and right sub-trees
for the particular key. Left sub-tree has the key values will be less and right sub-
tree has the key values will be more than the parent key.
4. Left and right sub-trees are m-way trees.

GIT-CSE-ADSA PAGE NO.41


GATES INSTITUTE OF TECHNOLOGY, GOOTY

4. B Trees:
4.1.Introduction:
 B-Tree is a height balanced M-way tree.
 The operations of B-Tree like insertion, deletion, search require O(h), where h is
height of B-Tree.

4.2.Definition:
B-Tree of order m may be empty. If it is not empty, then it satisfies the below properties:
1. All leaf nodes must be at same level.
2. All nodes except root must have at least [m/2]-1 keys and maximum of (m-
1) keys.
3. All non leaf nodes except root (i.e. all internal nodes) must have at least (m/2)
children.
4. If the root node is a non leaf node, then it must have at least 2 children.
5. A non leaf node with n-1 keys must have n number of children.
6. All the key values in a node must be in Ascending Order.
Example:

GIT-CSE-ADSA PAGE NO.49


GATES INSTITUTE OF TECHNOLOGY, GOOTY

4.3.Operations on B Tree:
 The following operations are performed on a B-Tree:
1. Search
2. Insertion
3. Deletion
4.3.1. Search Operation:
 The search operation in B-Tree is similar to the search operation in Binary Search
Tree.
 In a Binary search tree, the search process starts from the root node and we make a 2-
way decision every time (we go to either left subtree or right subtree).
 In B-Tree also search process starts from the root node but here we make an n-way
decision every time. Where 'n' is the total number of children the node has.
 The search operation is performed as follows:
Algorithm:

Step 1 - Read the search element from the user.

Step 2 - Compare the search element with first key 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 key value.

Step 5 - If search element is smaller, then continue the search process in left sub-
tree.

Step 6 - If search element is larger, then compare the search element with next key
value in the same node and repeat steps 3, 4, 5 and 6 until we find the exact match
or until the search element is compared with last key value in the leaf node.

Step 7 - If the last key value in the leaf node is also not matched then display
"Element is not found" and terminate the function.

GIT-CSE-ADSA PAGE NO.50


GATES INSTITUTE OF TECHNOLOGY, GOOTY

 Example:

4.3.2. Insert Operation:


 In a B-Tree, a new element must be added only at the leaf node.
 The insertion operation is performed as follows:
Algorithm:
Step 1 - Check whether tree is Empty.

Step 2 - If tree is Empty, then create a new node with new key value and insert it into
the tree as a root node.

Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key
value is added using Binary Search Tree logic.

Step 4 - If that leaf node has empty position, add the new key value to that leaf node in
ascendingorder of key value within the node.

Step 5 - If that leaf node is already full, split that leaf node by sending middle value to
its parent node. Repeat the same until the sending value is fixed into a node.

Step 6 - If the splitting is performed at root node then the middle value becomes new
root node for the tree and the height of the tree is increased by one.

GIT-CSE-ADSA PAGE NO.51


GATES INSTITUTE OF TECHNOLOGY, GOOTY

 Example:

GIT-CSE-ADSA PAGE NO.52


GATES INSTITUTE OF TECHNOLOGY, GOOTY

GIT-CSE-ADSA PAGE NO.53


GATES INSTITUTE OF TECHNOLOGY, GOOTY

GIT-CSE-ADSA PAGE NO.54


GATES INSTITUTE OF TECHNOLOGY, GOOTY

4.3.3. Delete Operation:


 The delete operation has more rules than insert and search operations.
 The Deletion operation as follows: :
Algorithm:

Step-1: Run the search operation and find the target key in the nodes.

Note: Three conditions applied based on the location of the target key, as explained in
the following steps.

Step-2: If the target key is in the leaf node

Case-1: Target is in the leaf node, more than min keys.

 Deleting this will not violate the property of B Tree


Case-2: Target is in leaf node, it has min key nodes.

 Deleting this will violate the property of B Tree.


 Target node can borrow key from immediate left node, or immediate right node
(sibling)
 The sibling will say yes if it has more than minimum number of keys
 The key will be borrowed from the parent node, the max value will be
transferred to a parent, the max value of the parent node will be transferred to
the target node, and remove the target value
Case-3: Target is in the leaf node, but no siblings have more than min number of keys

 Search for key


 Merge with siblings and the minimum of parent nodes
 Total keys will be now more than min
 The target key will be replaced with the minimum of a parent node
Step-3: If the target key is in an internal node

 Either choose, in- order predecessor or in-order successor


 In case the of in-order predecessor, the maximum key from its left subtree will

GIT-CSE-ADSA PAGE NO.55


GATES INSTITUTE OF TECHNOLOGY, GOOTY

be selected
 In case of in-order successor, the minimum key from its right subtree will be
selected
 If the target key's in-order predecessor has more than the min keys, only then it
can replace the target key with the max of the in-order predecessor
 If the target key's in-order predecessor does not have more than min keys, look
for in-order successor's minimum key.
 If the target key's in-order predecessor and successor both have less than min
keys, then merge the predecessor and successor.
Step-4: If the target key is in a root node

 Replace with the maximum element of the in-order predecessor subtree


 If, after deletion, the target has less than min keys, then the target node will
borrow max value from its sibling via sibling's parent.
 The max value of the parent will be taken by a target, but with the nodes of the
max value of the sibling.

 Example:

GIT-CSE-ADSA PAGE NO.56


GATES INSTITUTE OF TECHNOLOGY, GOOTY

GIT-CSE-ADSA PAGE NO.57


GATES INSTITUTE OF TECHNOLOGY, GOOTY

GIT-CSE-ADSA PAGE NO.58


GATES INSTITUTE OF TECHNOLOGY, GOOTY

4.3.4. Advantages of B Tree:


1. Reduces the number of reads made on the disk
2. B Trees can be easily optimized to adjust its size (that is the number of child nodes)
according to the disk size
3. It is a specially designed technique for handling a bulky amount of data.
4. It is a useful algorithm for databases and file systems.
5. A good choice to opt when it comes to reading and writing large blocks of data.

4.3.5. Disadvantages of B Trees


1. B-tree has lower branching factor when compared to b+ tree, which increases tree
height and average number of disk seeks.
2. B-trees are not very efficient, when compared to other balanced trees, when they
reside in RAM.
3. B-tree is much more complicated than a hash table to implement.

4.3.6. Applications of B Trees:


1. 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.
2. Searching of data in larger unsorted data sets takes a lot of time but this can be
improved significantly with indexing using B tree.

GIT-CSE-ADSA PAGE NO.59

You might also like