UNIT 3 - R23
UNIT 3 - R23
Algorithms
UNIT 3
Result: C=
The Closest-Pair •
•
Find the closest pair in the LEFT (dl)
Find the closest pair in the RIGHT (dr)
using merge sort the points that are right to the median.
● https://www.gorillasun.de/blog/quickh
ull-algorithm-for-convex-hulls/
Instance-Simplification Approach:
• This approach involves transforming an
unbalanced binary search tree into a
balanced one, hence termed self-
balancing trees.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Introduction to 2-3 Trees:
• A 2-3 tree is a type of balanced search Node Structure and Property:
tree that allows nodes to contain either • In a 2-node, the left child contains
one or two keys. keys smaller than the node's key, and
the right child contains keys larger
• Introduced by John Hopcroft in 1970, it than the node's key.
addresses the issue of unbalanced
trees by maintaining perfect height • In a 3-node, the left child contains
balance. keys smaller than the first key, the
middle child contains keys between
• Nodes in a 2-3 tree can be of two
the first and second keys, and the
types: 2-nodes containing one key and
right child contains keys larger than
two children, and 3-nodes containing
the second key.
two keys and three children.
Generalization to B-Trees:
4. B-trees are a significant generalization of
2-3 trees, allowing for more keys per
node and greater flexibility in tree
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept structure.
An AVL tree is a type of binary search
AVL Trees
tree (BST) that maintains its balance to
ensure efficient search, insertion, and
deletion operations. It was invented in
1962 by two Russian scientists, G. M.
Adelson-Velsky and E. M. Landis —
that's where the name AVL comes from.
1. An AVL tree is a binary search tree When a tree becomes unbalanced, it can be
(BST) — meaning: fixed using four types of rotations:
1. All values in the left subtree of a 1. Single Right Rotation (R-rotation):
node are smaller than the node's 1. Used when the node’s left subtree is
value. heavier than the right subtree.
2. All values in the right subtree of a 2. Single Left Rotation (L-rotation):
node are greater than the node's 1. Used when the node’s right subtree is
value. heavier than the left subtree.
3. Left-Right Rotation (LR-rotation):
2. The difference in height between the left 1. Used when the right subtree of the
and right subtrees of any node (called left child causes imbalance.
the balance factor) is always between - 2. First, left-rotate the left child → Then,
1, 0, or +1. right-rotate the root.
1. Balance factor = Height of left 4. Right-Left Rotation (RL-rotation):
subtree – Height of right subtree 1. Used when the left subtree of the
2. If the balance factor becomes +2 or right child causes imbalance.
-2 after an insertion or deletion, the 2. First, right-rotate the right child →
tree becomes unbalanced and needs Then, left-rotate the root.
to be fixed by rotation.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Example: Balance Factor
Balance factor = Height of left subtree –
Height of right subtree 25
10
22 30
5 15
20
2 9 20 28 40
12
3 17 30 5
5
30 75 Left Rotation 30 80
80
75 92
92
65
65
90 20 80
20
Right Rotation
80
72 90
72
3 3
3
1 1
2
1 3
2 2
After LR Rotation
Tree is balanced
Tree is imbalanced 1
LL Rotation
because node 3 has balance factor 2
RR Rotation
1 1 1
2
3 3 2
1 3
2 2 3
After RL Rotation
LL Rotation Tree is balanced
RR Rotation
Tree is imbalanced
because node 1 has balance factor -2
Heap Sort
heap) n−1 times, resulting in a
sorted array.
2. Heap Construction:
1. In Stage 1, a heap is constructed
from the given array using the
bottom-up heap construction
algorithm.
2. This stage has a time complexity of
O(n), where n is the number of
elements in the array.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept Two Approaches: Left to Right, Right to Left
Right-to-Left Binary Exponentiation
Left-to-Right binary exponentiation
Algorithm:
Algorithm:
Problem Reduction
complex task or a challenge that you're facing.