0% found this document useful (0 votes)
34 views

Notes

The document contains information about various data structures topics like trees, binary search trees, graphs and hashing. It provides definitions and implementations of binary trees, binary search trees, tree traversals like inorder, preorder and postorder. It also discusses insertion and deletion operations in binary search trees. The document explains concepts of graphs like adjacency matrix and lists representations. It describes depth first search and breadth first search graph traversal algorithms. It covers hashing techniques like division method, mid-square method and multiplication method hash functions. It distinguishes between static and dynamic hashing approaches.

Uploaded by

Sneha Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Notes

The document contains information about various data structures topics like trees, binary search trees, graphs and hashing. It provides definitions and implementations of binary trees, binary search trees, tree traversals like inorder, preorder and postorder. It also discusses insertion and deletion operations in binary search trees. The document explains concepts of graphs like adjacency matrix and lists representations. It describes depth first search and breadth first search graph traversal algorithms. It covers hashing techniques like division method, mid-square method and multiplication method hash functions. It distinguishes between static and dynamic hashing approaches.

Uploaded by

Sneha Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

RAJARAJESWARI COLLEGE OF ENGINEERING

Kumbalgodu, Bangalore-74
DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING
INTERNALS ASSESSMENT-II
Subject : DATA STRUCTURE
AND APPLICATIONS
Subject Code : 21CS32
Semester& Section : 3rd sem
Q.No SCHEME AND SOLUTIONS MARKS
1 a) A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such (10)
that each node of the tree stores a value, a list of references to nodes (the “children”).
i)A Binary tree is a finite set of elements that is either empty or is partitioned into two
disjoint subsets. The first subset contains a single element called the root of the tree. The
other two subsets are themselves binary trees, called the left and right subtrees of the
original tree. A left or right sub tree can be empty.
ii)Complete binary tree is a binary tree of depth k having 2k-1 nodes, k>=1
iii)If every non leaf node in a binary tree has nonempty left and right subtrees, the tree is
termed a Strictly binary tree. A strictly binary tree with n leaves always contains 2n-1
nodes.
iv)Skewed tree is a tree consisting of only left sub tree or only right sub tree
b) i) Implementaion of Inorder traversal: (10)
void inorder(node *root)
{
if(root != NULL)
{
inorder(root->lptr);
printf("%d ",root->info);
inorder(root->rptr);
}
}
ii) Implementation of Preorder Traversal:
void preorder(node *root)
{
if(root != NULL)
{
printf("%d ",root->info);
preorder(root->lptr);
preorder(root->rptr);
}
}
iii) Implementation of postorder traversal:
void postorder(node *root)
{
if( root != NULL )
{
postorder(root->lptr);
postorder(root->rptr);
printf("%d ",root->info);
}
}
2 a) Preorder traversal - +^+6*-32523 (10)
Postorder traversal - 632-5*+2^3+
Construct a binary tree for this
b) Insertion into BST: (10)
node* insert(node *root)
{
node *new1, *cur = root, *prev= NULL;
new1 = (node *)malloc(sizeof(node));
printf("\nEnter The Element ");
scanf("%d",&new1->item);
new1->lptr = new1->rptr = NULL;
if (root == NULL)
return new1;
while(cur != NULL)
{
prev = cur;
cur = new1->item < cur->item ?
cur->lptr : cur->rptr;
}
if (new1->item < prev->item)
prev->lptr = new1;
else
prev->rptr = new1;
return root;
}

C Function to delete a node from BST:


node* Delete(node *root, int data)
{
node *temp; int min;
if (root == NULL)
{
printf("tree is empty\n");
return NULL;
}
if (data < root->item)
{
root->lptr = Delete(root->lptr, data);
return(root);
}
if (data > root->item)
{
root->rptr = Delete(root->rptr, data);
return(root);
}
if (root->lptr == NULL && root->rptr == NULL)
{
printf("deleted data %d",root->item);
free(root);
root = NULL;
return(root);
}
if (root->rptr == NULL)
{
temp = root->lptr;
printf("deleted data %d",root->item);
free(root);
return(temp);
}
if (root->lptr == NULL)
{
temp = root->rptr;
printf("deleted info %d",root->item);
free(root); return(temp);
}
3 a) i) Insertion into BST: (10)
node* insert(node *root)
{
node *new1, *cur = root, *prev= NULL;
new1 = (node *)malloc(sizeof(node));
printf("\nEnter The Element ");
scanf("%d",&new1->item);
new1->lptr = new1->rptr = NULL;
if (root == NULL)
return new1;
while(cur != NULL)
{
prev = cur;
cur = new1->item < cur->item ?
cur->lptr : cur->rptr;
}
if (new1->item < prev->item)
prev->lptr = new1;
else
prev->rptr = new1;
return root;
}
ii) C Function to search a node in BST:
void search(node *root, int key)
{
if(root != NULL)
{
if( root->info == key)
{
printf(“search is success”);
return (1);
}
if(key > root->info )
search(root->rptr,key);
else
search(root->lptr,key);
}
return (0);
}

b) (10)
Inorder traversal : IDJBFAGKCH

Preorder traversal : ABDIJFCGKH

Postorder traversal : IJDFBKGHCA

4 a) HASHING: (10)
Hashing is a technique or process of mapping keys, values into the hash table by using a
hash function. It is done for faster access to elements. The efficiency of mapping depends on
the efficiency of the hash function used.
Hashing is an effective way to store the elements in some data structure. It allows to reduce
the number of comparisons.
DIFFERENT HASH FUNCTIONS
1. Division method
It is the most simple method of hashing an integer x. This method divides x by M and then
uses the remainder obtained. In this case, the hash function can be given as:
h(x) = x mod M
Example:- Calculate the hash values of keys 1234
Solution Setting M = 97, hash values can be calculated as:
h(1234) = 1234 % 97 = 70
2.Mid-Square Method
The mid-square method is a hash function which works in two steps:
Step 1: Square the value of the key. That is, find k2
Step 2: Extract the middle r digits of the result obtained in Step 1.
3.Multiplication Method
The steps involved in the multiplication method are as follows:
Step 1: Choose a constant A such that 0 < A < 1.
Step 2: Multiply the key k by A.
Step 3: Extract the fractional part. kA.
Step 4: Multiply the result of Step 3 by the size of hash table (m).
Hence, the hash function can be given as:
h(k) = floor( m (kA mod 1)
where (kA mod 1) gives the fractional part of kA and m is the total number of indices in the
hash table.
b) Static and dynamic hashing: (10)
Static hashing is a technique in which the resultant bucket size remains the same. There will
not be any change in the bucket address. Various techniques of static hashing are linear
probing, chaining.
Dynamic Hashing
Dynamic Hashing is a hashing technique in which bucket size is not fixed. It can grow and
shrink. a typical example of dynamic hashing is extensible hashing technique. It is a
technique which handles a large amount of data. The data is to be placed in the hash table is
by extracting certain number of bits (Similar to B-Trees). In extensible hashing, referring the
size of directory, the elements are to be placed in the bucket.
5 a) GRAPH : (10)
G(V,E) is a graph G consists of set of vertices and set of edges.V is a finite set of vertices.
E is a set of pairs of vertices, these pairs are called edges.

Adjacency Matrix
Let G=(V, E) be a graph with n vertices, n >= 1.
The adjacency matrix of G is a two-dimensional n*n array with the property that
a[i][j]=1 iff the edge (i,j) is in E(G). a[i][j]=0 if there is no such edge in G

Adjacency Lists
● The n rows of the adjacency matrix are represented as n chains.
● There is one chain for each vertex in G.
● The data field of a chain node stores the index of an adjacent vertex.
● For an undirected graph with n vertices and e edges, this representation requires an
array of size n and 2e chain nodes.

Construct a adjacency matrix and adjacency list representation


DFS: Depth First Search Algorithm
DFS will visit the child vertices before visiting siblings using this algorithm:
b) ● Mark the starting node/arbitrary vertex of the graph as visited and push it onto the
stack .
● While the stack is not empty
o Peek at top node on the stack
o If there is an unvisited child of that node, Mark the child as visited and push the child node
onto the stack
Else Pop the top node off the stack.
Note: 1. On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to
the one its is currently in.
2. The process continues until a dead end ( a vertex with no adjacent vertices is
encountered).
3. At a dead end, the algorithm backs up one edge to the vertex it comes from and tries
to continue visiting unvisited vertices from there.
4. The algorithm eventually halts after backing up to the starting vertex, with the latter
being a dead end.

BFS: Breadth First Search Algorithm:


BFS will visit the sibling vertices before the child vertices using this algorithm:
Mark the starting node of the graph as visited and enqueue it into the queue.
While the queue is not empty.
o Dequeue the next node from the queue to become the current node.
o While there is an unvisited child of the current node.
o Mark the child as visited and enqueue the child node into the queue.
Note: 1. It proceeds in a systematic manner by visiting first all the vertices that are adjacent
to a starting vertex then all unvisited vertices two edges apart from it, and so on.. until all the
vertices in the same connected component as the starting vertex are visited.
2. Queue is initialized with the traversals starting vertex, which is marked as visited.
3. On each iteration, the algorithm identifies the unvisited vertices that are adjacent to the
first vertex, mark them as visited, and adds them to the queue, after that, the front vertex is
removed from the queue.

Subject Coordinator HOD

You might also like