Unit 4 Search Tree 2024 Compressed
Unit 4 Search Tree 2024 Compressed
Prepared By
Prof. Anand N. Gharu
(Assistant Professor)
Computer Dept.
CLASS : SE COMPUTER 2019 15 March 2024
SUBJECT : DSA (SEM-II)Note: The material to prepare this presentation has been taken from internet and are generated only
1
.UNIT : IV .
for students reference and not for commercial use
SYLLABUS
Symbol Table-Representation of Symbol Tables- Static
tree table and Dynamic tree table, Weight balanced
tree - Optimal Binary Search Tree (OBST), OBST as
an example of Dynamic Programming, Height
Balanced Tree- AVL tree. Red-Black Tree, AA tree,
K-dimensional tree, Splay Tree
UNIT-IV
SEARCH
TREE
SYMBOL TABLE
SYMBOL TABLE MEANS
PHASES OF COMPILER
Preprocessor
Compiler
Assembler
Linker
Parameter
Constant
NAME Recor
d
RecordField
Procedure
Array and files
10
• We know the key values of each node in the tree, and we also
know the frequencies of each node in terms of searching means
how much time is required to search a node.
• The frequency and key-value determine the overall cost of
searching a node. The cost of searching is a very important factor
in various applications.
Optimal Binary Search Tree
• The overall cost of searching a node should be less.
tree.(OBST)
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
• Given: k1<k2<k3<k4<k5
Tree 1:
k2/[k1,k4]/[nil,nil],[k3,k5]
Tree 2:
k2/[k1,k5]/[nil,nil],[k4,nil]/[nil,nil],[nil,nil],[k3,nil],[nil,nil]
Algorithm for finding optimal tree for sorted, distinct keys ki…kj:
For each possible root kr for i≤r≤j
Make optimal subtree for ki,…,kr−1
Make optimal subtree for kr+1,…,kj
Select root that gives best total tree
Formula: e(i,j) = expected number of comparisons for optimal tree for keys ki…kj
e(i,j)={0, if i=j+1mini≤r≤j{e(i,r−1)+e(r+1,j)+w(i,j)}, if i≤j
where w(i,j)=∑k=ijpi is the increase in cost if ki…kj is a subtree of a node
Work bottom up and remember solution
General formula for calculating the minimum cost is:
C[i,j] = min{c[i, k-1] + c[k,j]} + w(i,j)
Optimal Binary Search Tree
https://www.javatpoint.com/optimal-binary-search-tree
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
3) r(i,i)=0
4) w(i,j)=p(j)+q(j)+w(i,j-1)
6) r(i,j)=k
7) r(i,i+1)=i+1
OBST Example
OBST Example
Solution:-
This computation is carried out row-wise from row 0 to row 4. Initially, W (i,
i) = Q
(i) and C (i, i) = 0 and R (i, i) = 0, 0 < i < 4.
Start with i = 0; so j = 1; as i < k ≤ j, so the possible value for k = 1
W (0, 1) = P (1) + Q (1) + W (0, 0) = 3 + 3 + 2 = 8
C (0, 1) = W (0, 1) + min {C (0, 0) + C (1, 1)} = 8
R (0, 1) = 1 (value of 'K' that is minimum in the above equation).
Next with i = 1; so j = 2; as i < k ≤ j, so the possible value for k = 2
W (1, 2) = P (2) + Q (2) + W (1, 1) = 3 + 1 + 3 = 7
C (1, 2) = W (1, 2) + min {C (1, 1) + C (2, 2)} = 7
R (1, 2) = 2
Next with i = 2; so j = 3; as i < k ≤ j, so the possible value for k = 3
W (2, 3) = P (3) + Q (3) + W (2, 2) = 1 + 1 + 1 = 3
C (2, 3) = W (2, 3) + min {C (2, 2) + C (3, 3)} = 3 + [(0 + 0)] = 3
R (2, 3) = 3
• Properties
• Follows all properties of the tree data structure.
• Self-balancing.
• Each node stores a value called a balanced factor, which is
the difference in the height of the left sub-tree and right
sub-tree.
• All the nodes in the AVL tree must have a balance factor of
-1, 0, and
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962.
The tree is named AVL in honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which
each node is associated with a balance factor which is calculated by
subtracting the height of its right sub-tree from that of its left sub-tree.
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations. For a tree to be unbalanced,
minimum height must be at least 2, Let us understand each rotation
AVL Rotation
1. RR Rotation :
When BST becomes unbalanced, due to a node is inserted into the right
subtree of the right subtree of A, then we perform RR rotation, RR
rotation is an anticlockwise rotation, which is applied on the edge below a
node having balance factor -2
When BST becomes unbalanced, due to a node is inserted into the left
subtree of the left subtree of C, then we perform LL rotation, LL rotation
is clockwise rotation, which is applied on the edge below a node having
balance factor 2
Double rotations are bit tougher than single rotation which has already
explained above. LR rotation = RR rotation + LL rotation, i.e., first RR
rotation is performed on subtree and then LL rotation is performed on full
tree, by full tree we mean the first node from the path of inserted node
whose balance factor is other than -1, 0, or 1.
AVL Rotation
3. LR Rotation :
State Action
A node B has been inserted into the right subtree of A the left
subtree of C, because of which C has become an unbalanced node
having balance factor 2. This case is L R rotation where: Inserted
node is in the right subtree of left subtree of C
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
2. Insert B, A
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
3. Insert E 3 a) We first perform RR rotation on node B
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
3b) We first perform LL rotation on the node I
The resultant balanced tree after LL rotation is:
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
4. Insert C, F, D 4a) We first perform LL rotation on node E
The resultant tree after LL rotation is:
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
4. Insert C, F, D 4b) We then perform RR rotation on node B
The resultant balanced tree after RR rotation is:
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
5. Insert G 5 a) We first perform RR rotation on node C
The resultant tree after RR rotation is:
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
5. Insert G 5 b) We then perform LL rotation on node H
The resultant balanced tree after LL rotation is:
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
6. Insert K
AVL Tree Example
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
7. Insert L
AVL Tree Example
AVL Tree Example
AVL Tree Example
AVL Tree Example
Red Black Tree
Red-Black tree is a self-balancing binary search tree in
which each node contains an extra bit for denoting the
color of the node, either red or black.
First, you have to insert the node similarly to that in a binary tree and
assign a red colour to it. Now, if the node is a root node then change
its colour to black, but if it is not then check the colour of the parent
node. If its colour is black then don’t change the colour but if it is not
i.e. it is red then check the colour of the node’s uncle. If the node’s
uncle has a red colour then change the colour of the node’s parent
and uncle to black and that of grandfather to red colour and repeat
But, if the node’s uncle has black colour then there are 4 possible cases:
Red Black Tree Operations
Left Left Case (LL rotation):
Red Black Tree Operations
Left Right Case (LR rotation):
Red Black Tree Operations
Right to Right Case (RR rotation):
Red Black Tree Operations
Right Left Case (RL rotation):
Now, after these rotations, if the colours of the nodes are miss matching then recolour
them.
ALGORITHMS OF RBT
Algorithm:
Let x be the newly inserted node.
Perform standard BST insertion and make the colour of newly inserted nodes as RED.
If x is the root, change the colour of x as BLACK (Black height of complete tree increases by
1).
Do the following if the color of x’s parent is not BLACK and x is not the root.
a) If x’s uncle is RED (Grandparent must have been black from property 4)
(i) Change the colour of parent and uncle as BLACK.
(ii) Colour of a grandparent as RED.
(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s
grandparent (g) (This is similar to AVL Tree)
(i) Left Left Case (p is left child of g and x is left child of p)
(ii) Left Right Case (p is left child of g and x is the right child of p)
(iii) Right Right Case (Mirror of case i)
(iv) Right Left Case (Mirror of case ii)
APPLICATION OF RBT
1. Most of the self-balancing BST library functions
like map and set in C++ (OR TreeSet and TreeMap
in Java) use Red-Black Tree.
2. It is used to implement CPU Scheduling Linux.
Completely Fair Scheduler uses it.
3. Besides they are used in the K-mean clustering
algorithm for reducing time complexity.
4. Moreover, MySQL also uses the Red-Black tree for
indexes on tables.
EXAMPLE OF RBT
Example: Creating a red-black tree with elements 3, 21, 32 and 15 in an
empty tree.
:
When the first element is inserted it is inserted as a root node and as root node
has black colour so it acquires the colour black.
The new element is always inserted with a red colour and as 21 > 3 so it becomes
the part of the right subtree of the root node.
EXAMPLE OF RBT
Example: Creating a red-black tree with elements 3, 21, 32 and 15 in an
empty tree.
:
Now, as we insert 32 we see there is a red father-child pair which violates the Red-
Black tree rule so we have to rotate it. Moreover, we see the conditions of RR rotation
(considering the null node of the root node as black) so after rotation as the root node
can’t be Red so we have to perform recolouring in the tree resulting in the tree shown
above.
EXAMPLE OF RBT
Example: Creating a red-black tree with elements 3, 21, 32 and 15 in an
empty tree.
:
FINAL TREE :
EXAMPLE OF RBT
Insertion in Red Black tree :
The following are some rules used to create the Red-Black tree:
1. If the tree is empty, then we create a new node as a root node with the color
black.
2. If the tree is not empty, then we create a new node as a leaf node with a color
red.
3. If the parent of a new node is black, then exit.
4. If the parent of a new node is Red, then we have to check the color of the
parent's sibling of a new node.
4a) If the color is Black, then we perform rotations and recoloring.
4b) If the color is Red then we recolor the node. We will also check whether the
parents' parent of a new node is the root node or not; if it is not a root node, we will
recolor and recheck the node
http://www.btechsmartclass.com/data_structures/red-black-trees.html
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
Step 1: Initially, the tree is empty, so we create a new node having value 10. This is
the first node of the tree, so it would be the root node of the tree. As we already
discussed, that root node must be black in color, which is shown below:
Step 2: The next node is 18. As 18 is greater than 10 so it will come at the right of 10
as shown below.
We know the second rule of the Red Black tree that if the tree is not empty then the newly
created node will have the Red color. Therefore, node 18 has a Red color, as shown in the
below figure:
Now we verify the third rule of the Red-Black tree, i.e., the parent of the new node is black
or not. In the above figure, the parent of the node is black in color; therefore, it is a Red-
Black tree.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
Step 3: Now, we create the new node having value 7 with Red color. As 7 is less than
10, so it will come at the left of 10 as shown below
Now we verify the third rule of the Red-Black tree, i.e., the parent of the new node is
black or not. As we can observe, the parent of the node 7 is black in color, and it obeys
the Red-Black tree's properties..
Step 4: The next element is 15, and 15 is greater than 10, but less than 18, so the new
node will be created at the left of node 18. The node 15 would be Red in color as the
tree is not empty.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
The above tree violates the property of the Red-Black tree as it has Red-red parent-
child relationship. Now we have to apply some rule to make a Red-Black tree. The
rule 4 says that if the new node's parent is Red, then we have to check the color of the
parent's sibling of a new node. The new node is node 15; the parent of the new node is
node 18 and the sibling of the parent node is node 7. As the color of the parent's
sibling is Red in color, so we apply the rule 4a. The rule 4a says that we have to
recolor both the parent and parent's sibling node. So, both the nodes, i.e., 7 and 18,
would be recolored as shown in the below figure.
We also have to check whether the parent's parent of the new node is the root node or
not. As we can observe in the above figure, the parent's parent of a new node is the
root node, so we do not need to recolor it.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
Step 5: The next element is 16. As 16 is greater than 10 but less than 18 and greater
than 15, so node 16 will come at the right of node 15. The tree is not empty; node 16
would be Red in color, as shown in the below figure:
In this, it violates the property of the parent-child
relationship as it has a red-red parent-child relationship.
We have to apply some rules to make a Red-Black tree.
Since the new node's parent is Red color, and the parent
of the new node has no sibling, so rule 4a will be
applied. The rule 4a says that some rotations and
recoloring would be performed on the tree.
Since node 16 is right of node 15 and the parent of node 15
is node 18. Node 15 is the left of node 18. Here we have an
LR relationship, so we require to perform two rotations.
First, we will perform left, and then we will perform the
right rotation. The left rotation would be performed on nodes
15 and 16, where node 16 will move upward, and node 15
will move downward. Once the left rotation is performed,
the tree looks like as shown in the below figure:
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
In the above figure, we can observe that there is an LL
relationship. The above tree has a Red-red conflict, so
we perform the right rotation. When we perform the
right rotation, the median element would be the root
node. Once the right rotation is performed, node 16
would become the root node, and nodes 15 and 18
would be the left child and right child, respectively, as
shown in the below figure.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
After rotation, node 16 and node 18 would be recolored; the color of node
16 is red, so it will change to black, and the color of node 18 is black, so it
will change to a red color as shown in the below figure:
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
Step 6: The next element is 30. Node 30 is inserted at the right of node 18.
As the tree is not empty, so the color of node 30 would be red.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
The color of the parent and parent's sibling of a new node is Red, so rule 4b
is applied. In rule 4b, we have to do only recoloring, i.e., no rotations are
required. The color of both the parent (node 18) and parent's sibling (node
15) would become black, as shown in the below image..
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
We also have to check the parent's parent of the new node, whether it is a
root node or not. The parent's parent of the new node, i.e., node 30 is node
16 and node 16 is not a root node, so we will recolor the node 16 and
changes to the Red color. The parent of node 16 is node 10, and it is not in
Red color, so there is no Red-red conflict.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
Step 7: The next element is 25, which we have to insert in a tree. Since 25 is
greater than 10, 16, 18 but less than 30; so, it will come at the left of node
30. As the tree is not empty, node 25 would be in Red color. Here Red-red
conflict occurs as the parent of the newly created is Red color.
After recoloring, we also have to check the parent's parent of a new node, i.e., 25,
which is not a root node, so recoloring would be performed, and the color of node 25
changes to Red.
After recoloring, red-red conflict occurs between nodes 25 and 16. Now node 25
would be considered as the new node. Since the parent of node 25 is red in color, and
the parent's sibling is black in color, rule 4a would be applied. Since 25 is at the right
of the node 16 and 16 is at the right of its parent, so there is an RR relationship. In the
RR relationship, left rotation is performed. After left rotation, the median element 16
would be the root node, as shown in the below figure
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
As the color of parent and parent's sibling node of a new node is Red so recoloring
would be performed. The color of both the nodes would become black, as shown in the
below image.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
After rotation, recoloring is performed on nodes 16 and 10. The color of node 10 and
node 16 changes to Red and Black, respectively as shown in the below figure
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
Step 9: The next element is 60. Since 60 is greater than 16, 25, 30, and 40, so node 60
will come at the right of node 40. As the tree is not empty, the color of node 60 would
be Red.
As we can observe in the above tree that there is a Red-red conflict occurs. The parent
node is Red in color, and there is no parent's sibling exists in the tree, so rule 4a would
be applied. The first rotation would be performed. The RR relationship exists between
the nodes, so left rotation would be performed.
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
When left rotation is performed, node 40 will come upwards, and node 30 will come
downwards, as shown in the below figure:
EXAMPLE OF RBT
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60 https://www.javatpoint.com/red-black-tree
After rotation, the recoloring is performed on nodes 30 and 40. The color of node 30
would become Red, while the color of node 40 would become black.
AVL Vs Red Black Tree
AA TREE
AA trees were introduced by Arne Andersson in 1993 and hence the name
AA. They are a type of balanced binary search trees. It was developed as a
simpler alternative to red black trees.
The first element inserted is (7,8). It will serve as the base node in the following
K-D Tree example.
KD TREE
The second element inserted is (12,3). It is placed to the right leaf node of (7,8)
because the X value, 12, is greater than the X value of base, 7.
KD TREE
The next element inserted into the K-D Tree is (14,1). At the first level, we
compare the X values of the set and since 14 is greater than 7 it moves to the
right of the tree. Next is the comparison between (12,3) and (14,1). Each level the
comparison operator changes, which means that we will be comparing the Y
value of each set. Since the Y of the inserted set is 1 which is less than 3, it is
inserted to the left leaf node of (12,3).
The next set inserted is (4,12). Because the X, 4, is less than the base nodes
X, which is 7, the set is inserted to the left leaf node.
KD TREE
The next set is (9,1). Since the X is greater than the base, it moves to the
right leaf. At level 2 we compare the Y values, which is less than the current
leaf, so we move to the left leaf of this node. On level 3 we restart and
compare by the X value again, but if we had multiple dimensions it would
moved to the next dimension. Since 9 is less than 14, we insert it to the left
of this leaf node.
KD TREE
The next set inserted is (2,7). Since the X is less than 7 it compares to the
left leaf node. As the Y value is less than 12, the second levels Y value, this
set is inserted to the left of the (4,12) leaf node.
KD TREE
The next set inserted is (10,19). 10 is greater than 7 so it moves to the right
leaf node. Additionally, 19 is greater than the second levels Y, so it it
inserted to the right leaf node.
KD TREE
In the case we are given a duplicate value for the level (such as the X of the
current leaf node is 10 and the insertion data X is also 10) we will insert
move to the right leaf node of the current node.
SPLAY TREE
Splay trees are the self-balancing or self-adjusted binary search trees. In other
words, we can say that the splay trees are the variants of the binary search
trees. The prerequisite for the splay trees that we should know about the
binary search trees.
• A splay tree is a self-balancing tree, but AVL and Red-Black trees are also
self-balancing trees then. What makes the splay tree unique two trees. It
has one extra property that makes it unique is splaying.
• A splay tree contains the same operations as a Binary search tree, i.e.,
Insertion, deletion and searching, but it also contains one more operation,
i.e., splaying. So. all the operations in the splay tree are followed by
splaying.
SPLAY TREE
Splay trees are not strictly balanced trees, but they are roughly balanced trees.
Let's understand the search operation in the splay-tree.
Suppose we want to search 7 element in the tree, which is shown below:
SPLAY TREE
To search any element in the splay tree, first, we will perform the
standard binary search tree operation. As 7 is less than 10 so we will
come to the left of the root node. After performing the search
operation, we need to perform splaying. Here splaying means that the
operation that we are performing on any element should become the
root node after performing some rearrangements. The rearrangement
of the tree will be done through the rotations.
SPLAY TREE
Rotations
There are six types of rotations used for splaying:
The following are the factors used for selecting a type of rotation:
Scenario 2: If the node is left of a parent, but the parent is right of its
parent, then zig zag right left rotation is performed.
Scenario 3: If the node is right of the parent and the parent is right of its
parent, then zig zig left left rotation is performed.
Scenario 4: If the node is right of a parent, but the parent is left of its
parent, then zig zag right-left rotation is performed.
SPLAY TREE
1. Zig Rotation
The Zig Rotation in splay tree is similar to the single right rotation in AVL
Tree rotations. In zig rotation, every node moves one position to the right
from its current position. Consider the following example...
SPLAY TREE
2. Zag Rotation :
The Zag Rotation in splay tree is similar to the single left rotation in
AVL Tree rotations. In zag rotation, every node moves one position
to the left from its current position. Consider the following
example...
SPLAY TREE
3. Zig-Zig Rotation
The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig
rotation, every node moves two positions to the right from its current
Email : [email protected]