Академический Документы
Профессиональный Документы
Культура Документы
QUESTION BANK
YEAR : II
REGULATION : 2008
Prepared By
G.P.Marrina M.Tech
Asst.Prof/CSE. A.C.T.C.E.T
PART A (2 MARKS)
Data Structures is defined as the way of organizing all data items that consider not only the
elements stored but also stores the relationship between the elements.
Linked list consists of a series of structures, which are not necessarily adjacent in memory. Each
structure contains the element and a pointer to a structure containing its successor. We call this theNext
Pointer. The last cell’s Next pointer points to NULL.
The different types of linked list include singly linked list, doubly linked list and circular linked
list.
• It is not necessary to specify the number of elements in a linked list during its declaration
• Linked list can grow and shrink in size depending upon the insertion and deletion that occurs in the list
• Insertions and deletions at any place in a list can be handled easily and efficiently
• A linked list does not waste any memory space
Some of the important applications of linked lists are manipulation of polynomials, sparse
matrices, stacks and queues.
8. Define ADT.
An abstract data type (ADT) is a set of operations. Abstract data types are mathematical
abstractions; nowhere in an ADT's definition is there any mention of how the set of operations is
implemented. This can be viewed as an extension of modular design.
Each node contains two pointers; one is its predecessor and another to its successor. In fact, in the
context of doubly linked lists, the term predecessor and successor are meaningless, since the list is
entirely symmetric. Doubly linked list may be either linear or circular and may or may not contain a
header node as illustrated in the below figure.
10. Define
Circular linked list.
Suppose that a small change is made to the structure of a linear list, so that the next field in the
last node contains a pointer back to the first node rather than the null pointer. Such a list is called circular
list and is illustrated below. From any point in such a list it is possible to reach any other point in the list.
If we begin at a given node and traverse the entire list, we ultimately end up at the starting point.
A stack is an ordered collection of items into which new items may be inserted and from which
items may be deleted at one end, called the top of the stack.
A queue is an ordered collection of items from which items may be deleted at one end called the
front of the queue and into which items may be inserted at other end called the rear of the queue. For this
reason sometimes the queue is also called FIFO (first-in, first-out).
There are two types of operations that takes place in queue, they are
Enqueue – To insert the item into the queue at the end.
Dequeue – To delete the item from the queue in the front.
It is a data structure in which the intrinsic ordering of the elements does determine the results of
its basic operations. There are two types of priority queue,
Ascending priority queue – It is a collection of items into which items can be inserted arbitrarily
and from which only the smallest item can be removed.
Descending priority queue – Allows deletion of only the largest item.
PART A (2 MARKS)
1. Define a tree.
A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a
distinguished node r, called the root, and zero or more nonempty (sub) trees T.
2. Define root.
This is the unique node in the tree to which further sub-trees are attached. Here, A is the root.
The total number of sub-trees attached to that node is called the degree of the node. For node A,
the degree is 2 and for B and C, the degree is 0.
4. Define leaves.
These are the terminal nodes of the tree. The nodes with degree 0 are always the leaves. Here, B
and C are leaf nodes.
The nodes other than the root and the leaves are called internal nodes. Here, C is the internal
node.
The node which is having further sub-branches is called the parent node of those sub-branches.
Here, node C is the parent node of D and E.
The depth of n is the length of the unique path from the root to n.
The height of n is the length of the longest path from n to a leaf.
Define depth and height of a tree.
The depth of the tree is the depth of the deepest leaf. The height of the tree is equal to the height
of the root. Always depth of the tree is equal to height of the tree.
The root node is always considered at level zero, and then its adjacent children are supposed to be
at level 1 and so on. Here, node A is at level 0, nodes B and C are at level 1 and nodes D and E are at level
2.
9. Define forest.
A tree may be defined as a forest in which only a single node (root) has no predecessors. Any
forest consists of a collection of trees.
A binary tree is a finite set of nodes which is either empty or consists of a root and two disjoint
binary trees called the left sub-tree and right sub-tree.
A path in a tree is a sequence of distinct nodes in which successive nodes are connected by edges
in the tree.
All intermediate nodes that traverse the given tree from its root node to the terminal nodes are
referred as non-terminal nodes.
A full binary tree is a tree in which all the leaves are on the same level and every non-leaf node
has exactly two children.
A complete binary tree is a tree in which every non-leaf node has exactly two children not
necessarily to be on the same level.
A right-skewed binary tree is a tree, which has only right child nodes.
The maximum number of nodes on level n of a binary tree is 2 n-1, where n≥1.
The maximum number of nodes in a binary tree of height n is 2 n-1, where n≥1.
For any non-empty tree, n1=nd+1 where n1is the number of leaf nodes and nd is the number of
nodes of degree 2.
Traversing a binary tree means moving through all the nodes in the binary tree, visiting each node
in the tree only once.
•Preorder traversal
•Inorder traversal
•Postorder traversal
•Levelorder traversal
21. What are the tasks performed while traversing a binary tree?
•Visiting a node
•Traverse the left sub-tree
•Traverse the right sub-tree
Insertions and deletions in a node take an excessive amount of processing time due to data
movement up and down the array.
Insertions and deletions in a node involve no data movement except the rearrangement of
pointers, hence less processing time.
A binary search tree is a special binary tree, which is either empty or it should satisfy the
following characteristics.
•Every node has a value and no two nodes should have the same value i.e) the values in the binary
search tree are distinct.
•The values in any left sub-tree is less than the value of its parent node
•The values in any right sub-tree is greater than the value of its parent node
•The left and right sub-trees of each node are again binary search trees
32. Why it is said that searching a node in a binary search tree is efficient than that of a
simple binary tree?
In binary search tree, the nodes are arranged in such a way that the left node is having
less data value than root node value and the right nodes are having larger value than that of
root. Because of this while searching any node the value of the target node will be compared with
the parent node and accordingly either left sub branch or right sub branch will be searched. So, one has to
compare only particular branches. Thus searching becomes efficient.
In threaded binary tree, the NULL pointers are replaced by some addresses. The left
pointer of the node points to its predecessor and the right pointer of the node points toits successor.
An expression tree is a tree which is build from infix or prefix or postfix expression. Generally, in
such a tree, the leaves are operands and other nodes are operators.
Right-in threaded binary tree is defined as one in which threads replace NULL pointers
in nodes with empty right sub-trees.
PART A (2 MARKS)
An empty tree is height balanced. If T is a non-empty binary tree with T L and TR as its left and right
subtrees, then T is height balanced if
2. | hL - hR | ≤ 1.
Balanced trees have the structure of binary trees and obey binary search tree properties.
Apart from these properties, they have some special constraints, which differ from one data
structure to another. However, these constraints are aimed only at reducing the height of the
tree, because this factor determines the time complexity. Eg: AVL trees, Splay trees.
Let A be the nearest ancestor of the newly inserted nod which has the balancing
factor ±2. Then the rotations can be classified into the following four categories:
Left-Left: The newly inserted node is in the left subtree of the left child of A.
Right-Right: The newly inserted node is in the right subtree of the right child of A.
Left-Right: The newly inserted node is in the right subtree of the left child of A.
Right-Left: The newly inserted node is in the left subtree of the right child of A.
The height of left subtree minus height of right subtree is called balance factor of a
node in AVL tree. The balance factor may be either 0 or +1 or -1. The height of an empty tree is
-1.
A splay tree is a binary search tree in which restructuring is done using a scheme called splay.
The splay is a heuristic method which moves a given vertex v to the root of the splay tree using a
sequence of rotations.
6. What is the idea behind splaying?
Splaying reduces the total accessing time if the most frequently accessed node is moved
towards the root. It does not require maintaining any information regarding the height or
balance factor and hence saves space and simplifies the code to some extent.
Let us assume that the splay is performed at vertex v, whose parent and grandparent are p
and g respectively. Then, the three rotations are named as:
Zig:
If p is the root and v is the left child of p, then left-left rotation at p would suffice. This case always
terminates the splay as v reaches the root after this rotation.
Zig-Zig:
If p is not the root, p is the left child and v is also a left child, then a left-left rotation at g followed by
a left-left rotation at p, brings v as an ancestor of g as well as p.
Zig-Zag:
If p is not the root, p is the left child and v is a right child, perform a left-right rotation at g and
bring v as an ancestor of p as well as g.
8. Define Heap.
A heap is defined to be a complete binary tree with the property that the value of each
node is at least as small as the value of its child nodes, if they exist. The root node of the heap has the
smallest value in the tree.
The minimum number of nodes S(h), in an AVL tree of height h is given by S(h)=S(h-
1)+S(h-2)+1. For h=0, S(h)=1.
A B-tree of order M is a tree that is not binary with the following structural properties:
•The root is either a leaf or has between 2 and M children.
•All non-leaf nodes (except the root) have between ┌M/2┐ and M children.
A B-tree of order 3 is called 2-3 trees. A B-tree of order 3 is a tree that is not binary with
the following structural properties:
•The root is either a leaf or has between 2 and 3 children.
•All non-leaf nodes (except the root) have between 2 and 3 children.
A B-tree of order 4 is called 2-3-4 tree. A B-tree of order 4 is a tree that is not binary with the
following structural properties:
•The root is either a leaf or has between 2 and 4 children.
•All non-leaf nodes (except the root) have between 2 and 4 children.
•Database implementation
•Indexing on non primary key fields.
In a multiuser environment, the operating system scheduler must decide which of the several
processes to run only for a fixed period of time. One algorithm uses queue. Jobs are initially placed at the
end of the queue. The scheduler will repeatedly take the first job on the queue, run it until either it finishes
or its time limit is up and place it at the end of the queue if it does not finish. This strategy is not
appropriate, because very short jobs will soon to take a long time because of the wait involved in the run.
Generally, it is important that short jobs finish as fast as possible, so these jobs should have precedence
over jobs that have already been running. Furthermore, some jobs that are not short are still very
important and should have precedence. This particular application seems to require a special kind of
queue, known as priority queue. Priority queue is also called as Heap or Binary Heap.
i)Structure Property
A heap is a binary tree that is completely filled with the possible exception at the bottom level,
which is filled from left to right. Such a tree is known as a complete binary tree.
In a heap, for every node X, the key in the parent of X is smaller than (or equal to) the
key in X, with the exception of the root (which has no parent).
•Event simulation
PART A (2 MARKS)
A hash table similar to an array of some fixes size-containing keys. The keys specified here might be
either integer or strings, the size of the table is taken as table size or the keys are mapped on to some
number on the hash table from a range of 0 to table size.
2. Explain Hashing .
Hashing is a technique used to identify the location of an identifier ‘x’ in the memory by some
arithmetic functions like f(x), which gives address of ‘x’ in the table.
Hash Function takes an identifier and computes the address of that identifier in the hash table.
1.Division method
2.Square method
3.Folding method
5.Define Collision.
When two different keys compute in the same location or address in the hash table through any
one of the hashing function then it is termed as collision.
Separate chaining.
Open Addressing
Linear Probing
Quadratic Probing
Double Hashing.
Separate Chaining is a technique used to avoid collision, where a linked list is used to store the
keys which fall into the same address in the hash table.
Open addressing is an alternative to resolving collisions with linked lists. In an open addressing
hashing system, if a collision occurs, alternative cells are tried until an empty cell is found.
In Linear Probing, F is a linear function of i, typically F(i)=i. This amount to trying cells sequentially in
search of an empty cell.
Quadratic Probing is a collsion resolution method that eliminates the primary clustering problem of linear
probingThe popular choice is F(i)=i2.
For Double Hashing ,One popular choice is F(i)=i . hash2(X).This formula says that we apply a second
hash function to X and probe at a distance hash2(X), hash2(X)…and so on.
1. Compilers can use hash table to keep track of declared variable in source code.
2. A hash table is useful for any graph theory problem where nodes have real names instead of numbers
A relation R is defined on a set S if for every pair of element (a, b), a, bS, aRb is either true or
false if aRb is true, then we say that a is related to b.
Where all connection are metal wires, in an equivalence relation. The relation is clearly
reflexive ,as any component is connected to itself. If a is electrically connected to b, then b must be
electrically connected to a, so this relation is symmetric. Finally if a is connected to b and b is connected
to c then a is connected to c. Thus electrical connectivity is an equivalence relation.
The equivalence class of an element a € S is the subset of S that contain all the element that are
related to a.
During the course of the algorithm, the set can change via the Union operation. The algorithm
must also operate on-line: when a find is preformed ,it must give an answer before continuing.
Always to make the smaller tree a sub tree of the larger, breaking ties by any method Union by
size Union by height.
This is the only way to speed the algorithm up , without reworking the data structure entirely.
Path compression is performed during a Find operation.
20. Write the code disjoint set Find with path compression method.
Set Type
if(S[X]<=0)
return X;
else
return S[X]=Find(S[X],S);
UNIT V – GRAPHS
PART A (2 MARKS)
1. Define Graph.
A Graph G, consists of a set of vertices V, and a set of edges E.V is a finite non-empty set consisting of
vertices of vertices of the graph. The set of edges E consists of a pair of vertices from the vertex set.
a b
a b
A graph is said to be weighted graph if every edge in the graph is assigned some weight or
value.The weight of an edge is a positive value that may be representing the distance between the vertices
or the weights of the edges along the path.
2
a b
5 11
a b
a
c d
a a
8. List some representation of Graphs?
-adjacency matrix
-Incident matrix
-Adjacency list
-Adjacency multilist
If G=(V, E) is a graph. The edge between v1 and v2 is represented as (v1, v2). If the edges of the form
(v1, v2) and (v2, v1) are treated as the same edge, then G is said to be an undirected graph.
In case of a directed graph, the edge <v1, v2> and <v2, v1> are different.
In a directed graph, for any node ‘v’, the number of outgoing edges from ‘v’ are called out degree
of a node ‘v’. Ex : out degree of c =2
a b
c
13. Define In degree of a graph.
In a directed graph, for any node ‘v’, the number of incoming edges to ‘v’ are called In degree of
a node ‘v’. Ex : In degree of c =1
a b
The sum of the In degree and out degree of a node is called the total degree of the node. Ex :
total degree of a node c = 1 +2 =3
a b
The path in a graph is the route taken to reach the terminal node from a starting node.
a b
a
e
a
c d
a a
P1 = ((a,b),(b,e))
P2 = ((a,c),(c,d),(d,e))
A complete graph is a graph in which there is an edge between every pair of vertices.
a b
a
c d
a a
A B
a
A B C D
C D
a
0 1 1 1
0 0 0 1
0 0 0 1
1 1 1 0
Dept. of CSE -A.C.T College of Engineering & Technology, Nelvoy Page 21
Question Bank –CS2201 DATA STRUCTURES
A spanning tree of a connected graph G, is a tree consisting of edges and all the vertices of G.
In minimum spanning tree T, for a given graph G, the total weights of the edges of the spanning tree
must be minimum compared to all other spanning trees generated from G.
-Prim’s and Kruskal is the algorithm for finding Minimum Cost Spanning Tree.
For a given graph G=(V, E), with weights assigned to the edges of G, we have to find the shortest path
(path length is defined as sum of the weights of the edges) from any given source vertex to all the
remaining vertices of G.
A topological sort is an ordering of vertices in a directed acyclic graph, such that if there is a path
from vi to vj appears after vi in the ordering.
22. What is the use of Kruskal’s algorithm and who discovered it?
Kruskal’s algorithm is one of the greedy techniques to solve the minimum spanning tree
problem. It was discovered by Joseph Kruskal when he was a second-year graduate student.
Dijkstra’s algorithm is used to solve the single-source shortest-paths problem: for a given vertex
called the source in a weighted connected graph, find the shortest path to all its other vertices. The single-
source shortest-paths problem asks for a family of paths, each leading from the source to a different
vertex in the graph, though some paths may have edges in common.
24. Prove that the maximum number of edges that a graph with n Vertices is
n*(n-1)/2.
Choose a vertex and draw edges from this vertex to the remaining n-1 vertices. Then, from these n-
1 vertices, choose a vertex and draw edges to the rest of the n-2 Vertices. Continue this process till it
ends with a single Vertex.
(n-1)+(n-2)+(n-3)+…+1 =n*(n-1)/2.
Two Vertices u and v are said to be connected if there exists a path from u to v in the graph. A directed
graph is said to be connected if every pair of vertices in the graph is connected.
A directed graph is said to be strongly connected if for every pair of distinct vertices v i and vj, there
exists two disjoint paths, one from vi to vj and the other from vj to vi.