Professional Documents
Culture Documents
Q 1. What Is Data Structure? Also Define Structures and Operations
Q 1. What Is Data Structure? Also Define Structures and Operations
Q 1. What Is Data Structure? Also Define Structures and Operations
Push is putting data into stack and POP is removing data from Stack.
2. Queue: In Queue data is arranged in FIFO fashion. i.e. First In First Out.
3. Linked List: A linked list is a finite sequence of nodes each of which contains
a pointer field pointing to the next node.
4. Deleting
5. Sorting
6. Merging
1. Traversing- It is used to access each data item exactly once so that it can be
processed.
2. Searching- It is used to find out the location of the data item if it exists in the
given collection of data items.
3. Inserting- It is used to add a new data item in the given collection of data
items.
4. Deleting- It is used to delete an existing data item from the given collection
of data items.
5. Sorting- It is used to arrange the data items in some order i.e. in ascending
or descending order in case of numerical data and in dictionary order in case of
alphanumeric data.
6. Merging- It is used to combine the data items of two sorted files into single
file in the sorted form.
Q2.Compare between array and linked list. Also define types of linked list.
Define
Array is a collection of
elements having same
data type with common
name.
Access
Memory
Structure
Insertion &
Deletion
Memory
Allocation
Types
In array, memory is
allocated at compile time In linked list, memory is allocated at run
i.e. Static Memory
time i.e. Dynamic Memory Allocation.
Allocation.
Array can be single
dimensional, two
dimension or
multidimensional.
Extra Space
2.
3. Circular Linked List: In it the last node does not contain NULL pointer. Instead
the last node contains a pointer that has the address of first node and thus
points back to the first node.
printf("%d ",value);
}
Applications of recursion:
In sorting algorithms
Fibonacci Series
Q3. What is stack? Explain push and pop operations of the stack.
In Stack data is arranges in LIFO fashion. LIFO stands for Last-in-first-out. Here,
the element which is placed (inserted or added) last, is accessed first.
Basic features of Stack
1. Stack is an ordered list of similar data type.
2. Stack is a LIFO structure. (Last in First out).
3. push() function is used to insert new elements into the Stack
and pop() is used to delete an element from the stack. Both insertion
and deletion are allowed at only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said
to be in Underflow state if it is completely empty.
The process of putting a new data element onto stack is known as PUSH
Operation. Push operation involves series of steps
Step 3 If stack is not full, increment top to point next empty space.
Step 4 Add data element to the stack location, where top is pointing.
return null
endif
top top + 1
stack[top] data
end procedure
if stack is full
return null
endif
top top + 1
stack[top] data
end procedure
Pop Operation
Accessing the content while removing it from stack, is known as pop operation.
In array implementation of pop() operation, data element is not actually
removed, instead top is decremented to a lower position in stack to point to
next value. But in linked-list implementation, pop() actually removes data
element and deallocates memory space.
A POP operation may involve the following steps
Step 3 If stack is not empty, access the data element at which top is
pointing.
Q5. What is polish notation? Define conversion of infix notation into postfix notation.
Definition: Polish notation also known as prefix notation is defined as: In this notation
system operator is written before the operands.
Example of polish notation or prefix notation is +AB or /CD. In these two examples we can
see that here A and B are two operands and + is an operator which is written before
operands.
Conversion of infix notation into postfix notation:
Infix Expression :
Any expression in the standard form like "2*3-4/5" is an Infix(Inorder)
expression.
Postfix Expression :
The Postfix(Postorder) form of the above expression is "23*45/-".
Infix to Postfix Conversion:
In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+.
The algorithm for the conversion is as follows:
If the scanned character is an operand, add it to the Postfix string. If the scanned character is
an operator and if the stack is empty Push the character to stack.
If the scanned character is an Operand and the stack is not empty, compare the
precedence of the character with the element on top of the stack(topStack).
If topStack has higher precedence over the scanned character Popthe stack
else Push the scanned character to stack. Repeat this step as long as stack is not
empty and topStack has precedence over the character.
(After all characters are scanned, we have to add any character that the stackmay have to
the Postfix string.) If stack is not empty add topStack to Postfix string andPop the stack.
Repeat this step as long as stack is not empty.
Example :
Let us see how the above algorithm will be implemented using an example.
Infix String : a+b*c-d
Initially the Stack is empty and our Postfix string has no characters. Now, the first character scanned
is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is
pushed to the stack.
Postfix String
Stack
Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is
an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will
be pushed to the stack.
Postfix String
Stack
The next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The
topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped
out from the stack and added to the Postfix string. Even now the stack is not empty. Now the
topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and
add it to the Postfix string. The '-' will be pushed to the stack.
Postfix String
Stack
Next character is 'd' which is added to Postfix string. Now all characters have been scanned so we
must pop the remaining elements from the stack and add it to the Postfix string. At this stage we
have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters
are scanned, this is how the stack and Postfix string will be :
Postfix String
Stack
End result :
Infix String : a+b*c-d
Postfix String : abc*+d-
Q6.What is a queue? Write Algorithms for the insertion and deletion operations performed on
circular queue.
DEFINITION:
In programming, a queue is a data structure in which elements are removed in the same order they
were entered. This is often referred to as FIFO (first in, first out).
Once a new element is inserted into the Queue, all the elements inserted before the new element in
the queue must be removed, to remove the new element.
Algorithm for Insertion in a circular queue
2.
Print: Overflow
3. Else
4.
6.
Set REAR = 1
7. Else
8.
11. Exit
Print: Underflow
3. Else
4.
ITEM = QUEUE[FRONT]
5.
Set FRONT = 1
8. Else
9.
), (mergesort (L2,
))
}
}
Let T(n) be the running time of Mergesort on an input list of size n. Then,
T(n)
C1 (if n = 1)
(C1 is a constant)
(if n > 1)
T(n)
2kT(1) + C2k2k
Root Node at the top of the tree is called root. There is only one root
per tree and one path from root node to any node.
Parent Any node except root node has one edge upward to a node
called parent.
Leaf Node which does not have any child node is called leaf node.
Q8. Explain the pre-order, in-order and post-order traversal of binary tree.
Traversal is a process to visit all the nodes of a tree and may print their values
too. Because, all nodes are connected via edges (links) we always start from
the root (head) node.
Inorder traversal: In this traversal
method, the left left-subtree is visited first, then root and then the right subtree. We should always remember that every node may represent a subtree
itself.If a binary tree is traversed inorder, the output will produce sorted key
values in ascending order.
We start from A, and following pre-order traversal, we first visit A itself and
then move to its left subtree B. B is also traversed pre-ordered. And the
process goes on until all the nodes are visited. The output of pre-order traversal
of this tree will be
ABDECFG
Algorithm
Until all nodes are traversed
Step 1 Visit root node.
Step 2 Recursively traverse left subtree.
Step 3 Recursively traverse right subtree.
PostOrder Traversal:
In this traversal method, the root node is visited last. First we traverse left
subtree, then right subtree and finally root.
Left rotation
Right rotation
Left-Right rotation
Right-Left rotation
example of AVL trees is the behavior on a worst-case add sequence for regular
binary trees:
1, 2, 3, 4, 5, 6, 7
All insertions are right-right and so rotations are all single rotate from
the right. All but two insertions require re-balancing:
As depicted, the unbalanced node becomes right child of its left child by
performing a right rotation.
Q. What is B-tree? Explain algorithm of b-tree in order.
Definition: A B-tree is a tree data structure that keeps data sorted and allows
searches, insertions, and deletions in logarithmic amortized time. Unlike selfbalancing binary search trees, it is optimized for systems that read and write
large blocks of data. It is most commonly used in database and file systems.
Important properties of a B-tree:
(* In this algorithm
Found :
Ri
*)
Found = false
read root at location ROOT_RRN
P = ROOT_RRN
repeat
N = number of records in current node
case
KEY = key of record in current node (set i): found = true
KEY < key(R1)
KEY > key(RN)
Otherwise
< KEY < key(Ri))
endcase
if P not null then
read node pointed to by P
endif
until Found or P is null
if Found then
NODE_POS = i
NODE_RRN = P
endif
Search = Found
P = P0
P = PN
:
return
Insertion
1) Initialize x as root.
2) While x is not leaf, do following
..a) Find the child of x that is going to to be traversed next. Let the child be y.
..b) If y is not full, change x to point to y.
..c) If y is full, split it and change x to point to one of the two parts of y. If k is
smaller than mid key in y, then set x as first part of y. Else second part of y.
When we split y, we move a key from y to its parent x.
3) The loop in step 2 stops when x is leaf. x must have space for 1 extra key as
we have been splitting all nodes in advance. So simply insert k to x.
Deletion
(* In this algorithm
Finished
TWOBNODE :
normal node
A-sibling
Out-rec
*)
case
key(Out-rec) = key of record in current node: found = true
key(Out-rec) < key(R1)
: P = P0
: P = PN
Otherwise
1) < KEY < key(Ri))
endcase
if P not null then
push onto stack address of current node
read node pointed to by P
endif
until Found or P is null
Finished = false
repeat
Remove Out-rec (record Ri) and pointer Pi
if current node is root or is not too small then
Finished = true
else
if redistribution possible then (* an A-sibling > minimum *)
(* redistribute *)
copy best A-sibling, intermediate parent record, and current (too-small) node
into TWOBNODE
copy records and pointers from TWOBNODE to best A-sibling, parent, and
current node so A-sibling and current node are roughly equal size
Finished = true
else
Put in the leftmost of the current node and A-sibling the contents of both nodes
and the intermediate record from the parent
Discard rightmost of the two nodes
Intermediate record in parent now becomes Out-rec
until Finished
First, we shall determine the half of the array by using this formula
Now we compare the value stored at location 4, with the value being searched i.e. 31. We find that
value at location 4 is 27, which is not a match. Because value is greater than 27 and we have a
sorted array so we also know that target value must be in upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is less that what we are looking for. So the
value must be in lower part from this location.
We compare the value stored ad location 5 with our target value. We find that it is a match.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
It swaps 33 with 27. Also it checks with all the elements of sorted sublist. Here we see that sorted
sub-list has only one element 14 and 27 is greater than 14. Hence sorted sub-list remain sorted after
swapping.
By now we have 14 and 27 in the sorted sublist. Next it compares 33 with 10,.
So we swap them.
And we swap them. By the end of third iteration we have a sorted sublist of 4 items.
This process goes until all the unsorted values are covered in sorted sublist. And now we shall see
some programming aspects of insertion sort.
Algorithm
Step 1 If it is the first element, it is already sorted. return 1;
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We
swap these values.
After two iterations, two least values are positioned at the the beginning in the sorted manner.
The same process is applied on the rest of the items in the array
Algorithm
Step 1 Set MIN to location 0
Step 2 Search the minimum element in the list
Step 3 Swap with value at location MIN
Step 4 Increment MIN to point to next element
Step 5 Repeat until list is sorted
Hash table: Hash Table is a data structure which store data in associative manner. In hash table,
data is stored in array format where each data values has its own unique index value.
Basic Operations
Following are basic primary operations of a hashtable which are following.
YEAR 2010
Q. What do you mean by abstract data structure.
Definition: A data type can be considered abstract when it is defined in terms of operations on it,
and its implementation is hidden.An Abstract Data Type is an organized collection of information
and a set of operations used to manage that information. The set of operations defines the interface
of the ADT. As long as the ADT fulfills the conditions of the interface, it doesnt really matter how
the ADT is implemented. Since, in ADT, the data values and operations are defined with
mathematical precision, rather than as an implementation in a computer language, we may reason
about effects of the operations, relations to other abstract data types whether a program implements
the data type etc. One of the simplest abstract data type is the stack data type for which functions
might be provided to create an empty stack, to push values onto a stack and to pop values from a
stack.
An Abstract Data Type is a data type that satisfies the following two conditions:
1. The representation, or definition, of the type and the operations are contained in a single
syntactic unit.
2. The representation of objects of the type is hidden from the program units that use the type,
so only direct operations possible on those objects are those provided in the types
definition.
A user defined Abstract Data Type should provide:
1. A type definition that allows program units to declare variables of the type, but hides the
representation of these variables.
2. A set of operations for manipulating objects of the type.
An example of a user defined abstract data type is structure. C provides four basic types: int, char,
float and double. However, C also provides the programmer with the ability to define his/her own
types. Structure is one such example. A structure is an aggregate of different parts, where each part
is of some existing type.
struct abc
{int x;
float y;
};
Q2 What is stack? Explain primitive operation which can be performed on stack.
Already answered. PUSH AND POP also primitive operations.
Q3. What is queue? Discuss circular queue and priority queue in data structure.
Circular queue: Circular queue is a linear data structure. It follows FIFO principle.
In circular queue the last node is connected back to the first node to make a circle.
Elements are added at the rear end and the elements are deleted at front end of the queue
Both the front and the rear pointers points to the beginning of the array.
It
Priority Queue is more specialized data structure than Queue. Like ordinary queue, priority queue
has same method but with a major difference. In Priority queue items are ordered by key value so
that item with the lowest value of key is at front and item with the highest value of key is at rear or
vice versa. So we're assigned priority to item based on its key value. Lower the value, higher the
priority.
Basic Operations
Q. What do you mean by linked list of stack and linked list pf queue.? explain doubly and circular
linked list.
Linked-List Implementation of Stacks
a stack uses a linked list, which is a data structure that links together individual data objects as if
they were ``links'' in a ``chain'' of data.
Here is a sketch of a linked-list-based ``stack'' that holds 1, then 5, and then 20 at the bottom:
--top ---> | 1 |
|---|
---
| o-|---> | 5 |
---
|---|
----
| o-|---> | 20 |
---
|----|
|null|
----
The list consists of three ``cells,'' each of which holds a data object and a ``link'' to another cell. A
variable, top, holds the address of the first cell in the list. An empty stack looks like this:
top ---> null
(That is, variable top holds value, null.) Each time an object is pushed, a cell is constructed, the
object is inserted into the cell, and the cell is linked to the front of the chain of cells. For example,
pushing 20onto the empty stack gives us
---top ---> | 20 |
|--- |
|null|
---After 5 and then 1 are pushed, we obtain the picture first seen.
Say that we pop an object from the stack; the picture changes to this:
--top ---> | 5 |
|---|
----
| o-|---> | 20 |
---
|----|
|null|
----
Doubly Linked List: Doubly Linked List is a variation of Linked list in which navigation is possible
in both ways either forward and backward easily as compared to Single Linked List. Following are
important terms to understand the concepts of doubly Linked List
Link Each Link of a linked list can store a data called an element.
Next Each Link of a linked list contain a link to next link called Next.
Prev Each Link of a linked list contain a link to previous link called Prev.
LinkedList A LinkedList contains the connection link to the first Link called First and to
the last link called Last.
As per above shown illustration, following are the important points to be considered.
Each Link carries a data field(s) and a Link Field called next.
Each Link is linked with its next link using its next link.
Each Link is linked with its previous link using its prev link.
Last Link carries a Link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by an list.
Binary Tries - Used in almost every high-bandwidth router for storing routertables.
T-tree - Though most databases use some form of B-tree to store data on the
drive, databases which keep all (most) their data in memory often use T-trees
to do so.
Q. Explain sequential seaarch, binary search, insertion sort and quick sort.
SEQUENTIAL SEARCH: When data items are stored in a collection such as a list, each data item is
stored in a position relative to the others. Since these index values are ordered, it is possible for us to
visit them in sequence. This process gives rise searching technique, the sequential search.
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
QUICK SORT: Quick sort is not stable search, but it is very fast and requires very less additional
space. It is based on the rule of Divide and Conquer(also called partition-exchange sort). This
algorithm divides the list into three main parts :
1. Elements less than the Pivot element
2. Pivot element
3. Elements greater than the pivot element
In the list of elements, mentioned in below example, we have taken 25 as pivot. So after the first
pass, the list will be changed like this.
6 8 17 14 25 63 37 52
Hence after the first pass, pivot will be set at its position, with all the elements smaller to it on its
left and all the elements larger than it on the right. Now 6 8 17 14 and 63 37 52 are considered as
two separate lists, and same logic is applied on them, and we keep doing this until the complete list
is sorted.
Q What do you mean by directed and undirected and weighted graph. Also discuss representation of
graphs.
Undirected Graphs.
In an undirected graph, the order of the vertices in the pairs in the Edge set doesn't matter. Thus, if
we view the sample graph above we could have written the Edge set as {(4,6),(4,5),(3,4),(3,2),
(2,5)),(1,2)),(1,5)}. Undirected graphs usually are drawn with straight lines between the vertices.
The adjacency relation is symetric in an undirected graph.
Directed Graphs.
In a directed graph the order of the vertices in the pairs in the edge set matters. Thus u is adjacent to
v only if the pair (u,v) is in the Edge set. For directed graphs we usually use arrows for the arcs
between vertices. An arrow from u to v is drawn only if (u,v) is in the Edge set. The directed graph
below
Note that both (B,D) and (D,B) are in the Edge set, so the arc between B and D is an arrow in both
directions.
Weighted Graphs.
A weighted graph is an edge labeled graph where the labels can be operated on by the usual
arithmetic operators, including comparisons like using less than and greater than. the weights are
drawn adjacent to the edges and appear in dark purple.
The Edge set = {(1,4,5) ,(4,5,58) ,(3,5,34) ,(2,4,5) ,(2,5,4) ,(3,2,14) ,(1,2,2)}
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33
with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We swap these values. We find that we reach at the end of the array. After one iteration the array
should look like this
To be precise, we are now showing that how array should look like after each iteration. After second
iteration, it should look like this
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that array is completely sorted.
return list
end BubbleSort
Redix sort: Radix Sort is a clever and intuitive little sorting algorithm. Radix Sort puts the elements in
order by comparing the digits of the numbers.
Example:
let this is original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the
previous list.]
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802