DSA Practical
DSA Practical
Problem Statement:
Consider telephone book database of N clients. Make use of a hash table implementation to quickly
look up client’s telephone number
Objectives:
Learning Objectives
Learning Outcome
Theory:
Hash tables are an efficient implementation of a keyed array data structure, a structure
sometimes known as an associative array or map. If you're working in C++, you can take advantage
of the STL map container for keyed arrays implemented using binary trees, but this article will give
you some of the theory behind how a hash table works.
Hashing Functions
A hashing function can be just about anything. How the hashing function is actually coded depends
on the situation, but generally the hashing function should return a value based on a key and the size
of the array the hashing table is built on. Also, one important thing that is sometimes overlooked is
that a hashing function has to return the same value every time it is given the same key.
Basic Operations
Search Operation
Whenever an element is to be searched, compute the hash code of the key passed and locate the
element using that hash code as index in the array. Use linear probing to get the element ahead if the
element is not found at the computed hash code.
Example
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
return NULL;
}
Problems, of course, arise when we have last names with the same first letter. So "Webster" and
"Whitney" would correspond to the same index number, 22. A situation like this when two keys
get sent to the same location in the array is called a collision. If you're trying to insert an
element, you might find that the space is already filled by a different one.
Of course, you might try to just make a huge array and thus make it almost impossible for
collisions to happen, but then that defeats the purpose of using a hash table. One of theadvantages
of the hash table is that it is both fast and small.
Conclusion: In this way we have implemented Hash table for quick lookup using C++.
Practical 02
Problem Statement:
Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys
must be unique
Objectives:
Learning Objectives:
To understand Dictionary(ADT)
To understand concept of hashing
To understand concept & features like searching using hash function.
Learning Outcome:
Theory:
Dictionary ADT
Dictionary Operations
Dictionary create()
creates empty dictionary
boolean isEmpty(Dictionary d)
tells whether the dictionary d is empty
put(Dictionary d, Key k, Value v)
associates key k with a value v; if key k already presents in the dictionary old value is
replaced by v
remove(Dictionary d, Key k)
removes key k and associated value
destroy(Dictionary d)
destroys dictionary d
Hash Table is a data structure which stores data in an associative manner. In a hash table, data
is stored in an array format, where each data value has its own unique index value. Access of data
becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash
technique to generate an index where an element is to be inserted or is to be located from.
Hashing
Define a data item having some data and key, based on which the search is to be conducted in a
hash table.
struct DataItem {
int data;
int key;
};
2. Hash Method
Define a hashing method to compute the hash code of the key of the data item.
Whenever an element is to be searched, compute the hash code of the key passed and locate the
element using that hash code as index in the array. Use linear probing to get the element ahead if the
element is not found at the computed hash code.
Example
struct DataItem *search(int key) {
//get the hash
int hashIndex = hashCode(key);
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
return NULL;
}
4. Insert Operation
Whenever an element is to be inserted, compute the hash code of the key passed and locate the index
using that hash code as an index in the array. Use linear probing for empty location, if an element is
found at the computed hash code.
Example
void insert(int key,int data) {
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
hashArray[hashIndex] = item;
}
5. Delete Operation
Whenever an element is to be deleted, compute the hash code of the key passed and locate the index
using that hash code as an index in the array. Use linear probing to get the element ahead if an
element is not found at the computed hash code. When found, store a dummy item there to keep the
performance of the hash table intact.
Example
struct DataItem* delete(struct DataItem* item) {
int key = item->key;
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
return NULL;
}
Output: Create dictionary using hash table and search the elements in table.
OUTCOME
Practical 03
Problem Statement:
A book consists of chapters, chapters consist of sections and sections consist of subsections.
Construct a tree and print the nodes. Find the time and space requirements
of your method.
Objectives:
Learning Objectives
Learning Outcome
Understand & implement concept of creating nodes with insert and display operation on
it.
Theory
A tree is a hierarchical data structure defined as a collection of nodes. Nodes represent value
and nodes are connected by edges. A tree has the following properties:
1. The tree has one node called root. The tree originates from this, and hence it does
not have any parent.
2. Each node has one parent only but can have multiple children.
3. Each node is connected to its children via edge.
A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such
that each node of the tree stores a value, a list of references to nodes (the “children”).
Basic Terminology In Tree Data Structure:
Parent Node: The node which is a predecessor of a node is called the parent node of that node.
{2} is the parent node of {6, 7}.
Child Node: The node which is the immediate successor of a node is called the child node of that
node. Examples: {6, 7} are the child nodes of {2}.
Root Node: The topmost node of a tree or the node which does not have any parent node is called
the root node. {1} is the root node of the tree. A non-empty tree must contain exactly one root
node and exactly one path from the root to all other nodes of the tree.
Degree of a Node: The total count of subtrees attached to that node is called the degree of the
node. The degree of a leaf node must be 0. The degree of a tree is the maximum degree of a node
among all the nodes in the tree. The degree of the node {3} is 3.
Leaf Node or External Node: The nodes which do not have any child nodes are called leaf nodes.
{6, 14, 8, 9, 15, 16, 4, 11, 12, 17, 18, 19} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {1, 2} are the parent nodes of the node {7}
Descendant: Any successor node on the path from the leaf node to that node. {7, 14} are the
descendants of the node. {2}.
Sibling: Children of the same parent node are called siblings. {8, 9, 10} are called siblings.
Depth of a node: The count of edges from the root to the node. Depth of node {14} is 3.
Height of a node: The number of edges on the longest path from that node to a leaf.
Height of node {3} is 2.
Height of a tree: The height of a tree is the height of the root node i.e the count of edges
from the root to the deepest node. The height of the above tree is 3.
Level of a node: The count of edges on the path from the root node to that node. The root
node has level 0.
Internal node: A node with at least one child is called Internal Node.
Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
Subtree: Any node of the tree along with its descendant.
A Binary tree is implemented with the help of pointers. The first node in the tree is represented
by the root pointer. Each node in the tree consists of three parts, i.e., data, left pointer and right
pointer. To create a binary tree, we first need to create the node. We will create the node of
user-defined as shown below:
1. struct node
2. {
3. int data,
4. struct node *left, *right;
5. }
In the above structure, data is the value, left pointer contains the address of the left node,
and right pointer contains the address of the right node.
Output: Create root node as book name having child chapters,sections and subsections.
Conclusion: This program gives us the knowledge of create and display nodes in tree.
OUTCOME
Practical 04
Problem Statement:
Construct an expression tree from the given prefix expression eg. +--a*bc/def and
traverse it using post order traversal (non recursive) and then delete the entire tree.
Objectives:
2. To understand the concept of converting the given expression into prefix and postfix
order.
Learning Objectives
Learning Outcome
Understand & implement concept of creating creating expression tree from given
expression, delete tree
Theory
The expression tree is a binary tree in which each internal node corresponds to the operator and each
leaf node corresponds to the operand so for example expression tree for 3 + ((5+9)*2) would be:
Evaluating the expression represented by an expression tree:
Let t be the expression tree
If t is not null then
If t.value is operand then
Return t.value
A = solve(t.left)
B = solve(t.right)
Scan the given prefix expression from right to left character by character.
But if the character is an operator, pop the top two values from stack.
Concatenate this operator with these two values (operator+1st top value+2nd top value) to
get a new string.
Repeat this process untill the end of prefix expression. Now the value in the stack is the
desired postfix expression.
Scan the given postfix expression from left to right character by character.
But if the character is an operator, pop the top two values from stack.
Concatenate this operator with these two values (operator+2nd top value+1st top value) to
get a new string.
Repeat this process untill the end of postfix expression. Now the value in the stack is the
desired prefix expression.
Output: Convert the given prefix expression into tree and write the post order traversal (non
recursive) and then delete the entire tree.
Conclusion: This program gives us the knowledge of create and display expression tree.
OUTCOME
Practical 05
Problem Statement:
Beginning with an empty binary search tree, Construct binary search tree by inserting the
values in the order given. After constructing a binary tree -
i. Insert new node
ii. Find number of nodes in longest path from root
iii. Minimum datavalue found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at
every node
v. Search a value
Objectives:
2. To understand the concept of Insert new node, Find number of nodes, Minimum
datavalue found in the tree
Learning Objectives
Learning Outcome
Understand & implement concept of Insert new node, Find number of nodes,
Minimum datavalue found in the tree
Theory
char data;
struct BinTree *right;
};
Rcreate ( ) function:
Nrcreate ( ) function
begin
initialize temp to point to Root
while(1)
begin
temp = temp->left
else
temp->left = newnode
break
temp = temp->right
else
temp->right = newnode
break
End
End
End of Nrcreate( )
Find Height of the tree-
There are two conventions to define height of Binary Tree
1) Number of nodes on longest path from root to the deepest node.
2) Number of edges on longest path from root to the deepest node.
In this post, the first convention is followed. For example, height of the below tree is 3.
Recursive method to find height of Binary Tree is discussed here. How to find height without
recursion? We can use level order traversal to find height without recursion. The idea is to traverse
level by level. Whenever move down to a level, increment height by 1 (height is initialized as 0).
Count number of nodes at each level; stop traversing when count of nodes atnext level is 0.
Recursive Function-
int i, j, max=0;
i=1,j=1;
if(root!=NULL)
i=i+nheight(root->left);
j=j+nheight(root->right);
if(i>j)
max=i;
else
max=j;
}
return(max);
Non-recursive function-
if(Root == Null)
return 0;
return 0;
heightL = TreeHeight(Root->Lchild);
heightR = TreeHeight(Root->Rchild);
return(heightR + 1);
return(heightL + 1);
This is quite simple. Just traverse the node from root to left recursively until left is NULL. The
node whose left is NULL is the node with minimum value.
For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see
NULL. Since left of 4 is NULL, 4 is the node with minimum value.
In binary search tree, the smallest node is in the left side and the largest node is in the right side. To
find the smallest node, the process will check the parent node. In case that the parent node isnot
empty, if it doesn't have a left child node, the smallest node is the parent node; otherwise the smallest
node is its left child node.
node *temp;
temp=root;
while(temp->left!=NULL)
temp=temp->left;
node *temp;
temp=root;
while(temp->right!=NULL)
temp=temp->right;
}
Getting Mirror, Replica, or Tree Interchange of Binary Tree
The Mirror() operation finds the mirror of the tree that will interchange all left and right
Recursive Function
TreeNode *Tmp;
if(Root != Null)
Tmp = Root->Lchild;
Root->Lchild = Root->Rchild;
Root->Rchild = Tmp;
Mirror(Root->Lchild);
Mirror(Root->Rchild);
}
}
Mirror( ) Function (Non-Recursive) using Queue-
Add(temp) to queue
begin
temp = delete
if (temp->left != NULL)
add(temp->left)
if(temp->right != NULL)
add (temp->right)
change = temp->left
temp->Left = temp->right
temp->right = change
end
end
To search for a target key, we first compare it with the key at the root of the tree. If it is the
same, then the algorithm ends. If it is less than the key at the root, search for the target key in
the left subtree, else search in the right subtree. Let us, for example, search for the key
„Saurabh‟ in following Figure
We first compare „Saurabh‟ with the key of the root, „Jyoti‟. Since „Saurabh‟ comes after „Jyoti‟
in alphabetical order, we move to the right side and next compare it with the key „Rekha‟. Since
„Saurabh‟ comes after „Rekha‟, we move to the right again and compare with „Teena‟. Since
„Saurabh‟ comes before „Teena‟, we move to the left. Now the question is to identify what event will
be the terminating condition for the search. The solution is if we find the key, the function finishes
successfully. If not, we continue searching until we hit an empty subtree.
Program Code shows the implementation of search () function, both nonrecursive and recursive
implementations.
Non-recursive-
while(Tmp)
if(Tmp->Data == Key)
return Tmp;
else if(Tmp->data < Key)
Tmp = Tmp->Lchild;
return NULL;
Recursive-
{ if(root == Null)
return(root);
else
root = Rec_Search(root->Lchild);
root = Rec_Search(root->Rchild);
Output: implement concept of Insert new node, Find number of nodes, Minimum data value
found in the tree
Conclusion: Successfully implemented Binary search tree as an ADT using linked list in C++
language.
.
OUTCOME
Practical 06
Problem Statement:
Represent a given graph using adjacency matrix/list to perform DFS and using adjacency list to
perform BFS. Use the map of the area around the college as the graph. Identify the prominent land
marks as nodes and perform DFS and BFS on that.
Objectives:
Learning Objectives
To understand concept of adjacency matrix/list to perform DFS and using adjacency list
to perform BFS
Learning Outcome
Understand & implement concept of adjacency matrix/list to perform DFS and using
adjacencylist to perform BFS
Theory
Depth–first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One
starts at the root (selecting some arbitrary node as the root for a graph) and explore as far as possible
along each branch before backtracking.
A Depth–first search (DFS) is a way of traversing graphs closely related to the preorder
traversal of a tree. Following is the recursive implementation of preorder traversal:
procedure preorder(treeNode v)
{
visit(v);
for each child u of v
preorder(u);
}
To turn this into a graph traversal algorithm, replace “child” with “neighbor”. But to prevent
infinite loops, keep track of the vertices that are already discovered and not revisit them.
procedure dfs(vertex v)
{
visit(v);
for each neighbor u of v
if u is undiscovered
call dfs(u);
}
Breadth–first search (BFS) is a graph traversal algorithm that explores vertices in the order of their
distance from the source vertex, where distance is the minimum length of a path from the source
vertex to the node as evident from the above example.
Applications of BFS
Copying garbage collection, Cheney‟s algorithm.
Finding the shortest path between two nodes u and v , with path length measured by the
Input: Graph
.
OUTCOME
ELO1: Learn object oriented Programming features for DFS and BFS.
GROUP C
Practical 07
Problem Statement:
There are flight paths between cities. If there is a flight between city A and city B then there is an
edge between the cities. The cost of the edge can be the time that flight take to reach city Bfrom A,
or the amount of fuel used for the journey. Represent this as a graph. The node can be represented
by airport name or name of the city. Use adjacency list representation of the graph or use
adjacency matrix representation of the graph.
Check whether the graph is connected or not. Justify the storage representation used
Objectives:
Learning Objectives
Learning Outcome
Theory
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also
referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph.
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34,
04, 14, 13}.
Graphs are used to solve many real-life problems. Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also
used in social networks like linkedIn, Facebook. For example, in Facebook, each person is represented
with a vertex(or node). Each node is a structure and contains information like personid, name,
gender, locale etc.
The following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of
graph representation is situation-specific. It totally depends on the type of operations to be performed
and ease of use.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the
2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.
Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to
represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight
w.
Adjacency List:
An array of lists is used. The size of the array is equal to the number of vertices. Let the array be
an array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. This representation
can also be used to represent a weighted graph. The weights of edges can be represented as lists of
pairs. Following is the adjacency list representation of the above graph.
Software Required: Dev C++ compiler- / 64 bit windows
Input: Graph
Output: implement concept of Graph for flight path between two cities.
Conclusion: Successfully implemented graph in C++ language.
.
OUTCOME
Practical 08
Problem Statement:
Given sequence k = k1 <k2 < … < kn of n sorted keys, with a search probability pi for each key
ki. Build the Binary search tree that has the least search cost given the access probability for
each key?
Objectives:
Learning Objectives:
To understand concept of OBST.
To understand concept & features like extended binary search tree.
Learning Outcome:
Define class for Extended binary search tree using Object Oriented features.
Analyze working of functions.
Theory:
An optimal binary search tree is a binary search tree for which the nodes are arranged on
levels such that the tree cost is minimum.
For the purpose of a better presentation of optimal binary search trees, we will consider
“extended binary search trees”, which have the keys stored at their internal nodes. Suppose “n” keys
k1, k2, … k n are stored at the internal nodes of a binary search tree. It is assumed that the keys are
given in sorted order, so that k1< k2 < … < kn.
An extended binary search tree is obtained from the binary search tree by adding successor
nodes to each of its terminal nodes as indicated in the following figure by squares:
In the extended tree:
The squares represent terminal nodes. These terminal nodes represent unsuccessful searches
of the tree for key values. The searches did not end successfully, that is, because they represent
key values that are not actually stored in the tree;
The round nodes represent internal nodes; these are the actual keys stored in the tree;
Assuming that the relative frequency with which each key value is accessed is known, weights
can be assigned to each node of the extended tree (p1 … p6). They represent the relative
frequencies of searches terminating at each node, that is, they mark the successful searches.
If the user searches a particular key in the tree, 2 cases can occur:
1 – the key is found, so the corresponding weight „p‟ is incremented;
2 – the key is not found, so the corresponding „q‟ value is incremented.
GENERALIZATION:
The terminal node in the extended tree that is the left successor of k1 can be interpreted as representing
all key values that are not stored and are less than k1. Similarly, the terminal node in the extended
tree that is the right successor of kn, represents all key values not stored in the tree that are greater
than kn. The terminal node that is successes between ki and ki-1 in an inorder traversal represent all
key values not stored that lie between ki and ki - 1.
ALGORITHMS
We have the following procedure for determining R(i, j) and C(i, j) with 0 <= i <= j <= n:
PROCEDURE COMPUTE_ROOT(n, p, q; R, C)
begin
for i = 0 to n do
C (i, i) ←0
W (i, i) ←q(i)
for m = 0 to n do
for i = 0 to (n – m) do
j ←i + m
W (i, j) ←W (i, j – 1) + p (j) + q (j)
*find C (i, j) and R (i, j) which minimize the
tree cost
end
The following function builds an optimal binary sea
rch tree
FUNCTION CONSTRUCT(R, i, j)
begin
*build a new internal node N labeled (i, j)
k ←R (i, j)
f i = k then
*build a new leaf node N‟ labeled (i, i)
else
*N‟ ←CONSTRUCT(R, i, k)
*N‟ is the left child of node N
if k = (j – 1) then
*build a new leaf node N‟‟ labeled (j, j)
else
*N‟‟ ←CONSTRUCT(R, k + 1, j)
*N‟‟ is the right child of node N
return N
end
COMPLEXITY ANALYSIS:
The algorithm requires O (n2) time and O (n2) storage. Therefore, as „n‟ increases it will run out of
storage even before it runs out of time. The storage needed can be reduced by almost halfby
implementing the two-dimensional arrays as one-dimensional arrays.
Conclusion: This program gives us the knowledge OBST, Extended binary search tree.
OUTCOME
Practical 09
Problem Definition:
A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating
values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how
many maximum comparisons may require for finding any keyword. Use Height balance tree and findthe complexity
for finding a keyword.
Prerequisite:
Theory :
An empty tree is height balanced tree if T is a nonempty binary tree with TL and TR as its left and right sub
trees. The T is height balance if and only if Its balance factor is 0, 1, -1.
AVL (Adelson- Velskii and Landis) Tree: A balance binary search tree. The best search time, that is O (log N)
search times. An AVL tree is defined to be a well-balanced binary search tree in which each of its nodes hasthe
AVL property. The AVL property is that the heights of the left and right sub-trees of a node are either equalor if
they differ only by 1
44
GROUP E
Practical 10
Problem Statement:
Implement the Heap/Shell sort algorithm implemented in Java demonstrating heap/shell data
structure with modularity of programming language
Prerequisite:
Theory:
This is a Java Program to implement Heap Sort on an integer array. Heapsort is a comparison-based sorting
algorithm to create a sorted array (or list), and is part of the selection sort family. Although somewhat
slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more
favorable worst-case O(n log n) runtime. Heapsort is an in-place algorithm, but it is not a stable sort.
Algorithm:
STEP 1: Logically, think the given array as Complete Binary Tree,
STEP 2: For sorting the array in ascending order, check whether the tree is satisfying
Max-heap property at each node,
(For descending order, Check whether the tree is satisfying Min-heap
property) Here we will be sorting in Ascending order,
STEP 3: If the tree is satisfying Max-heap property, then largest item is stored at the root of
the heap. (At this point we have found the largest element in array, Now if we
place this element at the end(nth position) of the array then 1 item in array is at
proper place.)
We will remove the largest element from the heap and put at its proper place(nth
position) in array.
After removing the largest element, which element will take its place?
We will put last element of the heap at the vacant place. After placing the last
element at the root, The new tree formed may or may not satisfy max-heap
property.
So, If it is not satisfying max-heap property then first task is to make changes to
the tree, So that it satisfies max-heap property.
(Heapify process: The process of making changes to tree so that it satisfies max-heap property
is called heapify)
When tree satisfies max-heap property, again largest item is stored at the root of the heap. We
will remove the largest element from the heap and put at its proper place(n-1 position) in array.
Repeat step 3 until size of array is 1 (At this point all elements are sorted.)
Output:
Problem statement:
Department maintains a student information. The file contains roll number, name, division and
address. Allow user to add, delete information of student. Display information of particular
employee. If record of student does not exist an appropriate message is displayed. If it is, then the
system displays the student details. Use sequential file to main the data
Prerequisite:
Theory:
File structure–
A file is a collection of records which are related to each other. The size of file is limited by the
size of memory and storage medium.
I. File Activity:
It specifies that percent of actual records proceeds in single run. If a small percent of record is
accessed at any given time, the file should be organized on disk for the direct access in contrast.
If a fare percentage of records affected regularly than storing the file on tape would be more
efficient & less costly.
File organisation –
A file is organised to ensure that records are available for processing. It should be designed with
the activity and volatility information and the nature of storage media, Other consideration are
cost of file media, enquiry, requirements of users and file‟s privacy, integrity, security and
confidentiality.
There are four methods for organising files-
1. Sequential organisation
2. Indexed Sequential organisation
3. Inverted list organization
5. Chaining
1. Sequential organization:
Sequential organization means storing and sorting in physical, contiguous blocks within files
on tape or disk. Records are also in sequence within each block. To access a record previous
records within
the block are scanned. In a sequential organization, records can be added only at the end of the
file. It is not possible to insert a record in the middle of the file without rewriting the file.
In a sequential file update, transaction records are in the same sequence as in the master file.
Records from both the files are matched, one record at a time, resulting in an updated master file.
In a
personal computer with two disk drives, the master file is loaded on a diskette into drive A,
while the transaction file is loaded on another diskette into drive B. Updating the master file
transfers data from drive B to A controlled by the software in memory.
Like sequential organization, keyed sequential organization stores data in physically contiguous
blocks. The difference is in the use of indexes to locate records. There are three areas in disk
storage: prime area, overflow area and index area. The prime area contains file records stored by
key or id numbers. All records are initially stored in the prime area. The overflow area contains
records added to the file that cannot be placed in logical sequence in the prime area. The index
area is more like a data dictionary. It contains keys of records and their locations on the disk. A
pointer associated with each key is an address that tells the system where to find a record.
Advantages:
i. Indexed sequential organization reduces the magnitude of the sequential search and
provides quick access for sequential and direct processing.
ii Records can be inserted in the middle of the file.
Disadvantages:
i. It takes longer to search the index for data access or
retrieval.
ii. Unique keys are required.
iii. Periodic reorganization is required
3. Inverted list organization:
Like the indexed- sequential storage method the inverted list organization maintains an index. The
two methods differ, however, in the index level and record storage. The indexed sequential method
has a multiple index for a given key, whereas the inverted list method has a single index for each
key type. In an inverted list, records are not necessarily stored in a particular sequence. They are
placed in the
data storage area, but indexes are updated for the record key and location. The inverted keys are
best for applications that request specific data on multiple keys. They are ideal for static files
because additions and deletions cause expensive pointer updating.
Advantages
i. Used in applications requesting specific data on multiple keys.
Aabsolute address represents the physical location of the record. It is usually stated in the formatof
sector/track/record number. One problem with absolute address is that they become invalid whenthe
file that contains the records is relocated on the disk.
A relative address gives a record location relative to the beginning of the file. There must be fixed
length records for reference. Another way of locating a record is by the number of bytes it is from the
beginning of the file. When the file is moved, pointers need not be updated because the relative location
remains the same.
Advantages:
i. Records can be inserted or updated in the middle of the file.
ii. Better control over record allocation.
Disadvantages:
i. Calculating address required for processing.
ii. Impossible to process variable length records.
5. Chaining:
File organization requires that relationships be established among data items. It must show how
characters form fields, fields form files and files relate to each other. Establishing relationship is done
through chaining. It uses pointers.
Example: The file below contains auto parts that are an indexed sequential file sequenced by part no. A
record can be retrieved by part no. To retrieve the next record, the whole file has to be searched. This can
be avoided by the use of pointers.
Algorithm:
Output:
Enter File Name: employee.txt
Enter details of the employee:
Name:Akash
Post: Worker
Name:Shrikant
Post:Manager
The content of employee.txt file
Name:Akash Post: Worker
Name:Shrikant Post: Manager
Problem Statement:
Learning Objectives:
To understand index sequential file to maintain the data.
Learning Outcome:
In Indexed Sequential Search a sorted index is set aside in addition to the array.
Each element in the index points to a block of elements in the array or another expanded index.
The index is searched 1st then the array and guides the search in the array.
Note: Indexed Sequential Search actually does the indexing multiple time, like creating the index of an
index.
58
Explanation by diagram “Indexed Sequential Search”:
In indexed sequential access file, sequential file and random file access is possible.
It accesses the records very fast if the index table is properly organized.
The records can be inserted in the middle of the file.
It provides quick access for sequential and direct processing.
It reduces the degree of the sequential search.
Disadvantages of Indexed sequential access file organization
Indexed sequential access file requires unique keys and periodic reorganization.
Indexed sequential access file takes longer time to search the index for the data access or retrieval.
It requires more storage space.
It is expensive because it requires special software.
It is less efficient in the use of storage space as compared to other file organizations
59
Mini Project
Problem Statement:
Design a mini project using JAVA which will use the different data structure with or without Java
collection library and show the use of specific data structure on the efficiency (performance) of the code
Prerequisite:
Theory:
The data structures provided by the Java utility package are very powerful and perform a wide range of
functions. These data structures consist of the following interface and classes −
Enumeration
BitSet
Vector
Stack
Dictionary
Hashtable
Properties
All these classes are now legacy and Java-2 has introduced a new framework called Collections
Framework, which is discussed in the next chapter. −
The Enumeration
The Enumeration interface isn't itself a data structure, but it is very important within the context of
other data structures. The Enumeration interface defines a means to retrieve successive elements
from a data structure.
For example, Enumeration defines a method called nextElement that is used to get the next element
in a data structure that contains multiple elements.
The BitSet
The BitSet class implements a group of bits or flags that can be set and cleared individually.
This class is very useful in cases where you need to keep up with a set of Boolean values; you just
assign a bit to each value and set or clear it as appropriate.
60
The Vector
The Vector class is similar to a traditional Java array, except that it can grow as necessary to
accommodate new elements.
Like an array, elements of a Vector object can be accessed via an index into the vector.
The nice thing about using the Vector class is that you don't have to worry about setting it to a specific
size upon creation; it shrinks and grows automatically when necessary.
The Stack
You can think of a stack literally as a vertical stack of objects; when you add a new element, it gets
stacked on top of the others.
When you pull an element off the stack, it comes off the top. In other words, the last element you
added to the stack is the first one to come back off.
The Dictionary
The Dictionary class is an abstract class that defines a data structure for mapping keys to values.
This is useful in cases where you want to be able to access data via a particular key rather than an integer
index.
Since the Dictionary class is abstract, it provides only the framework for a key-mapped data structure
rather than a specific implementation.
The Hashtable
The Hashtable class provides a means of organizing data based on some user-defined key structure.
For example, in an address list hash table you could store and sort data based on a key such as ZIP
code rather than on a person's name.
The specific meaning of keys with regard to hash tables is totally dependent on the usage of the hash
table and the data it contains.
The Properties
Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a
String and the value is also a String.
The Properties class is used by many other Java classes. For example, it is the type of object returned by
System.getProperties( ) when obtaining environmental values.
61