Unit IV
Unit IV
Search Trees
Symbol Table
• In the world of computer science, compiler design is a complex field that bridges the
gap between human-readable programming languages and machine-executable
code.
• While compilers and assemblers are scanning a program, each identifier must be
examined to determine if it is a keyword.
• This information concerning the keywords in a programming language is stored in a
symbol table.
• The symbol table is a kind of a ‘keyed table’ which stores <key, information> pairs with
no additional logical structure.
• A symbol table in compiler design is like a dictionary or a lookup table that keeps track
of various pieces of information about identifiers (variables, functions, labels, etc.)
used in the source code.
The operations performed on symbol tables are the following:
int limit;
• When a compiler processes this statement, it will identify that int is a keyword and limit
is an identifier.
• For identifying int as a keyword, the compiler is provided with a table of keywords.
• For faster search through a list of keywords, the symbol table is used as an efficient
data structure.
Trees implementation of Symbol table -
• It is a more efficient method of organizing symbol tables. Each record now has
two link fields, LEFT and RIGHT.
Representation of Symbol Table
• There are two different techniques for implementing a keyed table, namely, the symbol table
• When symbols are known in advance and no insertion and deletion is allowed, such a
• If an identifier is not present in the reserved word table, then it is searched for in
another table.
• When we know the keys and their probable frequencies of being searched, we can
optimize the search time by building an optimal binary search tree (OBST).
• The keys have history associated with their use, which is referred to as their probability
of occurrence.
Dynamic Tree Tables –
• A dynamic tree table is used when symbols are not known in advance but are inserted
• The dynamically built tree that is a balanced BST is the best choice.
OPTIMAL BINARY SEARCH TREE
• Here, the total number of nodes are n, and l(ai) is the length of the ith key, a.
• Here, we assume that all the keys are searched with equal probabilities.
• However, in reality, the keys are searched with different probabilities, and it
should be taken care of while constructing the tree so that the keys searched
more often should require less time as compared to those searched rarely.
• This can be achieved by placing the more frequently searched key nodes
closer to the root as compared to those that are searched rarely, to reduce
the total number of average searches.
• A node is said to be closer to the root when its path length is lesser than that
of the other nodes.
• Then, cost of a tree is computed with respect to its node’s probability of search
and path length. Hence,
where,
Li = level of a particular node calculated from the root node treated from level 0
• In BST all nodes have some frequency.
• According to frequency BST has some cost.
• Our objective is find minimum cost of BST.
• Let’s take on small example
• Key:- 10,11,12 and Frequency:- 2,1,6
• For the BSTs , all the keys represent internal nodes.
• all successful searches will always end at an internal node.
• all squares denote external nodes, which are fictitious.
• all unsuccessful searches will end at some external node.
• If there are n keys, there are n + 1 external nodes.
• So all the keys that are not a part of a BST belong to one of (n + 1) equivalence
classes Ei for 0 ≤ i ≤n.
• Hence, the cost of a BST is given as follows:
if(i==j)
p=NULL;
else
{
p=new node;
p->key=keys[R[i][j]];
p->left=construct(i,R[i][j]-1);
p->right=construct(R[i][j],j);
}
return p;
}
int obst::cost(int* p,int* q,int n)
{ for(int h=2;h<=n;h++)
//Constructing the weight matrix {
for(int i=0;i<=n;i++) for(int i=0;i<=n-h;i++)
{ {
W[i][i]=q[i]; int j=i+h;
int m=R[i][j-1];
for(int j=i+1;j<=n;j++) int min=C[i][m-1]+C[m][j];
{ for(int k=m+1;k<=R[i+1][j];k++)
W[i][j]=W[i][j-1]+p[j]+q[j]; {
} int x=C[i][k-1] + C[k][j];
} if(x<min)
//Constructing the cost matrix {
for(int i=0;i<=n;i++) m=k;
{ min=x;
}
C[i][i]=W[i][i]; }
} C[i][j]=W[i][j]+min;
R[i][j]=m;
for(int i=0;i<=n-1;i++) }
{ }
int j=i+1;
C[i][j]=C[i][i]+C[j][j]+W[i][j];
R[i][j]=j;
}
cout<<"\nThe weight matrix: ";
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
cout<<W[i][j]<<" ";
cout<<"\n";
}
cout<<"\n";
}
cout<<"\n";
}
return C[0][n];
}
int main()
{
int n;
cout<<"\nEnter the number of keys: ";
cin>>n;
obst obj(n);
for(int i=1;i<=n;i++)
{
cout<<"Keys["<<i<<"]:";
cin>>keys[i];
cout<<"p["<<i<<"]:";
cin>>p[i];
}
for(int i=0;i<=n;i++)
{
cout<<"q["<<i<<"]:";
cin>>q[i];
}
int c=obj.cost(p,q,n);
cout<<"\nMinimum cost: "<<c;
node* root=obj.construct(0,n);
cout<<"\nInorder traversal";
obj.display(root);
cout<<"\n";
return 0;
}
Height Balanced Tree- AVL tree
• An AVL tree is a BST where the heights of the left and right subtrees of the root
differ by utmost 1 and the left and right subtrees are again AVL trees.
• The formal definition is as follows:
• In an AVL tree with n nodes, the searches, insertions, and deletions can all be
achieved in time O(log n), even in the worst case.
• To keep the tree height-balanced, we have to find out the balance factor of each
node in the tree after every insertion or deletion.
Balance Factor
• In an AVL tree, the balance factor of a node is the height difference between
its left and right subtrees. It ensures the tree remains balanced, with a
• All nodes have balance factors within the range [-1, 1], indicating a balanced
AVL tree. Mathematically, the balance factor (BF) of a node N is given by:
bf = h l − h r
Example of AVL Tree
AVL Tree Rotation
• In AVL tree, after performing operations like insertion and deletion we need to
• If every node satisfies the balance factor condition then we conclude the
• Whenever the tree becomes imbalanced due to any operation we use rotation
operations to make the tree balanced.Rotation operations are used to make the
tree balanced.
• Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
• There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation) -
• If a node in a tree becomes unbalanced due to the nodes of the right subtree,
then we left-rotate the parent node to balance the tree.
• Left rotation is performed on a right-skewed binary search tree.
• In LL Rotation, every node moves one position to left from the current position.
• To understand LL Rotation, let us consider the following insertion operation in
AVL Tree...
Single Right Rotation (RR Rotation)
• If a tree is not height-balanced due to the left subtree, we rotate it in the
rightward direction to balance it.
• Right rotation is performed from left-skewed binary search trees.
• In RR Rotation, every node moves one position to right from the current position.
• To understand RR Rotation, let us consider the following insertion operation in
AVL Tree...
Left Right Rotation (LR Rotation)
• The LR Rotation is a sequence of single left rotation followed by a single right
rotation.
• In LR Rotation, at first, every node moves one position to the left and one position
to right from the current position.
• To understand LR Rotation, let us consider the following insertion operation in
AVL Tree...
Right Left Rotation (RL Rotation)
• The RL Rotation is sequence of single right rotation followed by single left
rotation.
• In RL Rotation, at first every node moves one position to right and one position
to left from the current position.
• To understand RL Rotation, let us consider the following insertion operation in
AVL Tree...
Operations on an AVL Tree
1. Search
2. Insertion
3. Deletion
Search Operation in AVL Tree -
In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree. We
use the following steps to search an element in AVL tree...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the 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 node value.
Step 5 - If search element is smaller, then continue the search process in left subtree.
Step 6 - If search element is larger, then continue the search process in right subtree.
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.
Insertion Operation in AVL Tree -
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL
Tree, a new node is always inserted as a leaf node. The insertion operation is performed
as follows...
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
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.
Algorithm
• The following steps are involved in performing the insertion operation of an
AVL Tree −
Step 1 − Create a node
Step 2 − Check if the tree is empty
Step 3 − If the tree is empty, the new node created will become the
root node of the AVL Tree.
Step 4 − If the tree is not empty, we perform the Binary Search Tree
insertion operation and check the balancing factor of the node
in the tree.
Step 5 − Suppose the balancing factor exceeds ±1, we apply suitable
rotations on the said node and resume the insertion from Step 4.
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
Deletion Operation in AVL Tree
• But after every deletion operation, we need to check with the Balance Factor
condition.
• If the tree is balanced after deletion go for next operation otherwise perform suitable
• We know that deletion of any node in any binary search tree is handled in three
different cases:
return b_factor;
}
// Right- Right Rotation
avl_node *avlTree::rr_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
// Left- Left Rotation
avl_node *avlTree::ll_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
// Left - Right Rotation
avl_node *avlTree::lr_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = ll_rotation (temp);
return rr_rotation (parent);
}
1. Construct an AVL tree for the following data: 30, 31, 32, 23, 22, 28, 24, 29,
2. Construct an AVL tree for the following data: STA, add, lda , mov , jmp, trim,
3. Construction of AVL tree (a) Key = STA (b) Key = ADD (c) Key = LDA (d)
4. Construct an AVL tree for the set of keys = {50, 55, 60, 15, 10, 40, 20, 45,
that.
• For all types of in-memory collections, including sets and dictionaries, AVL
• Database applications, where insertions and deletions are less common but
frequent data lookups are necessary Software that needs optimized search.
4. Better searching time complexity compared to other trees like binary tree.
1. It is difficult to implement.
4. Due to its rather strict balance, AVL trees provide complicated insertion and
In Red Black Tree, the color of a node is decided based on the properties of Red-Black Tree.
3. The children of Red colored node must be colored BLACK. (There should not be two
consecutive RED nodes).There are no two adjacent red nodes (A red node cannot
4. Every simple path from root to descendant leaf node contains same number of black
nodes.
While inserting a new node, the new node is always inserted as RED node. After
insertion of a new node, if the tree is violating the properties of the red-black tree
1. Recolor
2. Rotation
Case -1
• The imbalancing is concerned with the color of grandparent child i.e., Uncle Node.
• If uncle node is red then four cases arises, and by doing recoloring imbalancing
can be removed.
• In this, Red-Black Tree violates its property in such a manner that parent and
inserted child will be in red color at a position of left and right with respect to
grandparent.
• The imbalancing can also be occurred when the child of grandparent i.e., uncle
node is black.
• Then also four cases will arise and in these cases imbalancing can be removed
1. LR imbalancing
2. LL imbalancing
3. RR imbalancing
4. RL imbalancing
Insertion into RED BLACK Tree
ii. For LR/RL imbalancing, recolor u to black and recolor p and g to red.
Insertion into RED BLACK Tree
Steps for Insertion:
Insert the Node: Insert the new node like in a binary search tree. New nodes are always
inserted as red nodes.
ROOT Node: If new node is root, change color of new node as BLACK.
Check for Violations: After insertion, check for Red-Black Tree property violations and fix them
using Recoloring or Rotations followed by Recoloring.
Recoloring: If a parent and uncle are red then
a) Change color of parent and uncle as BLACK.
b) color of grand parent as RED if not ROOT.
c) repeat steps a and b till we reach the ROOT.
Rotations followed by Recoloring: If the parent is red but the uncle is black then, perform
rotations. There are four cases:
a) Left-Left (LL) Case and swap the colors of grand parent and parent.
b) Left-Right (LR) Case and swap the colors of grand parent and new node.
c) Right-Right (RR) Case and swap the colors of grand parent and parent.
d) Right-Left (RL) Case and swap the colors of grand parent and new node.
Algorithm for Insertion Operations ofRed Black Tree
RB-INSERT(T, k)
BST-INSERT(T, k) //normal BST insertion
while k.parent.color == RED
if k.parent == k.parent.parent.right
u = k.parent.parent.left //uncle
if u.color == RED
u.color = BLACK
k.parent.color = BLACK
k.parent.parent.color = RED
k = k.parent.parent
else if k == k.parent.left
k = k.parent
LEFT-ROTATE(T, k)
k.parent.color = BLACK
k.parent.parent.color = RED
RIGHT-ROTATE(T, k.parent.parent)
else (same as then clause with “left” and “right” exchanged)
T.root.color = BLACK
Example
Deletion Operation on RED BLACK Tree
1. Find the Node: Like in a regular binary search tree, find the node that needs to
be deleted.
(b) Rotate at sibling node in the direction opposite to the DB node. Hence, perform right
The double black node still haunts the tree! Re-check the case that can be applied to this
tree and we find that case 6 (don’t fall for case 3) seems to fit.
Apply case 6 as follow-
• AA trees are more advantageous as they simplify the algorithms. The following
• If an internal node has only one child, that child must be a red child.
• We can always replace a node with the smallest child in the right subtree; it
• A simple example to showcase the insertion into a K-Dimensional Tree, we will use a k = 2.
• The points we will be adding are: (7,8), (12,3), (14,1), (4,12), (9,1), (2,7), and (10,19).
• We can see that the depth is
equivalent to 3, which is also
Log2(n) of the current K-D
Tree.
• On a plotted 2 dimensional graph the following K-D Tree will be the equivalent to:
1. Insert (3, 6): Since tree is empty, make it the root node.
2. Insert (17, 15): Compare it with root node point. Since root
node is X-aligned, the X-coordinate value will be compared to
determine if it lies in the right subtree or in the left subtree.
This point will be Y-aligned.
3. Insert (13, 15): X-value of this point is greater than X-value of
point in root node. So, this will lie in the right subtree of (3, 6).
Again Compare Y-value of this point with the Y-value of point
(17, 15) (Why?). Since, they are equal, this point will lie in the
right subtree of (17, 15). This point will be X-aligned.
4. Insert (6, 12): X-value of this point is greater than X-value of
point in root node. So, this will lie in the right subtree of (3, 6).
Again Compare Y-value of this point with the Y-value of point
(17, 15) (Why?). Since, 12 < 15, this point will lie in the left
subtree of (17, 15). This point will be X-aligned.
5. Insert (9, 1):Similarly, this point will lie in the right of (6, 12).
6. Insert (2, 7):Similarly, this point will lie in the left of (3, 6).
7. Insert (10, 19): Similarly, this point will lie in the left of (13,
15).
How is space partitioned?
Y axis
1. Point (3, 6) will divide the space into two parts: Draw line X =
3.
2. Point (2, 7) will divide the space to the left of line X = 3 into
two parts horizontally. Draw line Y = 7 to the left of line X = 3.
3. Point (17, 15) will divide the space to the right of line X = 3 into
two parts horizontally. Draw line Y = 15 to the right of line X =
3.
4. Point (6, 12) will divide the space below line Y = 15 and to the
right of line X = 3 into two parts. Draw line X = 6 to the right of
line X = 3 and below line Y = 15.
5. Point (13, 15) will divide the space below line Y = 15 and to
the right of line X = 6 into two parts. Draw line X = 13 to the
right of line X = 6 and below line Y = 15.
6. Point (9, 1) will divide the space between lines X = 3, X = 6
and Y = 15 into two parts. Draw line Y = 1 between lines X = 3
and X = 13.
X axis
7. Point (10, 19) will divide the space to the right of line X = 3
and above line Y = 15 into two parts. Draw line Y = 19 to the
right of line X = 3 and above line Y = 15.
Advantages of K Dimension Trees
• Efficient search: K-D trees are effective in searching for points in k-dimensional space,
such as in nearest neighbor search or range search.
• Dimensionality reduction: K-D trees can be used to reduce the dimensionality of the
problem, allowing for faster search times and reducing the memory requirements of the
data structure.
• Versatility: K-D trees can be used for a wide range of applications, such as in data
mining, computer graphics, and scientific computing.
• Balance: K-d trees are self-balancing, which ensures that the tree remains efficient
even when data is inserted or removed.
• Incremental construction: K-D trees can be incrementally constructed, which means
that data can be added or removed from the structure without having to rebuild the
entire tree.
• Easy to implement: K-D trees are relatively easy to implement and can be
implemented in a variety of programming languages.
SPLAY TREES
inserts the new element using the binary search tree insertion process, then the newly inserted element is
splayed so that it is placed at the root of the tree. The search operation in a splay tree is nothing but
searching the element using binary search process and then splaying that searched element so that it is
1. Insertion: To insert a new element into the tree, start by performing a regular binary search tree
insertion. Then, apply rotations to bring the newly inserted element to the root of the tree.
2. Deletion: To delete an element from the tree, first locate it using a binary search tree search. Then, if
the element has no children, simply remove it. If it has one child, promote that child to its position in
the tree. If it has two children, find the successor of the element (the smallest element in its right
subtree), swap its key with the element to be deleted, and delete the successor instead.
3. Search: To search for an element in the tree, start by performing a binary search tree search. If the
element is found, apply rotations to bring it to the root of the tree. If it is not found, apply rotations to
the last node visited in the search, which becomes the new root.
4. Rotation: The rotations used in a splay tree are either a Zig or a Zig-Zig rotation. A Zig rotation is
used to bring a node to the root, while a Zig-Zig rotation is used to balance the tree after multiple
Rotations in Splay Tree
1. Zig Rotation
2. Zag Rotation
3. Zig - Zig Rotation
4. Zag - Zag Rotation
5. Zig - Zag Rotation
6. Zag - Zig Rotation
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...
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...
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 position. Consider the following example...
Zag-Zag Rotation - The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation, every
node moves two positions to the left from its current position. Consider the following example...
Zig-Zag Rotation - The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed by zag
rotation. In zig-zag rotation, every node moves one position to the right followed by one position to the left
from its current position. Consider the following example...
Zag-Zig Rotation -The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed
by zig rotation. In zag-zig rotation, every node moves one position to the left followed by one
position to the right from its current position. Consider the following example...