ADA Module 3 - Full
ADA Module 3 - Full
of Algorithms
transform conquer
Balanced Search Trees
Introduction:
• Elements in left subtree are smaller than the element in the subtree’s
root, and elements in right subtree are greater than it.
2. Representation-Change
• 2-3 Trees: Each node can have 2 or 3 children.
• 2-3-4 Trees: Each node can have 2, 3, or 4 children.
• B-Trees: Generalization allowing a larger number of children per
node.
AVL Trees: Definition
An AVL tree is a binary search tree in which the balance factor of
every node, which is defined as the difference between the heights
of the node’s le and right subtrees, is either 0 or +1 or −1.
Types of rotations:
- Single
right rotation, or R-rotation
left rotation, or L-rotation
- Double
left-right rotation (LR-rotation)
right-left rotation (RL-rotation)
Construction of an AVL tree for the list
2) 5, 6, 8, 3, 2, 4, 7
3) C, O, M, P, U, T, E, R
5, 6, 8, 3, 2, 4, 7
2-3 Trees
A 2-3 tree is a tree that can have nodes of two kinds:
A 3-node contains two ordered keys K1 and K2 (K1 < K2) and has three
children.
The leftmost child has keys < K1,
the middle child has keys between K1 and K2, and
the rightmost child has keys > K2
NOTE: All leaf nodes in a 2-3 Tree should be in the same level.
Steps to solve using 2-3 Trees
• If the node has more than 1 key, then place them in order
- If there are 3 keys in the leaf, promote the middle
one as the parent.
- If the node with 3 keys is the root, split it and create
a new root with the middle element.
Solve 6, 5, 1, 9, 7, 8, 10
Heaps
A heap can be defined as a binary tree with keys assigned
to its nodes, one key per node, with the following two
conditions are met:
1. The shape property the binary tree is essentially
complete (or simply complete), i.e., all its levels are full
except possibly the last level, where only some rightmost
leaves may be missing.
2. The parental dominance or heap property the key in
each node is greater than or equal to the keys in its
children.
Heaps
Example:
1. Starting with the last parental node, the algorithm checks whether the
parental dominance holds for the key in this node.
2. If it does not, the algorithm exchanges the node’s key K with the
larger key of its children and checks whether the parental dominance
holds for K in its new position. This process continues until the
parental dominance for K is satisfied.
3. After completing the “heapification” of the sub-tree rooted at the
current parental node, the algorithm proceeds to do the same for the
node’s immediate predecessor.
4. The algorithm stops after this is done for the root of the tree.
Example:
Figure: Bottom-up construction of a heap for the list 2, 9, 7, 6, 5, 8. The double headed
arrows show key comparisons verifying the parental dominance
Solve: 2, 7, 6, 5, 10, 8, 3
Construction of Heap
// left child
(right child)
// if right child is larger
// If the current value v >= largest child
= 2n
In Summary: