DSA - Unit IV - Search Trees
DSA - Unit IV - Search Trees
Symbol table:
Symbol table is an important data structure used in a compiler.
Symbol table is used to store the information about the occurrence of various
entities such as objects, classes, variable name, interface, function name etc. it is
used by both the analysis and synthesis phases.
OR
Symbol Table is an important data structure created and maintained by the
compiler in order to keep track of semantics of variables i.e. it stores
information about the scope and binding information about names, information
about instances of various entities such as variable and function names, classes,
objects, etc.
The symbol table used for following purposes:
o It is used to store the name of all entities in a structured form at one place.
o It is used to verify if a variable has been declared.
o It is used to determine the scope of a name.
o It is used to implement type checking by verifying assignments and
expressions in the source code are semantically correct.
o It is built-in lexical and syntax analysis phases.
o The information is collected by the analysis phases of the compiler and
is used by the synthesis phases of the compiler to generate code.
o It is used by the compiler to achieve compile-time efficiency.
A symbol table can either be linear or a hash table. Using the following format,
it maintains the entry for each name.
<symbol name, type, attribute>
For example, suppose a variable store the information about the following
variable declaration:
static int salary
then, it stores an entry in the following format:
<salary, int, static>
The clause attribute contains the entries related to the name.
Implementation
The symbol table can be implemented in the unordered list if the compiler is
used to handle the small amount of data.
A symbol table can be implemented in one of the following techniques:
o Linear (sorted or unsorted) list
o Hash table
o Binary search tree
Symbol table are mostly implemented as hash table.
Insert ()
o Insert () operation is more frequently used in the analysis phase when the
tokens are identified and names are stored in the table.
o The insert() operation is used to insert the information in the symbol table
like the unique name occurring in the source code.
o In the source code, the attribute for a symbol is the information associated
with that symbol. The information contains the state, value, type and
scope about the symbol.
o The insert () function takes the symbol and its value in the form of
argument.
For example:
int x;
Should be processed by the compiler as:
insert (x, int)
lookup()
In the symbol table, lookup() operation is used to search a name. It is used to
determine:
o The existence of symbol in the table.
o The declaration of the symbol before it is used.
Representation of Symbol Tables- Static tree table and Dynamic tree table
Static Tree Table
A static tree table is a type of table that represents hierarchical data in a tree-like
structure. It is commonly used to display data that has a parent-child
relationship, such as organizational structures or file directories.
In a static tree table, the structure of the tree is predetermined and does not
change dynamically. The table consists of rows and columns, where each row
represents a node in the tree and each column represents a specific attribute or
property of the node.
Here is an example of a static tree table representing an organizational structure:
Employee ID Name Position
1 John CEO
2 Emily Manager
3 Sarah Manager
4 Michael Employee
6 David Employee
7 Lisa Employee
In this example, the CEO (John) is at the top of the hierarchy, followed by two
managers (Emily and Sarah), and then employees under each manager.
Pictures Directory -
Dynamic programming
Dynamic programming is a technique that breaks the problems into sub-
problems, and saves the result for future purposes so that we do not need to
compute the result again. The subproblems are optimized to optimize the overall
solution is known as optimal substructure property. The main use of dynamic
programming is to solve optimization problems. Here, optimization problems
mean that when we are trying to find out the minimum or the maximum solution
size[n.right] + 1). Based on the size, one defines the weight to be weight[n] =
size[n] + 1.
The above figure shows the weight of nodes along with its value. The node with
the greatest weight is the root of the tree. The leaf nodes have size = 0 and
weight = 1. The weight of the nodes decreases from root to leaf nodes.
Operations that modify the tree must make sure that the weight of the left and
right subtrees of every node remain within some factor α of each other.
Rebalancing of the tree is done through rotations and double rotations.
Formally, node balance is defined as follows:
A node is α-weight-balanced if α ≤ |Left(n)| / |Left(n)| + |Right(n)| ≤ 1 - α