0% found this document useful (0 votes)
3 views64 pages

DSL writeup

The document outlines a series of assignments for a Data Structure Lab, covering various topics such as hash tables, sets, trees, graphs, and priority queues. Each assignment includes problem statements, objectives, learning outcomes, and theoretical explanations related to the data structures being implemented. The assignments aim to enhance understanding of data structures through practical implementation and analysis.

Uploaded by

Aniket Kachole
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)
3 views64 pages

DSL writeup

The document outlines a series of assignments for a Data Structure Lab, covering various topics such as hash tables, sets, trees, graphs, and priority queues. Each assignment includes problem statements, objectives, learning outcomes, and theoretical explanations related to the data structures being implemented. The assignments aim to enhance understanding of data structures through practical implementation and analysis.

Uploaded by

Aniket Kachole
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/ 64

Data Structure Lab

List of Assignments and Write up


SR_NO Group/ Problem Statement
Assignment
No
1 A-01 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
2 A-04 To create ADT that implements the "set" concept.
a. Add (new Element) -Place a value into the set , b. Remove (element) Remove
the value c. Contains (element) Return true if element is in collection, d. Size ()
Return number of values in collection Iterator () Return an iterator used to loop
over collection, e. Intersection of two sets , f. Union of two sets, g. Difference
between two sets, h. Subset
3 B-05 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.
4 B-07 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
iii.Minimum data value found in the tree
iv.Search a value

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:

1. To understand concept of Hashing


2. To understand to find record quickly using hash function.
3. To understand concept & features of object oriented programming.

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.

Keyed Arrays vs. Indexed Arrays

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

...

and so on until Z --> 25.

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

Following are the basic primary operations of a hash table.

 Search − Searches an element in a hash table.


 Insert − inserts an element in a hash table.
 delete − Deletes an element from a hash table.

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.

int hashCode(int key){

return key % SIZE;

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

struct DataItem *search(int key)

{
//get the hash

int hashIndex = hashCode(key);

//move in array until an empty

while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)

return hashArray[hashIndex];

//go to next cell

++hashIndex;

//wrap around the table

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

void insert(int key,int data)

{
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));

item->data = data;

item->key = key;

//get the hash

int hashIndex = hashCode(key);

//move in array until an empty or deleted cell

while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { //go to next


cell

++hashIndex;

//wrap around the table

hashIndex %= SIZE;

hashArray[hashIndex] = item;

Conclusion: (write it in your words.)

Assignment Questions:

1.What is Hash Function?

2.what is Good Hash function?

3.How many ways are there to implement hash function?

4.What are the Collision Resolution Strategies?

5.What is the Hash Table Overflow?


Assignment No: A4
Problem statement: To create ADT that implements the "set" concept. Add (new Element) -Place a value into the set
, b. Remove (element) Remove the value c. Contains (element) Return true if element is in collection, d. Size () Return
number of values in collection Iterator () Return an iterator used to loop over collection, e. Intersection of two sets , f.
Union of two sets, g. Difference between two sets, h. Subset

Title:

Create ADT that implements the "set operations.

Objectives:

1. To understand concept of set .


2. To implement add , remove, size and contain functions of set container.
3. To find intersection union and differences between two sets.

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.

Methods for Sets

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

Input: a = {'wel', 'come'}


Output: 2
Sample code to find size of set:

# Python program to find the length

# of set

set1 = set()

# Adding element and tuple to the Set

set1.add(8)

set1.add(9) :

set1.add((6, 7))

print("The length of set is:", len(set1))

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

2. Input two sets s1, set s2

3. Use add function to add() elements in set.

4. Use of len () function to find length of set.

5. Apply set union, intersection and difference function on two set to find result.

6. Stop.

Conclusion:
Assignment Questions:

1. Explain properties of sets?


2. Explain set operations with syntax in python a) Set difference b) union?

Program Code:

# Python program to demonstrate working# of

# Set in Python

# Creating two sets

set1 = set()

set2 = set()

# Adding elements to set1

for i in range(1, 6):

set1.add(i)

# Adding elements to set2

for i in range(3, 8):

set2.add(i)

print("Set1 = ", set1)

print("Set2 = ", set2)

print("\n")

# Union of set1 and set2

set3 = set1 | set2# set1.union(set2)

print("Union of Set1 & Set2: Set3 = ", set3)


# Intersection of set1 and set2

set4 = set1 & set2# set1.intersection(set2)

print("Intersection of Set1 & Set2: Set4 = ", set4)

print("\n")

# Checking relation between set3 and set4

if set3 > set4: # set3.issuperset(set4)

print("Set3 is superset of Set4")

else if set3 < set4: # set3.issubset(set4)

print("Set3 is subset of Set4")

else : # set3 == set4

print("Set3 is same as Set4")

# displaying relation between set4 and set3

if set4 < set3: # set4.issubset(set3)

print("Set4 is subset of Set3")

print("\n")

# difference between set3 and set4

set5 = set3 - set4

print(" Difference, Elements in Set3 and not in Set4: Set5 = ",


set5)

print("\n")

# Removing all the values of set5

set5.clear()
print("After applying clear on sets Set5: ")

print("Set5 = ", set5)

/*Output:
('Set1 = ', set([1, 2, 3, 4, 5]))
('Set2 = ', set([3, 4, 5, 6, 7]))

('Union of Set1 & Set2: Set3 = ', set([1, 2, 3, 4, 5, 6, 7]))


('Intersection of Set1 & Set2: Set4 = ', set([3, 4, 5]))
Difference, Elements in Set3 and not in Set4: Set5 = ", set5/*
Assignment No:- B-5
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.

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.

Software Required: g++ / gcc compiler- / 64 bit Fedora, eclipse IDE

Input: Book name & its number of sections and subsections along with name.

Output: Formation of tree structure for book and its sections.


Theory:
Introduction to Tree:

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.

A node may contain a value or a condition or represent a separate data structure or a


tree of its own. Each node in a tree has zero or more child nodes, which are one level lower
in the tree hierarchy (by convention, trees grow down, not up as they do in nature). A node
that has a child is called the child's parent node (or ancestor node, or superior). A node has
at most one parent. A node that has no childs is called a leaf, and that node is of course at
the bottommost level of the tree. The height of a node is the length of the longest path to a
leaf from that node. The height of the root is the height of the tree. In other words, the
"height" of tree is the "number of levels" in the tree. Or more formaly, the height of a tree is
defined as follows:

1. The height of a tree with no elements is 0

2. The height of a tree with 1 element is 1

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.

Fig1. An example of a tree

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

Following are the important terms with respect to tree.


 Path − Path refers to the sequence of nodes along the edges of a tree.

 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.

 Subtree − Subtree represents the descendants of a node.

 Visiting − Visiting refers to checking the value of a node when control is on the node.

 Traversing − Traversing means passing through nodes in a specific order.

 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.

 keys − Key represents a value of a node based on which a search operation is to be


carried out for a node.

Advantages of trees
Trees are so useful and frequently used, because they have some very serious advantages:

 Trees reflect structural relationships in the data

 Trees are used to represent hierarchies

 Trees provide an efficient insertion and searching

 Trees are very flexible data, allowing to move subtrees around with minumum effort

For this assignment we are considering the tree as follows.


Conclusion: This program gives us the knowledge tree data structure.
Assignment No:- B-7
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
iii.Minimum data value found inthe tree
iv. Change a tree so that the roles of the left and right pointers are swapped at
every node
v. Search a value

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.

Software & Hardware Requirements:


1. 64-bit Open source Linux or its derivative
2. Open Source C++ Programming tool like G++/GCC

Theory – Concept in brief


Tree represents the nodes connected by edges. Binary Tree is a special data structure used for
data storage purposes. A binary tree has a special condition that each node can have a maximum
of two children. A binary tree has the benefits of both an ordered array and a linked list as search
is as quick as in a sorted array and insertion or deletion operation are as fast as in linked list.

Binary Search Tree Representation

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;
};

Fig: Binary Search Tree BST

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,

Insert Operation: Algorithm


Search Operation:

Algorithm

Tree Traversal
In order traversal algorithm
Pre order traversal Algorithm

Post order traversal Algorithm

Deleting in a BST

case 1: delete a node with zero child-


if x is left of its parent, set
parent(x).left = null else set
parent(x).right = null
case 2: delete a node with one child
link parent(x)
to the child of x case
3: delete a node with
2 children
Replace inorder successor to deleted node position.

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.

2. Knowledge of the Object Oriented Language like C++.

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.

More tree terminology:

 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:

 Trees reflect structural relationships in the data 


 Trees are used to represent hierarchies 
 Trees provide an efficient insertion and searching 
 Trees are very flexible data, allowing to move subtrees around with minumum effort 

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 

There are three different types of depth-first traversals, :

 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.

As an example consider the following tree and its


four traversals:

PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3


InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2

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).

There are two types of threaded binary trees.


Single Threaded: Where a NULL right pointers is made to point to the inorder successor (if successor 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:

1. Function to put the Nodes in inorder into queue.


2. Function to traverse queue, and make tree threaded.
3. If right pointer is NULL, link it to the inorder successor and set 'isThreaded' bit.
4. This function uses populateQueue( ) and createThreadedUtil( ) to convert a given binary tree
to threaded tree.
5. Create a queue to store inorder traversal.
6. Store inorder traversal in queue.
7. Link NULL right pointers to inorder successor.
8. A utility function to find leftmost node in a binary tree rooted with 'root'. This function is used
in inOrder().
9. Function to do inorder traversal of a threadded binary tree.
10. Find the leftmost node in Binary Tree.
11. If this Node is a thread Node, then go to inorder successor Else go to the leftmost child in
right subtree.
12. A utility function to create a new node.

Following is structure of single threaded binary tree.

struct Node
{
int key;
Node *left, *right;

// Used to indicate whether the right pointer is a normal right


// pointer or a pointer to inorder successor.
bool isThreaded;
};

// Helper function to put the Nodes in inorder into queue


void populateQueue(Node *root, std::queue <Node *> *q)
{
if (root == NULL)
return; if (root->left)
populateQueue(root->left,
q); q->push(root);
if (root->right)
populateQueue(root->right, q);
}

// Function to traverse queue, and make tree threaded


void createThreadedUtil(Node *root, std::queue <Node *> *q)
{
if (root == NULL) return;

if (root->left)
createThreadedUtil(root->left, q);
q->pop();

if (root->right)
createThreadedUtil(root->right, q);

// If right pointer is NULL, link it to the


// inorder successor and set 'isThreaded'
bit. else
{
root->right = q->front();
root->isThreaded = true;
}
}

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:

 Define class for graph using Object Oriented features.


 Analyze working of functions.

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.

Undirected graph definition:


 An undirected graph is a set of nodes and a set of links between the nodes.

 Each node is called a vertex, each link is called an edge, and each edge connects two vertices.

 The order of the two connected vertices is unimportant.


 An undirected graph is a finite set of vertices together with a finite set of edges. Both sets might be
empty, which is called the empty graph.

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.

Representing Graphs with an Adjacency Matrix


Definition:

 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.

We can use a two-dimensional array to store an adjacency matrix:

boolean[][] adjacent = new boolean[4][4];

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.

Representing Graphs with Edge Lists


Definition:

 A directed graph with n vertices can be represented by n different linked lists.

 List number i provides the connections for vertex i.

 For each entry j in list number i, there is an edge from i to j.

Loops and multiple edges could be allowed.

Representing Graphs with Edge Sets

To represent a graph with n vertices, we can declare an array of n sets of integers. For example:

IntSet[] connections = new IntSet[10]; // 10 vertices

A set such as connections[i] contains the vertex numbers of all the vertices to which vertex i is connected.

Software Required: g++ / gcc compiler- / 64 bit Fedora, eclipse IDE


Input: 1.Number of cities.
2.Time required to travel from one city to another.

Output: Create Adjacency matrix to represent path between various cities.


Conclusion: This program gives us the knowledge of adjacency matrix graph.

Questions asked in university exam.

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 −

Step 1 - Remove all loops and parallel edges


Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one which
has the least cost associated and remove all others.

Step 2 - Choose any arbitrary node as root node

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

1 for create graph

2 for prims_mst

3 for exit

Enter Your Choice1

enter no. of
offices 5
enter no. of
lanes 8

enter lane in v1,v2 form


01

enter weight of
Lane 3

enter lane in v1,v2 form


12

enter weight of
Lane 3

enter lane in v1,v2 form


23

enter weight of
Lane 4

enter lane in v1,v2 form


30

enter weight of
Lane 3

enter lane in v1,v2 form


34

enter weight of
Lane 1

enter lane in v1,v2 form


41

enter weight of
Lane 2

enter lane in v1,v2 form


04

enter weight of
Lane 2

enter lane in v1,v2 form


42

enter weight of Lane 1

1 for create graph

2 for prims_mst

3 for exit

Enter Your Choice2 1


The o/p is : H 0-->H 4 H 4-->H
2 H 2-->H 3
The Cost Of MST Is :7 1 for create graph

2 for prims_mst

3 for exit

Enter Your Choice3

Assignment Questions:

1. What is Spanning Tree?


2. What is Minimum Spanning Tree?
3. What is Prim‟s Algorithm?
4. What is Kruskal‟s Algorithm?
5. What are Applications of Minimum Spanning Tree?
6. How to decide whether to select Kruskal‟s algorithm or Prim‟s algorithm

7. What is shortest path algorithm?

8. What is minimum spanning tree of a graph?

9. How to calculate shortest path of graph?


Assignment D18
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?

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:

1. To understand concept of OBST.


2. To understand concept & features like extended binary search tree.

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.

Software Required: g++ / gcc compiler- / 64 bit Fedora, eclipse IDE

Input: 1.No.of Element.


2. key values
3. Key Probability

Output: Create binary search tree having optimal searching cost.


Conclusion: This program gives us the knowledge OBST, Extended binary search tree.

OUTCOME

Upon completion Students will be able to:

ELO1: Learn object oriented Programming features.


ELO2: Understand & implement extended binary search tree.

Questions asked in university exam.

1. What is Binary Search Tree? Explain with an example?


2. Explain various operations on BST?
3. What is inorder traversal of BST?
4. What is a B tree?
5. What are threaded binary trees?
6. What is a B+ tree?
7. How do you find the depth of a binary tree?
8. Explain pre-order and in-order tree traversal.
9. Define threaded binary tree. Explain its common uses
10. Explain various operations on BST?
Assignment D-19
Problem Statement : 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.

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:

1. To understand concept of height balanced tree data structure.


2. To understand procedure to create height balanced tree.

Learning Objectives:
 To understand concept of height balanced tree data structure.
 To understand procedure to create height balanced tree.

Learning Outcome:

 Define class for AVL using Object Oriented features.


 Analyze working of various operations on AVL Tree .

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.

BalanceFactor = height(left-sutree) − height(right-sutree)

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.

We first perform the left rotation on the left subtree of C. This


makes A, the left subtree of B.

Node C is still unbalanced, however now, it is because of the left-


subtree of the left-subtree.
We shall now right-rotate the tree, making B the new root node of
this subtree. C now becomes the right subtree of its own left
subtree.

The tree is now balanced.

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.

First, we perform the right rotation along C node, making C the


right subtree of its own left subtree B. Now, B becomes the right
subtree of A.

Node A is still unbalanced because of the right subtree of its right


subtree and requires a left rotation.
A left rotation is performed by making B the new root node of the
subtree. A becomes the left subtree of its right subtree B.

The tree is now balanced.

Algorithm AVL TREE:


Insert:-
1. If P is NULL, then
I. P = new node
II. P ->element = x
III. P ->left = NULL
IV. P ->right = NULL
V. P ->height = 0
2. else if x>1 => x<P ->element
a.) insert(x, P ->left)
b.) if height of P->left -height of P ->right =2
1. insert(x, P ->left)
2. if height(P ->left) -height(P ->right) =2
if x<P ->left ->element
P =singlerotateleft(P)
else
P =doublerotateleft(P)
3. else
if x<P ->element
a.) insert(x, P -> right)
b.) if height (P -> right) -height (P ->left) =2
if(x<P ->right) ->element
P =singlerotateright(P)
else
P =doublerotateright(P)
4. else
Print already exits
5. int m, n, d.
6. m = AVL height (P->left)
7. n = AVL height (P->right)
8. d = max(m, n)
9. P->height = d+1
10. Stop

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;

doubleWithLeftChild( AvlNode k3)


 k3.left = rotateWithRightChild( k3.left );
 return rotateWithLeftChild( k3 );

doubleWithRightChild( AvlNode k1 )

 k1.right = rotateWithLeftChild( k1.right );


 return rotateWithRightChild( k1 );

Software Required: g++ / gcc compiler- / 64 bit Fedora, eclipse IDE

Input: Dictionary word and its meaning.

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

Upon completion Students will be able to:


ELO1: Learn height balanced binary tree in data structure.
ELO2: Understand & implement rotations required to balance the tree.

Questions asked in university exam.

1. What is AVL tree?


2. In an AVL tree, at what condition the balancing is to be done
3. When would one want to use a balance binary search tree (AVL) rather than an array data
structure
Assignment No. E-20

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

2) Explain how Queue can be implemented as an ADT.


Theoretically, one characteristic of a queue is that it does not have a specific capacity. Regardless of how
many elements are already contained, a new element can always be added. It can also be empty, at which point removing
an element will be impossible until a new element has been added again. A practical implementation of a queue, e.g.
with pointers, of course does have some capacity limit, that depends on the concrete situation it is used in. For a
data structure the executing computer will eventually run out of memory, thus limiting the queue size. Queue
overflow results from trying to add an element onto a full queue and queue underflow happens when trying to remove
an element from an empty queue. A bounded queue is a queue limited to a fixed number of items.

3) What is Priority Queue? Explain with example.


Priority queue is an abstract data type in computer programming that supports the following threeoperations:
 insertWithPriority: add an element to the queue with an associated priority
 getNext: remove the element from the queue that has the highest priority, and return it (also knownas
"PopElement(Off)", or "GetMinimum")
 peekAtNext (optional): look at the element with highest priority without removing it.

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

Display Queue Front:


Front is pointer variable of type Queue, which is 1st node of Queue.Display (Front)
Step 1: Temp = Front;
Step 2: Do Steps while Temp! = NULL
a) Display Temp Data
b) If Priority 1 Then ¯General Checkup‖;

Else If Priority 2 Then Display ¯ Non-serious"; ElseIf


Priority 3 Then Display "Serious"
Else Display "Unknown";
c)Temp = Temp->Next;
Step 3: Stop.

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:

1. Knowledge of the Object Oriented Language like C++.

2. Concept of dynamic allocation.

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.

Two characteristics determine how the file is organised:

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.

II. File Volatility:


It addresses the properties of record changes. File records with many changes are highly volatile
means the disk design will be more efficient than tape.

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.

2. Indexed sequential organization:

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.

4. Direct access organization:


In direct access file organization, records are placed randomly throughout the file. Records need not be
in sequence because they are updated directly and rewritten back in the same location. New records are
added at the end of the file or inserted in specific locations based on software commands.
Records are accessed by addresses that specify their disk locations. An address is required for locating
a record, for linking records, or for establishing relationships. Addresses are of two types:
i. Absolute
ii Relative.

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:

Step 1: Start the Program


Step 2:Obtain the required data through char and int datatypes.
Step 3:Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5:Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution.

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

Conclusion:

Assignment Questions:

1. What is file organization?


2. List three important factors of file organization.
3. What is indexed file organization?
4. List the four types of indexes used for primary and secondary index.
5. What is hashed file organization?
6. What are the two hashing techniques mentioned in this tutorial?
Assignment no. F-25

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.

File Access Method

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,

Direct or Random Access Methods

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy