Вы находитесь на странице: 1из 53

DATA STRUCTURES

Prepared by
Ch.Uday
Bhaskar
M.Anil Kumar
M.Raja Babu
S.Neelima
DATA STRUCTURES

INTRODUCTION
Introduction The Problem Solving Top down design Strategy Algorithms Vs Programs
Implementations of algorithms Program Verification The efficiency of algorithms
Algorithmic Notation Asymptotic Notation Mathematical Induction Analysis of Algorithms
Recurrence Relations.
RECURSION AND LINEAR SEARCH:
Preliminaries of algorithm, Algorithm analysis and complexity, Recursion: Definition, Design
Methodology and Implementation of recursive algorithms, Linear and binary recursion, recursive
algorithms for factorial function, GCD computation, Fibonacci sequence, Towers of Hanoi, Tail
recursion
List Searches using Linear Search, Binary Search, Fibonacci Search, Analyzing search
algorithms.
SORTING TECHNIQUES:
Basic concepts, Sorting by : insertion (Insertion sort), selection (heap sort), exchange (bubble
sort, quick sort), distribution (radix sort ) and merging (merge sort ) Algorithms.
STACKS AND QUEUES:
Basic Stack Operations, Representation of a Stack using Arrays, Stack Applications: Reversing
list, Factorial Calculation, In-fix- to postfix Transformation, Evaluating Arithmetic Expressions.
Queues: Basic Queues Operations, Representation of a Queue using array, Implementation of
Queue Operations using Stack, Applications of Queues-Round robin Algorithm, Enqueue,
Dequeue, Circular Queues, Priority Queues.
LINKED LISTS:
Introduction, single linked list, representation of a linked list in memory, Operations on a
single linked list, merging two single linked lists into one list, Reversing a single linked list,
applications of single linked list to represent polynomial expressions and sparse matrix
manipulation, Advantages and disadvantages of single linked list, Circular linked list, Double
linked list

TREES:
Basic tree concepts, Binary Trees: Properties, Representation of Binary Trees using arrays and
linked lists, operations on a Binary tree , Binary Tree Traversals, Creation of binary tree from inorder and pre(post)order traversals, Tree Travels using stack, Threaded Binary Trees.
ADVANCED CONCEPTS OF TREES:
Binary search tree, Basic concepts, BST operations: insertion, deletion, balanced binary trees
AVL Search Trees basic concepts , operations: insertion ,deletion. m-way search trees operations:
insertion ,deletion, B Trees, operations: insertion , deletion
GRAPHS:
Basic concepts, Representations of Graphs: using Linked list and adjacency matrix, Graph
algorithms Graph Traversals (BFS & DFS), Topological Sort, applications: Dijkstras shortest
path, Transitive closure, Minimum Spanning Tree using Prims Algorithm, warshalls Algorithm.
SETS:
Definition, Representation of Sets using Linked list, operations of sets using linked lists,
application of sets- Information storage using bit strings
ABSTRACT DATA TYPE
Introduction to abstraction, Model for an Abstract Data Type, ADT Operations, ADT Data
Structure, ADT Implementation of array, Linked list and stack.

Hashing - Hashing Functions, collision Resolution Techniques.

1. ALGORITHMS
1.1 BASICS
I. Statements

1.

Algorithm efficiency is measured in terms of it's time complexity and space


complexity.

II. Short Answers


Easy:
1. What is Time complexity of an Algorithm?
It is the time required by a program to run to completion.
2. What is space complexity of an Algorithm?
It is the amount of space required by a program to run to completion.
3. What is meant by apriori analysis?
It is analysing an algorithm without implementing it in any language.
4. What is meant by posterior analysis?
It is analysing an algorithm after implementing it in some language.
5. Which analysis is best apriori or posterior?
Apriori.
6. What is meant by operation count?
Identifying an operation in the algorithm/program as a key operation and counting how many
times it took place.
7.

Does operation count mechanism always yields good results?


No. It depends on the selection of key operation.

8.

9.

What is step count?


This mechanism involves counting the total number of steps executed by an
algorithm/program.
Mention at least TWO algorithm design strategies.
1. Incremental

2. Divide and Conquer

10. What is meant by asymptotic efficiency of an algorithm?


a) When we look at input sizes large enough to make only the order of growth of the
running time relevant, we are studying the asymptotic efficiency of algorithms.
b) We are concerned with how the running time of an algorithm increases with the size of
the input in the limit, as the size of the input increases without bound.
c) An Algorithm that is asymptotically more efficient will be the best choice for all but very
small inputs.
11. Define -notation.

We write f(n) = (g(n)) (read as f(n) is theta of g(n)) ,if there exists two positive constants
c, d and positive integer p such that
c | g(n) | <= | f(n) | <= d | g(n) | for all n >= p
12. Define BIG O-notation
We write f(n) = O(g(n)) (read as f(n) is big oh of g(n)) ,if there exists a positive integer p,
positive number D such that
| f(n) | <= D | g(n) | for all n >= p
13. Define notation.
We write f(n) = (g(n)) (read as f(n) is omega of g(n)) ,if there exists a positive integer p,
positive number D such that
| f(n) | >= D | g(n) | for all n >= p
Medium:
14. What is the solution to the recurrence relation T(n) =2 T(|_ n/2 _| ) + n
T(n) =O(n logn )
15. What is the solution to the recurrence relation T(n) =2 T(|_ n _| ) + log n
O( log n log log n)
16. What is the solution to the recurrence relation T(n)=T(n-1)+n
O(n2)
17. What is the solution of the recurrence relation T(n)=9T(n/3) +n
(n2)
18. What is the solution of the recurrence relation T(n)=T(2n/3) + 1
(log n)
19. What is the solution of the recurrence relation T(n) = 3T(n/4) +n log n
(n log n)
20. What is the solution of the recurrence relation T(n) =T(n/2) + (1)
(log n)
III.

Multiple Choice Questions:


1.
2.
3.
4.

Big O notation represents


a. Upper bound
b. Lower Bound

c. Both

d. None

Sigma notation represents


a. Upper bound
b. Lower Bound

c. Both

d. None

Master theorem is related the solution of


a. Normal equations b. Recurrence relations

c. Both

d. None

Insertion sort is an example for


a. Incremental
b. Divide and conquer

c. Greedy

d. None

5.

Merge sort is an example for


a. Incremental
b. Divide and conquer

c. Greedy

d. None

6.

Which of the following ways can be used to solve a recurrence relation


a. Substitution
b. Recursion tree
c. Master method
d. All

7.

Two main measures for the efficiency of an algorithm are


a. Processor and memory
b. Complexity and capacity
c. Time and space
d. Data and space

8.

The time factor when determining the efficiency of algorithm is measured by


a. Counting microseconds
b. Counting the number of key operations
c. Counting the number of statements
d. Counting the kilobytes of algorithm

9.

10.

The space factor when determining the efficiency of algorithm is measured by


a. Counting the maximum memory needed by the algorithm
b. Counting the minimum memory needed by the algorithm
c. Counting the average memory needed by the algorithm
d. Counting the maximum disk space needed by the algorithm
Which is true regarding the complexity of the average case of an algorithm

a. Much more complicated to analyze than that of worst case


b. Much more simpler to analyze than that of worst case
c. Sometimes more complicated and some other times simpler than that of
worst case
d. None or above
Medium:
11.
Ackermans function is defined on the non-negative integers as follows
a (m,n) = n+1 if m=0
= a (m-1, 1) if m>0, n=0
= a (m-1, a(m, n-1)) if m>0, n >0
The value of a (1, 3) is
a. 4
12.

b. 5

c. 6

d. 7

An algorithm is made up of 2 modules M1&M2. If order of M1 is f(n) & M2 is


g(n) then the order of algorithm is?
a. max(f(n),g(n))

13.

b. min(f(n),g(n))

c. f(n)+g(n)

What is the time complexity of the following code snippet?


for (inti=0; i<10; i++)
for(int j=0; j<N; j++)

d. f(n)-g(n)

for(int k=N-2; k<N+2; k++)


cout<<i<< " " << j <<endl;
a. O(log n)
b. O(N log n)
14.

c. O(n)

d. O(n2)

What is the time complexity of the following code snippet?


for (int j = n; j > 0; j--) {
for (int k = 1; k < j; k = k+k) {
cout<<j+k<< ;
}
cout<<endl;
}
a. O(log n)

15.

b. (n)

c. (n)

d. (35)

b. Quadratic

c. Logarithmic

d. Linear

What is notation for the formula (n - 21)*(n -14)


a. (n2)

18.

d. O(n2)

What term is used to describe an O(n) algorithm


a. Constant

17.

c. O(n log n)

What is the omega notation for the expression n+35n+6


a. (n)

16.

b. O(n)

b. (80)

c. (log n)

d. (n)

When we say an algorithm has a time complexity of O (n), what does it mean?
a. The algorithm has n loops
b. The computation time taken by the algorithm is proportional to n
c. The algorithm is n times slower than a standard algorithm
d. There are n number of statements in the algorithm

19.

Read the following statements carefully, and choose the correct answer
I.
The notation is Anti Symmetric.
II.
The big Oh notation is Semi Equivalence.
a. (I) is FALSE but (II) is TRUE

b. Both (I), (II) are TRUE

c. (I) is TRUE but (II) is FALSE

d. Both (I), (II) are FALSE

20.

There are 4 different algorithms ALI, AL2, AL3, AL4 to solve a given problem
with the order log(n), log(log(n)), n log(n) ,n / log(n) respectively. Which is the
best algorithm
a. AL1

21.

b. AL2

c. AL3

d. AL4

The concept of Big O notation is important because


a. It can be used to decide the best algorithm that solves a given problem
b) It determines the maximum size of a problem that can be solved in a given
system, in a given amount of time
c. Both(a) and (b)
d. None of the above

22.

The complexity of multiplying two matrices of orders M*N and N*P is


a. MNP

23.

b. MP

d. NP

Which of the following properties holds for THETA notation


a. Symmetric

1. a

c. MN

2.b

b. Reflexive

c. Transitive

d. all

3.b

4.a

5.b

6.d

7.c

8.b

9.a

10.a

11. b. 12.a

13.c

14.b

15.b

16.d

17.a

18.b

19.b

20.b

21. c

23.d

22.a

1.2 RECURSION & SEARCHING TECHNIQUES


I. Statements:
1.

The method of defining a function in which the function being defined is applied
within its own definition is known as Recursion.

2.

Searching involves locating an element in a given set of elements.

II. Short Answers


EASY:
1. Describe the steps needed to write a recursive function.
There are two steps to write recursive functions:
1.BASIS: This step defines the case where recursion ends.
2.RECURSIVE STEP: Contains recursive definition for all other cases (other than
base case) to reduce them towards the base case.
2. Write the recursive definition for the function to find the sum of first n natural
numbers.
If n=1 then return 1
else return (n + Sum(n-1))
3. Write the recursive definition for the function to find the factorial of a given number.
If n=0 or n=1 then return 1
else return (n * Factorial(n-1))
4. Write the recursive definition for the function to find nth Fibonacci number
If n=0 or n=1 then return n
else return (Fib(n-1) + Fib(n-2))
5. What is direct recursion?
In direct recursion a function makes a call to itself in its definition
6. What is indirect recursion?
In indirect recursion a function makes a call to another function in its definition and the
latter function calls the former function in its definition.

7. What is linear recursion?


If a function makes only one call to itself in its definition, then it is known as Linear
recursion.
8. What is binary recursion?
If the function makes two calls to itself in its Definition ,then it is known as Binary recursion.
9. Write the recurrence relation for the recursive function of linear search.
T(n) = c
= T(n-1) + d

for n<=1
for n>1

10. Write the recurrence relation for the recursive function of binary search.
T(n) = c
= T(n/2) + d
III.

for n<=1
for n>1

Multiple Choice Questions:

Easy:
1.

A technique for direct search is


a. Binary Search

b. Linear Search

c. Tree Search

d. Hashing

2. The time complexity of binary search in best, worst cases for an array of size N is
a. N, N2
3.

b. N, N

c. Log N, N2

d. 1, log N

Which of the following is not a required condition for binary search Algorithm.
a. The list must be sorted
b. There should be the direct access to the middle element in any sublist
c. There must be mechanism to delete and/or insert elements in list
d. None

4. Which of the following is not a limitation of binary search Algorithm.


a. Must use a sorted array
b. Requirement of sorted array is expensive when a lot of insertion and deletions are needed
c. There must be a mechanism to access middle element directly
d. Binary search algorithm is not efficient when the data elements are more than 1000
5.

The data structure used to perform recursion is

a. Queue
6.

d. None

c. O(n2)

b. O(log n)

d. O(n log n)

The complexity of binary search algorithm is.


a. O(n)

8.

c. Stack

The complexity of linear search algorithm is


a. O(n)

7.

b. Linked List

c. O(n2)

b. O(logn)

d. O(n log n)

The complexity of Fibonacci search algorithm is.


a. O(n)

c. O(n2)

b. O(logn)

d. O(n log n)

Medium:
9.If memory for the run-time stack is only 150 cells(words), how big can N be in
Factorial(N) before encountering stack overflow?
a. 24
10.

b. 12

c. 26

d. 50

Sequential search has a time complexity of O(n), and binary search has a time complexity
of O(log(n)). What difference will it make when the size n is 1000?
a. You would not notice much difference because computers run very fast anyway
b. As n is 1000, binary search is twice as fast as sequential search
c. As n is 1000, binary search is 10 times as fast as sequential search
d. As n is 1000, binary search is 100 times as fast as sequential search.

11.A characteristic of the data that binary search uses but the linear search ignores is
the___________.

12.

13.

a. Order of the elements of the list.

b. Length of the list.

c. Maximum value in list.

d. Type of elements of the list

Binary search algorithm cannot be applied to


a. Sorted linked list

b. Sorted binary trees

c. Sorted linear array

d. Pointer array

When Worst case occurs in linear search algorithm

a. Item is somewhere in the middle of the array


b. Item is not in the array at all
c. Item is the last element in the array
d. Item is the last element in the array or is not there at all
14.

When Average case occurs in linear search algorithm


a. When Item is somewhere in the middle of the array
b. When Item is not in the array at all
c. When Item is the last element in the array
d. When Item is the last element in the array or is not there at all

15.

In binary search, average number of comparisons required to search an element in a list


of n elements
a. log2 n

16.

b. n/2

c. n

d. n-1

An algorithm that calls itself directly or indirectly is known as


a. Sub algorithm

b. Recursion

c. Polish notation

d. None

Answers for MCQs


1. d

2.d

13.d

14. a

3.c

4.d

5.c 6.a

7b

8.b

9.a

10.d 11.a

12.a

15.a 16.b

2. SORTING
2.1 INTRODUCTION TO SORTING
I. Statements/Definitions
1. Sorting is a process that organizes a collection of data into either ascending or
descending order.
2. Sorting algorithms are classified as either internal or external.
3. An internal sort requires that the collection of data fit entirely in the computers main
memory.

4. We can use an external sort when the collection of data cannot fit in the computers
main memory all at once but must reside in secondary storage such as on a disk.
Internal sorting algorithms

Selection (selection sort, heap sort)

Insertion (insertion sort, shell sort)

Exchange(Bubble sort, quick sort)

External sorting algorithms

Distribution(Radix sort)

Merging(Merge sort)

II. Short Answers


1. What is sorting?
Sorting is the process of arranging the given items in a logical order.
2. What is the need of external sorting?
External sorting is required where the input is too large to fit into memory. So external
sorting is necessary where the program is too large.
3. What is internal sorting?
In internal sorting all the data to sort is stored in memory at all times while sorting is in
progress.
4. What is external sorting?
If the number of objects is so large that some of them reside on external storage during
the sort, it is called external sorting

2.2 SELECTION SORT


I. Statements/Definitions
1. To sort elements first find the smallest element in the array. Exchange it with the element
in the first position
Find the second smallest element and exchange it with the element in the second position.
Continue until the array is sorted

II. Multiple Choice Questions


EASY:
1. What is the worst case complexity for selection sort algorithm
a. O(n)

b. O(n*n)

c. O(nlogn)

d. O(logn)

2. What is the average case complexity for selection sort algorithm


a. O(n)

b. O (n*n)

c. O(nlogn)

d. O(logn)

3. What is the best case complexity for selection sort algorithm


a. O(n)

b. O(n*n)

c. O(nlogn)

d. O(logn)

4. In a selection sort of n elements, how many times is the swap function called in the
complete execution of the algorithm?
a. 1

b. n 1

c. n log n

d. n

5. A sorting technique in which successive elements are selected in order and placed into
their proper sorted positions is called
a. Selection sort

b. Quick sort

c. Bubble sort

d. Merge sort

6. In which cases are the time complexities same in selection sort?


a. Worst and Best

b. Best and Average

c. Worst and Average

d. Worst, average and Best

7. The efficiency of selection sort is

a. n*(n/2)

b. n*n

c. n(n-1)/2

d. none

c. n

d. log n

8. The frequency count of selection sort is


a. n*n

b. n(n-1)/2

9. The total number of iterations in selection sort is


a. n-1

b. n

c. n*n

d. 2*n

Answers for MCQs


1. b

2.b

3.b

4.b

5.a

6.d

7.a

8.b

9.a

2.3 INSERTION SORT


I .Statements/Definitions
1. Every iteration of insertion sort removes an element from the data, inserting it into the
correct position in the already-sorted list.
2. This process is repeated until no elements remain. The choice of which element to
remove from the data is random.
II. Multiple choice questions
Easy:
1.

Sorting of playing cards is an example of


a. Bubble sort

2.

b. O(n*n)

c. O(nlogn)

d. O(logn)

b. O(n*n)

c. O(nlogn)

d. O(logn)

What is the best case complexity for insertion sort algorithm


a. O(n)

5.

d. Quick sort

What is the worst case complexity for insertion sort algorithm


a. O(n)

4.

c. Selection sort

What is the average case complexity for insertion sort algorithm


a. O(n)

3.

b. Insertion sort

b. O(n*n)

c. O(nlogn)

d. O(logn)

Insertion sort is an example of


a. Incremental algorithm

b. detrimental algorithm

c. both

d. none

Medium:
6.

What is the output of insertion sort after the 1st iteration given the following
sequence of numbers: 7 3 5 1 9 8 4 6
a. 3 7 5 1 9 8 4 6 b. 1 3 7 5 9 8 4 6

7.

9.

b. n(n+1)/2

12.

d. n*n

a. Worst and Best

b. Best and Average

c. Worst and Average

d. Worst, average and Best

The frequency count of insertion sort in the worst case is


b. n(n-1)/2

c. n

d. logn

The frequency count of insertion sort in the average case is


a. n*n

11.

c. n

In which cases are the time complexities same in insertion sort?

a. n*n
10.

d. 1 3 4 5 6 7 8 9

Total number of comparisons for insertion sort is


a. n(n-1)/2

8.

c. 3 4 1 5 6 8 7 9

b. n(n-1)/2

c. n(n-1)/4

d. logn

The best case running time T(n) for insertion sort is


a. a quadratic function of n

b. a linear function of n

c. a logarithmic function of n

d. none

The worst case running time T(n) for insertion sort is


a. a quadratic function of n

b. a linear function of n

c. a logarithmic function of n

d. none

Answers for MCQs


1. b
12.a

2.b

3.b

4.a

5.c 6.a

7.a

8.c

9.b

10.c

11.

2.4 BUBBLE SORT


I. Statements/Definitions
1. Bubble sort is the simplest sorting algorithm.
2. The Bubble sort works by iterating down an array to be sorted from the first element to
the last, comparing each pair of elements and switching their positions if necessary.
3. This process is repeated as many times as necessary, until the array is sorted.
4. Since the worst case scenario is that the array is in reverse order, and that the first element
in sorted array is the last element in the starting array, the most exchanges that will be
necessary is equal to the length of the array.
II. Multiple choice questions
Easy:
1.

Bubble sort is also called


a. Exchange sort

2.

b.O(n*n)

c.O(nlogn)

b.O(n*n)

c.O(nlogn)

b.O(n*n)

c.O(nlogn)

When does the worst case for Bubble sort occur?


a. When the numbers are placed in random
b. When the numbers are placed in descending order
c. When the numbers are placed in ascending order
d. When all the numbers are same

6.

d.O(logn)

d.O(logn)

What is the best case complexity for bubble sort algorithm


a.O(n)

5.

d. none

What is the average case complexity for bubble sort algorithm


a.O(n)

4.

c. Both

What is the worst case complexity for bubble sort algorithm


a.O(n)

3.

b. Sinking sort

When does the best case for Bubble sort occur?

d.O(logn)

a. When the numbers are placed in random


b. When the numbers are placed in descending order
c. When the numbers are placed in ascending order
d. When all the numbers are same
7.

8.

In which cases are the time complexities same in Bubble sort?


a. Worst and Best

b. Best and Average

c. Worst and Average

d. none of the above

The most exchanges that will be necessary for bubble sort for an array of length n
is
a.n

9.

b.n+1

c.n-1

d.n*n

Which of the following sorting methods would be most suitable for sorting a list
which is almost sorted
a. Bubble Sort

b. Insertion Sort

c. Selection Sort

d. Quick Sort

Answers for MCQs


1. b

2.b

3.b

4.a

5.b

6.c

7.c

8.a

9.a

2.5 QUICK SORT


I. Statements/Definitions
1.
2.
3.
4.

As the name implies, it is quick, and it is the algorithm generally preferred for sorting.
The quick sort algorithm was developed in 1960 by Tony Hoare.
Quick sort also known as Partition Exchange Sort is a divide and conquer algorithm.
Quick sort involves placing the pivot element (generally first element) in the correct
position, so that it divides the list into two parts such that the elements to the left of pivot

are less than pivot and elements to the right of pivot are greater than pivot.
5. Now we apply the technique to these sub arrays continuously until the entire array is
sorted.
II. Multiple Choice Questions

Easy:
1.

Quick sort is also known as


a. Pointer sort

2.

b. External sorting

c. Both

d. none

Which sorting technique is also called partition exchange sort?


a. selection sort

b. quick sort

c. bubble sort

d. merge sort

3. In which cases are the time complexities same in quick sort?

4.

a. Worst and Best

b. Best and Average

c. Worst and Average

d. Worst, average and Best

Quick sort uses ___________


a. Divide and Conquer Technique

b. Greedy Approach

c. Back Tracking

d. None of the above

5.

What pattern of splits gives the worst-case performance for quick sort?
a. Ascending order.
b. Descending order.
c. Any order.
d. None of the above.
Medium:
6.

What is the average case complexity for quick sort algorithm


a. O(n)

7.

10.

d. O(logn)

b. O(n*n)

c. O(nlogn)

d. O(logn)

What is the best case complexity for quick sort algorithm


a. O(n)

9.

c. O(nlogn)

What is the worst case complexity for quick sort algorithm


a. O(n)

8.

b. O(n*n)

b. O(n*n)

c. O(nlogn)

d. O(logn)

In quick sort the pivot value is usually the


a. first element
b. last element c. middle element d. any element
Which of the following statements about Quick sort is/are incorrect?
a. Average running time is O(N log N).
b. The best case always occurs when the pivot partitions the set of numbers to be

sorted into two equal-sized subsets.


c. The worst case always occurs when the pivot partitions the set of numbers to be
sorted into two equal-sized subsets.
d. The worst case always occurs when the smallest item is selected from the items
to be sorted as the pivot.
11.

If one chooses the middle key as the pivot, what would be the Big-O value for the
Best case of the Quick Sort Algorithm?
a. n*n
b .n
c. a constant
d. n log n

12.

Which of the following activities is/are not relevant to the quick sort algorithm?
a. Initially divide the entire file into N sub-files.
b. Choose a pivot value and store it in a correct place.
c. The sorting technique is sorting by exchange.
d. The sorting technique is sorting by selection.

13.

If the last element is chosen as the pivot at each iteration, the Quick-sort tree for
the sequence in question 1 will have height
a.1
b.2
c. 3
d.4

14.

Given a large amount of highly random data which fits into the computers RAM,
the best choice for sorting would be
a. Bubble-sort b. Insertion-sort
c. Merge-sort
d. Quick-sort

15.

In Quick sort , after the completion of every pass the pivot/key element moves to
the ________ position.
a. First
b. second
c. middle
d. exact sorted position

Answers for MCQs


1.c
11. d

2.b
3.b
12.a,d 13.c

4.a
14.d

5.b
15.d

6. c

7.b

8.c 9.d

10.c

2.6 MERGE SORT


I. Statements/Definitions
1.

Merge sort is a divide and conquer algorithm that was invented by John von

2.

Neumann in 1945
To sort elements Divide the unsorted array into two sub lists at the middle. Further
divide these sub lists into sub lists at their middles until each sub list contain only

3.

one element.
Merge these sub lists two at a time till we get the final sorted list with the
following steps:
i) Compare the sub-lists first elements.
ii) Remove the smallest element and put it into the result array.
iii) Continue the process until all elements have been put into the result array.

Medium:
II. Multiple choice questions
1.

What is the average case complexity for merge sort algorithm


a. O(n)

2.

5.

d. O(logn)

b. O(n*n)

c. O(nlogn)

d. O(logn)

What is the best case complexity for merge sort algorithm


a. O(n)

4.

c. O(nlogn)

What is the worst case complexity for merge sort algorithm


a. O(n)

3.

b. O(n*n)

b. O(n*n)

c. O(nlogn)

d. O(logn)

In which cases are the time complexities same in merge sort?


a. Worst and Best

b. Best and Average

c. Worst and Average

d. Worst, average and Best

Total numbers of passes in merge sort of n numbers is _______.


a. n

b.n-1

c.log(n)

d. nlogn

6.

Merge sort is based on the divide and conquer strategy consists of the following
steps

7.

a. Divide, Recursive and Conquer

b. Divide and Conquer

c. Divide and Recursive

d. None of the above

Given two sorted lists of size m, n the number of comparisons needed in the worst
case by the merge sort algorithm is
a.mn

8.

b.max(m,n)

c.min(m,n)

d.m+n-1

Suppose the array A contains 14 elements as follows:


A = [66, 33, 40, 22, 55, 88, 60, 11, 80, 20, 50, 44, 77, 30]
How many Passes are required to sort the above array A using the Merge sort
algorithm?
a. 3

9.

b. 4

d. 7

e. 14

On each iteration, the size of the sorted lists in a merge sort


a. doubles

10.

c. 5

b. halves

c. remains the same

d. increases 4 times

In every pass, maximum number of comparisons in the merge sort is _________.


a. n-1

b. n

c.log n

d. n*n

Answers for MCQs


1. c

2.c

3.c

4.d

5.c

6.a

7.d

8.c

9.a

10.a

2.7 HEAP SORT


I. Statements/Definitions
1.
2.

Heap sort is procedure belonging to the family of sorting by selection.


This algorithm is based on repeated selection of either the smallest or the largest

3.

element from the elements in unsorted list and their inclusion in sorted list.
This process continues until all elements have been selected from the unsorted
list.

4.

Heap sort is built on a data structure called heap and hence the name heap sort.

II. Multiple Choice Questions


Easy:
1.

What is the average case complexity for heap sort algorithm


a. O(n)

2.

4.

5.

c. O(nlogn)

d. O(logn)

What is the best case complexity for heap sort algorithm


a. O(n)

3.

b. O(n*n)

b. O(n*n)

c. O(nlogn)

d. O(logn)

The condition applicable for max-heap is


a. A[Parent(i)]>=A[i]

b. A[Parent(i)]<=A[i]

c. A[Parent(i+1)]>A[i]

d. A[Parent(i+1)]<a[i]

The condition applicable for min-heap is


a. A[Parent(i)]>=A[i]

b. A[Parent(i)]<=A[i]

c. A[Parent(i+1)]>A[i]

d. A[Parent(i+1)]<a[i]

The number of operations required for heap sort regardless of the order of I
input
a. O(1)

6.

b. O(logn)

c. O(nlogn)

d. O(n*n)

In which cases are the time complexities same in heap sort?


a. Worst and Best

b. Best and Average

c. Worst and Average

d. Worst, average and Best

7. The worst case running time of heap sort is


a. O(n log n)

b. O(log n)

c. (log n)

d. (n log n)

Complex:
8.

Which of the following arrays represent descending (max) heaps?


a. [10,7,7,2,4,6]

b. [10,7,6,2,4,7]

c. [10]

d. [10,6,7,2,4,6]

9.

The frequency count for heavily process is


a. log n

10.

b. n log n

b. n log n

c. n*n

d. none

For an array of n nodes, build heap takes _______ time


a. O(logn)

12.

d. none

The frequency count for heap sort process is


a. log n

11.

c. n*n

b. O(n)

c. O(nlogn)

d .none

The __________ data structure is a n array object that can be viewed as a nearly
complete binary tree.
a. stack

13.

b. queue

d. linked list

The MAX-HEAPIFY procedure has running time


a. O(n)

14.

c. binary heap

b. O(n log n)

c. O(log n)

d. O(n)

The worst case running time of MAX-HEAPIFY on a heap of size n is ______


a. O(log n)

b. (log n)

c. (log n)

d. O(n)

7.d

10.b

Answers for MCQs


1. c

2.c

3.a

12.c

13. c

14.c

4.b

5.c

6.d

8.c 9.a

11.b

3. STACKS AND QUEUES


3.1 INTRODUCTION TO STACK
I . Statements/Definitions
1.

Stack is a linear data structure which stores the elements in LIFO(Last In First
Out) order.

2.

The last element that is inserted will be the first to be removed from the stack. So

3.

stack is called LIFO data structure.


It is an ordered list of elements in which addition or deletion of elements is done

4.
5.
6.

at only one end called TOP of stack.


Adding an element to a stack is referred to as pushing that element onto the stack.
Removing an element from the stack is referred to as popping the stack.
The basic operations performed on stack are:
Create
Push
Pop
Top(Peep)
Empty
Display

The process of adding a new element to the top of stack is called push operation.

The process of deleting an element from the top of the stack is called POP operation.

3.2 APPLICATIONS OF STACKS


I. Statements/Definitions

Reversing a list
Parentheses checking
Conversion of infix expression to postfix expression
Evaluation of postfix expression
Recursion
Towers of Hanoi

II. Short Answers


1. What is the difference between a stack and a Queue?
Stack Represents the collection of elements in Last In First Out order.
Queue - Represents the collection of elements in First In First Out order.
2. Can a stack be described as a pointer? Explain
A stack is represented as a pointer. The reason is that, it has a head pointer which points
to the top of the stack. The stack operations are performed using the head pointer. Hence,
the stack can be described as a pointer.
3. What is recursion?
Recursion is an approach in which a function calls itself with an argument. Upon
reaching a termination condition, the control returns to the calling function.
4. Is it possible to insert different types of element in a stack? How?
Different elements can be inserted into a stack. This is possible by implementing union /
structure data type. It is efficient to use union rather than structure, as only one items
memory is used at a time.
5. What is the data structures used to perform recursion?
Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows
whom to return when the function has to return. Recursion makes use of system stack for
storing the return addresses of the function calls.
III. Multiple Choice Questions

Easy:
1. The operation for adding an entry to a stack is traditionally called:
a. add

b. append

c. insert

d. push

2. The operation for removing an entry from a stack is traditionally called:


a. delete
3.

4.

5.

b. peek

c. pop

d. remove

Which of the following applications may use a stack?


a. parentheses balancing program.

b. Keeping track of local variables at run time

c. Syntax analyzer for a compiler.

d. All of the above.

Which one of the following is an application of Stack Data Structure?


a. managing function calls

b. expression conversion

c. expression evaluation

d. all the above

If the sequence of operations - push(1), push(2), pop, push(1), push(2), pop, pop,
pop, push(2), pop are performed on a stack, the sequence of popped out values are
?
a. 2, 2, 1, 1, 2

b. 2, 2, 1, 2, 2

c. 2, 1, 2, 2, 1

d. 2, 1, 2, 2, 2

Medium:
6.

Entries in a stack are "ordered". What is the meaning of this statement?


a. A collection of stacks can be sorted.
b. Stack entries may be compared with the '<' operation.
c. The entries must be stored in a linked list.
d. There is a first entry, a second entry, and so on

7.

8.

9.

Which of the following stack operations could result in stack underflow?


a. is empty

b. pop

c. push

d. two or more of the above answers

Which of the following stack operations could result in stack overflow?


a. is empty

b. pop

c. push

d. two or more of the above answers

The postfix equivalent of the prefix * + ab - cd is ?


a. ab + cd - *

10.

c. + cd * -

d. ab + - cd *

What data structure is used to perform recursion?


a. Circular list

11.

b. abcd + - *

b. Array

c .Stack

d. Queue

Which of the following is used in conversion of an infix expression to a postfix


expression?
a. Circular list

b. Array

c .Stack

d. Queue

e. Dequeue.
12.

The result of evaluating the postfix expression 546+*493/+* is


a.600

13.

c.650

d.588

The data structure required to evaluate a postfix expression is


a. Queue

14.

b.350

b. Stack

c. Array

d. Linked-list

The process of accessing data stored in a serial access memory is similar to


manipulating data on a------a. Heap

b. Queue

c. Stack

d. Binary tree

Answers for MCQs


1.d

2.c

3.d

4.d

11. c

12.b

13.b

14.c

5.d

6. d

7.b

8.c 9.d

10.c

3.3 QUEUE
I. Statements/Definitions
1.
2.

Queue is a linear data structure which stores data in FIFO(First In First Out)order.
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 the

3.

other end (the rear of the queue).


The first element inserted into the queue is the first element to be removed. For
this reason a queue is called FIFO data structure.
Eg: people waiting for a bus, cars lined for filling petrol, luggage kept on
conveyor belt, cars lined at a toll bridge.

4.

The basic operations that can be performed on queue are:


1.insert: Insertion or addition of elements to a queue.Also referred to as enqueue.
2.delete: Deletion or removal of elements from a queue.Also referred to as
dequeue.
3.display: To display the contents of a queue.

3.4 TYPES OF QUEUES


I. Statements/Definitions
A Queue data structure can be classified into the following types
1. Circular Queue
2. Dequeue
3. Priority Queue
3.4.1 Circular Queue
1. In a Linear queue we cannot reuse the space i.e. it is not possible to insert a new element
into the queue though it has empty locations.
2. To avoid this problem we use circular queue which also follows FIFO order.
3. In a circular queue we can join front and rear ends to make a circle.
3.4.2 Dequeue
1. A double ended queue is a variation in linear queue in which insertions and deletions are
performed at both ends i.e. elements can be added or deleted from front or rear ends.
2. A deque is pronounced as DEqueue or Deck.
3. There are two types of dequeues:
1. Input Restricted Dequeue
-Insertion is restricted at front end.
2. Output Restricted Dequeue
-Deletion is restricted at rear end.
3.4.3 Priority Queue
1. It is a special form of linear queue where each element in the queue is associated with a
priority
2. In a priority queue an element with high priority is served before an element with low
priority.if two elements have the same priority they are served according to their order of
arrival in the queue.
II. Multiple Choice Questions
Easy:

1.

Which data structure allows deleting data elements from front and inserting at
rear?
a. Stacks

2.

b. Queues

c. Deques

d. Binary search tree

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then
removed one at a time, in what order will they be removed?
a. ABCD

3.

b. ABDC

c. DCAB

d. DCBA

Identify the data structure which allows deletions at both ends of the list but
insertion at only one end.

4.

a. Input-restricted deque

b. Output-restricted deque

c. Priority queues

d. None of above

A data structure where elements can be added or removed at either end but not in
the middle
a. Linked lists

b. Stacks

c. Queues

d. Deque

Complex:
5.

Queue can be used to implement?


a. Radix sort b. Quick sort

6.

c. Recursion

d. Depth first search

Which of the following data structure is the best example for the multitasking
systems?
a. Tree

7.

b. Queue

c. Stack

d. Linked List

To implement the Round Robin algorithm, which of the following data structure
is used?
a. Stack

b. Linear Queue

c. Circular Queue

Answers for MCQs


1. b

2.d

3.a

4.d

5.a

6.b

7.b

d. Priority Queue

4. TREES&GRAPHS
4.1 TREE BASICS
I. Statements/Definitions
1.

A tree is a finite set of one or more nodes such that here is a specially designated node
called the root.

2.

The remaining nodes are partitioned into n 0 disjoint sets T1,,Tn, where each of
these sets is a tree. We call T1,,Tn the sub trees of the root.

3.

Root of the tree: The top node of the tree that is not a sub tree to other node, and has
two children of sub trees.

4.
5.
6.
7.
8.
9.

Node: It is stands for the item of information and the branches to other nodes.
The degree of a node: It is the number of sub trees of the node.
The degree of a tree: It is the maximum degree of the nodes in the tree
The parent node: a node that has sub trees is the parent of the roots of the sub trees
The child node: a node that is the roots of the sub trees are the children of the node
The Level of the tree: We define the level of a node by initially letting the root be at

level one
10. The depth of a tree: It also called height of a tree. It is the maximum level of any node
in the tree
II. Short Answers
Easy:
1. Define tree?
A tree is a non-linear data structure that consists of a root node and

potentially many

levels of additional nodes that form a hierarchy.


2. Define Binary tree?
A binary tree is made of nodes, where each node contains a "left" reference, a "right"
reference, and a data element.
3. Define Binary search tree?
A BST is a binary tree where nodes are ordered in the following way:

1.
2.
3.
4.

Each node contains one key (also known as data)


The keys in the left sub tree are less than the key in its parent node, in short l < p;
The keys in the right sub tree are greater the key in its parent node, in short p < r;
Duplicate keys are not allowed.

4. How to visit each node in the tree exactly once?


By using the tree traversal techniques we can visit the each vertex exactly once.
5. Explain preorder traversal?
Visit the parent first and then left and right children;
6. Explain inorder traversal?
Visit the left child, then the parent and the right child
7. Explain postorder traversal?
Visit left child, then the right child and then the parent
8. What are the operations on binary tree?
There are three operations
Insertion
Deletion
Searching
Medium:
9. What is Threaded Binary Tree?
A Binary Tree is threaded by making all right child pointers that would normally be null
point to the in order successor of the node (if it exists) , and all left child pointers that
would normally be null point to the in order predecessor of the node.
10. What are the operations of Binary Search Tree?
There are three operations
1. Insertion
2. Deletion
3. Searching
11. What is meant by Balanced Tree?
A binary tree is balanced if for each node it holds that the number of inner nodes in the
left sub tree and the number of inner nodes in the right sub tree differ by at most 1.
12. Define AVL tree?

An AVL tree is a binary search tree which has the following properties:
1. The sub-trees of every node differ in height by at most one.
2. Every sub-tree is an AVL tree.
13. What are the operations of AVL tree?
There are three operations
1. Insertion
2. Deletion
3. Searching
Complex:
14. What is the purpose of using B tree?
By using the B tree dynamically we can increase and decrease the size of tree.
15. Define B tree?
The B-Tree has additional constraints to ensure the tree is always balanced, and the space
wasted by deletion never becomes excessive.
16. What are the advantages of trees?
Trees are so useful and frequently used, because they have some very serious advantages:
Trees

reflect structural relationships in the data

Trees

are used to represent hierarchies

Trees

provide an efficient insertion and searching

Trees are very flexible

data, allowing to move sub trees around with minimum effort

17. How many types of threaded binary trees and what are those?
There are two types of threaded binary trees those are
Single Threaded
Double threaded
18. What is single threaded binary tree?
Each node is threaded towards either (right)' the in-order predecessor or' successor.
19. What is double threaded binary tree?
Each

node

is

threaded

towards both

(left

&

right)'

the

in-order

predecessor and' successor.


20. Define balance factor?
It is the difference between the height of the left sub tree and height of the right sub tree.

21. What are the balance factor values for height balanced trees?
BF values for height balanced trees are -1, 0 and +1.
22. How to balance the AVL tree in case of tree is in unbalanced state?
By using the rotations we can balance the tree. There are two types of rotations. Those are
1) Single rotations
i)
LL rotation
ii)
RR rotation
2) Double rotations.
i)
LR rotation
ii)
RL rotation
III. Multiple Choice Questions
1.

2.

A Binary Tree whose every node has either zero or two children is called
a. Complete binary tree

b. Binary search tree

c. Extended binary tree

d. None of above

In a binary tree, certain null entries are replaced by special pointers which
point to nodes higher in the tree for efficiency. These special pointers are called
a. Leaf

3.

b. branch

c. path

The in order traversal of tree will yield a sorted listing of elements of tree in
a. Binary trees b. Binary search trees

4.

d. thread

c. Heaps d. None of above

Which of the following is not the required condition for binary search algorithm?

a. The list must be sorted


b. there should be the direct access to the middle element in any sublist
c. There must be mechanism to delete and/or insert elements in list
d. none of above
5.
6.

Binary trees with threads are called as


a. Threaded trees. b. Pointer trees. c. Special trees.
Each node N of binary tree T has how many fields.
a. 2.
b. 3.
c. 4.

d. Special pointer trees.


d. none of the above.

Medium:
7.

When converting binary tree into extended binary tree, all the original nodes in binary
tree are

8.

a. internal nodes on extended tree

b. external nodes on extended tree

c. vanished on extended tree

d. None of above

The post order traversal of a binary tree is DEBFCA. Find out the pre order
traversal
a. ABFCDE

b. ADBFEC

c. ABDECF

d. ABDCEF

9 Which of the following is not a limitation of binary search algorithm?


a. must use a sorted array
b. requirement of sorted array is expensive when a lot of insertion and deletions are needed
c. there must be a mechanism to access middle element directly
d. binary search algorithm is not efficient when the data elements are more than 1000.
10.

Which of the following statements about binary trees is NOT true?


a. Every binary tree has at least one node.
b. Every non-empty tree has exactly one root node.
c. Every node has at most two children.
d. Every non-root node has exactly one parent.

11.

Sequential representation of binary tree uses


a. Array with pointers.
b. Single linear array.
c. Two dimensional arrays.
d. Three dimensional arrays.

12.

TREE [1] = NULL indicates tree is


a. Overflow.
b. Underflow.

13.

c. Empty.

d. Full.

Linked representation of binary tree needs how many parallel arrays.


a. 4.
b. 2.
c. 3.
d. 5.

14.

15.

In a binary tree a sequence of consecutive edges is called ____


a. Route.
b. Connecting lines.
c. Two-way.

d. Path.

The root R of the binary tree is assigned a level number of----a. 1.


b. 0.
c. -1.

d. Null.

Complex:
16.

In order traversal is sometimes called as


a. Left-node-right traversal.
b. Right-node-left traversal.
c. Node-left-right traversal.
d. Right-left-node traversal.

17.

Any node N in a binary tree T has how many nodes


a. 0 or 1.
b. 0, 1 or 2.
c. atleast 2.

d. atleast 1.

18.

While using header node in binary trees the condition LEFT [HEAD] =NULL
indicates a ___
a. Empty tree.
b. Tree is full.
c. Free space available.
d. Avail list full.

19.

The threads in a diagram of a threaded tree are usually indicated by ---a. Straight line.
b. Arrow.
c. Dotted line.
d. Curve.

20.

If node N is a terminal node in a binary tree then its -----a. Right tree is empty.
b. Left tree is empty.
c. Both left & right sub trees are empty.
d. Root node is empty.

Answers for MCQs


1.c
2.d
11.b 12.c 13.c 14.d

3.b
15.b

4.c
16.a

17.b

5.a
18.a

6.b
7.a
19.c 20.c

8.c

9.d

10.a

4.2 GRAPHS
I .Statements/Definitions
1.
2.
3.

A graph is a collection of vertices and edges.


An edge connects two vertices to show that there is a relationship between them.
If the edges in a graph are all one-way, we say that the graph is a directed graph,

4.
5.
6.
7.

or a digraph.
A path in a graph is a sequence of vertices that are connected by edges.
A cycle in a directed graph is a path that starts and ends at the same vertex.
A graph with no cycles is called an acyclic graph.
Undirected Graph: The pairs in E are unordered pairs, i.e., there is no tail or head

8.
9.
10.

and there is no direction to the edge.


Adjacent Vertices - means there is an edge from u to v, i.e., (u, v)
Degree - the number of edges incident on a vertex
A self loop is a cycle of length 1, and a directed graph with no self loops is

11.

referred to as simple
Undirected graph is connected - when every pair of vertices in V is connected by

12.

a path.
Directed graph strongly connected - when every two of vertices in V is reachable
from each other

II. Short Answers


1.

Which data structures used for the representation of


graphs?
Adjacency matrix and adjacency list are used for representing the graphs.

2. Explain adjacency matrix representation?

A two-dimensional matrix, in which the rows represent source vertices and columns
represent destination vertices. Data on edges and vertices must be stored externally. Only
the cost for one edge can be stored between each pair of vertices.
3. Explain adjacency list representation?

Vertices are stored as records or objects, and every vertex stores a list of adjacent
vertices. This data structure allows the storage of additional data on the vertices.
Additional data can be stored if edges are also stored as objects, in which case each
vertex stores its incident edges and each edge stores its incident vertices.

4. How to visit the each vertex in the graph at most one time?

By using graph traversing techniques we can traverse the graph. There are two techniques
those are
1) Depth First Search
2) Breadth First Search
5. What are the operations of graphs?

There are total six operations those are


1. Insert vertex
2. Delete vertex
3. Add edge
4. Delete edge
5. Find vertex
6. Traverse graph.
6. What are the differences between DFS and BFS?
BFSuses queue implementation ie.FIFO, dfs uses stack implementation ie. LIFO
DFS is faster than BFS
DFS requires less memory than BFS
DFS is used to perform recursive procedures.

III . Multiple Choice Questions


Easy:
1.

The minimum number of edges in a connected cyclic graph on N vertices is


a. N-1

2.

b. N

c.N+1

d. None of the above

Which of the following is useful in traversing a given graph by breadth first


search
a. Stack

3.

b .Set

The degree of any vertex of graph is ?

a. The number of edges incident with vertex


b. Number of vertex in a graph

c. List

d. Queue

c. Number of vertices adjacent to that vertex


d. Number of edges in a graph

4.

A tree having a main node, which has no predecessor is ?


a. Spanning tree

5.

b. Rooted tree c. Weighted tree

d. None of these

A vertex of a graph is called even or odd depending upon ?


a. Total number of edges in a graph is even or odd
b. Total number of vertices in a graph is even or odd
c. Its degree is even or odd
d. None of these

6.

Given an undirected graph G with V vertices and E edges, the sum of the degrees
of all vertices is
a. E

7.

b. 2E

d. 2V

A graph is said to be ________ if there is a path between any two of its nodes
a. Connected.

8.

c. V

b. Coupled.

c. Attached.

d. Allied.

A graph is said to be ____if every node u in G is adjacent to every other node v in


G.
a. Absolute.

9.

c. Inclusive.

d. Complete.

A graph is said to be ______if its edges are assigned data.


a. Tagged.

10.

b. Entire.

b. Marked.

c. Labeled.

d. Sticked.

c. Dir-graph.

d. Digraph.

Other name for directed graph is ________


a. Direct graph.

b. Digraph.

11.

12.

An edge is called a loop if it has ____


a. Identical end points.

b. Different end points.

c. Two end points.

d. Many end points.

A node v is said to be _____ from node if there is path from u to v.


a. Joined.

13.

b. Reachable.

c. non cycle graph.

d. circular graph.

In a graph if e= (u, v) means_______.


a. u is adjacent to v but v is not adjacent to u.

b. e begins at u and ends at v.

c. u is processor and v is successor.


15.

d. Directed.

A connected graph T without any cycles is called_________.


a. free graph. b. no cycle graph.

14.

c. Connected.

d. both b and c.

In a graph if e= [u, v], Then u and v are called____.


a. End points of e.

b. Adjacent nodes.

c. Neighbours. d. All of above.

Complex:

16.

A graph with no edges is known as empty graph. Empty graph is also known
as ?
a. Trivial graph

17.

c. Bipartite graph

d. None of these

A graph G is called a .....if it is a connected acyclic graph ?


a. Cyclic graph

18.

b. Regular graph

b. Regular graph

c. Tree

d. Not a graph

The complete graph K, has... different spanning trees?


a. n^n-2

b. n*n

c. n^n

d. n^2

19.

Which of the following is not a type of graph ?


a. Euler

20.

b. Hamiltonian

d. Path

A minimal spanning tree of a graph G is ?


a. A spanning sub graph

21.

c. Tree

b. A tree c. Minimum weights d. All of above

Graph G is ______ if for any pair u, v of nodes in G there is a path from u to v


(or) a path from v to u.

22.

a. Laterally connected.

b. Widely connected.

c. Un literally connected.

d. Literally connected.

How many lists are needed for linked representation of graphs?


a. 1.

23.

d. 2.

b. Queue.

c. Tree.

d. Array.

Which data structure is used in depth-first search of a graph to hold nodes?


a. Array.

25.

c. 4.

Which data structure is used in breadth-first search of a graph to hold nodes?


a. Stack.

24.

b. 3.

b. Tree.

c. Stack.

d. Queue.

To implement Dijkstras shortest path algorithm on unweighted graphs so that it


runs in linear time, the data structure to be used is:
a. Queue

26.

b. Stack

c. Heap

d. B-Tree

A minimal spanning tree of a graph G is ?


a. A spanning sub graph

b. A tree c. Minimum weights

Answers for MCQs

d. All of above

1.b

2.d

3.a

4.b

5.c

6.b

7.a

8.d 9.c 10.d

15.d

16.a

17.c

18.a

19.a

20.d

21.c

22.d

11.a
23.b

12.b
24.c

13.a 14.d
25.a

26.d

5. ABSTRACT DATA TYPE


5.1 ABSTRACT DATA TYPE
I .Statements/Definitions
1.

The abstract data type is a triple of D set of domains, F set of functions, A


axioms in which only what is to be done is mentioned but how is to be done is not

2.
3.
4.
5.
6.

mentioned.
In ADT, all the implementation details are hidden.
In short
ADT= TYPE+ Function names + Behaviour of each function
The abstract data type consists of following things
Data used along with its data type.
Declaration of functions which specify only the purpose. That means What is to
be done in Particular function has to be mentioned but how is to be done must

7.

be hidden.
Behavior of function can be specified

8.

together.
Thus ADT allows programmer to hide the implementation details. Hence it is

with the help of data and functions

called abstract.
5.2 ADT OPERATIONS
Various Abstract Data Type operations are
1.
2.
3.
4.
5.

Create : This operation creates the data base


Display: This operation is for displaying all the elements of the data structure
Insertion: By this operation the element can be inserted at any desired position .
Deletion: By this operation any desired element can be deleted from the data structure.
Modification: This operation modifies the desired elements value by any desired new
value.

ADT Data structure


The ADT operations are carried out with the help of data structure .this part describes the
structure of the data used in the data used in the ADT in an informal way. Various data structures

That can be used for ADT are arrays , Set , Linked list , Stack , Queues and so on.
Examples
1. ADT for Set
If you want to write ADT for a set of integers , then we will use following method
Abstract Data Type Set
Instances :Set is a collection of integer type of elements .
Preconditions :none
Operations :
1. Store( ) : This operation is for storing the integer element in a set.
2. Retrieve( ) : This operation is for retrieving the desired element from the given set.
3. Display( ) : This operation is for displaying the contents of set.
There is a specific method using which an ADT can be written. We begin with keyword
Abstract data type which is then followed by name of the data structure for which we want to
write an ADT. In above given example we have taken set data structure.

We must first write instances in which the basic idea about the corresponding data
structure must be given. Generally in this section definition of corresponding data

structure is given.
Using preconditions or post conditions we can mention specific conditions that must be

satisfied before or after execution of corresponding function.


Then a listing of all the required operations must be given. In this section, we must
specify the purpose of the function. We can also specify the data types of these functions.

2. ADT for Arrays


Abstract Data Type Array
Instances: An array A of some size, index i and total number of elements in the array n.
Operations:
1. Store()-This operation stores the desired elements at each successive location.
2. Display () This operation displays the elements of the array.
ADT Implementation of Stack and Queue
Stack is a data structure which posses LIFO i.e. Last In First Out property. The abstract data type
for stack can be as given below.

Abstract Data Type stack


Instances: stack is a collection of elements in which insertion and deletion of elements is done
by one end called top.
Preconditions:
1. Stfull (): This condition indicates whether the stack is full or not. If the stack is

full then we cannot insert the elements in the stack


2. Stempty (): This condition indicates whether the stack is empty or not. If the
stack is empty then we cannot pop or remove any element from the stack.
Operations:
1. Push: By this operation one can push elements onto the stack. Before performing
push we must check stfull () condition.
2. Pop: By this operation one can remove the elements from stack. Before popping
the elements from stack we should check stempty () condition.
The queue is a FIFO data structure in which the element which is inserted first will be the first to
be removed. The ADT for queue will be as follows
Abstract Data Type queue
Instance: The queue is collection of elements in which the element can be inserted by one end
called rear and elements get deleted from the end called front.
Preconditions:
1.Queue_full() checks whether queue is full or not. If the Queue is full then we cannot insert
the elements.
2.Queue_empty () checks whether queue is empty or not. If the Queue is empty then we
cannot delete the elements.
Operations:
1.Queue_insert()- Inserts the element in queue from rear end.
2.Queue_delete()- Deletes the element from the queue by front end.
Thus the ADT for queue gives the abstract for what has to be implemented, which are the various
operations on queue. But it never specifies how to implement those.

5.3 HASHING TECHNIQUES


I. Statements/Definitions
In general, in all searching techniques, search time is dependent on the number of items.
Sequential search, binary search and all the search trees are totally dependent on number
of items and many key comparisons are involved.
Hashing is a technique where search time is independent of the number of items or
elements. In this technique a hash function is used to generate an address from a key. The
hash function takes a key as input and returns the hash value of that key which is used as
an address index in the array.
We can write hash function as follows
h(k)=a
Where h is hash function, k is the key, a is the hash value of the key.
While choosing a hash function we should consider some important points.

It should be easy to compute


It should generate address with minimum collision.

Techniques for making hash function.

Truncation Method
Mid square Method
Folding Method
Division Method
Truncation Method

This is the simplest method for computing address from a key. In this method we take
only a part of the key as address.
A collision occurs whenever a key is mapped to an address that is already occupied.
Collision Resolution technique provides an alternate place in hash table where this key
can be placed.
Collision Resolution technique can be classified as:
1) Open Addressing (Closed Hashing)
a) Linear Probing
b) Quadratic Probing
c) Double Hashing

2) Separate Chaining (Open Hashing)

II. Multiple Choice Questions


Easy:
1.

An Abstract Data type ADT is


a. Same as an abstract class
b. A data type that cannot be instantiated
c. A data type for which only the operations defined, but none else
d. All of the above

2.

ADT consists of
a. Type

3.

5.

d. All

b. basic operation

c. both

d. None

An implementation of ADT consists of algorithms


a. To store the data items

b. Basic operation

c. Both

d. None

Representation of data structure in memory is known as


a. Recursion

6.

c. Behaviour

An implementation of ADT consists of storage structures to store the


a. data items

4.

b. Function names

b.ADT

c. Storage structure

d. File structure

An ADT is defined to be a mathematical model of a user-defined type along with


the collection of all ____________ operations on that model
a. Cardinality

7.

b. assignment

c. Primitive

d. Structured

An ADT is defined to be a mathematical model of a user-defined type along


with the collection of all ____________ operations on that model
a. Cardinality

8.

b. Assignment

c. Primitive

d. Structured

The Ordinary queue is based on the principle


a. LIFO

b. FIFO

c. FILO

d. None

9.

The Stack is based on the principle


a. LIFO

10.

c. Any of a or b

d. None

which of the following is not a stack operation?


a. Push

11.

b. FIFO

b. Pop

c. peek

d. None

The best structure to check whether an arithmetic expression has balanced


Parentheses is a
a. Queue

12.

c. Tree

d. List

which of the following data structure is used for evaluation of post fix expression
a. Stack

13.

b. Stack

b. Array

c. Queue

d. Linked list

Which of the following is application of stack


a. Conversion from infix to postfix

b. Conversion from infix to prefix

c. Both

d. None

Medium:
14.

What are members of ADT data structures


a. Instances

15.

b. Preconditions

c. Operations

A mathematical-model with a collection of operations defined on that model is


called

16.

d. All

a. Data Structure

b. Abstract Data Type

c. Primitive Data Type

d. Algorithm

Representation of data structure in memory is known as


a. Recursive

b. Abstract data type

c. Storage structure

d. File structure

17.

An ADT is defined to be a mathematical model of a user-defined type along with


the collection of all ____________ operations on that model
a. Cardinality

18.

b. Assignment

c. Primitive

d. Structured

Which of the following abstract data types can be used to represent a many to
many relation?
a. Tree

19.

c. Graph

d. b&c

The information about an array that is used in a program will be stored in


a. Symbol table

20.

b. plex

b. Activation record c. System table

d. Dope vector

Which of the following abstract data types can be used to represent a many to
many relation?
a. Tree

21.

b. Plex

c. Graph

d. b &c

Which of the following operations is performed more efficiently by doubly linked


list than by singly linked list?
a. Deleting a node whose location in given
b. Searching of an unsorted list for a given item
c. Inverting a node after the node with given location
d. Traversing a list to process each node

22.

23.

A set is ___________
a. Collection of elements

b. Any objects in the world

c. Well defined collection of elements

d. None

Which of the following is not a set


a. Natural No set

24.

b. Integer set

c. Clever people set

d. None

Which of the following is not a set operation


a. Union

b. Intersection

c. Join

d. None

25.

A set can be represented by _______


a. Array

26.

d. None

b. Null set

c. Powerful set

d. Exponential set

The set can be represented by


a. Listing Method

28.

c. Both

A power set is ____


a. Set of all sub sets

27.

b. Linked list

b. Describing Method c. Recursion Method d. All

which is not a set operation


a. Union

b. Intersection

c. Equality

d. Dictionary

29.

Application of dictionary
a. Student register

30.

b. Telephone directory

c. Word dictionary

d. All

The situation in which the hash function returns same hash key is called
a. Clash

b. Collision

c. Synonym

d. None

Complex:
31.

Two same hash keys returned for different records is called


a. Twins

32.

b. Synonyms

b. Location

b. Hash function

d. Memory

c. Hash domain

d. Mapping table

Which of the following are Techniques for making hash function.


a. Truncation Method

35.

c. Bucket

__________is used to map several dictionary entries in the hash table


a. Key table

34.

d. None

Each position of the hash table is called


a. Space

33.

c. Antonym

b. Mid square Method c. Folding Method

d. All

The characteristic of good hashing function is_____


a. Minimize collisions b. Maximize collisions c. Minimize entries d. None

36.

Which of the following is not a closed hashing method


a. Linear probing

37.

d. None

Another name for closed hashing is


a. Open addressing

38.

b. Quadratic probing c. Double hashing

b. Closed addressing

Another name for open hashing is

c. Separate chaining

d. None

a. Open addressing
39.

b. Closed addressing

c. Separate chaining

d. None

Quadratic probing formula is


a.Hashkey+i2%M

b. Hashkey+i4%M

c. Hashkey2%M d. Hashkey+i2

40.

By applying second hashing function we will get


a. Exact location number
b. Number of positions from collision
c. Any of a and b
d. None

Answers for MCQs


1. d

2.d

3.a

11.b

12.a

22.c

23.c

33.b

4.b

5.d

6. c

7.c

13.c 14.d

15.b

16b

17.c

24.c

25.c

26.a

27.d

28.d

29.d

30.b 31. b

34.d

35.a

36.d

37.a

38.c

39.a

40.b

18.d

8.b

9.a

19.d

20.d

10.d
21. a
32.c

Вам также может понравиться