dsa unit 4
dsa unit 4
Unit – IV
Search Trees
This PDF is watermarked and traceable. Unauthorized sharing will result in
permanent access ban and legal action.
1. Search Trees
Search trees are a type of data structure used to
store data in a hierarchical form to allow for
efficient searching, inserting, deleting, and traversal of elements.
A symbol table is a data structure used mainly by compilers. It keeps track of all identifiers (like
variable names, function names, etc.) used in a program, and stores related information such as:
Why is it Important?
The symbol table handles all of this — allowing the compiler to track and manage
identifiers efficiently.
Example structures:
Example structure:
o AVL Tree
1. Fast Lookup : Helps the compiler quickly find identifiers during compilation.
3 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
3. Scope Handling : Manages variable scopes and avoids errors like multiple or implicit
declarations.
Steps:
o Name
o Type
o Scope
Steps:
4. Return its data (type, memory, etc.), or give an error if not found.
4 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
A static tree table is a type of search tableimplemented using tree structures where:
The data (identifiers) does not change after the table is created.
So, once the tree is built, no insertion or deletion is allowed during execution — only
searching is performed.
From above two BSTs if we want to search a node A, then we compare key value A with 3
nodes, in case of Tree 1. But for Tree 2, to search node A will require 2 comparisons only. This
shows that arrangement of nodes is an important factor in searching. It is therefore necessary
to compute cost of a tree. The formula for computing the cost of a tree is:
(1×3)+(1×2)+(1×1)
= =2
3
Cost of Tree 1 = 2.
=1.66
Optimal Binary Search Tree (OBST): Tree built to minimize average search cost based on
frequency of use
Huffman Tree: Used for compression (e.g., zip files) based on symbol frequency
It’s called “dynamic” because the data can change at any time — unlike static tree tables
where data is fixed after construction.
Where is it Used?
6 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
How is it Implemented?
In simple words: It arranges keys in such a way that the most frequently searched items
appear closer to the root, reducing the average search time.
An Optimal Binary Search Tree (OBST) is a special type of weight-balanced tree where:
The tree is built in a way that minimizes the total search cost.
Why OBST?
If we place the frequently used keys nearer the root, searching becomes faster
overall.
Let’s assume that we have the following keys in our tree: 15, 25, 35, 45, 55, 65, 75
The maximum time required to search a node in a binary search tree is equal to the minimum
height of the tree i.e. log n.
If we have keys: 15, 25, 35, then see how many BSTs can be formed. We will use the formula
given below:
1 2𝑛
Number of BSTs=𝐶= 𝑛 + 1 ൬ ൰
𝑛
1 6 1
Number of BSTs=𝐶ଷ= ൬൰= ∙20=5
3 + 1 3 4
So, we can see that 5 trees will be formed. Now to find average number of comparisons:
Case 1:
1+2+3
Avg.number of comparisons= 3 =2
Case 2:
ଵ ାଶା ଷ
Avg.number of comparisons= =2
ଷ
8 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Case 3:
1 + 2 + 2 5
Avg.number of comparisons= =
3 3
Case 4:
1+2+3
Avg.number of comparisons= 3 =2
Case 5:
1+2+3
Avg.number of comparisons= 3 =2
DP Table Setup
Let:
n = number of keys
Algorithm Steps:
Example: Consider the below table, which contains the keys and frequencies.
Now to clculate the cost, we will consider only the jth value.
The cost of c[0,1] is 4 (The key is 10, and the cost corresponding to key 10 is 4).
The cost of c[1,2] is 2 (The key is 20, and the cost corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost corresponding to key 40 is 3)
As i < k ≤ j
To find c[0,2], k = 1, 2
= min{0 + 2, 4 + 0} = min{2, 4} + 6
= 2 + 6 = 81
To find c[1,3], k = 2, 3
11 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
= min{0 + 6, 2 + 0} = min{6, 2} + 8
= 6 + 8 = 103
To find c[2,4], k = 3, 4
= min{0 + 3, 6 + 0} = min{3, 6} + 9
= 3 + 9 = 123
To find c[0,3], k = 1, 2, 3
= 8 + 12 = 203
To find c[1,4], k = 2, 3, 4
= 5 + 11 = 163
12 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
To find c[0,4], k = 1, 2, 3, 4
= 11 + 15 = 263
A Height Balanced Tree is a binary tree where the height difference (also called balance
factor) between the left and right subtrees ofevery node is at most 1.
In normal Binary Search Trees (BST), if elements are inserted in sorted order, the tree
becomes skewed (like a linked list), and searching takes O(n) time.
2. Red-Black Tree
An AVL tree is defined as a self-balancing Binary Search Tree (BST) where the difference
between heights of left and right subtrees for any node cannot be more than one.
The absolute difference between the heights of the left subtree and the right subtree for
any node is known as the balance factor of the node. The balance factor for all nodes
must be less than or equal to 1.
Every AVL tree is also a Binary Search Tree (Left subtree values are smaller and right
subtree values are greater for every node), but every BST is not an AVL Tree.
The main advantage of an AVL Tree is, the time complexities of all operations (search,
insert and delete, max, min, floor and ceiling) become O(log n). This happens because
height of an AVL tree is bounded by O(log n). In case of a normal BST, the height can
go up to O(n).
An AVL tree maitains its height by doing some extra work during insert and delete
operations. It mainly uses rotations to maintain both BST properties and height balance.
There exist other self-balancing BSTs also like Red Black Tree. Red Black tree is more
complex, but used more in practice as it is less restrictive in terms of left and right
subtree height differences.
The balance factors for different nodes are: 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all
differences are less than or equal to 1, the tree is an AVL tree.
Searching
: It is the same as a normal Binary Search Tree (BST) as an AVL Tree is always a BST. So, we
can use the same implementation as BST. The advantage here is time complexity is O(log n).
15 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Insertion: It does rotations along with normal BST insertion to make sure that the balance
factor of the impacted nodes is less than or equal to 1 after insertion
Deletion: It also does rotations along with normal BST deletion to make sure that the balance
factor of the impacted nodes is less than or equal to 1 after deletion.
Example:
Replace 14 with its inorder succssor i.e. 18, and delete 14.
If we encounter unbalancing in subtree then we balance that subtree using appropriate single
or double rotations.
An AVL tree may rotate in one of the following four ways to keep itself balanced while making
sure that the BST properties are maintained.
Left Rotation:
When a node is added into the right subtree of the right subtree, if the tree gets out of balance,
we do a single left rotation.
16 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Algorithm/Pseudocode:
Node* leftRotate(Node* x) {
Node* y = x->right; // Step 1: y is x's right child
Node* T2 = y->left; // Step 2: T2 stores y's left subtree
Right Rotation:
If a node is added to the left subtree of the left subtree, the AVL tree may get out of balance,
we do a single right rotation.
Algorithm/Pseudocode:
17 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Node* rightRotate(Node* y) {
Node* x = y->left; // Step 1: x is y's left child
Node* T2 = x->right; // Step 2: T2 stores x's right subtree
Left-Right Rotation:
A left-right rotation is a combination in which first left rotation takes place after that right
rotation executes.
Algorithm/Pseudocode:
Node* leftRightRotate(Node* z) {
// Step 1: Store pointer to left child of z
Node* x = z->left;
Right-Left Rotation:
A right-left rotation is a combination in which first right rotation takes place after that left
rotation executes.
Algorithm/Pseudocode:
Node* rightLeftRotate(Node* z) {
// Step 1: Store pointer to right child of z
Node* x = z->right;
19 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Example: Obtain AVL Tree for the following data: 30, 50, 110, 80, 40, 10, 120, 60, 20, 70,
100, 90.
Step 10:
Insert 70.
21 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Example: Obtain an AVL Tree by accepting the following numbers (keys) one at a time. Show
balance factor of each node, the type of rotation and the transformation required for each
insertion in AVL Tree. 30, 31, 32, 23, 22, 28, 24, 29, 26, 27, 34, 36.
1. AVL trees can self-balance themselves and provide time complexity of O(log n) for
search, insert and delete.
3. Since the balancing rules are strict compared to Red Black Tree, AVL trees in general
have relatively less height and hence the search is faster.
4. AVL tree is relatively less complex to understand and implement compared to Red
Black Trees.
1. It is difficult to implement compared to normal BST and easier compared to Red Black.
2. Less used compared to Red-Black trees. Due to its rather strict balance, AVL trees
provide complicated insertion and removal operations as more rotations are performed.
1. AVL Tree is used as a first example self-balancing BST in teaching DSA as it is easier
to understand and implement compared to Red Black.
2. Applications, where insertions and deletions are less common but frequent data lookups
along with other operations of BST like sorted traversal, floor, ceil, min and max.
3. Red Black tree is more commonly implemented in language libraries like map in
C++, set in C++, TreeMap in Java and TreeSet in Java.
4. AVL Trees can be used in a real time environment where predictable and consistent
performance is required.
4. RED nodes cannot have RED children (No two reds in a row)
5. Every path from a node to its descendant NULL nodes must have the same number
1. Recoloring
2. Rotation
Recoloring is the change in color of the node i.e. if it is red then change it to black and vice
versa. It must be noted that the color of the NULL node is always black. Moreover, we always
try recoloring first, if recoloring doesn’t work, then we go for rotation. Following is a detailed
algorithm. The algorithms have mainly two cases depending upon the color of the uncle. If the
uncle is red, we do recolor. If the uncle is black, we do rotations and/or recoloring.
First, we have to insert the node similarly to that in a binary tree and assign a red color to it.
Now, if the node is a root node, then change its color to black, but if it is not, then check the
color of the parent node. If its color is black then don’t change the color but if it is not i.e. it is
red then check the color of the node’s uncle. If the node’s uncle has a red color, then change
the color of the node’s parent and uncle to black and that of grandfather to red color and
repeat the same process for him (i.e. grandfather). If grandfather is root, then don’t change
grandfather to red color.
But if the node’s uncle has black color, then there are 4 possible cases:
Step 1: Insert 2. Initially the red-black tree is empty. The newly inserted node should always
be red.
26 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Step 2: Insert 1. Make node for value 1 and it should be red, as it is a newly inserted node.
Since 1 < 2, attach this red node as left child of 2.
Step 3: Insert 4. Make node 4 as red node and attach it as right child of 2.
Step 4: Insert 5. Here, node 5 is a red node attached as a right child of red node 4. But there
should not be a red child of a red node. So, we make adjustments.
Step 6: Insert 3. Create a new node which is red in color and whose value is 3. Attach it as left
child of 4.
Step 7: Insert 6. Create a new node which is red in color and whose value is 6. Attach this node
as a left child of 9.
28 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Step 8: Insert 7. Create a new node which is red in color and whose value is 7. Attach this node
as right child of node 6.
Deletion in a red-black tree is a bit more complicated than insertion. When a node is to be
deleted, it can either have no children, one child or two children.
1. If the node to be deleted has no children, simply remove it and update the parent node.
2. If the node to be deleted has only one child, replace the node with its child.
3. If the node to be deleted has two children, then replace the node with its in-order
successor, which is the leftmost node in the right subtree. Then delete the in-order
successor node as if it has at most one child.
4. After the node is deleted, the red-black properties might be violated. To restore these
properties, some color changes and rotations are performed on the nodes in the tree.
The changes are similar to those performed during insertion, but with different
conditions.
5. The deletion operation in a red-black tree takes O(log n) time on average, making it a
good choice for searching and deleting elements in large data sets.
Deletion is a fairly complex process. To understand deletion, the notion of double black is
used. When a black node is deleted and replaced by a black child, the child is marked as double
black. The main task now becomes to convert this double black to single black. Following are
detailed steps for deletion.
1) Perform standard BST deletion. When we perform standard delete operation in BST, we
always end up deleting a node which is an either leaf or has only one child (For an internal
node, we copy the successor and then recursively call delete for successor, successor is always
30 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
a leaf node or a node with one child). So, we only need to handle cases where a node is leaf or
has one child. Let v be the node to be deleted and u be the child that replaces v (Note that u is
NULL when v is a leaf and color of NULL is considered as Black).
2) Simple Case: If either u or v is red, we mark the replaced child as black (No change in
black height). Note that both u and v cannot be red as v is parent of u and two consecutive reds
are not allowed in red-black tree.
3.1) Color u as double black. Now our task reduces to convert this double black to single black.
Note that if v is a leaf node, then u is NULL and color of NULL is considered black. So, the
deletion of a black leaf also causes a double black.
3.2) Do following while the current node u is double black, and it is not the root. Let sibling of
node be s.
(a): If sibling s is black and at least one of sibling’s children is red, perform rotation(s). Let
the red child of s be r. This case can be divided in four subcases depending upon positions of
s and r.
31 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
(i) Left Left Case (s is left child of its parent and r is left child of s or both children of s are
red). This is mirror of right right case shown in below diagram.
(ii) Left Right Case (s is left child of its parent and r is right child). This is mirror of right left
case shown in below diagram.
(iii) Right Right Case (s is right child of its parent and r is right child of s or both children of
s are red)
(iv) Right Left Case (s is right child of its parent and r is left child of s)
(b): If sibling is black and its both children are black , perform recoloring, and recur for the
parent if parent is black.
32 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
In this case, if parent was red, then we didn’t need to recur for parent, we can simply make it
black (red + double black = single black)
(c): If sibling is red, perform a rotation to move old sibling up, recolor the old sibling and
parent. The new sibling is always black (See the below diagram). This mainly converts the tree
to black sibling case (by rotation) and leads to case (a) or (b). This case can be divided in two
subcases.
(i) Left Case (s is left child of its parent). This is mirror of right right case shown in below
diagram. We right rotate the parent p.
(ii) Right Case (s is right child of its parent). We left rotate the parent p.
33 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
1.9 AA tree
An AA Tree is a simplified version of a Red-Black Tree. It maintains balance using a single value
called "level" instead of using colors like red and black. It enforces strict rules to keep the tree
balanced and operations efficient.
Key Concepts
o Key (data)
Only right horizontal links are allowed (no left horizontal links)
AA Tree Properties
4. No two consecutive right horizontal links (i.e., if node A → B → C are all right
children, it violates the rule)
Advantages of AA Trees
34 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
1. Simpler than Red-Black Trees : Only uses one field (level) and two operations: skew
& split
4. Fewer rotations: Only needs right (skew) and left (split) rotations
Insertion in AA Trees
If the parent and its child are on the same level then horizontal links are possible.
There are some restrictios on horizontal links. These restrictions are for avoiding unbalancing
in AA trees. Following figure represents the restrictions on horizontal links and remedy on
violation.
If the parent and its child are on the same level then horizontal links are possible.
There are some restrictions on horizontal links. These restrictions are for avoiding unbalancing
in AA trees. Following figure represents the restrictions on horizontal links and remedy on
violation.
1) Skew Operation: The skew operation is a single right rotation when an insertion creates
horizontal link.
35 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
2) Split Operation: The split operation is single left rotation when insertion creates two
horizontal links.
We insert the elements one by one. We apple skew and split operations when horizontal links
cause unbalancing in the tree.
Step 1: Insert 8.
Step 2: Insert 3.
36 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
But since the right left child is not allowed in AA tree, we apply skew operation to balance the
AA Tree.
Here two consecutive horizontal links occur. This is not allowed. Hence, we apply split
operation. The resultant tree will be
37 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Again, left link from 18 to 12 is not allowed. Hence, we apply skew operation. The balanced
AA tree will then be
38 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
But still two horizontal links are not allowed. Hence, we apply split operation.
Step 6: Insert 2.
The left horizontal link is not allowed. Hence, we apply skew operation.
Deletion in AA Tree
This deletion may cause unbalancing in AA tree which can be rebalanced with the help of skip
and skew operations.
Following are the rules that can be applied to rebalance the AA tree.
Rule 2: Skew the level of the node whose level was decreased.
39 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Skew the subtree from the root node, where the decremented node is root.
Skew the root node's right child.
Skew the root node's right-right child.
Rule 3: Split the level of the node whose level was decreased.
Step 2: Decrease the level of root, i.e. 8 and its right child, i.e. 16.
That means make 8 as parent of 12 and 12 as parent of 16. Attach 14 as left child of 16.
Step 4: Skew at root’s right right. That means, focus at node 16.
In this skew operation, 14 becomes parent of 16. And the right child of 14 which was actually
15 becomes left child of 16.
Step 5: Now we get consecutive horizontal red links. To balance such AA tree, we will perform
split operation.
41 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
A K-Dimensional Tree (KD-Tree) is a binary tree used to store k-dimensional points (like
(x, y), (x, y, z), etc.) and perform efficient searches like:
Range searches
It's like a Binary Search Tree (BST), but for multiple dimensions.
Example: Draw a K-Dimensional tree for following points. (5,8), (18,14), (12,14), (8,11),
(10,2), (3,6), (11,20).
Step 1: We will read the point (5,8) and make it the root node. The root is said to be x-aligned.
Step 2: Now, the next point is (18,14). As root is x aligned, we will compare x value of (5,8)
with x value of (18,14). That means compare 18 with 5. As 18 > 5, make (18,14) the right child
of (5,8).
Step 3: Now read (12,14). We start from root node. The x-value of (5,8) is compared with x-
value of (12,14).
Here (18,14) is y-aligned. Hence, the y-value of (18,14) is compared with y-value of (12,14).
As 14=14 move on to right branch and attach (12,14) as right child of (18,14).
43 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Step 4: The next point is (8,11). Starting from root, we will compare x and y values of (8,11)
alternatively and attach (8,11) as left child of (18,14). The partial construction is as follows:
ii) For point (3,6), Y=6. Hence draw a horizontal line at Y=6. But as (3,6) is a left child of child
of (5,8), draw this line at the left of x=5 line.
44 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
In this way, the lines X and Y can be drawn to divide the space. The K-D space spotting is as
shown below:
2. Good for Nearest Neighbor & Range Queries: Helps in quickly finding nearby points
or points in a region.
3. Simple & Structure: Easier to implement than other spatial data structures like R-
trees.
A Splay Tree is a type of self-adjusting binary search tree (BST). It reorganizes itself every
time you access a node — by moving the accessed node to the root .
What is Splaying?
Splaying is a series of rotations (like in AVL) that moves a node to the root of the tree.
This helps frequently accessed nodesstay near the top, making future accesses faster.
TheZig
1) ZigRotation:
Rotation in splay trees operates in a manner similar to the single right rotation in AVL
Tree rotations. This rotation results in nodes moving one position to the right from their
current location. For example, consider the following scenario:
46 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
2) Zag Rotation: The Zag Rotation in splay trees operates in a similar fashion to the single left
rotation in AVL Tree rotations. During this rotation, nodes shift one position to the left from
their current location. For instance, consider the following illustration:
3) Zig-Zig Rotation: The Zig-Zig Rotation in splay trees is a double zig rotation. This rotation
results in nodes shifting two positions to the right from their current location. Take a look at
the following example for a better understanding:
4) Zag-Zag Rotation: In splay trees, the Zag-Zag Rotation is a double zag rotation. This
rotation causes nodes to move two positions to the left from their present position. For example:
47 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
5) Zig-Zag Rotation: The Zig-Zag Rotation in splay trees is a combination of a zig rotation
followed by a zag rotation. As a result of this rotation, nodes shift one position to the right and
then one position to the left from their current location. The following illustration provides a
visual representation of this concept:
6) Zag-Zig Rotation: The Zag-Zig Rotation in splay trees is a series of zag rotations followed
by a zig rotation. This results in nodes moving one position to the left, followed by a shift one
position to the right from their current location. The following illustration offers a visual
representation of this concept:
Step 2: Insert 2.
Step 3: Insert 4.
Step 4: Insert 6.
Step 5: Insert 8.
If we want to delete the node 8, we just copy the in-order successor of node 8. As there is no
in-order successor of 8, we delete 8. Then the parent node of the deleted node, i.e. 6 becomes
root node, by applying zig operation.
50 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.
Delete 12: Now to delete 12, we copy 15 at 12's place. And then allow 10 (i.e. parent of 12) to
be the root node. Then we get:
1. No Extra Storage Needed : Doesn’t require height or color info like AVL or Red-Black
trees.
2. Fast Access to Frequently Used Nodes: Recently accessed nodes are moved to the
root.
4. Network Routing Tables : Access patterns tend to repeat, benefiting from splaying.