DS IA3 Scheme
DS IA3 Scheme
1 Write C functions for DLL to i) insert front ii) insert rear iii) delete front iv) delete end v) search a
value
void insert_front()
{
node *n;
n=getnodeDLL();
if(start == NULL)
{
start = n;
return;
}
n->rptr=start;
start->lptr=n;
start=n;
}
5X2=10
void insert_rear()
{
node *n1,*temp = start;
n1=getnodeDLL();
if(start==NULL)
{
start=n1;
return;
}
while(temp->rptr!=NULL)
temp=temp->rptr;
temp->rptr=n1;
n1->lptr=temp;
}
void delete_front()
{
node *temp = start;
if(start==NULL)
{
printf(“\n List is empty”);
return;
}
Course Code: BCS304 Scheme and Solution
void delete_end()
{
node *temp =start;
if(start==NULL)
{
printf(“\n Empty list”);
return;
}
if(start->next==NULL)
{
printf(“\nt%d\t is deleted”,start->info);
free(start);
start=NULL; return;
}
while(temp->rptr!=NULL)
temp=temp->rptr;
(temp->lptr)->rptr=NULL;
printf(“\nThe deleted node is %d\t”, temp->info);
free(temp);
}
void search_key()
{
int key;
node *new1,*temp = start;
if(start == NULL)
{
printf("empty ");
return;
}
printf("enter the key");
scanf("%d",&key);
while(temp != NULL && temp->info != key)
{
temp = temp->next;
}
if(temp == NULL)
{
printf("key not found");
return;
}
printf(" key found");
}
Course Code: BCS304 Scheme and Solution
2 Write a C function to add two polynomials represented in Singly Circular Linked List format.
10
3 Define and identify with a suitable diagram the following terms with respect to tree data structure.
i) Degree of a node ii) Degree of a tree iii) Leaf node iv) Non terminal node v) Level of a tree
Course Code: BCS304 Scheme and Solution
Degree of a node: In a tree data structure, the total number of children of a node is called as DEGREE of that
Node. In simple words, the Degree of a node is total number of children it has. The highest degree of a node among
all the nodes in a tree is called as 'Degree of Tree'.Ex: Degree of B is 3, A is 2 and of F is 0
Degree of a Tree: The highest degree of the node among all the nodes in a tree is called the Degree of Tree.
Leaf Node: In a tree data structure, the node which does not have a child is called as LEAF Node. In simple words, 5X2=10
a leaf is a node with no child. In a tree data structure, the leaf nodes are also called as External Nodes. External
node is also a node with no child. In a tree, leaf node is also called as 'Terminal' node.Ex: K,L,F,G,M,I,J are leaf
node
Non-Terminal Node: In a tree data structure, the node which has atleast one child is called as INTERNAL Node.
In simple words, an internal node is a node with atleast one child. In a tree data structure, nodes other than leaf
nodes are called as Internal Nodes. The root node is also said to be Internal Node if the tree has more than one
node. Internal nodes are also called as 'Non-Terminal' nodes.
Level of a Tree: In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In simple words, in a tree
each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each
level (Step).
4 Write an explanatory note on the following representation of a tree with suitable diagrams. i) List
representation ii) Left Child-Right sibling representation iii) Representation as a Degree-two tree.
i) List representation
In this representation, we use two types of nodes one for representing the node with data called 'data node' and
another for representing only references called 'reference node'. We start with a 'data node' from the root node in the
tree. Then it is linked to an internal node through a 'reference node' which is further linked to any other node
directly. This process repeats for all the nodes in the tree.
The above example tree can be represented using List representation as follows...
Course Code: BCS304 Scheme and Solution
To obtain degree-two tree representation of a tree, rotate the right- sibling pointers in the left child- right sibling tree
clockwise by 45 degrees. In a degree-two representation, the two children of a node are referred as left and right
children.
5. a Define the following types of binary trees, with suitable diagram. i) Full binary tree ii) Skewed tree
iii) Complete binary tree
3X2=6
Course Code: BCS304 Scheme and Solution
A tree with only left subtrees is called Left Skewed Binary Tree.
A tree with only right subtrees is called Right Skewed Binary Tree.
6. a Explain with a suitable diagram how a tree can be represented using an array. Mention the draw-
backs of the same if any.
Array representation:
A tree can be represented using an array, which is called sequential representation.
The nodes are numbered from 1 to n, and one dimensional array can be used to store the
nodes.
Position 0 of this array is left empty and the node numbered i is mapped to position i of
3
the array.
Below figure shows the array representation for both the trees of figure (a).
Course Code: BCS304 Scheme and Solution
Drawbacks 3
7. a Explain with a suitable diagram and algorithm how threads can be created in a threaded binary
tree
Course Code: BCS304 Scheme and Solution
Explanation
3
7. b Define the following with respect to graph data structure. i) Path ii) Length of a path
Path
A “path” from vertex u to vertex v in graph G is a sequence of vertices u, i1, i2…..ik, v such that (u, i1), (i1, i2),
2X2=4
…..(ik, v) are edges in E(G).
Length of a path
The “Length of a path” is the number of edges on it.
Ex: Considering graph G1, path is (0, 1), (1, 3), (3, 2) is also written as 0, 1,3, 2. Paths 0, 1, 3, 2 and 0, 1, 3, 1 of G1
are both of length 3. 0, 1, 3, 2 is a simple path and 0, 1, 3, 1 is not a simple path.
8. a Define the following with respect to hashing. i) Static hashing ii) Dynamic hashing
Static Hashing:
Is a hashing technique in which the table(bucket) size remains the same (Fixed during compilation time) is called 3
static hashing.
Various techniques of static hashing are linear probing and chaining
As the size is fixed, this type of hashing consist handling overflow of elements (Collision) efficiently.
Dynamic Hashing: Dynamically increases the size of the hash table as collision occurs. There are two types:
1) Dynamic hashing using directory or (Extendible hashing) : uses a directory that grows or shrinks
depending on the data distribution. No overflow buckets 3
2) Directory less Dynamic hashing or (Linear hashing): No directory. Splits buckets in linear order,
uses overflow buckets
Course Code: BCS304 Scheme and Solution
8. b Define a forest and specify the algorithm to convert a forest into a binary tree.
A forest is a set of disjoint trees. Removing the root of a tree produces a forest. A forest can be transformed into a
single binary tree by linking the binary tree representations of each tree in the forest through the rightChild field. 2
Forest is a collection of some trees. As the tree can correspond to a unique binary tree,
the forest can also correspond to a unique binary tree [2].
If F={T1,T2,…,Tn} is a forest, according the following rules to convert F into a binary tree B={root,LB,RB}:
(1) If F is empty, that is n=0, B will be an empty tree;
(2) Otherwise, follow these steps to convert:
① The root of B (named root) is the root of the first tree of the forest
(ROOT(T1)); 2
② The left subtree of B (named LB) is a binary tree converted from F1=
{T11,T12,…,T1m}, which is the subtree forest of the root of T1;
9. a Define the following methods in hashing. i) Division method ii) Mid square method iii) Folding
method
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 The division method is quite good for just 2
about any value of M and since it requires only a single division operation, the method works very fast. However,
extra care should be taken to select a suitable value for M. Generally, it is best to choose M to be a prime number
because making M a prime number increases the likelihood that the keys are mapped with a uniformity in the
output range of value
Mid square method: Here, the key K is squared. A number ‘l’ in the middle of K2 is selected by removing the
digits from both ends. H(k)=l 2
Example 1: Solution: Let key=2345, Its square is K 2 =574525 H (2345) =45=>by discarding 57 and 25
Folding method:
Step 1: Divide the key value into a number of parts. That is, divide k into parts k1, k2, ..., kn, where each part has
the same number of digits except the last part which may have lesser digits than the other parts.
Step 2: Add the individual parts. That is, obtain the sum of k1 + k2 + ... + kn. The hash value is produced by 2
ignoring the last carry, if any. Note that the number of digits in each part of the key will vary depending upon the
size of the hash table
9. b Write a note on representation of disjoint sets in the form a tree
Trees can be used for the representation of sets. Sets being considered are pairwise disjoint.If Si and Sj are two sets
and i≠j, then there is no element that is in both Si and Sj. If 10 elements numbered 0 through 9 are partitioned into 3
disjoint sets
Postfix form
ab+c+de*+