Notes
Notes
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;
}
b) (10)
Inorder traversal : IDJBFAGKCH
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.