DSL Lab Manual

Embed Size (px)

DESCRIPTION

It is lab manual for dsl.

Citation preview

Maharashtra Academy of Engineering

Lab Manual

Maharashtra Academy of Engineering

Pune-412105Lab ManualPage No.01

Department of Computer engineeringFormat no.RevisionRevision

DateClassSE

Academic Year: 2010-2011MAE/ACA/COMP/0121/12/11SemesterII

Subject:

Work LoadExam Schemes

TheoryPracticalTerm WorkPracticalOral

--04 Hrs. Per Week5050--

Sr. No. Title of AssignmentTime Span

(No. of weeks)

1Write a C++ program to create binary tree and perform recursive and non-recursive traversals.1

2Write a C++ program to create binary tree. Find height of the tree and print leaf nodes. Find mirror image, print original and mirror image using level-wise printing.1

3Write a C++ program to create a binary search tree. Insert and delete elements after creation.

4Write a C++ program to create in-order threaded binary tree and perform recursive and non recursive traversals.

5Write a C++ program to represent graph using adjacency matrix and perform DFS and BFS.1

6Write a C++ program to represent graph using adjacency list and perform DFS and BFS.

7Represent graph using adjacency matrix and generate minimum spanning tree using Prims algorithm.1

8Represent graph using adjacency matrix and find shortest path using Dijkstras algorithm.

9Write a C++ program for implementation of B tree.

10Write a C++ program for implementation of AVL tree.

11Implementation of direct access file using different collision resolution techniques1

12Write a program to add binary numbers (assume one bit as one number) Use STL stack.

13Write a C++ program to implement SLL, DLL, CLL and compare with STL implementation.1

14Write a C++ program to implement stack and queue using SLL and compare with STL implementation

15Write a program to implement Dequeue (Double ended queue) using STL.

16Write a program to use STL for sorting and searching with user defined records.

17Mini Project

Prepared By:

Checked By:

Subject In charge

Academic Council Member

Head of Department

1. Mrs. Anita Thite

1.Prof. S.A.Jain

2. Ms.Sujata Tapkir

.

Assignment No. 1Title of Assignment: Creation of binary tree and perform recursive and non-

Recursive traversals.

Algorithms:Create and Insert:

1. Accept a random order of numbers from the user. The first number would be inserted at the root node.

2. Thereafter, accept the left child and right child of each node

3. Perform this till you reach a null pointer, the place where the present data is to be inserted.

4. Allocate a new node and assign the data in this node and allocate pointers appropriately.

Traversals (Recursive):

1. Inorder: The recursive function will receive the root of the tree (or subtree) from where inorder traversal is to be initiated. Algorithm is to proceed left, which in this case is to call the same function with the left child of the node, print the data and then proceed right, which in this case is to call the same function with the right child of the node.

2. Preorder : Inorder was LDR, first move left, access data and then move right. Preorder is DLR, first access data, move left and then move right.

3. Post order is LRD, first move left, then move right and then access the data.

Traversals (Non Recursive)

1. The same effect which happened during the recursive run needs to be effected here.

2. Declare a stack which holds pointers ( the nodes of the BT).

3. In the inorder case, begin with a loop which moves left as far as it can, pushing the current node on the stack.

4. When you reach null, pop a node from the stack, access and display its data, move right and go back to step 3. If stack is empty at step 4, exit the loop.

5. The preorder and postorder traversals would proceed in an identical manner, except the position of displaying the contents of data would vary.

Testing:

Test Conditions:

Simple input of random numbers. Display the inorder traversal. It must be a sorted list of the numbers entered, since a BT has been created. This would be proved that the BT has been constructed properly. The preorder and post order traversals would give a listing of data, which could be checked by drawing the BT on paper and checking if it were true.

Input:Enter data (numbers) to be stored in the binary tree. Every node in the BT would contain 3 fields: data, left child pointer and right child pointer.

Output:Display the list of elements in the serial order as they appear in the three traversals: inorder, preorder and postorder. (both recursively and non recursively)

FAQs:

1. What is the time complexity of binary search algorithm?

2. What are right and left spin binary trees?

3. What is the concept of threads in trees? How it is helpful?4. What is height and weight balanced trees?

Conclusion:

Assignment No.2Title of Assignment: Write a program to create a Binary tree and find the height of tree

and print its leaf nodes. Find its mirror image, print original and mirror image using level-wise printing.

Algorithms:Create and Insert:

1. Accept a random order of numbers from the user. The first number would be inserted at the root node.2. Thereafter, every number is compared with the root node. accept the left child and right child of each node.

3. Perform this till you reach a null pointer, the place where the present data is to be inserted.

4. Allocate a new node and assign the data in this node and allocate pointers appropriately.

Height of BT :

1. This algorithm is based on the idea of storing the nodes of every level of the BST in a dynamic queue (link list). It is also simultaneously useful to print the tree level wise. The total number of levels accessed would be the height of the tree.

2. Initialize the contents of the list with the root of the BST. The counter no_of_nodes_in_current_level =1 and the level_accessed =1.

3. Access no_of_nodes_in_current_level from the link list and add all their children to the list at the end and simultaneously keep track of the number of nodes accessed in the next level in a variable which at the end is assigned back to no_of_nodes_in_current_level. Also increment level_accessed, indicating one more level accessed in the BST.

4. Continue step 3 repeatedly till no_of_nodes_in_current_level is 0, which means no more nodes in the next level. The value stored in the variable level_accessed is the height of the BST.

(The above is a non recursive implementation to find the height of the BST. One could also write a recursive algorithm to do the same.)

Leaf Nodes of BT :

1. There are many algorithms to find the leaf nodes of a BT. The one considered here is based on the idea that one could do a simple inorder traversal of the BT and just before printing the data as one normally does in an inorder traversal, check if both the left and right nodes are NULL. If so, it means the node under consideration is a leaf node and must be printed.

2. Inorder: The recursive function will receive the root of the tree (or subtree) from where inorder traversal is to be initiated. Algorithm is to proceed left, which in this case is to call the same function with the left child of the node, print the data if both left and right pointers are NULL and then proceed right, which in this case is to call the same function with the right child of the node.

3. Thus all the leaf nodes of the BT are printed.

Mirror of Tree :

1. Following is a algorithm of a recursive function to find mirror of the tree. The function mirror_Tree accepts a pointer to a tree as the parameter. Initially the root node is passed later the roots of the subsequent subtrees are passed as the parameter.

2. The function begins by checking if the pointer passed is not NULL. If not, allocates a new node. Assigns the data of the original node to the copied node. Assigns the left child of the new node by calling the function mirror_Tree with the right child of the original node and assigns the right child of the new node by calling the function mirror_Tree with the left child of the original node. If the pointer passed is NULL, NULL is returned by the function else the new node created is returned.

Level wise printing :

1. This algorithm is based on the idea of storing the nodes of every level of the BT in a dynamic queue (link list).

2. Initialize the contents of the list with the root of the BT. The counter no_of_nodes_in_current_level =1 and the level_accessed =1.

3. Access no_of_nodes_in_current_level from the link list. Print the Level Number and all the data of all the nodes of the current level. Simultaneously add all their children to the list at the end and keep track of the number of nodes accessed in the next level in a variable which at the end is assigned back to no_of_nodes_in_current_level. Also increment level_accessed, indicating one more level accessed in the BT.

4. Continue step 3 repeatedly till no_of_nodes_in_current_level is 0, which means no more nodes in the next level.

(The above is a non recursive implementation to do level wise printing of the BT. One could also write a recursive algorithm to do the same.)

Testing:

Test Conditions:

Simple input of random numbers. Display the height of the tree and the leaf nodes. The BST entered could be drawn on a rough page and one could check if the height calculated and the leaf nodes printed are correct.

For e.g., Enter: 34, 12, 56, 6, 14, 40, and 70.

The height of the BST is 3.

The leaf nodes are 6, 14, 40 and 70.

For Mirror Image:

Enter : 34, 12, 56, 6, 14, 40, 70.

Level wise printing of original tree

Level 1 : 34

Level 2 : 12, 56,

Level 3 : 6, 14, 40, 70

Level wise printing of the mirror tree

Level 1 : 34

Level 2 : 56, 12

Level 3 : 70,40, 14,6

Input :

Enter data (numbers) to be stored in the binary tree. Every node in the BT would contain 3 fields: data, left child pointer and right child pointer

Output The height of the tree and the list of its leaf nodes.

The original and mirror image printed level wise.

FAQs:

1. How to create the mirror image of the tree?

2. What is the logic for printing the tree level wise? 3. What is the advantage of creation of the mirror image?

Conclusion:

Assignment No.3

Title of Assignment: Write a program to create a binary search tree. Insert and delete elements after creation.

Relevant Theory / Literature Survey: (Brief Theory Expected)

Binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties

1. The left subtree of a node contains only nodes with keys less than the node's key.

2. The right subtree of a node contains only nodes with keys greater than the node's key.

3. Both the left and right subtrees must also be binary search trees.

Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.

Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.Algorithms:Insertion: Insertion begins as a search would begin; if the root is not equal to the value, we search the left or right subtrees as before. Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value. In other words, we examine the root and recursively insert the new node to the left subtree if the new value is less than the root, or the right subtree if the new value is greater than or equal to the root.

Here's how a typical binary search tree insertion might be performed in C++:

/* Inserts the node pointed to by "newNode" into the subtree rooted at "treeNode" */

void InsertNode(Node* &treeNode, Node *newNode)

{

if (treeNode == NULL)

treeNode = newNode;

else if (newNode->key < treeNode->key)

InsertNode(treeNode->left, newNode);

else

InsertNode(treeNode->right, newNode);

}Deletion: There are three possible cases to consider:

Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree.

Deleting a node with one child: Remove the node and replace it with its child.

Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Replace the value of N with the value of R, then delete R.

As with all binary trees, a node's in-order successor is the left-most child of its right sub tree, and a node's in-order predecessor is the right-most child of its left sub tree. In either case, this node will have zero or one children. Delete it according to one of the two simpler cases above.

Deleting a node with two children from a binary search tree. The triangles represent sub trees of arbitrary size, each with its leftmost and rightmost child nodes at the bottom two vertices.

Consistently using the in-order successor or the in-order predecessor for every instance of the two-child case can lead to an unbalanced tree, so good implementations add inconsistency to this selection.

Testing:

Test Conditions:

Simple input of random numbers. It must be a sorted list of the numbers entered, since a BST has been created. This would be proved that the BT has been constructed properly.

Input:Enter data (numbers) to be stored in the binary search tree. Every node in the BST would contain 3 fields: data, left child pointer and right child pointer.

Output:

Element should be inserted/deleted at the proper places in the BST.

FAQs:

1. How to create the BST?

2. What is the logic for printing the BST? 3. What is the logic for inserting element into and deleting element from BST?

Conclusion:

Assignment No.4

Title of Assignment: Creation of binary inorder threaded tree (BST) and perform inorder

Traversal using the inorder threads.

Algorithms:

Create and Insert:

1. Accept a random order of numbers from the user. The first number would be inserted at the root node.

2. Thereafter, every number is compared with the root node. If less than or equal to the data in the root node, proceed left of the BST, else proceed right.

3. Perform this till you reach a null pointer, the place where the present data is to be inserted.

4. Allocate a new node and assign the data in this node and allocate pointers appropriately. Initialize all the thread values to f (false).

Creation of Threads :

Could be done in two ways: one after BST is created and the other inserting nodes in the inorder position of the BST as and when it appears.

1. For the first method, run an inorder traversal (by previous algorithm) and store the nodes of the inorder traversal as they appear in a linear list.

2. Then read the list of nodes and if a node has its left or right pointer pointing to NULL, make it now point to the inorder predecessor or successor respectively and assign the thread values to t indicating true. Assign a head node, who will be pointed at by the left pointer of the 1st node in the inorder traversal and by the right pointer of the last node in the inorder traversal.

In the other technique, when you want to insert nodes in an existing threaded tree, do the following :

1. If a node t is to be inserted as the right child of s in a threaded binary tree, ts right child will point to ss right child and so will ts right thread.

2. ts left child will point to s and its left thread will be assigned t (true). ss right child will point to t and its right thread will be f.

3. Find the inorder successor of t and its left child will now point to t.

Inorder travesal using the threads

1. Write a function which will take any node and will give you its inorder successor. This function will access the right child of the given node.

2. If thread value is f, then as long as left thread is f keep moving left through the left pointer.

3. The node thus finally reached would be the inorder successor.

4. The Inorder traversal now becomes easy : Beginning with the first node of the inorder traversal, which can be reached from the header node, call the inorder successor repeatedly, display its data, and go on till you meet the header node again, which signals the end of the inorder traversal.

Testing:

Test Conditions:

Simple input of random numbers. Display the inorder traversal. It must be a sorted list of the numbers entered, since a BST has been created. This would be prove that the threaded BST has been constructed properly

Input:

Enter data (numbers) to be stored in the binary search tree. Every node in the BST would contain 5 fields: data, left child pointer and right child pointer, left thread tag and a right thread tag

Output :

Display the list of elements in the serial order as they appear in the inorder traversal.

FAQs:

1. How to create Threaded BST?

2. What is use of Threaded BST?

3. How to travel Treaded BST?

Conclusion:

Assignment No. 5

Title of Assignment: Write a program to represent a graph using adjacency matrix and

Perform BFS and DFS.

Algorithms:

Creation of Adjacency matrix :

1. Declare an matrix to store edges in the given graph. The number of entries in the graph should be according to start and end vertex of the given edges in the graph.

2. Take the edge set from the user. If for e.g. vertex 1 is connected to vertex 2 and 3 in the graph, the 1st location of the array of pointers (corresponding to vertex 1) would point to 2 nodes, one having the data 2 (corresponding to vertex 2) and the other having data 3.

3. In this way construct the entire adjacency list.

DFS (Depth First Search).

1. The start vertex is visited. Next an unvisited vertex w adjacent to v is selected and a DFS from w initiated.

2. When a vertex u is reached such that all its adjacent vertices have been visited, we back up to the last vertex visited which has an unvisited vertex w adjacent to it and initiate a DFS search from w.

3. The search terminates when no unvisited vertex can be reached from any of the visited ones.

BFS(Breadth First Search).

1. Starting at vertex v and marking it as visited, BFS differs from DFS in that all unvisited vertices adjacent to v are visited next.

2. Then unvisited vertices adjacent to these vertices are visited and so on.

3. A queue is used to store vertices as they are visited so that later search can be initiated from those vertices.

Testing:Test Conditions:

Enter the graph with 8 vertices and 10 edges (1,2), (1,3), (2,4), (2,5), (3,6), (3,7), (4,8), (5,8), (6,8),(7,8).

The order of the vertices visited by DFS is : 1, 2, 4, 8, 5, 6, 3, 7.

The order of the vertices visited by BFS is : 1, 2, 3, 4, 5, 6, 7, 8.

Input :

The number of vertices and the edge set of the graph

Output :

The order of vertices visited in both DFS and BFS.

FAQs:1. What is the time complexity of BFS &DFS Graph?

2. How to create BFS?

3. How to create DFS?

4. What is difference between BFS & DFS?

Conclusion:

Assignment No.6

Title of Assignment: Write a program to represent a graph using adjacency list and

Perform BFS and DFS.

Algorithms:

Creation of Adjacency list :

4. Declare an array of pointers to a link list having a data field (to store vertex number) and a forward pointer. The number of array of pointers would equal the total number of vertices in the graph.

5. Take the edge set from the user. If for eg, vertex 1 is connected to vertex 2 and 3 in the graph, the 1st location of the array of pointers (corresponding to vertex 1) would point to 2 nodes, one having the data 2 (corresponding to vertex 2) and the other having data 3.

6. In this way construct the entire adjacency list.

DFS (Depth First Search).

4. The start vertex is visited. Next an unvisited vertex w adjacent to v is selected and a DFS from w initiated.

5. When a vertex u is reached such that all its adjacent vertices have been visited, we back up to the last vertex visited which has an unvisited vertex w adjacent to it and initiate a DFS search from w.

6. The search terminates when no unvisited vertex can be reached from any of the visited ones.

BFS(Breadth First Search).

4. Starting at vertex v and marking it as visited, BFS differs from DFS in that all unvisited vertices adjacent to v are visited next.

5. Then unvisited vertices adjacent to these vertices are visited and so on.

6. A queue is used to store vertices as they are visited so that later search can be initiated from those vertices.

Testing:Test Conditions:

Enter the graph with 8 vertices and 10 edges (1,2), (1,3), (2,4), (2,5), (3,6), (3,7), (4,8), (5,8), (6,8),(7,8).

The order of the vertices visited by DFS is : 1, 2, 4, 8, 5, 6, 3, 7.

The order of the vertices visited by BFS is : 1, 2, 3, 4, 5, 6, 7, 8.Input :

The number of vertices and the edge set of the graph

Output :

The order of vertices visited in both DFS and BFS.

FAQs:5. What is the time complexity of BFS &DFS Graph?

6. How to create BFS?

7. How to create DFS?

8. What is difference between BFS & DFS?

Conclusion:

Assignment No.7

Title of Assignment: Write a program to represent graph using adjacency list or matrix and generate minimum spanning tree using PRIMs algorithm.

Algorithms:Both the methods are implemented using the Greedy approach. The difference lies in the way the cycle is prevented. To construct a M.S.T. from a graph of n vertices, we need n-1 edges exactly. Also, these must be selected in a way that the sum of the n-1 edges is minimum and so cycle is formed.

The Prims method to find the M.S.T is as follows :

1. Start from an arbitrary root vertex, say vertex number 1.

2. Assign two arrays mindist, in which we note the minimum distance from each of the vertex to vertex 1 and array nearest in which all the vertex are initially near vertex 1.

3. Among mindist find the smallest value and include that vertex in the MST along with the respective edge. Assign the value in the corresponding nearest entry to 1.

4. Now consider all the other vertices with respect to the vertex recently added to the MST. If the edge from any of these vertices to the recently added vertex is smaller than the current value stored in mindist then change the mindist value to this value and change nearest array value to the recently added node.

5. If n-1 edges are not yet part of the answer goto step 3.

6. The time complexity of the above algorithm is n2, where n is the number of vertices. There is an outer for loop that loops n-1 times to find the n-1 edges and there is a inner for loop that scans the mindist array which of size n to find the smallest entry.The Kruskals method to find the M.S.T proceeds in the following manner :

1. Form a min heap of all the edges according to their weights. Any call to delete an edge from the min heap will return the smallest weighted edge and rearrange the heap in such a way that the heap property is maintained.

2. Form a list of connected components in which maintains all the vertices in different sets.

3. Delete the smallest edge from the heap. Check if the two vertices of the edge belong to different sets; if they do then add the edge to the minimum spanning tree and merge the sets to which the vertices belong into 1 set.

4. If the two vertices of the set belong to a single set, then do not add the edge to the MST, in this way cycles are prevented.

5. If n-1 edges do not yet belong to the solution then go back to step 3.

6. The time complexity to sort the edges is aloga, the number of edges all the other steps are carried out in a time less than this one. Thus for a sparse graph where a is approximately equal to n the time complexity is nlogn and for a dense graph where a is approximately equal to n2, the time complexity is n2logn2.

Thus for sparse graphs Kruskals algorithm is faster and for a dense graph Prims algorithm is faster.

Testing:

Test Conditions:

The graph for which MST is to be found out is as follows :

Vertex 1

Vertex 2

Edge Weight

1

2

1

2

3

2

1

4

4

2

4

6

2

5

4

3

5

5

3

6

6

4

5

3

5

6

8

4

7

4

5

7

7

6

7

3The Minimum Spanning Tree for the above graph is

Vertex 1

Vertex 2

Edge Weight

1

2

1

2

3

2

1

4

4

4

5

3

4

7

4

6

7

3

The total weight of the M.S.T is 17.

Input:

Graph entered as an adjacency matrix for n vertices i.e. fill an n*n matrix with the values of the weights of the edges of the graph where the row number and column number as the two vertices of one edge.Output :

The Minimum Spanning Tree of the particular graph, i.e. the set of edges which are part of the M.S.T along with their respective weights and the total weight of the M.S.T.

FAQs:

1. What is the Prims algorithm?

2. What Kruskals algorithm?

3. What is the diffrence between Prims & Kruskals algorithm?

Conclusion:

Assignment No.8

Title of Assignment: Write a program to represent graph using adjacency list or matrix and find shortest path using Dijkstras algorithm.

Algorithms:

A Shortest Path Tree in G from start node s is a tree (directed outward from s if G is a directed graph) such that the shortest path in G from s to any destination vertex t is the path from s to t in the tree.

Dijkstra's algorithm is called the single-source shortest path. It is also known as the single source shortest path problem. It computes length of the shortest path from the source to each of the remaining vertices in the graph.

The single source shortest path problem can be described as follows: Let G= {V, E} be a directed weighted graph with V having the set of vertices. The special vertex s in V, where s is the source and let for any edge e in E, EdgeCost(e) be the length of edge e. All the weights in the graph should be non-negative.

Dijkstras algorithm, builds the tree outward from s in a greedy fashion.

Dijkstras Algorithm:

1. Input: Graph G, with each edge e having a length len(e), and a start nodes.

2. Initialize: tree = {s}, no edges. Label s as having distance 0 to itself.

3. Invariant: nodes in the tree are labeled with the correct distance to s.

4. Repeat:

a. For each neighbor x of the tree, compute an (over)-estimate of its distance to s: distance(x) = min [distance(v) + len(e)]

e=(v,x):vtree In other words, by our invariant, this is the length of the shortest path to x whose Only edge not in the tree is the very last edge.

b. Insert the node x of minimum distance into tree, connecting it via the argmin (the edge e used to get distance(x) in the expression).

Testing:Input:Output :

FAQs:

1. What is the shortest path tree?

2. What is Dijkstras algorithm?

3. What is its efficiency??

Conclusion:

Assignment No.9

Title of Assignment: Implement B-Tree of Order 5 and perform following operations on

B-Tree: Insert ,Delete

Algorithms:

Search:

1. Searching is similar to searching abinary search tree. Starting at the root, the tree is recursively traversed from top to bottom.

2. At each level, the search chooses the child pointer (subtree) whose separation values are on either side of the search value.Create and Insert:

1. All insertions start at a leaf node. To insert a new element

2. Search the tree to find the leaf node where the new element should be added. Insert the new element into that node with the following steps:

3.1. If the node contains fewer than the maximum legal number of elements, then there is room for the new element. Insert the new element in the node, keeping the node's elements ordered.

3.2. Otherwise the node is full, so evenly split it into two nodes.A single median is chosen from among the leaf's elements and the new element.

1. Values less than the median are put in the new left node and values greater than the median are put in the new right node, with the median acting as a separation value.

2. Insert the separation value in the node's parent, which may cause it to be split, and so on. If the node has no parent (i.e., the node was the root), create a new root above this node (increasing the height of the tree).

3. If the splitting goes all the way up to the root, it creates a new root with a single separator value and two children, which is why the lower bound on the size of internal nodes does not apply to the root.Delete:

There are two popular strategies for deletion from a B-Tree.

1. locate and delete the item, then restructure the tree to regain its invariants OR

2. do a single pass down the tree, but before entering (visiting) a node, restructure the tree so that once the key to be deleted is encountered, it can be deleted without triggering the need for any further restructuring

The algorithm below uses the former strategy.

There are two special cases to consider when deleting an element:

the element in an internal node may be a separator for its child nodes

Deleting an element may put its node under the minimum number of elements and children.

Each of these cases will be dealt with in order.

Deletion from a leaf node1. Search for the value to delete.

2. If the value is in a leaf node, it can simply be deleted from the node,

3. If underflow happens, check siblings to either transfer a key or fuse the siblings together.

4. if deletion happened from right child retrieve the max value of left child if there is no underflow in left child

5. in vice-versa situation retrieve the min element from right

Deletion from an internal node1. Each element in an internal node acts as a separation value for two subtrees, and when such an element is deleted, two cases arise. In the first case, both of the two child nodes to the left and right of the deleted element have the minimum number of elements; unless it is known that this particular B-tree does not contain duplicate data, we must then also (recursively) delete the element in question from the new node.

2. In the second case, one of the two child nodes contains more than the minimum number of elements. Then a new separator for those subtrees must be found. Note that the largest element in the left subtree is still less than the separator. Likewise, the smallest element in the right subtree is the smallest element which is still greater than the separator. Both of those elements are in leaf nodes, and either can be the new separator for the two subtrees.

3. If the value is in an internal node, choose a new separator (either the largest element in the left subtree or the smallest element in the right subtree), remove it from the leaf node it is in, and replace the element to be deleted with the new separator.

4. This has deleted an element from a leaf node, and so is now equivalent to the previous case. Rebalancing after deletion1. If deleting an element from a leaf node has brought it under the minimum size, some elements must be redistributed to bring all nodes up to the minimum. In some cases the rearrangement will move the deficiency to the parent, and the redistribution must be applied iteratively up the tree, perhaps even to the root. Since the minimum element count doesn't apply to the root, making the root be the only deficient node is not a problem. The algorithm to rebalance the tree is as follows:

i. If the right sibling has more than the minimum number of elements

ii. Add the separator to the end of the deficient node.

iii. Replace the separator in the parent with the first element of the right sibling.

iv. Append the first child of the right sibling as the last child of the deficient node

v. Otherwise, if the left sibling has more than the minimum number of elements.

vi. Add the separator to the start of the deficient node.

vii. Replace the separator in the parent with the last element of the left sibling.

viii. Insert the last child of the left sibling as the first child of the deficient node

ix. If both immediate siblings have only the minimum number of elements

x. Create a new node with all the elements from the deficient node, all the elements from one of its siblings, and the separator in the parent between the two combined sibling nodes.

xi. Remove the separator from the parent, and replace the two children it separated with the combined node.

xii. If that brings the number of elements in the parent under the minimum, repeat these steps with that deficient node, unless it is the root, since the root may be deficient.

xiii. The only other case to account for is when the root has no elements and one child. In this case it is sufficient to replace it with its only child.

Testing:

Test Conditions:

Enter the element to be inserted in the B-tree then display the elements of the B-tree. Input the element to be searched if the element is found display the complete node else display element not found. Enter the element to be deleted and display the B tree after deletion of element.

Input:

Enter any random no to be inserted , searched and deleted

Output:

Display the tree after each operation i.e. Insertion, Deletion and searching

FAQs:

1. What is B tree?

2. Explain significance of order of B tree?

3. Explain sibling of B tree?

4. Explain splitting of node in a B tree?

Conclusion:

Assignment No.10

Title of Assignment: Implement AVL tree and perform following operations: Insertion,

Deletion and Searching

Algorithms:

Create and Insert:

1. Insert a new node as new leaf just as in ordinary search tree.

2. Now trace the path from insertion point towards root.

3. for each n encountered check if height of left(n) and right(n) differ by at the most 1.

4. If yes, move towards parent(n).

5. other wise restructure by doing either a single rotation or a double rotation. Different Rotations in AVL tree are: LL, RR, LR, RL rotation.Delete :

1. Search the node which is to be deleted.2. a. If the searched node to be deleted is a leaf node then simply make it null.

b. If the node to be deleted is not a leaf node, then node is to be swapped with its inorder successor. Once the node is swapped , we can remove this node.

3. Now we have to traverse back up the path towards root, checking the balance factor of every node along the path. If we encounter unbalancing in some subtree then balance that subtree using appropriate single or double rotations.Searching:

1. Consider the node to be searched as key node.

2. Compare key with root node. If key < root then go to the left node and call this left node as current node. If key > root then go to the right node and call this right node as current node, otherwise the desired node is the root node.

3. Compare key with current node. If key < current node then go to left node and call this left node as current node. Else if key > current node then go to right node and call this right node as current node. else key is current node.

4. Thus repeat step 3 until the desired node is found or visited the entire tree.

Testing:

Test Conditions:Simple input a random numbers. Insert the node, after insertion display inorder , preorder and post order traversal also after deletion display all traversalsInput:Enter data ( could be words, characters or numbers) to be stored in the Tree.

Output:

Display the inorder, preorder and postorder traversals of the tree.

FAQs:

1. How to insert the new node AVL Tree?

2. How to delete node from the AVL Tree ?

3. What is LL, RR, LR, RL rotation?

4. What is difference between AVL tree and Binary Search Tree?

Conclusion:

Assignment No.11

Title of Assignment: Write a program to implement direct access file.

Algorithms:Chaining :

Here we link all the synonyms in the file. Each time we come across a synonym we add it to empty space in the file and make its previous node with same hash key point to it. So we get different chains of the same synonym in the main file.

Addition of record :

1. Accept record from the user.

2. Calculate hashed value of the key field.

3. Check for that position in the file.

4. If position found empty insert record. If not goto next available location and store data there.

5. Connect the next pointer of the previous synonym of that key to this location and the previous pointer of this record to previous synonym.

Searching for a record :

1. Accept the key of record to be searched.

2. Calculate hash value of the key.

3. Check at the position in the file.

4. If not, match found, check for that record in the entire chain by using the next link.

5. If key matched then display the record.

6. If not found, display Record not found message.

Deletion of a record :

1. Accept the key field of the record to be deleted.

2. Search the record in the file using the chain.

3. If found, then delete the record by making key = -1.

4. Make the pointer of the previous node point to the next node the current record was pointing to.

Append a record proceeds similarly as display record, except that after the record is obtained changes are made to it and written in the same location as was read.

Testing:

Test Conditions:

Enter any student database with student name, roll number and marks. Use roll number as key. Display the records to see if they are accessed properly. Delete a record and then try to display it. Appropriate error message must be printed.

Input:

Student data to be entered in the Simple Index file

Output :

Data displayed on request.

FAQs:

1. What is chaining?

2. What are the types of chaining?

3. How to access data directly by using direct access file?

Conclusion:

Assignment No.12

Title of Assignment: Write a program to add binary numbers (assume one bit as one

Number) use STL stack.

Algorithms:

Standard Template Library:

Template can be use to create the generic classes and functions that extend support to generic programming. The collection of the generic classes and functions is called standard template library.

Components of STL:

1. Containers

2. Algorithms

3. Iterators

Containers:

It is an object that actually stores data. It is the way in which data is stored in memory. STL containers are implemented by template classes and therefore can be easily customized to hold different data types.

Algorithm:

An algorithm is a procedure that is used to process data that is held in containers. STL includes many different types of algorithm.

Iterator:

It is an object that points to an element in a container. We can use an iterator to more through the contents of a container. Iterators are handled just like like pointers.

Steps:

Accept and convert the no. in binary:-

1. Read numbers

2. Obtain remainder on division by 2

3. Push remainder onto stack

4. Divide no. by 2

5. If no.>0 , goto step 2

6. Display no.

7. return

Add:-

1. Initialize carry to zero

2. Pop one element from each stack

3. Add the two and push result into the third stackalso initialise carry caused due to addition

4. In both stacks are not empty , goto step 2

5. For stack that is not empty , perform addition with carry and push result into the third stack

6. Convert result intodecimal

7. Return

Testing:

Test Conditions:

Binary value for 7=111

Binary value for 9=1001

Result =16in binary = 10000

Input:

Enter the no. in decimal

Output :

Output is in decimal as well as in binary

FAQs:1. What is STL?

2. Why we are using STL?

3. What are the functions used in this program?

4. What is Itrator?

5. What is Container?

Conclusion:

Assignment No.13

Title of Assignment: Implementation of Single Linked List, Double Linked List and Circular Linked List using Standard Template Library.

Design Analysis / Implementation Logic:

Algorithms and Requirements: C++ provides LIST.H header file which contains built in functions for the implementation of single linked list. DBLIST.H header file can be used for the implementation of doubly linked list.

Create and Insert:

1. Accept data to be entered in the SLL one element at a time. For the 1st data create a node using List add function. For DLL use add function from DBLIST library.2. From the second element entry onwards, add function will automatically add the element to the first position which will become the new head of the list.

3. For Circular Linked list next pointer of last node points to the start of Linked List. Delete :

1. Use Detach function from List.h library to remove the object from the linked list.2. Which object has to be deleted and in which manner depends on the arguments passed to the function. E.g. virtual void detach( const Object& toDetach, DeleteType dt = NoDelete );Display :

1. Use the List Iterator container class to traverse through the linked list.

2. Use operator ++ function to traverse to the next node.

Testing:

Test Conditions:

Simple input of random numbers. After every insertion display to see if all the contents are displayed properly. After every deletion also do the same. Delete nodes at different locations in the SLL for e.g. start node, last node and middle node. Make sure that your delete function works for all cases.Input:

Enter data (could be words, characters or numbers) to be stored in the SLL or DLL. Every node in the SLL or DLL would contain 2fields: data, pointer.

Output :

Display the list of elements in the serial order as they appear in the SLL or DLL.

FAQs:1. What is a Standard Template Library?

2. What built in data structure implementations do it provide?

3. What are the built in functions for in List.h and Dblist.h?

Conclusion:In this assignment we have studied the basics about template library and how to create singly linked list, doubly linked list and circular linked list and various operations on it.

Assignment No.14

Title of Assignment: Implementation of stack & queue using Standard Template Library.

Design Analysis / Implementation Logic:

Algorithms and Requirements: C++ provides STACK.H header file which contains built in functions for the implementation of stack. QUEUE.H header file can be used for the implementation of queue.

Create and Insert:

4. Accept data to be entered in the stack one element at a time. Use stack: push function to add the data to stack.5. For queue, after accepting the data from user use queue: put function to add the data to queue.

6. For Circular Linked list next pointer of last node points to the start of Linked List. Delete :

3. Use stack: pop function to remove the data from the stack.

4. Use queue: get function to remove the data from the queue.Display :

3. We can use getItemsInContainer ( ) function to get the totals no of items in the stack. And can use a stack Iterator to iterate through the stack.

4. In the same way we can create a Iterator for the queue and display the contents of the queue.

Testing:

Test Conditions:

Simple input of random numbers. After every insertion display to see if all the contents are displayed properly. After every deletion also do the same. Input:

Enter data (could be words, characters or numbers) to be stored in the stack or queue. Every node in the Stack or queue would contain 2fields: data, pointer.

Output :

Display the list of elements in the serial order as they appear in the Stack or queue.

FAQs:1. What is a Standard Template Library?

2. What built in data structure implementations do it provide?

3. What are the built in functions for in stack.h and queue.h?

Conclusion:In this assignment we have studied the basics about template library and how to create stack and queue and various operations on it.

Assignment No.15

Title of Assignment: Write a program to implement Dequeue using STL.

Algorithms:

A deque is double ended queue with optimized operations both at the front and the back The deque is maintained into standard template library.

Some of the operations that can be performed are:

Front()

Back()

Push_front()

Pop_front()

Push_back()

Pop_back()

Empty()

1. Main()

Read choice from user

2. Switch(choice)

Case 1: read element

Call: push_front(element)

Case 2: read element from user

Call: Push_back()

Case 3: Display front()

Call: pop_front()

Case 4: Display back()

Call: pop_back()

3. If user wants to continue goto step 1

4. Exit

1. Display()

Declare an iterator of the container type . i.e. in this case dequeue say I

For(I=dq.begin();I!=dq.end;i++)

Display(*i)

Return

Testing:

Test Conditions:

1. Add from front

2. Add from rear

3. Delete from front

4. Delete from rear

Enter the element to add from front:2

Enter the element to add from front:1

Enter the element to add from rear:4

Enter the element to add from rear:7

The element of the deque are : 1 2 4 7

Delete the element from the front:

Delete the element from the rear:

The element of the deque are : 2 4

Input:Give the integer input from front and rear. store that data in deque.

Output:

Display the data present in the deque. Display it after deletion of the data from front and from rear.

FAQs:4. What are the deque functions declared in the STL?

5. How to use deque functions?

6. What is the use of these deque functions?

Conclusion:

Assignment No.16

Title of Assignment: Write a program to use STL for sorting and searching with user defined records.

Algorithms:

By including algorithm.h the above file many useful generic algorithms can be applied on containers. These algorithms can be applied to containers such as list ,vector ,deque, etc.

Some of the common generic algorithms are available:

Sort: sort the element present in the container class.

Binary search to search for the element in container class

Equal: to check two containers are equal or not.

Copy: to copy the item of one container class into another container

class.

Fill: to fill all the elements of a container class with a particular item.

Remove:

to remove all the elements from the container class

main()

1. Read choice from user

2. Select case choice

Case 1:

Read data from user

Add data to container

Case 2:

Call:display()

Case 3:

Read data to be searched

Declare an iterator of that type

Call: search(start,end,data)

Case 4:

Call: sort(start,end)

3.Exit

display()

1. Declare the iterator of the container type , say I

2. For I= begin() ; I not equal to end() ; I++

DISPLAY(*i);

3. Return

Testing:

Test Conditions:

1. ADD

2. DISPLAY

3. SEARCH

4. SORT

5. EXIT

ENTER YOUR CHOICE:1

ENTER ROLL_NO :1

ENTER NAME : A

1. ADD

2. DISPLAY

3. SEARCH

4. SORT

5. EXIT

ENTER YOUR CHOICE:1

ENTER ROLL_NO :2

ENTER NAME : B

1. ADD

2. DISPLAY

3. SEARCH

4. SORT

5. EXIT

ENTER YOUR CHOICE:1

ENTER ROLL_NO :3

ENTER NAME : C

1. ADD

2. DISPLAY

3. SEARCH

4. SORT

5. EXIT

ENTER YOUR CHOICE:2

ROLL NO:1

NAME :A

ROLL NO:2

NAME :B

ROLL NO:3

NAME :C

1. ADD

2. DISPLAY

3. SEARCH

4. SORT

5. EXIT

ENTER YOUR CHOICE:3

ENTER ROLL NO :2

THE RECORD WAS FOUND

ROLL NO:2

NAME :B

1. ADD

2. DISPLAY

3. SEARCH

4. SORT

5. EXIT

ENTER YOUR CHOICE:4

ROLL NO:1

NAME :A

ROLL NO:2

NAME :B

ROLL NO:3

NAME :C

Input:

Enter the name and roll number of the students.Output:

Whatever the name and roll number display the records.

Whatever the name and roll number search the records by roll number.

Whatever the name and roll number sort the records by roll number.

FAQs:

1. How to search the records by roll number?

2. How to sort the records by roll number?

Conclusion:

Assignment No.17

Title of Assignment: Write a C++ program to implement a small database mini project to understand persistent objects and operations on sequential files (e.g. library information, inventory systems, automated banking system, reservation systems etc.) For example, write a program to create a database for reservation system using information such as Name, sex, age, starting place of journey and destination. Program should have following facilities

a) To display entire passenger list b) To display particular record

c) To update record d) To delete and sort recordUse Exception Handling for data verification

Design Analysis / Implementation Logic:

Testing:

FAQs:

Conclusion:

4