0% found this document useful (0 votes)
56 views35 pages

DSA Practical Final

The document describes an assignment to construct a tree to represent the hierarchical structure of a book, with chapters, sections, and subsections as nodes. It discusses the time and space requirements for the tree data structure approach. The key concepts covered are classes, object-oriented programming, and tree data structures. Trees are used to store and represent hierarchical data by linking nodes together in a parent-child relationship.

Uploaded by

Riya Gunjal
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)
56 views35 pages

DSA Practical Final

The document describes an assignment to construct a tree to represent the hierarchical structure of a book, with chapters, sections, and subsections as nodes. It discusses the time and space requirements for the tree data structure approach. The key concepts covered are classes, object-oriented programming, and tree data structures. Trees are used to store and represent hierarchical data by linking nodes together in a parent-child relationship.

Uploaded by

Riya Gunjal
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/ 35

GROUP A

Assignment 1

Problem Statement:

o 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

Objective:

To understand the concept and basic of Hashing Technique in Data structure.

Theory Concepts:

we know that a list was ordered, we could search in logarithmic time using a binary search. With
balanced binary search tree, All of these operations search, insert and delete times can be guaranteed
to be in O(Logn) time. Another solution for these opration is use of direct access table ,in this program
we build a data structure that can be searched in O(1) time. This concept is referred to as hashing.

Hashing is an improvement over Direct Access Table. The idea is to use hash function that converts a
given phone number or any other key to a smaller number and uses the small number as index in a table
called hash table.

Hash Function: A function that converts a given big phone number to a small practical integer value. The
mapped integer value is used as an index in hash table. In simple terms, a hash function maps a big
number or string to a small integer that can be used as index in hash table.

A good hash function should have following properties

1) Efficiently computable.

2) Should uniformly distribute the keys (Each table position equally likely for each key)

Hash Table: An array that stores pointers to records corresponding to a given phone number. An entry
in hash table is NIL if no existing phone number has hash function value equal to the index for the entry.

Collision Handling: Since a hash function gets us a small number for a big key, there is possibility that
two keys result in same value. The situation where a newly inserted key maps to an already occupied
slot in hash table is called collision and must be handled using some collision handling technique.
Following are the ways to handle collisions:

•Chaining: The idea is to make each cell of hash table point to a linked list of records that have same
hash function value. Chaining is simple, but requires additional memory outside the table.

•Open Addressing: In open addressing, all elements are stored in the hash table itself. Each table entry
contains either a record or NIL. When searching for an element, we one
by one examine table slots until the desired element is found or it is clear that the element is not in the
table.

Following are the some Hash Function:

1.Division Modulo

2.Mid Square method

3.Digit Analysis Method

4. Folding Method

5. Extract Min Method

Etc.

Following are the Collision Resolution Method

–Linear Probing

-Quadratic Probing

-Double Hashing

Algorithm:

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

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

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;

Delete Operation

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;

Conclusion: Thus we have studied Hashing concept.


Assignment 2

Problem Statement:

To create ADT that implement 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

Learning Objectives:

Understand the implementation of set with various operations.like,union,Intersection,Difference,&


Symmetric Difference

New Concepts: 1) Array 2) Function 3) Set Theory

1) Array:

“An array is a finite collection of similar elements stored in adjacent memory locations.”

By finite we mean that there are specific number of elements in an array & by similar mean that all the
elements in an array are of same type.

An array may contain all integers or all characters but not both.

Example:

int arr[5]={1,2,3,4,5};

Char arr[5]={‘A’,’B’,’C’,’D’,’E’};

Float arr[5]=={1.5, 1.8,2.4, 2.8,3.5};

For example, a list of test scores will be an array of integers, since each

test score is an integer. If u have to combine data type then use structure concept.

Decide what the largest size your array can ever reach will be, because arrays have a fixed length.
You'll want to strike a balance between having your program able to handle unusually long data sets,
and having it require (and waste) a lot of memory.

2Function:

A function is a self-contained block of statements that perform a coherent task of some kind. Every C
program can be thought of as a collection of these functions.
Why Use Functions: Why write separate functions at all? Why not squeeze the entire logic into one
function, main ( )? Two reasons:

(a) Writing functions avoids rewriting the same code over and over. Suppose you have a section of code
in your program that calculates area

of a triangle. If later in the program you want to calculate the area of a different triangle, you won’t like
it if you are required to write the same instructions all over again. Instead, you would prefer to jump to a
‘section of code’ that calculates area and then jump back to the place from where you left off. This
section of code is nothing but a function.

(b) Using functions it becomes easier to write programs and keep track of what

they are doing. If the operation of a program can be divided into separate activities, and each activity
placed in a different function, then each could be written and checked more or less independently.
Separating the code into modular functions also makes the program easier to design and understand.

Example:

Void main ()

/ /statement

FunAdd(10,20); //function call

//statement

3.Set Theory and Operations :

Set Definition:

A set is, to put it simply, a collection of objects. The objects can be literally anything. Fruits, letters,
numbers, books, stars, anything. In other words, a set is a grouping of things, or elements. To put it
another way, a set is that which contains elements. Something is either in a set or not in a set, and it is in
a set only once.

Also, order is irrelevant. If my set contained A, B, and C, and your set contained C, A, and B, they would
be the exact same set.

Algorithm:

1) Start.

2) Print Menu.

3) Read choice.
4) If choice is read set then Read the value of Set

5) If choice is Union then perform Union operation.

6) If choice is intersection then perform Intersection operation.

7) If choice is Difference then perform Difference operation.

8) Stop

Conclusion:

In this way we have study set ADT operation.


GROUP B

Assignment 3

Problame:

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:

-To understand concept of class

-To understand concept & features of object oriented programming.

-To understand concept of tree data structure

-To understand concept of tree data structure

-To understand concept & features of object oriented programming.

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

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.

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.

Conclusion: This program gives us the knowledge tree data structure.


Assignment 4

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

Objective:

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.

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;

};
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:
Search Operation: Algorithm

Conclusion:

We are able to implement Binary search Tree and its operation in Data Structure.
Assignment 5

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.

Objective:

To understand the basic concept of Non Linear Data Structure “TREE ”and its construction by given
inorder sequence in Data structure.

Theory:

Tree

Tree represents the nodes connected by edges also a class of graphs that is acyclic is termed as

trees. Let us now discuss an important class of graphs called trees and its associated terminology.

Trees are useful in describing any structure that involves hierarchy. Familiar examples of such

structures are family trees, the hierarchy of positions in an organization, and so on.

Binary Tree

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.

Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first

locate its proper location. Start searching from the root node, then if the data is less than the

key value,

search for the empty location in the left subtree and insert the data. Otherwise, search for the empty

location in the right subtree and insert the data.

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

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
Algorithm to insert a node :

Step 1 - Search for the node whose child node is to be inserted. This is a node at some level i, and a node
is to be inserted at the level i +1 as either its left child or right child. This is the

node after which the insertion is to be made.

Step 2 - Link a new node to the node that becomes its parent node, that is, either the Lchild or the
Rchild.

Algorithm to traverse a tree :

Inorder traversal

Until all nodes are traversed -

Step 1 - Recursively traverse left subtree. Step 2 - Visit root node.

Step 3 - Recursively traverse right subtree.

Preorder

traversal

Until all nodes are traversed


- Step 1 - Visit root node.

Step 2 - Recursively traverse left subtree.

Step 3 - Recursively traverse right subtree.

Postorder

Until all nodes are traversed -

Step 1 - Recursively traverse left subtree.

Step 2 - Recursively traverse right subtree. Step 3 - Visit root node.

Algorithm to copy one tree into another tree : Step 1 – if (Root == Null)

Then return Null

Step 2 -Tmp = new TreeNode

Step 3 – Tmp->Lchild = TreeCopy(Root-

Lchild); Step 4 – Tmp->Rchild =

TreeCopy(Root->Rchild); Step 5 – Tmp-Data = Root->Data; Then return

Tmp;

Postorder

traversal

Preorder

traversal

Until all nodes are traversed

- Step 1 - Visit root node.

Step 2 - Recursively traverse left subtree.

Step 3 - Recursively traverse right subtree.

Algorithm to insert a node :

Step 1 - Search for the node whose child node is to be inserted. This is a node at some level i, and a node
is to be inserted at the level i +1 as either its left child or right child. This is the node after which the
insertion is to be made.

Step 2 - Link a new node to the node that becomes its parent node, that is, either the Lchild or the
Rchild.
Conclusion :

Thus we have studied the implementation of various Binary tree operations


GROUP C

Assignment 6

Problem:

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

Objective:

To understand the basic concept of Non Linear Data Structure “threaded binary tree ”

and its use in Data structure.

Theory:

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.

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.

Conclusion: we have Represented a given graph using adjacency matrix/list to perform DFS.
Assignment 7

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. Check whether the graph is connected or not. Justify the storage
representation used.

Objective:

1. To understand concept of Graph data structure

2. To understand concept of representation of graph.

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.

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


GROUP D

Assignment 8

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?

Objective:

To understand the concept and basic of OBST in Data structure.

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: 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; Optimal Binary Search
Trees 2 the round nodes represent internal nodes; these are the actual keys stored in the tree; assuming
that the relative frequency with which eac h 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 k1can 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 successed between kiand ki-1in an inorder traversal represents all key values
not stored that lie between ki and ki – 1.
ALGORITHMS IN PSEUDOCODE

We have the following procedure for determining R(i, j) and C(i, j) with 0 <= i <= j <= n:

The following function builds an optimal binary search tree


Conclusion: we understand the concept of OBST tree.
Assignment 9

Problem Statement:

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

Objective:

To understand the concept and basic of Height balanced Binary Search tree or AVL tree

in Data structure.

Theory:

AVL Tree

AVL tree is a self balanced binary search tree. That means, an AVL tree is also a binary search tree but it
is a balanced tree. A binary tree is said to be balanced, if the difference between the hieghts of left and
right subtrees of every node in the tree is either -1, 0 or +1. In other words, a binary tree is said to be
balanced if for every node, height of its children differ by at most one. In an AVL tree, every node
maintains a extra information known as balance factor. The AVL tree was introduced in the year of 1962
by G.M. Adelson-Velsky and E.M. Landis. An AVL tree is a balanced binary search tree. In an AVL tree,
balance factor of every node is either -1, 0 or +1.

Balance factor = heightOfLeftSubtree – heightOfRightSubtree

Every AVL Tree is a binary search tree but all the Binary Search Trees need not to be AVL trees.

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.

There are four rotations and they are classified into two types.

Single Left Rotation (LL Rotation)

Similarly RR and RL rotation can be performed

Algorithm:
The following operations are performed on an AVL tree...

1. Search

2. Insertion

3. Deletion

Search Operation in AVL Tree

In an AVL tree, the search operation is performed with O(log n) time complexity. The search operation is
performed similar to Binary search tree search operation. We use the following steps

to search an element in AVL tree...

• Step 1: Read the search element from the user

• Step 2: Compare, the search element with the value of root node in the tree.

• Step 3: If both are matching, then display "Given node found!!!" and terminate the function

• Step 4: If both are not matching, then check whether search element is smaller or larger than
that node value.

• Step 5: If search element is smaller, then continue the search process in left subtree.

• Step 6: If search element is larger, then continue the search process in right subtree.

• Step 7: Repeat the same until we found exact element or we completed with a leaf node

• Step 8: If we reach to the node with search value, then display "Element is found" and terminate
the function.

• Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found"
and terminate the function.

Insertion Operation in AVL Tree

In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, new
node is always inserted as a leaf node. The insertion operation is performed as follows...

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

Conclusion: We are able to implement AVL Binary Search tree and maintain its heigh aftere every
operation
GROUP E

Assignment 10

Problem Statement:

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.

Objective:

To understand the basic concept of Heap Data structure.

Theory:

A double ended priority queue is a data structure that support the following operation

1. Inserting an element with an arbitrary key

2. Deleting an element with largest key

3. Deleting an element with smallest key

When only insertion and one of the two deletion operation re to be supported, a max heap or min heap
may be used. A max-min heap support all of above operations.

Definition: A min-Max heap is a complete binary tree such that if it is not empty, each element has a
data member called key. Alternating levels of this tree are min levels and max level, respectively. the
root is on min level.

A 12 element Max-Min heap

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.

Algorithm:

Max heap Construction

Step 1 - Create a new node at the end of heap.

Step 2 - Assign new value to the node.

Step 3 - Compare the value of this child node with its parent.

Step 4 - If value of parent is less than child, then swap them.

Step 5 - Repeat step 3 & 4 until Heap property holds

Max heap Deletion Algorithm

Step 1 - Remove root node.

Step 2 - Move the last element of last level to root.

Step 3 - Compare the value of this child node with its parent.

Step 4 - If value of parent is less than child, then swap them.

Step 5 - Repeat step 3 & 4 until Heap property holds.

Max-Min Heap
Insert:

To add an element to a min-max heap perform following operations:

1. Append the required key to the array representing the min-max heap. This will likely break the
min-max heap properties, therefore we need to adjust the heap.

2. Compare this key with its parent:

. If it is found to be smaller (greater) compared to its parent, then it is surely smaller (greater) than all
other keys present at nodes at max(min) level that are on

he path from the present position of key to the root of heap. Now, just check for nodes on Min(Max)
levels.

2. If the key added is in correct order then stop otherwise swap that key with its parent.

Example

Here is one example for inserting an element to a Min-Max Heap.

Say we have the following min-max heap and want to install a new node with value 6.

Initially, element 6 is inserted at the position indicated by j. Element 6 is less than its parent element.
Hence it is smaller than all max levels and we only need to check the min levels. Thus,element 6 gets
moved to the root position of the heap and the former root, element 8, gets moved down one step.

If we want to insert a new node with value 81 in the given heap, we advance similarly. Initially the node
is inserted at the position j. Since element 81 is larger than its parent element and the parent element is
at min level, it is larger than all elements that are on min levels. Now we only need to check the nodes
on max levels.

Delete :

Delete the minimum element

To delete min element from a Min-Max Heap perform following operations.

The smallest element is the root element.

1. Remove the root node and the node which is at the end of heap. Let it be x.

2. Reinsert key of x into the min-max heap

Reinsertion may have 2 cases:


1. If root has no children, then x can be inserted into the root.

2. Suppose root has at least one child. Find minimum value ( Let this is be node m). m is in one of
the children or grandchildren of the root. The following condition must be considered:

1. x.key <= h[m].key: x must be inserted into the root.

2. x.key > h[m].key and m is child of the root L: Since m is in max level, it has no descendants. So,
the element h[m] is moved to the root and x is inserted into node m.

3. x.key > h[m].key and m is grandchild of the root: So, the element h[m] is moved to the root. Let
p be parent of m. if x.key > h[p].key then h[p] and x are interchanged.

Conclusion: we are able to imple Max /Min heap concept in Data Structure.
GROUP F

Assignment 11

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.

Objective:

To understand the concept and basic of sequential file and its use in Data structure

Theory:

Conclusion: we understand the concept and basic of sequential file and its use in Data structure
Assignment 12

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.

Objective:

To understand the basic concept of Hashing in Data structure

Theory:

Hashing

Suppose we want to design a system for storing employee records keyed using phone numbers. And we
want following queries to be performed efficiently:

1. Insert a phone number and corresponding information.

2. Search a phone number and fetch the information.

3. Delete a phone number and related information.

We can think of using the following data structures to maintain information about different phone
numbers.

1. Array of phone numbers and records.

2. Linked List of phone numbers and records.

3. Balanced binary search tree with phone numbers as keys.

4. Direct Access Table.

For arrays and linked lists, we need to search in a linear fashion, which can be costly in practice. If we
use arrays and keep the data sorted, then a phone number can be searched in O(Logn) time using Binary
Search, but insert and delete operations become costly as we have to maintain sorted order.With
balanced binary search tree, we get moderate search, insert and delete times. All of these operations
can be guaranteed to be in O(Logn) time.

Another solution that one can think of is to use a direct access table where we make a big array and use
phone numbers as index in the array. An entry in array is NIL if phone number is not present, else the
array entry stores pointer to records corresponding to phone number. Time complexity wise this
solution is the best among all, we can do all operations in O(1) time. For example to insert a phone
number, we create a record with details of given phone number, use phone number as index and store
the pointer to the created record in table.
This solution has many practical limitations. First problem with this solution is extra space required is
huge. For example if phone number is n digits, we need O(m * 10n) space for table where m is size of a
pointer to record. Another problem is an integer in a programming language may not store n digits.

Due to above limitations Direct Access Table cannot always be used. Hashing is the solution that can be
used in almost all such situations and performs extremely well compared to above data structures like
Array, Linked List, Balanced BST in practice. With hashing we get O(1) search time on average (under
reasonable assumptions) and O(n) in worst case.

Hashing is an improvement over Direct Access Table. The idea is to use hash function that converts a
given phone number or any other key to a smaller number and uses the small number as index in a table
called hash table.

Hash Function: A function that converts a given big phone number to a small practical integer value. The
mapped integer value is used as an index in hash table. In simple terms, a hash function maps a big
number or string to a small integer that can be used as index in hash table. A good hash function should
have following properties

1) Efficiently computable.

2) Should uniformly distribute the keys (Each table position equally likely for each key) For example
for phone numbers a bad hash function is to take first three digits. A better function is consider last
three digits. Please note that this may not be the best hash function. There may be better ways.

Hash Table: An array that stores pointers to records corresponding to a given phone number. An entry in
hash table is NIL if no existing phone number has hash function value equal to the index for the entry.

Collision Handling: Since a hash function gets us a small number for a big key, there is possibility that
two keys result in same value. The situation where a newly inserted key maps to an already occupied
slot in hash table is called collision and must be handled using some collision handling technique.
Following are the ways to handle collisions:

• Chaining: The idea is to make each cell of hash table point to a linked list of records that have
same hash function value. Chaining is simple, but requires additional memory outside the table.

• Open Addressing: In open addressing, all elements are stored in the hash table itself. Each table
entry contains either a record or NIL. When searching for an element, we one by one examine table slots
until the desired element is found or it is clear that the element is not in the table.

Conclusion: We are able to implement the concept of Hashing in programming.

You might also like