0% found this document useful (0 votes)
50 views10 pages

DSA - Unit IV - Search Trees

Uploaded by

bsjadhav.foe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views10 pages

DSA - Unit IV - Search Trees

Uploaded by

bsjadhav.foe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 1


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees

Operations of symbol Table


The symbol table provides the following operations:
Insert ()
lookup()

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.

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 2


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees

o Check whether the name is used in the scope.


o Initialization of the symbol.
o Checking whether the name is declared multiple times.

The basic format of lookup() function is as follows:


lookup (symbol)
[Note : This format is varies according to the programming language.]

Advantages of Symbol Table


1. The efficiency of a program can be increased by using symbol tables,
which give quick and simple access to crucial data such as variable and
function names, data kinds, and memory locations.
2. better coding structure Symbol tables can be used to organize and
simplify code, making it simpler to comprehend, discover, and correct
problems.
3. Faster code execution: By offering quick access to information like
memory addresses, symbol tables can be utilized to optimize code
execution by lowering the number of memory accesses required during
execution.
4. Symbol tables can be used to increase the portability of code by offering
a standardized method of storing and retrieving data, which can make it
simpler to migrate code between other systems or programming
languages.
5. Improved code reuse: By offering a standardized method of storing and
accessing information, symbol tables can be utilized to increase the reuse
of code across multiple projects.
6. Symbol tables can be used to facilitate easy access to and examination of
a program’s state during execution, enhancing debugging by making it
simpler to identify and correct mistakes.

Disadvantages of Symbol Table


7. Increased memory consumption: Systems with low memory resources
may suffer from symbol tables’ high memory requirements.
8. Increased processing time: The creation and processing of symbol tables
can take a long time, which can be problematic in systems with
constrained processing power.
9. Complexity: Developers who are not familiar with compiler design may
find symbol tables difficult to construct and maintain.
10.Limited scalability: Symbol tables may not be appropriate for large-scale
projects or applications that require o the management of enormous
amounts of data due to their limited scalability.

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 3


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees

11.Upkeep: Maintaining and updating symbol tables on a regular basis can


be time- and resource-consuming.
12.Limited functionality: It’s possible that symbol tables don’t offer all the
features a developer needs, and therefore more tools or libraries will be
needed to round out their capabilities.

Applications of Symbol Table


13.Resolution of variable and function names: Symbol tables are used to
identify the data types and memory locations of variables and functions
as well as to resolve their names.
14.Resolution of scope issues: To resolve naming conflicts and ascertain the
range of variables and functions, symbol tables are utilized.
15.Symbol tables, which offer quick access to information such as memory
locations, are used to optimize code execution.
16.Code generation: By giving details like memory locations and data
kinds, symbol tables are utilized to create machine code from source
code.
17.Error checking and code debugging: By supplying details about the
status of a program during execution, symbol tables are used to check for
faults and debug code.
18.Code organization and documentation: By supplying details about a
program’s structure, symbol tables can be used to organize code and
make it simpler to understand.

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

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 4


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees

Employee ID Name Position


5 Jessica 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.

Dynamic Tree Table


A dynamic tree table, on the other hand, allows for the dynamic addition or
removal of nodes in the tree structure. It provides a more interactive and flexible
way of representing hierarchical data.
In a dynamic tree table, the structure of the tree can change based on user
actions or data updates. Nodes can be expanded or collapsed to show or hide
child nodes, and new nodes can be added or removed dynamically.
Here is an example of a dynamic tree table representing a file directory:
Name Type Size (KB)
Documents Directory -

File 1 File 100

File 2 File 200

Pictures Directory -

File 3 File 150

File 4 File 250


In this example, the "Documents" and "Pictures" directories are the top-level
nodes. By clicking on the expand/collapse icon next to each directory, the user
can reveal or hide the files within that directory.
The dynamic nature of the tree table allows for a more interactive and user-
friendly experience when working with hierarchical data.

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

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 5


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees
of a problem. The dynamic programming guarantees to find the optimal solution
of a problem if the solution exists.
The definition of dynamic programming says that it is a technique for solving a
complex problem by first breaking into a collection of simpler subproblems,
solving each subproblem just once, and then storing their solutions to avoid
repetitive computations.
Dynamic Programming (DP) is a method used in mathematics and computer
science to solve complex problems by breaking them down into simpler
subproblems. By solving each subproblem only once and storing the results, it
avoids redundant computations, leading to more efficient solutions for a wide
range of problems.
How Does Dynamic Programming (DP) Work?
 Identify Subproblems: Divide the main problem into smaller, independent
subproblems.
 Store Solutions: Solve each subproblem and store the solution in a table or
array.
 Build Up Solutions: Use the stored solutions to build up the solution to the
main problem.
 Avoid Redundancy: By storing solutions, DP ensures that each
subproblem is solved only once, reducing computation time.
Examples of Dynamic Programming (DP)
Example 1: Consider the problem of finding the Fibonacci sequence:
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 6


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees
Approaches of Dynamic Programming (DP)
Dynamic programming can be achieved using two approaches:
1. Top-Down Approach (Memoization):
In the top-down approach, also known as memoization, we start with the final
solution and recursively break it down into smaller subproblems. To avoid
redundant calculations, we store the results of solved subproblems in a
memoization table.
Let’s breakdown Top down approach:
 Starts with the final solution and recursively breaks it down into smaller
subproblems.
 Stores the solutions to subproblems in a table to avoid redundant
calculations.
 Suitable when the number of subproblems is large and many of them are
reused.
2. Bottom-Up Approach (Tabulation):
In the bottom-up approach, also known as tabulation, we start with the
smallest subproblems and gradually build up to the final solution. We store the
results of solved subproblems in a table to avoid redundant calculations.
Let’s breakdown Bottom-up approach:
 Starts with the smallest subproblems and gradually builds up to the final
solution.
 Fills a table with solutions to subproblems in a bottom-up manner.
 Suitable when the number of subproblems is small and the optimal solution
can be directly computed from the solutions to smaller subproblems.

Weight Balanced Binary Tree


In a weight balanced binary tree, the aim is to balance the weight of a given tree
in terms of number of leaves. The weight of a node depends on the weight of its
children. A Binary Search Tree is weight balanced if for each node the number
of nodes in the left subtree is at least half and at most twice the number of nodes
in the right subtree.
A node in a weight-balanced binary search tree stores the sizes of subtrees in the
nodes. A node will contain the following data:
 value of the element stored
 left and right pointer to children node or null
 weight of the node
 The size of a leaf is usually considered as zero. The size of an internal node is
the sum of sizes of its two children, plus one: (size[n] = size[n.left] +

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 7


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees

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 - α

Optimal Binary Search Tree (OBST)

Height Balanced Binary Tree / AVL Tree


A height-balanced binary tree is defined as a binary tree in which the height of
the left and the right subtree of any node differ by not more than 1. AVL
tree, red-black tree are examples of height-balanced trees.
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.
Tree is said to be balanced if balance factor of each node is in between -1 to 1,
otherwise, the tree will be unbalanced and need to be balanced.

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 8


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees
Balance Factor (k) = height (left(k)) - height (right(k))
If balance factor of any node is 1, it means that the left sub-tree is one level
higher than the right sub-tree.

Conditions for Height-Balanced Binary Tree:


Following are the conditions for a height-balanced binary tree:
 The difference between the heights of the left and the right subtree for any
node is not more than one.
 The left subtree is balanced.
 The right subtree is balanced.
Note: An empty tree is also height-balanced.

What is the Height Balance of a node?


To check if the binary tree is height-balanced or not, you have to check the
height balance of each node. For this, you need to calculate the heights of the
two subtrees for each node making this impractical. Instead, we store the
height balance information of every subtree in the root node of that subtree.
Thus, each node not only maintains its data and children’s information but also
a height balance value.
The height balance of a node is calculated as follows:

height balance of node = height of right subtree – height of left subtree

The above formula means that:


 If the right subtree is taller, the height balance of the node will
be positive.
 If the left subtree is taller, the balance of the node will be negative.

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 9


© Copyright 2023-2024 www.javatpoint.com.
DSA Unit IV Search Trees

Subject Teacher : Ms. Atole S. M. 210252: Data Structures and Algorithms 10


© Copyright 2023-2024 www.javatpoint.com.

You might also like