DSL writeup
DSL writeup
5 B-09 Convert given binary tree into threaded binary tree. Analyze time and space
complexity of the algorithm.
6 C-14 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 takes to reach city B from 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.
7 C-15 You have a business with several offices; you want to lease phone lines to
connect them up with each other; and the phone company charges different
amounts of money to connect different pairs of cities. You want a set of lines
that connects all your offices with a minimum total cost. Solve the problem by
suggesting appropriate data structures.
8 D-18 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?
9 D-19 A Dictionary stores keywords and 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 find the complexity for finding a keyword
10 E-20 Consider a scenario for Hospital to cater services to different kinds of patients
as Serious (top priority), b) non-serious (medium priority), c) General Checkup
(Least priority). Implement the priority queue to cater services to the patients.
11 F-23 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.
12 F-25 Implementation of a direct access file -Insertion and deletion of a record from a
direct access file
Assignment No: A1
Problem Statement : Consider telephone book database of N clients. Make use of a hash
table implementation to quickly look up client‘s telephone number. Make use of two
collision handling techniques and compare them using number of comparisons required
to find a set of telephone numbers
Title:
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
To understand concept of hashing.
To understand operations like insert and search record in the database.
Learning Outcome
Learn object oriented Programming features
Understand & implement concept of hash table .
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.
One of the biggest drawbacks to a language like C is that there are no keyed arrays. In a normal C array (also called an
indexed array), the only way to access an element would be through its index number. To find element 50 of an array
named "employees" you have to access it like this:
1employees[50];
In a keyed array, however, you would be able to associate each element with a "key," which can be anything from a
name to a product model number. So, if you have a keyed array of employee records, you could access the record of
employee "John Brown" like this:
1employees["Brown, John"];
One basic form of a keyed array is called the hash table. In a hash table, a key is used to find an element instead of an
index number. Since the hash table has to be coded using an indexed array, there has to be some way of transforming
a key to an index number. That way is called the hashing function.
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.
Let's say you wanted to organize a list of about 200 addresses by people's last names. A hash table would be ideal for
this sort of thing, so that you can access the records with the people's last names as the keys.
First, we have to determine the size of the array we're using. Let's use a 260 element array so that there can be an
average of about 10 element spaces per letter of the alphabet.>
Now, we have to make a hashing function. First, let's create a relationship between letters and numbers:
A --> 0
B --> 1
C --> 2
D --> 3
...
The easiest way to organize the hash table would be based on the first letter of the last name.
Since we have 260 elements, we can multiply the first letter of the last name by 10. So, when a key like "Smith" is given,
the key would be transformed to the index 180 (S is the 19 letter of the alphabet, so S --> 18, and 18 * 10 = 180).
Since we use a simple function to generate an index number quickly, and we use the fact that the index number can be
used to access an element directly, a hash table's access time is quite small. A linked list of keys and elements wouldn't be
nearly as fast, since you would have to search through every single key-element pair.
Basic Operations
DataItem
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;
};
Hash Method
Define a hashing method to compute the hash code of the key of the data item.
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
{
//get the hash
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
++hashIndex;
hashIndex %= SIZE;
return NULL;
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
{
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
++hashIndex;
hashIndex %= SIZE;
hashArray[hashIndex] = item;
Assignment Questions:
Title:
Objectives:
Learning Objectives
To understand the concept of set operations using set container.
Learning Outcome
Learn python function to implement set operations.
Understand & implement the concept of set container.
Theory:
A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Python’s set
class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it
has a highly optimized method for checking whether a specific element is contained in the set. This is based on a
data structure known as a hash table. Since sets are unordered, we cannot access items using indexes like we do
in lists.
Create a Set:
The most common way of creating a set in Python is by using the built-in set() function.
The set() function takes in an iterable and yields a list of objects which will be inserted into the set. The {} syntax places
the objects themselves into the set.
Adding elements:
Insertion in set is done through set.add() function, where an appropriate record value is created to store in the hash
table. Same as checking for an item, i.e., O(1) on average. However, in worst case it can become O(n).
Size:
A set is a collection data type that is unordered and mutable. A set cannot have duplicate elements. Here, the
task is to find out the number of elements present in a set. See the below examples.
Examples:
Input: a = {1, 2, 3, 4, 5, 6}
Output: 6
# of set
set1 = set()
set1.add(8)
set1.add(9) :
set1.add((6, 7))
Union:
Two sets can be merged using the union() function or | operator. Both Hash Table values are accessed and traversed
with merge operation perform on them to combine the elements, at the same time duplicates are removed. Time
Complexity of this is O(len(s1) + len(s2)) where s1 and s2 are two sets whose union needs to be done.
Intersection
This can be done through intersection() or & operator. Common Elements are selected. They are similar to iteration
over the Hash lists and combining the same values on both the Table. Time Complexity of this is O(min(len(s1),
len(s2)) where s1 and s2 are two sets whose union needs to be done.
Difference
To find difference in between sets. Similar to find difference in linked list. This is done through difference() or –
operator. Time complexity of finding difference s1 – s2 is O(len(s1))
Remove:
A particular item can be removed from a set using the methods discard() and remove().
The only difference between the two is that the discard() function leaves a set unchanged if the element is not present in
the set. On the other hand, the remove() function will raise an error in such a condition (if element is not present in the
set).
Algorithm:
1. Start
5. Apply set union, intersection and difference function on two set to find result.
6. Stop.
Conclusion:
Assignment Questions:
Program Code:
# Set in Python
set1 = set()
set2 = set()
set1.add(i)
set2.add(i)
print("\n")
print("\n")
print("\n")
print("\n")
set5.clear()
print("After applying clear on sets Set5: ")
/*Output:
('Set1 = ', set([1, 2, 3, 4, 5]))
('Set2 = ', set([3, 4, 5, 6, 7]))
Title:
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:
1. To understand concept of tree data structure
2. To understand concept & features of object oriented programming.
Learning Objectives:
To understand concept of class
To understand concept & features of object oriented programming.
To understand concept of tree data structure.
Learning Outcome:
Define class for structures using Object Oriented features.
Analyze tree data structure.
Input: Book name & its number of sections and subsections along with name.
Definition:
A tree T is a set of nodes storing elements such that the nodes have a parent-child
relationship that satisfies the following
• if T is not empty, T has a special tree called the root that has no parent
• each node v of T different than the root has a unique parent node w; each node with parent
w is a child of w
Recursive definition
• T is either empty
• or consists of a node r (the root) and a possibly empty set of trees whose roots are the
children of r
Tree is a widely-used data structure that emulates a tree structure with a set of linked
nodes. The tree graphicaly is represented most commonly as on Picture 1. The circles are
the nodes and the edges are the links between them.
Trees are usualy used to store and represent data in some hierarhical order. The data
are stored in the nodes, from which the tree is consisted of.
3. The height of a tree with > 1 element is equal to 1 + the height of its tallest subtree.
The depth of a node is the length of the path to its root (i.e., its root path). Every child
node is always one level lower than his parent.
The topmost node in a tree is called the root node. Being the topmost node, the root
node will not have parents. It is the node at which operations on the tree commonly begin
(although some algorithms begin with the leaf nodes and work up ending at the root). All
other nodes can be reached from it by following edges or links. (In the formal definition, a
path from a root to a node, for each different node is always unique). In diagrams, it is
typically drawn at the top.
In some trees, such as heaps, the root node has special properties.
A subtree is a portion of a tree data structure that can be viewed as a complete tree in
itself. Any node in a tree T, together with all the nodes below his height, that are reachable
from the node, comprise a subtree of T. The subtree corresponding to the root node is the
entire tree; the subtree corresponding to any other node is called a proper subtree (in
analogy to the term proper subset).
Every node in a tree can be seen as the root node of the subtree rooted at that node.
An internal node or inner node is any node of a tree that has child nodes and is thus
not a leaf node.
There are two basic types of trees. In an unordered tree, a tree is a tree in a purely
structural sense — that is to say, given a node, there is no order for the children of that
node. A tree on which an order is imposed — for example, by assigning different natural
numbers to each child of each node — is called an ordered tree, and data structures built on
them are called ordered tree data structures. Ordered trees are by far the most common form
of tree data structure. Binary search trees are one kind of ordered tree.
Important Terms
Root − The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
Parent − Any node except the root node has one edge upward to a node called parent.
Child − The node below a given node connected by its edge downward is called its
child node.
Leaf − The node which does not have any child node is called the leaf node.
Visiting − Visiting refers to checking the value of a node when control is on the node.
Levels − Level of a node represents the generation of a node. If the root node is at
level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
Advantages of trees
Trees are so useful and frequently used, because they have some very serious advantages:
Trees are very flexible data, allowing to move subtrees around with minumum effort
Objective:
To understand the basic concept of Non Linear Data Structure ―TREE ‖and its basic
operation in Data structure.
Outcome:
To implement the basic concept of Binary Search Tree to store a numbers in it. Also
perform basic Operation Insert, Delete and search,Traverse in tree in Data structure.
Binary Search tree exhibits a special behavior. A node's left child must have a value less than its
parent's value and the node's right child must have a value greater than its parent value.
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
The left sub-tree of a node has a key less than or equal to its parent node's key.
The right sub-tree of a node has a key greater than or equal to its parent node's key.
Thus, BST divides all its sub-trees into two segments;the left sub-tree and the right sub-tree and
can be defined as –
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
Tree Node
Following Structure is used for Node creation
Struct node {
Int data ;
Struct node *leftChild;
Struct node *rightChild;
};
Basic Operations
The basic operations that can be performed on a binary search tree data structure, are the
following −
Insert− Inserts an element in a tree/create a tree.
Search− Searches an element in a tree.
Traversal− A traversal is a systematic way to visit all nodes of T -Inorder, Preprder, Postorder,
a. pre-order: Root, Left, Right
Parent comes before children; overall root first
B.post-order: Left, Right, Root
Parent comes after children; overall root last
c. In Order: In-order: Left, Root, Right,
Algorithm
Tree Traversal
In order traversal algorithm
Pre order traversal Algorithm
Deleting in a BST
Conclusion/analysis
We are able to implement Binary search Tree and its operation in Data Structure.
Assignment No. B 9
Problem Definition:
Write a programme to Convert given binary tree into threaded binary tree. Analyze time and space
complexity of the algorithm.
Prerequisite:
1. Basics of the Object oriented Programming & its features like Class, Object,
Constructor, Destructor and Friend Function etc.
Theory:
We extend the concept of linked data structures to structure containing nodes with more than one
self-referenced field. A binary tree is made of nodes, where each node contains a "left" reference, a
"right" reference, and a data element. The topmost node in the tree is called the root.
Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This
node is called a parent. On the other hand, each node can be connected to arbitrary number of nodes, called
children. Nodes with no children are called leaves, or external nodes. Nodes which are not leaves are
called internal nodes. Nodes with the same parent are called siblings.
The depth of a node is the number of edges from the root to the node.
The height of a node is the number of edges from the node to the deepest leaf.
The height of a tree is a height of the root.
A full binary tree.is a binary tree in which each node has exactly zero or two children.
A complete binary tree is a binary tree, which is completely filled, with the possible exception of
the bottom level, which is filled from left to right.
A complete binary tree is very special tree, it provides the best possible ratio between the number of nodes
and the height. The height h of a complete binary tree with N nodes is at most O(log N). We can easily
prove this by counting nodes on each level, starting with the root, assuming that each level has the
maximum number of nodes:
h-1 h h+1
n = 1 + 2 + 4 + ... + 2 +2 =2 -1
Solving this with respect to h, we obtain
h = O(log n)
Advantages of trees
Trees are so useful and frequently used, because they have some very serious advantages:
Traversals
A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is
no unique traversal. We will consider several traversal algorithms with we group in the following two kinds
depth-first traversal
breadth-first traversal
PreOrder traversal - visit the parent first and then left and right children;
InOrder traversal - visit the left child, then the parent and the right child;
PostOrder traversal - visit left child, then the right child and then the parent;
There is only one kind of breadth-first traversal--the level order traversal. This traversal visits nodes by
levels from top to bottom and from left to right.
In the next picture we demonstarte the order of node visitation. Number 1 denote the first node in a
particular traversal and 7 denote the last node.
These common traversals can be represented as a single algorithm by assuming that we visit each node three
times. An Euler tour is a walk around the binary tree where each edge is treated as a wall, which you cannot
cross. In this walk each node will be visited either on the left, or under the below, or on the right. The Euler
tour in which we visit nodes on the left produces a preorder traversal. When we visit nodes from the below,
we get an inorder traversal. And when we visit nodes on the right, we get a postorder traversal.
Threaded Binary Tree
Inorder traversal of a Binary tree is either be done using recursion or with the use of a auxiliary stack. The
idea of threaded binary trees is to make inorder traversal faster and do it without stack and without
recursion. A binary tree is made threaded by making all right child pointers that would normally be NULL
point to the inorder successor of the node (if it exists).
Double Threaded: Where both left and right NULL pointers are made to point to inorder predecessor and
inorder successor respectively. The predecessor threads are useful for reverse inorder traversal and
postorder traversal.
The threads are also useful for fast accessing ancestors of a node.
Following diagram shows an example Single Threaded Binary Tree. The dotted lines represent threads.
Algorithm:
struct Node
{
int key;
Node *left, *right;
if (root->left)
createThreadedUtil(root->left, q);
q->pop();
if (root->right)
createThreadedUtil(root->right, q);
Output:
Inorder traversal of creeated threaded tree is 4 2 5 1 6 3 7
Conclusion: In this way we have implemented the concept of threaded binary tree in c++.
Assignment Questions
1. What is a B tree?
2. What are threaded binary trees?
3. What is a B+ tree?
4. How do you find the depth of a binary tree?
5. Explain pre-order and in-order tree traversal.
6. Define threaded binary tree. Explain its common uses
7. Explain implementation of deletion from a binary tree.
Assignment C 14
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 B from
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. Justify the storage representation used.
Title:
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 B from 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. Justify
the storage representation used.
Objectives:
1. To understand concept of Graph data structure
2. To understand concept of representation of graph.
Learning Objectives:
To understand concept of Graph data structure
To understand concept of representation of graph.
Learning Outcome:
Theory:
Graphs are the most general data structure. They are also commonly used data structures.
Graph definitions:
A non-linear data structure consisting of nodes and links between nodes.
Each node is called a vertex, each link is called an edge, and each edge connects two vertices.
Graph Implementation:
Different kinds of graphs require different kinds of implementations, but the fundamental concepts of all
graph implementations are similar. We'll look at several representations for one particular kind of graph:
directed graphs in which loops are allowed.
An adjacency matrix is a square grid of true/false values that represent the edges of a graph.
If the graph contains n vertices, then the grid contains n rows and n columns.
For two vertex numbers i and j, the component at row i and column j is true if there is an edge from
vertex i to vertex j; otherwise, the component is false.
Once the adjacency matrix has been set, an application can examine locations of the matrix to determine which
edges are present and which are missing.
To represent a graph with n vertices, we can declare an array of n sets of integers. For example:
A set such as connections[i] contains the vertex numbers of all the vertices to which vertex i is connected.
1. What are different ways to represent the graph? Give suitable example.
Assignment No. C 15
Problem Definition:
You have a business with several offices; you want to lease phone lines to connect them up with each other;
and the phone company charges different amounts of money to connect different pairs of cities. You want a set
of lines that connects all your offices with a minimum total cost. Solve the problem by suggesting appropriate
data structures
Theory:
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses the greedy approach.
Prim's algorithm shares a similarity with the shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on adding
new nodes to the spanning tree from the given graph.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shall use the same example −
In this case, we choose S node as the root node of Prim's spanning tree. This node is arbitrarily chosen, so any
node can be the root node. One may wonder why any video can be a root node. So the answer is, in the
spanning tree all the nodes of a graph are included and because it is connected then there must be at least one
edge, which will join it to the rest of the tree.
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with weight 7 and 8, respectively.
We choose the edge S,A as it is lesser than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We select the one
which has the lowest cost and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will check all the edges again.
However, we will choose only the least cost edge. In this case, C-3-D is the new edge, which is less than
other edges' cost 8, 6, 4, etc.
After adding node D to the spanning tree, we now have two edges going out of it having the same cost, i.e. D-2-
T and D-2-B. Thus, we can add either one. But the next step will again yield edge 2 as the least cost. Hence, we
are showing a spanning tree with both edges included.
1. Create a tree containing a single vertex, chosen arbitrarily from the graph
2. Create a set containing all the edges in the graph
3. Loop until every edge in the set connects two vertices in the tree
a. Remove from the set an edge with minimum weight that connects a vertex in the tree with a vertex not in the tree
b. Add that edge to the tree
ALGORITHM:
Create Function:
Step 1: Start.
Step 2: Create char, integer variable.
Step 3: Take how many vertices of graph and then take vertices ofedges and cost of
edges. Step 4: Store zero in visited array and matrix array for graph.
Step 5: Vertex is -99 then break loop, otherwise stored arrayg[v1][v2]=cost. If cost is stored then also g[v2][v1]
alsostore same cost.
Step 6: Ask user whether he wants to enter more edges if ‗Yes‘ thengoto Step 7.
Step 7: Stop.
Display Function:
Step 1: Start.
Step 2: Using two for loop print cost of edges of matrix array onscreen.
Step 3: Stop
Prims Function:
Step 1: Start.
Step 2: Initially declare cost, matrix, visited from distance array.
Step 3: Using two for loop check if edge is present or not. If notthen stored infinity value else stored cost of G
matrix incost matrix and then stored zero in st matrix.
Step 4: Take first vertex and using for take all distances in from thatvertex in distance array and put zero in
from and visited arrayat that vertex position.
Step 5: Initialize min cost =0 and no of edge=n-1.
Step 6: Using while loop take min distance infinity and again use forloop, check vertex is not visited and distance
of that vertexis less min distance. If true then v = i and min distance=thatdistance.
Step 7: Stored that cost in st matrix for both vertices, edges anddecrease no of edge. Visited array is initialized =
1.
Step 8: Using for loop check vertex is not visited and distance isminimum. If true then stored that vertex in from
array andCost of that edges in distance array.
Step 9: Add all min cost stored in min cost and return min
cost. Stop 10: Stop.
Main Function:
Step 1: Start.
Step 2: Create object of graph and Integer and character variables.
Step 3: Print Menu like
1. Create.
2. Display.
3. Spanning Tree and find minimum cost.
Step 4: Take choice from user.
Step 5: If choice is 1 then call Create Function.
Step 6: If choice is 2 then call Display Function.
Step 7: If choice is 3 then call prims Function.
Step 8: Ask user whether he wants to continue or
not. Step 9: If yes then go to step 3.
Step 10: Stop.
OUTPUT :-
2 for prims_mst
3 for exit
enter no. of
offices 5
enter no. of
lanes 8
enter weight of
Lane 3
enter weight of
Lane 3
enter weight of
Lane 4
enter weight of
Lane 3
enter weight of
Lane 1
enter weight of
Lane 2
enter weight of
Lane 2
2 for prims_mst
3 for exit
2 for prims_mst
3 for exit
Assignment Questions:
Title:
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 half by
implementing the two-dimensional arrays as one-dimensional arrays.
OUTCOME
Title:
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 find the complexity for finding a keyword.
Objectives:
Learning Objectives:
To understand concept of height balanced tree data structure.
To understand procedure to create height balanced tree.
Learning Outcome:
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 has the AVL property. The AVL property is that the heights of the left and
right sub-trees of a node are either equal or if they differ only by 1.
What if the input to binary search tree comes in a sorted (ascending or descending) manner?
It will then look like this −
It is observed that BST's worst-case performance is closest to linear search algorithms, that is
Ο(n). In real-time data, we cannot predict data pattern and their frequencies. So, a need arises to
balance out the existing BST.
Named after their inventor Adelson, Velski & Landis, AVL trees are height balancing
binary search tree. AVL tree checks the height of the left and the right sub-trees and assures that the
difference is not more than 1. This difference is called the Balance Factor.
Here we see that the first tree is balanced and the next two trees are not balanced −
In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the
difference is 2. In the third tree, the right subtree of A has height 2 and the left is missing, so it is 0,
and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.
If the difference in the height of left and right sub-trees is more than 1, the tree is balanced using
some rotation techniques.
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations −
Left rotation
Right rotation
Left-Right rotation
Right-Left rotation
The first two rotations are single rotations and the next two rotations are double rotations. To
have an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's understand
them one by one.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right
subtree, then we perform a single left rotation −
In our example, node A has become unbalanced as a node is inserted in the right subtree of
A's right subtree. We perform the left rotation by making A the left-subtree of B.
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree.
The tree then needs a right rotation.
As depicted, the unbalanced node becomes the right child of its left child by performing a right
rotation.
Left-Right Rotation
Double rotations are slightly complex version of already explained versions of rotations. To
understand them better, we should take note of each action performed while rotation. Let's first check
how to perform Left-Right rotation. A left-right rotation is a combination of left rotation followed by
right rotation.
State Action
A node has been inserted into the right subtree of the left subtree.
This makes C an unbalanced node. These scenarios cause AVL
tree to perform left-right rotation.
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of right rotation
followed by left rotation.
State Action
A node has been inserted into the left subtree of the right subtree.
This makes A, an unbalanced node with balance factor 2.
RotateWithLeftChild( AvlNode k2 )
AvlNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
RotateWithRightChild( AvlNode k1 )
AvlNode k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = max( height( k2.right ), k1.height ) + 1;
return k2;
doubleWithRightChild( AvlNode k1 )
Output: Allow Add, delete operations on dictionary and also display data in sorted order.
Conclusion: This program gives us the knowledge height balanced binary tree.
OUTCOME
---------------------------------------------------------------------------------------------------------------------------------
Problem Statement : Consider a scenario for Hospital to cater services to different kinds of
patients as Serious (top priority), b) non-serious (medium priority), c) General Checkup (Least
priority). Implement the priority queue to cater services to the patients.
OBJECTIVE:
To understand the concept of priority Queue.
How data structures Queue is represented as an ADT.
THEORY:
1) What is Queue? Explain Queue operations with neat diagrams?
A queue is a particular kind of collection in which the entities in the collection are kept in order and the
principal (or only) operations on the collection are the addition of entities to the rear terminal positionand removal
of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a
FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to
the requirement that once an element is added, all elements that were added before have to be removed before the
new element can be invoked. A queue is an example of a linear data structure.
Queues provide services in computer science, transport, and operations research where various entities
such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue
performs the function of a buffer. Queues are common in computer programs, where they are implemented as data
structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes.
Common implementations are circular buffers and linked lists. Queue is a data structure that maintain "First In
First Out" (FIFO) order. And can be viewed as people queueing up to buy a ticket. In programming, queue is
usually used as a data structure for BFS (BreadthFirst Search).
Operations on queue:
1. enqueue - insert item at the back of queue Q
2. dequeue - return (and virtually remove) the front item from queue Q
3. displayfront - return (without deleting) the front item from queue Q
The rule that determines who goes next is called a queueing discipline. The simplest queueing discipline is
called FIFO, for "first-in-first-out." The most general queueing discipline is priority queueing, in which each customer is
assigned a priority, and the customer with the highest priority goes first, regardless of the order of arrival. The reason
I say this is the most general discipline is that the priority can be based on anything: what time a flight leaves, how
many groceries the customer has, or how important the customer is. Of course, not all queueing disciplines are "fair,"
but fairness is in the eye of the beholder.
The Queue ADT and the Priority Queue ADT have the same set of operations and their interfaces are the
same. The difference is in the semantics of the operations: A Queue uses the FIFO policy, and a
Priority Queue (as the name suggests) uses the priority queueing policy. As with most ADTs, there are anumber of
ways to implement queues Since a queue is a collection of items, we can use any of the basic mechanisms for
storing collections: arrays, lists, or vectors. Our choice among them will be based in part on their performance---
how long it takes to perform the operations we want to perform--- and partly on ease of implementation.
ALGORITHM: Define structure for Queue (Priority, Patient Info, Next Pointer).
Empty Queue:
Return True if Queue is Empty else False.isEmpty (Front)
Front is pointer of structure, which is first element of Queue.Step 1: If Front =
= NULL
Step 2: Return 1
Step 3: Return 0
Insert Function:
Insert Patient in Queue with respect to the Priority.
Front is pointer variable of type Queue, which is 1st node of Queue.
Patient is a pointer variable of type Queue, which hold the information about new patient.
Insert (Front, Queue)
Step 1: If Front = = NULL //Queue Empty
Then Front = Patient;
Step 2: Else if Patient->Priority > Front->Priority Then
i) Patient->Next = Front;
ii) Front=Patient;
Step 3: Else
A) Temp=Front;
B) Do Steps a while Temp! = NULL and Patient->Priority <= Temp->Next->Priority
a) Temp=Temp->Next;
c) Patient->Next = Temp->Next;
Temp->Next = Patient;
Step 4: Stop.
Delete Patient details from Queue after patient get treatment:
Front is pointer variable of type Queue, which is 1st node of Queue.Delete Node from Front.
Delete (Front)
Step 1: Temp = Front;
Step 2: Front = Front->Next;Step 3: return
Temp
INPUT:
Test Case O/P
Queue Empty Display message ¯Queue Empty‖
Queue Full Display message ¯Queue Full‖
Name of patients & category of patient like
a) Serious (top priority), b) non-serious (medium priority), c) General Checkup (Least priority).
E.g. Enter patient arrival in the hospital with following priorities 1, 3, 2, 2, 1, 3
OUTPUT:
Priority queue cater services to the patients based on priorities. Patient should be given service in thefollowing order 3, 3,
2, 2, 1, 1
Note: 3 means top priority.
-------------------------------------------------------------------------------------------------------------------------------------------
---
OUTCOME: After successful implementation of this assignment, we understood the priority queue as ADT using single linked
list.
-------------------------------------------------------------------------------------------------------------------------------------------
---
FAQ:
1. What are the types of data structure?
2. What are the operations can implement on queue?
3. What is circular queue?
4. What is Multi queue?
5. Explain importance of stack in recursion
6. Explain Implicit & Explicit Stack.
7. Applications of stack and Queue as a data structure
--------------------------------------------------------------------------------------------------------------------------------
Assignment F 23
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 organisation
4. Direct access organisation
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.
Advantages:
i.Simple to design
ii.Easy to program
iii.Variable length and blocked records
available iv.Best use of storage space
Disadvantages
i.Records cannot be added at the middle of the file.
Like sequential organization, keyed sequential organization stores data in physicallycontiguous 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 require
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, where as 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.
A absolute address represents the physical location of the record. It is usually stated in the format of
sector/track/record number. One problem with absolute address is that they become invalid when the
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
Conclusion:
Assignment Questions:
Problem statement - Implementation of a direct access file -Insertion and deletion of a record from a direct access
file
Theory :
File
A file is basically a sequence of bytes organized into blocks that are understandable by any machines. In other
words, the collection of related information that is stored in a secondary storage device is also called a file. The
file is a collection of logically related entities. According to the users view a file is the smallest allots space of the
logical secondary storage. The object that stores data, information, settings or commands used with a
computer program on a computer is called file. In graphical user interface (GUI) such as Microsoft Windows,
files display as icons that relate to the program that opens the file.
The file contains the information but when it required to used this information can be access by the access
methods and reads into the computer memory. Some system provides only one access method and some
provide more than on access method to access the file,
Sometimes it is not necessary to process every record in a file. It is not necessary to process all the records in
the order in which they are present in the memory. In all such cases, direct access is used.
The disk is a direct access device which gives us the reliability to random access of any file block. In the file,
there is a collection of physical blocks and the records of that blocks.
Eg. Databases are often of this type since they allow query processing that involves immediate access to large
amounts of information. All reservation systems fall into this category.
Not all operating systems support direct access files. The sequential and direct access of the file is defined at
the time of creation and accessed accordingly later. The direct access of a sequential file is not possible but
Sequential access to a direct access file is possible.
Direct Access • Sometimes it is not necessary to process every record in a file. • It is not necessary to process all the
records in the order in which they are present in the memory. In all such cases, direct access is used. • The disk is a direct
access device which gives us the reliability to random access of any file block. • In the file, there is a collection of physical
blocks and the records of that blocks. • Example: Databases are often of this type since they allow query processing that
involves immediate access to large amounts of information. All reservation systems fall into this category. In brief: • This
method is useful for disks. • The file is viewed as a numbered sequence of blocks or records.
There are no restrictions on which blocks are read/written, it can be dobe in any order. • User now says "read n" rather
than "read next". • "n" is a number relative to the beginning of file, not relative to an absolute physical disk location.
Advantages:
• Direct access file helps in online transaction processing system (OLTP) like online railway reservation system. • In direct
access file, sorting of the records are not required. • It accesses the desired records immediately. • It updates several files
quickly. • It has better control over record allocation.
Disadvantages:
• Direct access file does not provide backup facility. • It is expensive. • It has less storage space as compared to sequential
file