Updated - Final ADS LAB MANUAL 21-22 - Finalized
Updated - Final ADS LAB MANUAL 21-22 - Finalized
Updated - Final ADS LAB MANUAL 21-22 - Finalized
Prepared by,
Dr. A. A. Agarkar
Mrs. S. P. Mone
SE COMPUTER
Semester II Academic Year 2020-21
Savitribai Phule Pune University
Second Year of Engineering (2019 Course)
210253: Data Structures & Algorithms
Course Objectives:
● To develop a logic for graphical modeling of the real life problems.
● To suggest appropriate data structure and algorithm for graphical solutions of the problems. ● To
understand advanced data structures to solve complex problems in various domains. ● To operate
on the various structured data
● To build the logic to use appropriate data structure in logical and computational solutions. ∙
To understand various algorithmic strategies to approach the problem solution.
Course Outcomes:
On completion of the course, learner will be able to–
1:To identify & articulate the complexity goals and benefits of a good hashing scheme for real world
applications.
CO2:To apply non-linear data structures for solving problems of various domain. CO3:To design and
specify the operations of a nonlinear-based abstract data type and implement them in a high-level
programming language.
CO4:To analyze the algorithmic solutions for resource requirements and optimization CO5:To use
efficient indexing methods and multiway search techniques to store and maintain data. CO6:To use
appropriate modern tools to understand and analyze the functionalities confined to the secondary
storage.
Course Content
Unit I Hashing
Hash Table- Concepts-hash table, hash function, basic operations, bucket, collision, probe, synonym,
overflow, open hashing, closed hashing, perfect hash function, load density, full table, load factor,
rehashing, issues in hashing, hash functions- properties of good hash function, division, multiplication,
extraction, mid-square, folding and universal, Collision resolution strategies- open addressing and
chaining, Hash table overflow- open addressing and chaining, extendible hashing, closed addressing and
separate chaining.
Skip List- representation, searching and operations- insertion, removal
Unit II Graphs
Basic Concepts, Storage representation, Adjacency matrix, adjacency list, adjacency multi list, inverse
adjacency list. Traversals-depth first and breadth first, Minimum spanning Tree, Greedy algorithms for
computing minimum spanning tree- Prims and Kruskal Algorithms, Dikjtra's Single source shortest path,
All pairs shortest paths- Flyod-Warshall Algorithm Topological ordering.
Tree- basic terminology, General tree and its representation, representation using sequential and linked
organization, Binary tree- properties, converting tree to binary tree, binary tree traversals (recursive and
non-recursive)- inorder, preorder, post order, depth first and breadth first, Operations on binary tree.
Huffman Tree (Concept and Use), Binary Search Tree (BST), BST operations, threaded binary search tree-
concepts, threading, insertion and deletion of nodes in in order threaded binary search tree, in order
traversal of in-order threaded binary search tree.
Symbol Table-Representation of Symbol Tables- Static tree table and Dynamic tree table, Weight balanced
tree - Optimal Binary Search Tree (OBST), OBST as an example of Dynamic Programming, Height
Balanced Tree- AVL tree. Red-Black Tree, AA tree, K-dimensional tree, Splay Tree
Indexing and Multiway Trees- Indexing, indexing techniques-primary, secondary, dense, sparse,
Multiway search trees, B-Tree- insertion, deletion , B+Tree - insertion, deletion, use of B+ tree in
Indexing, Trie Tree.
Learning Resources
Text Books:
1.Horowitz, Sahani, Dinesh Mehata, ―Fundamentals of Data Structures in C++ǁ, Galgotia Publisher,
ISBN: 8175152788, 9788175152786.
2. M Folk, B Zoellick, G. Riccardi, ―File Structuresǁ, Pearson Education, ISBN:81-7758-37-5
3. Peter Brass, ―Advanced Data Structuresǁ, Cambridge University Press, ISBN: 978-1-107- 43982-5
1. Reference Books:A. Aho, J. Hopcroft, J. Ulman, ―Data Structures and Algorithms Pearson Education,
1998, ISBN-0-201-43578-0.
2. Michael J Folk, ―File Structures an Object Oriented Approach with C++, Pearson Education, ISBN:
81-7758-373-5.
3. Sartaj Sahani, ―Data Structures, Algorithms and Applications in C++, Second Edition, University
Press, ISBN:81-7371522 X.
4. G A V Pai, ―Data Structures and Algorithmsǁ, The McGraw-Hill Companies, ISBN - 9780070667266.
5. Goodrich, Tamassia, Goldwasser, ―Data Structures and Algorithms in Java, Wiley Publication,
ISBN: 9788126551903
PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO1
2
CO1 3 1 3 1 - - - - 1 - - 1
CO2 3 2 3 1 - - - - 1 - - -
CO3 3 2 3 1 - - - - 1 - - -
CO4 3 2 3 1 1 - - - 1 - - -
CO5 3 2 3 1 1 - - - 1 - - 1
CO6 3 2 3 1 1 - - - 1 - - 1
Operating System recommended: - 64-bit Open-source Linux or its derivative Programming tools
recommended: - Open Source Python - Group A assignments, C++ Programming tool like G++/GCC
Sr. Group A
No.
1 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 Implement all the functions of a dictionary (ADT) using hashing and handle collisions using
chaining with / without replacement.
Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must
be unique
Standard Operations: Insert(key, value), Find(key), Delete(key)
3 For given set of elements create skip list. Find the element in the set that is closest to some
given value. (note: Decide the level of element in the list Randomly with some upper limit)
5 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.
6 Beginning with an empty binary search tree, construct binary search tree by inserting the
values in the order given. After constructing a binary tree -
i. Insert new node
ii.Find number of nodes in longest path from root
iii.Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at every node
v.Search a value
7 Construct an expression tree from the given prefix expression eg. +--a*bc/def and traverse it
using post order traversal (non recursive) and then delete the entire tree.
8 Read for the formulas in propositional calculus. Write a function that reads such a formula and
creates its binary tree representation. What is the complexity of your function?
9 Convert given binary tree into threaded binary tree. Analyze time and space complexity of the
algorithm.
10 Consider threading a binary tree using preorder threads rather than in order threads. Design an
algorithm for traversal without using stack and analyze its complexity. _
11 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 Binary Search Tree for implementation.
12 Implement a file compression algorithm that uses binary tree. Your program should allow the user to
compress and decompress messages containing alphabets using the standard Huffman algorithm for
encoding and decoding.
Group C
13 Represent a given graph using adjacency matrix/list to perform DFS and using adjacency list to
perform BFS. Use the map of the area around the college as the graph. Identify the prominent land
marks as nodes and perform DFS and BFS on that.
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 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. Check whether the graph is connected or not. Justify the storage
representation used.
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.
16 Tour operator organizes guided bus trips across the Maharashtra. Tourists may have different
preferences. Tour operator offers a choice from many different routes. Every day the bus moves from
starting city S to another city F as chosen by client. On this way, the tourists can see the sights
alongside the route travelled from S to F. Client may have preference to choose route. There is a
restriction on the routes that the tourists may choose from, the bus has to take a short route from S to
F or a route having one distance unit longer than the minimal distance. Two routes from S to F are
considered different if there is at least one road from a city A to a city B which is part of one route,
but not of the other route.
17 Consider the scheduling problem. n tasks to be scheduled on single processor. Let t1, ...,tn be
durations required to execute on single processor is known. The tasks can be executed in any order
but one task at a time. Design a greedy algorithm for this problem and find a schedule that
minimizes the total time spent by all the tasks in the system. (The time spent by one is the sum of
the waiting time of task and the time spent on its execution.)
Group 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?
19 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
Group 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.
21 Implement the Heap/Shell sort algorithm implemented in Java demonstrating heap/shell data structure
with modularity of programming language
22 Read the marks obtained by students of second year in an online examination of particular subject.
Find out maximum and minimum marks obtained in that subject. Use heap data structure. Analyze
the algorithm.
Group 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.
24 Company maintains employee information as employee ID, name, designation and salary. Allow
user to add, delete information of employee. Display information of particular employee. If
employee does not exist an appropriate message is displayed. If it is, then the system displays the
employee details. Use index sequential file to maintain the data.
25 Implementation of a direct access file -Insertion and deletion of a record from a direct access file
26 Assume we have two input and two output tapes to perform the sorting. The internal memory can
hold and sort m records at a time. Write a program in java for external sorting. Find out time
complexity.
27 Design a mini project using JAVA which will use the different data structure with or without Java
collection library and show the use of specific data structure on the efficiency (performance) of the
code.
28 Design a mini project to implement Snake and Ladders Game using python.
30 Design a mini project for automated Term work assessment of student based on parameters like daily
attendance, Unit Test / Prelim performance, Student’s achievements if any, Mock Practical.
Course Objectives
1. To develop a logic for graphical modeling of the real-life problems.
2. To suggest appropriate data structure and algorithm for graphical solutions of the problems.
3. To understand advanced data structures to solve complex problems in various domains.
4. To operate on the various structured data
5. To build the logic to use appropriate data structure in logical and computational solutions.
6. To understand various algorithmic strategies to approach the problem solution
Course Outcomes
1. Classify the Tree data structures & apply it for problem solving
2. Summarize the basic concepts and operations on Graph data structure & apply it for solving real life
problems
3. Explain the concepts of Hashing and apply it to solve searching problems
4. Describe the concept of symbol tables with OBST and AVL trees
5. Demonstrate and apply knowledge of Indexing and Multiway Trees
6. Explain the concept and primitive operations on different types of file organizations
7. To use the programming paradigms effectively in application development.
Programme Outcomes
2. Problem analysis: Identify, formulate, research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools, including prediction and modeling to complex engineering activities,
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal, and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change
INDEX
1. Group
2. Assignment No.
3. Title
4. Objective
5. Problem Statement
6. Outcomes
7. Theory(in brief)
8. Algorithm
9. Flowchart
11. Conclusion
12. FAQs
GROUP: A
ASSIGNMENT NO: 1
Title: Implementation of hash Table.
Problem Statement: Consider a 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 (Python)
Outcome: Explain the concepts of Hashing and apply it to solve searching problems
Theory:
In a mathematical sense, a map is a relation between two sets. We can define Map M as a set of
pairs, where each pair is of the form (key, value), where for given a key, we can find a value
using some kind of a “function” that maps keys to values. The key for a given object can be
calculated using a function called a hash function. In its simplest form, we can think of an array
as a Map where key is the index and value is the value at that index. For example, given an array
A, if i is the key, then we can find the value by simply looking up A[i]. The idea of a hash table is
more generalized and can be described as follows. The concept of a hash table is a generalized
idea of an array where key does not have to be an integer. We can have a name as a key, or for
that matter any object as the key. The trick is to find a hash function to compute an index so that
an object can be stored at a specific location in a table such that it can easily be found. Example:
Suppose we have a set of strings {“abc”, “def”, “ghi”} that we’d like to store in a table. Our
objective here is to find or update them quickly from a table, actually in O(1). We are not
concerned about ordering them or maintaining any order at all. Let us think of a simple schema
to do this. Suppose we assign “a” = 1, “b”=2, … etc to all alphabetical characters. We can then
simply compute a number for each of the strings by using the sum of the characters as follows.
“abc” = 1 + 2 + 3=6, “def” = 4 + 5 + 6=15 , “ghi” = 7 + 8 + 9=24 If we assume that we have a
table of size 5 to store these strings, we can compute the location of the string by taking the sum
mod 5. So we will then store “abc” in 6 mod 5 = 1, “def” in 15 mod 5 = 0, and “ghi” in 24 mod 5
= 4 in locations 1, 0 and 4 as follows-
0 1 2 3 4
Def Abc ghi
Now the idea is that if we are given a string, we can immediately compute the location using a
simple hash function, which is sum of the characters mod Table size. Using this hash value, we
can search for the string. This seems to be great way to store a Dictionary. Therefore the idea of
hashing seems to be a great way to store pairs of (key, value) in a table.
void* A[n];
The array needs to be initialized using
for (i = 0; i < n ; i++)
A[i] = NULL;
Suppose we like to store strings in this table and be able to find them quickly. In order to find out
where to store the strings, we need to find a value using a hash function. One possible hash
function is
Given a string S = S1S2…. Sn
Define a hash function as
H(S) = H(“S1S2…. Sn”) = S1 + p S2 + p2 S3 + ….+ pn-1 Sn (1)
where each character is multiplied by a power of p, a prime number.
The above equation can be factored to make the computation more effective. Using the factored
form, we can define a function hash code that computes the hash value for a string s as follows—
int hashcode(char* s){
int sum = s[strlen(s)-1], p = 101;
int i;
for (i=1;i< strlen(s);i++)
sum-s[strlen(s)-i-1]+p*sum;
return sum;
}
This allows any string to be placed in the table as follows. We assume a table of size 101. A[hashcode(s)
%101] = s; // we assume that memory for s is already being allocated.
One problem with above method is that if any collisions occur, that is two strings with the same
hash code,& we will lose one of the strings. Therefore we need to find a way to handle collisions
in the table.
Collisions:
One problem with hashing is that it is possible that two strings can hash into the same location.
This is called a collision. We can deal with collisions using many strategies, such as linear
probing (looking for the next available location i+1, i+2, etc. from the hashed value i), quadratic
probing (same as linear probing, except we look for available positions i+1 , i + 4, i + 9, etc from
the hashed value i and separate chaining, the process of creating a linked list of values if they
hashed into the same location.
Algorithm:
1: Create a hash table with maximum size.
2: Define Hash function.
3: Read the telephone no & user’s information to insert it into hash table.
4: Insert telephone no value in the hash table.
5: If collision occurs, apply separate chaining method.
6: If user requires inserting another data, go to step4.
7: Display the all user’s data.
8: Read the telephone no from user to be found.
9: find the data from hash table.
10: Display the data with minimum no. of comparisons.
Flowchart:
Test Cases:
Conclusion:
Hence we have studied successfully the use of a hash table & implementing it.
Q 1. A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and
linear probing. After inserting 6 values into an empty hash
0
1
2 42
3 23
4 34
5 52
6 46
7 33
8
9
Q. 2 Which one of the following choices gives a possible order in which the key values
could have been inserted in the table?
(A) 46, 42, 34, 52, 23, 33
(B) 34, 42, 23, 52, 33, 46
(C) 46, 34, 42, 23, 52, 33
(D) 42, 46, 33, 23, 34, 52
Ans: (C)
The sequence (A) doesn’t create the hash table as the element 52 appears before 23 in this
sequence.
The sequence (B) doesn’t create the hash table as the element 33 appears before 46 in this
sequence.
The sequence (C) creates the hash table as 42, 23 and 34 appear before 52 and 33, and 46
appears before 33.
The sequence (D) doesn’t create the hash table as the element 33 appears before 23 in this
sequence.
Q 2. How many different insertion sequences of the key values using the same hash
function and linear probing will result in the hash table shown above?
(A) 10
(B) 20
(C) 30
(D) 40
Ans: (C)
Explanation: In a valid insertion sequence, the elements 42, 23 and 34 must appear before 52 and
33, and 46 must appear before 33.Total number of different sequences = 3! x 5 = 30. In the above
expression, 3! is for elements 42, 23 and 34 as they can appear in any order, and 5 is for element
46 as it can appear at 5 different places.
Q 3. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of
length 10 using open addressing with hash function h(k) = k mod 10 and linear probing.
What is the resultant hash table?
(A) A
(B) B
(C) C
(D) D
Ans: (C)
Explanation: To get the idea of open addressing concept, you can go through below lines from
.Open addressing, or closed hashing, is a method of collision resolution in hash tables. With this
method a hash collision is resolved by probing, or searching through alternate locations in the
array (the probe sequence) until either the target record is found, or an unused array slot is found,
which indicates that there is no such key in the table. Well known probe sequences include:
1. linear probing in which the interval between probes is fixed–often at 1.
2. quadratic probing in which the interval between probes increases linearly (hence, the indices
are described by a quadratic function).
3. double hashing in which the interval between probes is fixed for each record but is
computed by another hash function.
Q 6. You need to store a large amount of data, but you don't know the exact number of
elements. The elements must be searched quickly by some key. You want to waste no
storage space. The elements to be added are in sorted order. What is the simplest data
structure that meets your needs?
Ans: Hash tables provide fast searching, but they may waste storage space. A tree makes better
use of memory. Since the keys are in a sorted order, it's likely a binary tree will end up looking
like a linked list instead of a well-balanced tree. With a self-balancing tree, we can make sure
searching goes faster.
Q 8. In a hash table that uses separate chaining to handle collisions, what, if any,
restrictions should be placed on the table size?
Ans: With separate chaining, no probing is done. Each entry in the table is a linked list, and all
items with the hash index appear in the list.
Q 9. Hashing is the best search method (constant running time) if we don't need to have the
records sorted. In that case which method will you use separate chaining or open
addressing?
Ans: If there is enough memory to keep a table twice larger than the number of the records -
open addressing is the preferred method. Separate chaining is used when we don't know in
advance the number of the records to be stored. It requires additional time for list processing;
however, it is simpler to implement.
Objective: To understand the collision resolution techniques and various operations on hash table
Problem Statement: Implement all the functions of a dictionary (ADT) using hashing and handle
collisions using chaining with / without replacement. Data: Set of (key, value) pairs, Keys are mapped to
values, Keys must be comparable, Keys must be unique Standard Operations: Insert(key, value), Find(key),
Delete(key) (python)
Outcome: Define class for Dictionary using Object Oriented features. Analyze working of hash function.
Theory:
Dictionary ADT Dictionary (map, association list) is a data structure, which is generally an association of
unique keys with some values. One may bind a value to a key, delete a key (and naturally an associated value)
and lookup for a value by the key. Values are not required to be unique. Simple usage example is an explanatory
dictionary. In the example, words are keys and explanations are values.
Dictionary Operations:
Dictionary create() creates empty dictionary
boolean isEmpty(Dictionary d) tells whether the dictionary d is empty
put(Dictionary d, Key k, Value v) associates key k with a value v; if key k already presents in the
dictionary old value is replaced by v
Value get(Dictionary d, Key k) returns a value, associated with key kor null, if dictionary contains
no such key
remove(Dictionary d, Key k) removes key k and associated value
destroy(Dictionary d) destroys dictionary d Hash Table is a data structure which stores data in an
associative manner.
In a hash table, data is stored in an array format, where each data value has its own unique index value. Access
of data becomes very fast if we know the index of the desired data. Thus, it becomes a data structure in which
insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a
storage medium and uses hash technique to generate an index where an element is to be inserted or is to be
located from.
Hashing: Hashing is a technique to convert a range of key values into a range of indexes of an array. We're
going to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the
following items are to be stored. Item are in the (key,value) format.
Basic Operations of hash table 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.
1.Data Item 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;
}
2. 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; }
3. 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; }
4.. Delete Operation: Whenever an element is to be deleted, compute the hash code of the key passed and
locate the index using that hash code as an index in the array. Use linear probing to get the element ahead
if an element is not found at the computed hash code. When found, store a dummy item there to keep the
performance of the hash table intact.
Example struct DataItem* delete(struct DataItem* item)
{ int key = item->key;
//get the hash int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL)
{ if(hashArray[hashIndex]->key == key)
{ struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position hashArray[hashIndex] = dummyItem;
return temp; }
//go to next cell ++hashIndex;
//wrap around the table hashIndex %= SIZE; } return NULL; }
START
Check whether
No Choose suitable collision
the bucket is
resolution technique and
free?
Find suitable slot
Yes
END
Test Cases:
1 01 Insert (4, Asmita) Read (4, Asmita) The (4, Asmita) If bucket is Pass
is inserted in free and (4,
it’s home Asmita) is
bucket insered
Conclusion:
This program gives us the knowledge of dictionary(ADT).
OUTCOME Upon completion Students will be able to:
ELO1: Learn object-oriented Programming features.
ELO2: Understand & implement Dictionary (ADT) using hashing.
FAQ:
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
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.
Outcome:
Define class for structures using Object Oriented features.
Analyze tree data structure.
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
tree.
A node that has no child 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
formally, 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.
Every node in a tree can be seen as the root node of the subtree rooted at that node.
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 minimum effort for this assignment
we are considering the tree as follows.
Book
Chapter1 Chapter2
Input: Book name & its number of sections and subsections along with name.
Flowchart:
Test-cases:
1 ID1 Enter count of Number of Nodes for Book node and Pass
chapters chapters is chapters are its children
nonzero positive created in the chapters are
value tree and created
attached as the
children of
book node
2 ID2 Enter count of Number of Nodes for chapter node Pass
sections sections is sections are and its children
nonzero positive created in the sections are
value tree and created
attached as the
children of
chapter node
3 ID3 Enter count of sub- Number of sub- Nodes for sub- Section node Pass
sections sections is sections are and its children
nonzero positive created in the sub-section
value tree and nodes are
attached as the created
children of
section node
4 ID1 Enter Number of No subsection Number of Pass
number of subsections: 0 node should be subsections is
subsections created zero hence no
as 0 children node
are introduced
for section
node
OUTCOME:
Upon completion Students will be able to:
ELO1: Learn object-oriented Programming features.
ELO2: Understand & implement tree data structure.
FAQs:
5. How do you check if a given binary tree is a subtree of another binary tree?
Consider we have a binary tree T. We now want to check if a binary tree S is a subtree of T.To do this, first,
try to check if you find a node in T that is also in S. Once you find this common node, check if the
following nodes are also a part of S. If yes, we can safely say that S is a subtree of T
6. How do you find the distance between two nodes in a binary tree?
Consider two nodes n1 and n2 that are part of a binary tree. The distance between n1 and n2 is equal to
the minimum number of edges that need to be traversed to reach from one node to the other. It is
important to note that you traverse the shortest distance between the nodes.
binary trees are used in two very different ways: First, as a means of accessing nodes based on some
value or label associated with each node. Binary trees labelled this way are used to implement binary
search trees and binary heaps, and are used for efficient searching and sorting.
Usually, it's stored as an adjacency list. Which is basically a linked list for every single node. So the
linked list of a node u contains every node v such that (u,v) is a valid edge of the tree.But that's used when
the nodes of the tree are less (since it requires more memory).
GROUP: B
ASSIGNMENT NO: 04
Problem Statement: Beginning with an empty binary search tree, construct binary search tree by
inserting the values in the order given. After constructing a binary tree -
i. Insert new node
ii. Find number of nodes in longest path from root
iii. Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at every node
v. Search a value
Outcome: Classify the Tree data structures & apply it for problem solving
Theory:
A binary tree is composed of nodes connected by edges. Some binary tree is either empty or consists of a
single root element with two distinct binary tree child elements known as the left subtree and the right
subtree of a node. As the name binary suggests, a node in a binary tree has a maximum of children.
An important special kind of binary tree is the binary search tree (BST). In a BST, each node stores some
information including a unique key value, and perhaps some associated data. A binary tree is a BST iff, for
every node n in the tree:
● All keys in n's left subtree are less than the key in n, and
● all keys in n's right subtree are greater than the key in n.
Algorithm:
Algorithm to insert a node in the binary tree:
1. Create a new BST node and assign values to it.
2. insert(node, key)
i) If root == NULL,
return the new node to the calling function.
ii) if root=>data < key
call the insert function with root=>right and assign the return value in root=>right.
root->right = insert(root=>right,key)
iii) if root=>data > key
call the insert function with root->left and assign the return value in root=>left.
root=>left = insert(root=>left,key)
3. Finally, return the original root pointer to the calling function.
Algorithm to find the minimum value in binary tree:
1. Define Node class which has three attributes namely: data, left and right. Here, left represents the left
child of the node and right represents the right child of the node.
2. When a node is created, data will pass to data attribute of node and both left and right will be set to
null.
3. Define another class which has an attribute root.
A) Root represent root node of the tree and initialize it to null.
Flow-Chart:
START
END
Test Cases:
Conclusion:
Hence we have studied successfully how to delete any node from binary tree using operator
overloading.
FAQs:
Q 1. List any four Operators that cannot be overloaded?
Ans: Following operators cannot be overloaded are Class member access operator (. , .*), Scope
resolution operator (::), Size operator ( sizeof ) & Conditional operator (?:).
Q 6. List out the cases involved in deleting a node from a binary search tree.
Ans: Case 1: Node to be deleted is a Leaf node.
Case 2: Node to be deleted has one child.
Case 3: Node to be deleted has two children.
Ans: Expression tree is a binary tree in which non – leaf nodes are operators and the leaf nodes
are operands. In the above example, we have 5 operators. Therefore the number of non-leaf
nodes in the tree is 5.
Q 9. Define binary search tree. Why it is preferred rather than the sorted linear array and
linked list?
Ans: Definition: Binary search tree is a binary tree in which key values of the left sub trees are
lesser than the root value and the key values of the right sub tree are always greater than the root
value. Reason-
● In linear array or linked list the values are arranged in the form of increasing or
decreasing order. If we want to access any element means, we have to traverse the entire
list.
● But if we use BST, the element to be accessed is greater or smaller than the root element
means we can traverse either the right or left sub tree and can access the element
irrespective of searching the entire tree.
Problem Statement: Construct an expression tree from the given prefix expression eg. +--
a*bc/def and traverse it using post order traversal (non-recursive) and then delete the entire tree.
Theory:
The expression tree is a binary tree in which each internal node corresponds to the operator and
each leaf node corresponds to the operand so for example expression tree for 3 + ((5+9)*2)
would be:
Expression Tree is a special kind of binary tree with the following properties:
1. Each leaf is an operand. Examples: a, b, c, 6, 100
2. The root and internal nodes are operators. Examples: +, -, *, /, ^
3. Subtrees are subexpressions with the root being an operator.
Traversal Techniques:
There are 3 standard traversal techniques to represent the 3 different expression formats.
Inorder Traversal:
We can produce an infix expression by recursively printing out
the left expression,
the root, and
the right expression.
Postorder Traversal:
The postfix expression can be evaluated by recursively printing out
the left expression,
the right expression and
then the root
Preorder Traversal:
1) We can also evaluate prefix expression by recursively printing out:
2) the root,
3) the left expressoion and
4) the right expression.
Algorithm:
Construction of Expression Tree:
Test Cases:
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 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. Justify the
storage representation used.
Outcome:
Summarize the basic concepts and operations on Graph data structure & apply it for solving real
life problems
Theory:
Graph is a data structure that consists of following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge.
The pair is ordered because (u, v) is not same as (v, u) in case of directed graph(di-graph). The
pair of form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may
contain weight/value/cost.
Following is an example undirected graph with 5 vertices.
Adjacency List:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be
array[ ]. An entry array[i] represents the linked list of vertices adjacent to the i th vertex. This
representation can also be used to represent a weighted graph. The weights of edges can be
stored in nodes of linked lists. Following is adjacency list representation of the above graph.
Adjacency List Representation of the above Graph
Applications:
Graphs are used to represent many real life applications: Graphs are used to represent networks.
The networks may include paths in a city or telephone network or circuit network. Graphs are
also used in social networks like linkedIn, facebook. For example, in facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like person
id, name, gender and locale.
Algorithm:
Graph creation using Adjacency Matrix
Test cases:
1 ID1 Enter the All 0’s Graph is not If for any vertex Fail
matrix values connected there shows a
adjacent vertex
2 ID2 Enter the All 0’s Graph is not No Adjacent vertex Pass
matrix values connected shown
3 ID3 Enter the Some non zero Some No adjacent vertex Fail
matrix values values edges are present
and remaining present
zero’s
4 ID4 Print the {0,1,1,0,1,0,0,1, 1,2,4,3 1,2,4,3 or Pass
graph using DFS 1,0,0,1,0,1,1,0} 1,3,4,2
5 ID5 Print the {0,1,1,0,1,0,0,1, 1,2,4,3 1,3,2,4 Fail
graph using DFS 1,0,0,1,0,1,1,0}
Q1. What are the advantages of adjacency list representation over adjacency matrix
representation of a graph?
Ans : In adjacency list representation, space is saved for sparse graphs. DFS and BSF can be
done in O(V + E) time for adjacency list representation. These operations take O(V^2) time in
adjacency matrix representation. Here is V and E are number of vertices and edges respectively.
Adding a vertex in adjacency list representation is easier than adjacency matrix representation.
Q5. For an undirected graph with n vertices and e edges, the sum of the degree of each
vertex is equal to
Ans : 2e
Q6. The maximum degree of any vertex in a simple graph with n vertices is
Ans : n–1
Q9. Which algorithm can be used to most efficiently determine the presence of a cycle in a
given graph ?
Ans : Depth First Search
Q10. Given two vertices in a graph s and t, which of the two traversals (BFS and DFS) can
be used to find if there is path from s to t?
Ans :BFS and DFS
ASSIGNMENT NO: 7
Problem Statement : 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.
Outcomes : Explain the concepts of Hashing and apply it to solve searching problems
Theory :
Spanning Trees: A sub-graph T of a undirected graph G=(V,E) is a spanning tree of G if it is a
tree and contains every vertex of G. Minimum Spanning Tree in an undirected connected
weighted graph is a spanning tree of minimum weight (among all spanning trees).
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for
a connected weighted undirected graph. This means it finds a subset of the edges that forms
a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.
Start form any arbitrary vertex Find the edge that has minimum weight form all known vertices
Stop when the tree covers all vertices
Algortithm:
Prim’s Algorithm:
MST= {0} // start with vertex 0
For (T=0; T contains fewer than n-1 edges; add(u,v) to T)
{
Let (u,v) be a least cost edge such that u є TV & v not belonging to TV;
Add V to TV
}
Flow Chart :
Test Cases:
Q 2. Which algorithms are available for finding MST for given graph?
Ans: Prim's algorithm
Kruskal's algorithm
Q 3.Which algorithm you have used in program?
Ans: Prim's algorithm is used to find MST of given graph.
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 ki1 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 twodimensional arrays as onedimensional arrays.
Input:
No. of Element
key values
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.
FAQS:
Q 1.What is BST?
Ans: It is binary search tree. BST is binary tree in which left sub-tree contains data smaller than
node and right sub-tree contains data greater than node.
Q9. Inorder traversal will display the data of dictionary in which order/sequence?
Ans: Inorder traversal will display the data of dictionary in ascending order.
Q10. Out of two classes in program , which class object is created and which class pointer is
created?
Ans: Out of two classes in program, tree class object is created and node class pointer is created
ASSIGNMENT NO: 9
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.
Outcome: Describe the concept of symbol tables with OBST and AVL trees
Theory:
An AVL tree (Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing
binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at
most one;
Balance factor = heightOfLeftSubtree – heightOfRightSubtree
if at any time they differ by more than one, rebalancing is done to restore this property. Lookup,
insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the
number of nodes in the tree prior to the operation. Insertions and deletions may require the tree
to be rebalanced by one or more tree rotations.
Example AVL tree
AVL Tree Rotations:
In AVL tree, after performing every operation like insertion and deletion we need to check the
balance factor of every node in the tree. If every node satisfies the balance factor condition then
we conclude the operation otherwise we must make it balanced. We use rotation operations to
make the tree balanced whenever the tree is becoming imbalanced due to any operation.
Rotation operations are used to make a tree balanced. Rotation is the process of moving the
nodes to either left or right to make tree balanced.
There are four rotations and they are classified into two types.
Step 1: Insert the new element into the tree using Binary Search Tree insertion logic.
Step 2: After insertion, check the Balance Factor of every node.
Step 3: If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
Step 4: If the Balance Factor of any node is other than 0 or 1 or -1 then tree is said to be
imbalanced. Then perform the suitable Rotation to make it balanced. And go for next
operation.
1 ID1 Enter the data Happy Good Sorted list of data Cat Pet Animal Pass
and display AVL Mood Zoo Wild to be displayed Happy Good Mood
tree Animals Cat Pet Zoo Wild Animals
Animal
2 ID2 Enter the data Happy Good Required data Message displayed Fail
and Mood Zoo Wild is preseTextnt data is not present
search present Animals Cat Pet
data Animal
3 ID3 Enter the data Happy Good Required data Message displayed Fail
and search non Mood Zoo Wild is absent data is present
existing data Animals Cat Pet
Animal
4 ID4 Search in empty No Input AVL Tree is empty Message displayed Pass
AVL AVL Tree is empty
5 ID5 Enter the data , Happy Good Happy Good Mood Cat Pet Animal Fail
display AVL tree Mood Zoo Wild Zoo Wild Animals Happy Good Mood
and delete node Animals Cat Pet Zoo Wild Animals
Animal
Conclusion: Dictionary assignment has implemented successfully using binary search tree.
FAQs:
Q 1. What is AVL tree?
Ans: An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two
child subtrees of any node differ by at most one; if at any time they differ by more than one, re-
balancing is done to restore this property.
Q 3. Is AVL a BST?
Ans: Yes. AVL is binary search tree.
Q 4. What is balance factor in AVL?
Ans: Balance factor of AVL can be either -1, 0,1
Theory:
Heap: Heap is a special case of balanced binary tree data structure where the root-node key is
compared with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap. Based on
this criteria, a heap can be of two types −
Min-Heap − Where the value of the root node is less than or equal to either of its children.
Max-Heap − Where the value of the root node is greater than or equal to either of its children.
Implementation
Algorithm :
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 maintain the
data.
Outcomes:
To use effective and efficient data structures in solving various Computer Engineering domain
problems.
Theory :
Many real life problems use large volume of data and in such cases we require to use some
devices such as floppy disk or hard disk to store the data. The data in these devices is stored
using the concept of files . A file is a collection of related data stored in a particular area on the
disk. Programs can be designed to perform the read and write operations on these files.
A program involves either or both of following types of data communication.
1. Data transfer between the console and the program
2. Data transfer between the program and a disk file.
The I/O system of C++ contains a set of classes that define the file handling methods.
These include ifstream, ofstream and fstream . These classes are derived from fstream base
and from corresponding stream classes. These classes are declared in fstream.h header file.
We must include this file in the program that uses file.
● Using the constructor function of the class : This method is useful when we use only
one file in the stream.
● Using the member function open( ) of the class. : This method is used when we want to
manage multiple files using one stream.
Create a file stream object to manage the stream using appropriate class
Initialize the file object with the desired filename.
Ex. Ofstream outfile(“result”);
Open : File Modes
The general form of the function open( ) with two arguments is :
stream-object.open(“filename”, mode);
The second argument mode specifies the purpose for which the file is opened. The prototype of
the class member functions contain default values for the second argument .
Parameter Meaning
ios :: app Append to end-of –file.
ios::ate Go to end-of-file on opening.
ios::in Open file for reading only
ios::nocreate Open fails if file does not exist
ios::noreplace Open fails if file already exists
ios::out Open file for writing only
ios::trunc Delete the contents of the file if it exists
ios::binary Binary file
We can use these pointers to move through the files while reading or writing . The input pointer
is used for reading the contents of a given location and the output pointer is used for writing to a
given location.
Each time an input or output operation takes place , the appropriate pointer is automatically
adjusted
Default actions
When we open a file in read-only mode , the input pointer is automatically set at the beginning
so that we can read the file from the start.
When we open a file in write-only mode, the existing contents are deleted and the output pointer
is set at the beginning of the file. This enables to write to the file from the start
When we want to open an existing file to add more data , the file is opened in ‘append’ mode .
This moves the output pointer to the end of file.
The file stream classes support the functions to manage movements of the file
pointers seekg( ) Moves get pointer to a specified location.
seekp( ) Moves put pointer to a specified location.
tellg( ) Gives the current position of the get pointer.
tellp( ) Gives the current position of the put pointer.
Seek functions seekg( ) and seekp( ) can also be used with two arguments:
Seekg(offset, refposition);
Seekp(offset, refposition);
The parameter offset represents the number of bytes the file pointer is to be moved from the
location specified by the parameter refposition.
The refposition takes one of following three constants defined in the ios class:
The seekg() function moves associated file’s ‘get’ pointer while the seekp() function moves the
associated file’s ‘put’ pointer.
The first pair of functions , put( ) and get( ) are designed for handling a single character at a time
Second pair of functions, write( ) and read( ) are designed to write and read a blocks of binary
data.
The functions takes two arguments . The first is the address of the variable V and the second is
the length of variable in bytes.
Algorithm :
1 ID1 Open the file in 12 Amit Data to be written Data is written in Pass
ios::out mode I Kothrud in file file
2 ID2 Open the file in No input Data from file to be Data from file is Pass
ios::in mode displayed displayed
3 ID3 Open the file in Roll No 12 Respective student Different Fail
ios::in mode, record to be student record is
search record displayed played
4 ID4 Open the file in Roll No 10 All contents of file All contents of file Pass
ios::out mode, except except
delete record deleted deleted
and display record to record are
be displayed displayed
5 ID5 Open the file in 15 Rahul Newly added New record is Pass
ios::out I Karvenagar record to displayed as last
mode, be displayed at end record
append
record and
display
Conclusion: Sequential Files are implemented successfully for Student database system.
FAQs:
Q 1. What is file?
Ans: A file is a collection of related data stored in a particular area on the disk
Q 9. What is a stream :
Ans: Stream is sequence of bytes.
Objective: To understand the concept and basic of index sequential file and its use in Data structure
Problem Statement: Company maintains employee information as employee ID, name, designation and
salary. Allow user to add, delete information of employee. Display information of particular employee. If
employee does not exist an appropriate message is displayed. If it is, then the system displays the
employee details. Use index sequential file to maintain the data
Outcome:
To implement the concept and basic of index sequential file and to perform basic operation as adding
record, display all record, search record from index sequential file and its use in Data structure.
Theory :
Indexed sequential access file organization
•Indexed sequential access file combines both sequential file and direct access file organization.
•In indexed sequential access file, records are stored randomly on a direct access device such as magnetic
disk by a primary key.
•This file have multiple keys. These keys can be alphanumeric in which the records are ordered is called
primary key.
•The data can be access either sequentially or randomly using the index. The index is stored in a
file and read into memory when the file is opened.
Algorithm:
Step 1 - Include the required header files (iostream.h, conio.h, and windows.h for colors).
Step 2 - Create a class (employee) with the following members as public members. emp_number,
emp_name, emp_salary, as data members. get_emp_details(), find_net_salary() and show_emp_details() as
member functions.
Step 3 - Implement all the member functions with their respective code (Here, we have used scope
resolution operator ::). Step 3 - Create a main() method.
Step 4 - Create an object (emp) of the above class inside the main() method.
Step 5 - Call the member functions get_emp_details() and show_emp_details().
Step 6 - return 0 to exit form the program execution.
Approach:
1. For storing the data of the employee, create a user define datatype which will store the information
regarding Employee. Below is the declaration of the data type:
2. struct employee
{ 3. string name;
4. long int Employee_id;
5. string designation;
6. int salary;
7. };
Deleting in the record: Since we are using array to store the data, therefore to delete the data at any index
shift all the data at that index by 1 and delete the last data of the array by decreasing the size of array by 1
Searching in the record: For searching in the record based on any parameter, the idea is to traverse the data
and if at any index the value parameters matches with the record stored, print all the information of that
employee.
Conclusion: Index Sequential Files are implemented successfully for Student database system.
FAQs:
Problem Statement: Design a mini project to implement snake and ladder game using Python.
Theory: The idea is to consider the snakes and ladders board as a directed graph and run Breadth–first search
(BFS) from the starting node, vertex 0, as per game rules. We construct a directed graph, keeping in mind the
following conditions:
1. For any vertex in graph v, we have an edge from v to v+1, v+2, v+3, v+4, v+5, v+6 as we can reach any
of these nodes in one throw of dice from node v.
2. If any of these neighbors of v has a ladder or snake, which takes us to position x, then x becomes the
neighbor instead of the base of the ladder or head of the snake.
Now the problem is reduced to finding the shortest path between two nodes in a directed graph problem. We
represent the snakes and ladders board using a map.
Problem Statement: Implementation of DFS traversal on binary trees using Python classes.
Outcome: Classify the Tree data structures & apply it for problem solving
Theory:
Binary Tree: A binary tree is a tree data structure in which each node has at most two children,
which are referred to as the left child and the right child.
Example of binary tree
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one
logical way to traverse them, trees can be traversed in different ways. Following are the
generally used ways for traversing trees.
Inorder Traversal:
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Preorder Traversal:
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Postorder Traversal:
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
Example of Traversal
Test Cases:
FAQs:
Q1. What is BFS (with example)
Ans: BFS is breadth first
traversal. Example
Tree1
j <-- level 0
/\
f k <-- level 1
/ \ \
a h z <-- level 2
\
d <-- level 3
So, if we want to visit the elements level-by-level (and left-to-right, as usual), we would start at
level 0 with j, then go to level 1 for f and k, then go to level 2 for a, h and z, and finally go to
level 3 for d.
This level-by-level traversal is called a breadth-first traversal because we explore the breadth,
i.e., full width of the tree at a given level, before going deeper.
Tree1
j <-- root
/\
f k
/ \ \
a h z
\
d
This type of traversal is called a depth-first traversal. Why? Because it tries to go deeper in the
tree before exploring siblings. For example, the traversal visits all the descendants of f (i.e.,
keeps going deeper) before visiting f's sibling k (and any of k's descendants).
Solving maze-like puzzles with only one solution: DFS can be used to find all solutions to a maze problem by
only considering nodes on the current path in the visited set.
Topological Sorting:
This is mainly used for scheduling jobs from the given dependencies among jobs. DFS is highly
preferred approach while finding solutions to the following type of problems using Topological Sort:
instruction/job scheduling
ordering of formula cell evaluation when recomputing formula values in spreadsheets
determining the order of compilation tasks to perform in makefiles
data serialization
resolving symbol dependencies in linkers
Mapping Routes and Network Analysis
Path Finding: DFS is used for finding path between two given nodes - source and destination - in a graph.
Cycle detection in graphs
Q.6 Write an algorithm to check whether the graph is strongly connected or not?
Ans:
1. Start DFS(G, v) from a random vertex v of the graph G. If DFS(G, v) fails to reach every other vertex
in the graph G, then there is some vertex u, such that there is no directed path from v to u. Thus, G is not
strongly connected. If it does reach every vertex, then there is a directed path from v to every other vertex in
the graph G.
2. Reverse the direction of all edges in the directed graph G.
3. Again, run a DFS starting from vertex v. If the DFS fails to reach every vertex, then there is some vertex u,
such that in the original graph, there is no directed path from u to v. On the other hand, if it does reach every
vertex, then there is a directed path from every vertex u to v in the original graph.
Q. 7 How can you say that the graph is strongly connected?
Ans: We can say that G is strongly connected if:
1. DFS(G, v) visits all vertices in the graph G, then there exists a path from v to every other vertex in G, and
2. There exists a path from every other vertex in G to v.
Q.9 How the non-recursive implementation of DFS differs from the non-recursive implementation of
BFS?
Ans: it differs in following way:
• It uses a stack instead of a queue.
• The DFS should mark discovered only after popping the vertex, not before pushing it.
• It uses a reverse iterator instead of an iterator to produce the same results as recursive DFS.