Unit-1 Complete CSP
Unit-1 Complete CSP
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.
Theta notation
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.
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.
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.
The size of the input data is the primary Primarily determined by the
determinant. auxiliary variable size
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.
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.
Algorithm:
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:
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 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.
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:
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.
Example:
Step 2 - If it has only one child then replace the node with its child.
Example:
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 7 - Repeat the same process until the node is deleted from the tree.
Example:
in finding the smallest free chunk with size greater than or equal to size specified in
allocation request.
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...,
𝑂(log 𝑛).
AVL Tree is inverted by G.M. Adelson-Velski and E.M. Landis.
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:
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:
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:
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:
Example:
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.
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.
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.
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:
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 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.
Example:
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.
Example:
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.
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
Example: