Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

Visit : www.EasyEngineeering.

net

vector subscript: Amethod of specifying an array section by means of a vector containing the subscripts
of the elements of the parent array that are to constitute the array section.

virtual function: A genetic function, with a specific return type, extended later for each new argument
type.

void subprogram: A C++ subprogram with an empty argument list and/or a subroutine with no returned
argument.

work array: A temporary array used for the storage of intermediate results during processing.

Question Bank with Answer

UNIT – I

PART - A

1. What is the output of the following program, if it is correct? Otherwise


indicate the mistake: [May 2006]

int l=10;
Void main []
{int l=20;
{int l=30;
cout<<l<<::l;
}}

The program is in correct due to syntax error. Within the main function, there is no need
of another opening braces in the int l=20; and also closing braces.

2. Difference between Class and structure? [April -2010, Dec-2012]

 Class is the ADT where as structure is udt.


 Class needs access specifier such as private, public & private where as structure
 members can be accessed by public by default & don’t need any accessfiers.
 Class is oops where structure is borrowed from traditional structured [pop] concept.

3. What is abstract Class? [Nov-2009]

 An abstract class is a class that is designed to be specifically used as a base class.

107

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 An abstract class contains at least one pure virtual function. You declare a pure virtual
function by using a pure specifier [= 0] in the declaration of a virtual member function in
the class declaration

4. List out the advantages of new operator over malloc[] [Dec-2012]

 It automatically computes the size of the data object.


 It automatically returns the correct pointer type
 It is possible to initialize the objects while creating_ the memory space.
 It can be overloaded.

5. What are the basic concepts of OOS? [ April -2011]

1. Objects.
2. Classes.
3. Data abstraction and Encapsulation.
4. Inheritance.
5. Polymorphism.
6. Dynamic binding.
7. Message passing

6. What is the difference between local variable and data member? [Nov-2011]

 A data member belongs to an object of a class whereas local variable belongs to its
current scope.
 A local variable is declared within the body of a function and can be used only from the
point at which it is declared to the immediately following closing brace.
 A data member is declared in a class definition, but not in the body of any of the class
member functions.
 Data members are accessible to all member function of the class.

7. What is the function parameter? Difference between parameter and Argument. [Nov
2011]

 A function parameter is a variable declared in the prototype or declaration of a function:


o void foo[int x]; // prototype -- x is a parameter
o void foo[int x] // declaration -- x is a parameter
o {
o }
 An argument is the value that is passed to the function in place of a parameter

8. What is data hiding? [April -2011,Nov-2010]

108

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 The insulation of data from direct access by the program is called as data hiding or
information binding.
 The data is not accessible to the outside world and only those functions, which are
wrapped in the class, can access it.

9. What are the advantages of Default Arguments? [Nov-2010]

The function assigns a default value to the parameter which does not have a matching
argument in the function call. They are useful in situations where some arguments always have
the same value.
e.g., float amt [float P, float n, float r = 0.15];

10. List out the basic concepts of Object Oriented Programming. [Nov-2009]

 Objects
 Classes
 Data Abstraction and Encapsulation
 Inheritance
 Polymorphism
 Dynamic Binding
 Message Passing

11. What are abstract classes? [Nov 2009, Apr 2013]


o Classes containing at least one pure virtual function become abstract classes.
o Classes inheriting abstract classes must redefine the pure virtual functions; otherwise the
derived classes also will become abstract. Abstract classes cannot be instantiated.

12. Define abstraction and Encapsulation [Apr 2011]


Data Abstraction
Abstraction refers to the act of representing the essential features without including the
background details or explanations.

Data Encapsulation
The wrapping up of data and functions into a single unit is known as data encapsulation.

13. What is the Need for Static Members [April 2011]

 Class members can be declared using the storage class specifier static in the class
member list. Only one copy of the static member is shared by all objects of a class in a
program.
 When you declare an object of a class having a static member, the static member is not
part of the class object.

14. Define Polymorphism. [Apr 2013]


109

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 Polymorphism is another important oops concept.


 Polymorphism means the ability to take more than one form.
 For example, an operation may exhibit different behavior in different instances,
behavior depends upon the types of data used in the operation.

15. What do you mean by pure virtual functions? [Dec2008]


1. A pure virtual member function is a member function that the base class forces derived
classes to provide.
2. Any class containing any pure virtual function cannot be used to create object of its own
type.

16. Write a C++ program to check the given integer is prime or composite number. [Apr-
2010]

#include<conio.h>
#include<stdio.h>
int main[]
{
int num,d,ctr;
clrscr[];
printf["\n Enter a number="];
scanf["%d",&num];
d=1;
ctr=0;
while[d<=num]
{
if[num%d==0]
ctr++;
d=d+1;
}
if[ctr==2]
printf["\n %d is a prime number",num];
else
printf["\n %d is a composite number",num];
getch[];
return[0];
}

17. What is function Prototype? [DEC 2011]

A function prototype or function interface in C, Perl, PHP or C++ is a declaration of a


function that omits the function body but does specify the function's return type, name and
argument types.
While a function definition specifies what a function does, a function prototype can be
thought of as specifying its interface.
110

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

18. List out four Storage Classes in C++ [Nov 2008]

Storage classes are used to specify the lifetime and scope of variables. How storage is
allocated for variables and how variable is treated by complier depends on these storage
classes. These are basically divided into 5 different types :
1. Global variablES
2. Local variables
3. Register variables
4. Static variables
5. Extern variables

19. What is an identifier?

 Identifiers are names for various programming elements in c++ program. such as
variables, arrays, function, structures, union, labels ect.,
 An identifier can be Composed only of uppercase, lower case letter, underscore and
digits, but should start only with an alphabet or an underscore.

20. What is a keyword?


Keywords are word whose meanings have been already defined in the c compiler. They
are also called as reserved words.
(ex) main(), if, else, else, if, scanf, printf, switch, for, goto, while ect.,

21. List out the benefits of oops.


• Can create new programs faster because we can reuse code
• Easier to create new data types
• Easier memory management
• Programs should be less bug-prone, as it uses a stricter syntax and type checking.
• `Data hiding', the usage of data by one program part while other program parts cannot
access the data Will whiten your teeth

22. List out the application of oops.


• Client server computing
• Simulation such as flight simulations.
• Object-oriented database applications.
• Artificial intelligence and expert system
• Computer aided design and manufacturing systems.

23. Define data hiding.

111

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

The purpose of the exception handling mechanism is to provide a means to detect and
report an “exceptional circumstance” so that appropriate action can be taken.

24. What is the use of scope resolution operator?

In C, the global version of the variable cannot be accessed from within the inner block.
C++ resolves this problem by introducing a new operator :: called the scope resolution operator.
It is used to uncover a hidden variable.
Syntax:
:: variable name

25. When will you make a function inline?

When the function definition is small, we can make that function an inline function and
we can mainly go for inline function to eliminate the cost of calls to small functions.

26. What is overloading?

Overloading refers to the use of the same thing for different purposes.
There are 2 types of overloading:
• Function overloading
• Operator overloading

27. What is the difference between normal function and a recursive function?

 A recursive function is a function, which call it whereas a normal function does not.
 Recursive function can’t be directly invoked by main function

28. What are objects? How are they created?

Objects are basic run-time entities in an object-oriented programming system. The class
variables are known as objects. Objects are created by using the syntax:
classname obj1,obj2,…,objn;
(or) during definition of the class:
class classname
{
-------
}obj1,obj2,…,objn;

29. List some of the special properties of constructor function.

• They should be declared in the public section.


• They are invoked automatically when the objects are created.

112

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

• They do not have return types, not even void and cannot return values.
• Constructors cannot be virtual.
Like other C++ functions, they can have default arguments

30. Describe the importance of destructor.

A destructor destroys the objects that have been created by a constructor upon exit from
the program or block to release memory space for future use. It is a member function whose
name is the same as the class name but is preceded by a tilde.
Syntax:
~classname()
{}

31. What do you mean by friend functions?

C++ allows some common functions to be made friendly with any number of classes,
thereby allowing the function to have access to the private data of thse classes. Such a function
need not be a member of any of these classes. Such common functions are called friend
functions.

32. What are member functions?

Functions that are declared within the class definition are referred as member function.

33. Define dynamic binding.

Dynamic binding means that the code associated with a given procedure call is not
known until the time of the call at run-time.

34. Write any four properties of constructor.(DEC 2010)

 Constructors should be declared in the public section.


 They are invoked automatically when the objects are created.
 They do not have return types
 They cannot be inherited

35. List any four Operators that cannot be overloaded.(DEC 2010) (DEC 2009) (DEC 2011)

Class member access operator (. , .*)


Scope resolution operator (::)
Size operator ( sizeof )
Conditional operator (?:)

36. What is a Destructor? (DEC 2012)


113

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

A destructor is used to destroy the objects that have been created by a constructor. It is a
special member function whose name is same as the class and is preceded by a tilde ‘~’ symbol.
When an object goes out from object creation, automatically destructor will be executed.

Example:
class File {
public:
~File(); //destructor declaration
};
File::~File()
{ close(); // destructor definition
}

37. What is the Need for initialization of object using Constructor? (DEC 2012)

If we fails to create a constructor for a class, then the compiler will create a constructor
by default in the name of class name without having any arguments at the time of compilation
and provides the initial values to its data members. So we have to initialize the objects using
constructor

38. What is a Copy Constructor (DEC 2009)

A copy constructor is used to declare and initialize an object from another object. It takes
a reference to an object of the same class as an argument

Eg:
integer i2(i1);
would define the object i2 at the same time initialize it to the values of i1.
Another form of this statement is

Eg: integer i2=i1;


The process of initializing through a copy constructor is known as copy initialization.

39. Give an example for a Copy Constructor (JUNE 2013)

#include<iostream>
#include<conio.h>
using namespace std;
class Example {
// Variable Declaration
int a,b;
public:
114

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

//Constructor with Argument


Example(int x,int y) {
// Assign Values In Constructor
a=x;
b=y;
cout<<"\nIm Constructor";
}
void Display() {
cout<<"\nValues :"<<a<<"\t"<<b;
}
};
int main() {
Example Object(10,20);
//Copy Constructor
Example Object2=Object;
// Constructor invoked.
Object.Display();
Object2.Display();
// Wait For Output Screen
getch();
return 0;
}

40. What is the Need for Destructors? (June 2013)

 Destructor is used to destroy an object.


 By destroying the object memory occupied by the object is released.

41. Explain the functions of Default Constructor(MAY 2011)


The main function of the constructor is, if the programmer fails to create a constructor for
a class, then the compiler will create a constructor by default in the name of class name without
having any arguments at the time of compilation and provides the initial values to its data
members.
Since it is created by the compiler by default, the no argument constructor is called as
default constructor.

42. What is the need for Overloading an operator(MAY 2011)

 To define a new relation task to an operator, we must specify what it means in


relation to the class to which the operator is applied.
 This is done with the help of a special function called operator function.
 It allows the developer to program using notation closer to the target domain and
allow user types to look like types built into the language.

115

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 The ability to tell the compiler how to perform a certain operation when its
corresponding operator is used on one or more variables.

43. What is the function of get and put function (MAY 2010)

Cin.get(ch) reads a character from cin and stores what is read in ch.
Cout.put(ch) reads a character and writes to cout

UNIT -I
PART – B
1. State the merits and demerits of object oriented methodology.

Merits:
• Can create new programs faster because we can reuse code
• Easier to create new data types
• Easier memory management
• Programs should be less bug-prone, as it uses a stricter syntax and type checking.
• `Data hiding', the usage of data by one program part while other program parts cannot
access the data Will whiten your teeth

Demerits:
• disadvantages of C++ over Java:
• Java protects you from making mistakes that C/C++ don’t, as you’ve
• C++ has many concepts and possibilities so it has a steep learning curve
• extensive use of operator overloading, function overloading and virtual
• functions can very quickly make C++ programs very complicated
• shortcuts offered in C++ can often make it completely unreadable, just like in C

2. Explain the basic concepts of object oriented programming in detail with example.
BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING

These include:
 Objects
 Classes
 Data abstraction and encapsulation
 Inheritance
 Polymorphism
 Dynamic binding
 Message passing

Objects:

116

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Objects are the basic run-time entities in an object oriented programming. They may
representa person, a place, a bank account or any item that the program has to handle. They may
represent user-defined data such as vectors, time and lists. Programming problem is analyzed in
terms of objects and the nature of communication b/n them.
When a program is executed, the objects interact by sending messages to another. Each
object contains data and code to manipulate the data. Objects can interact without having to
know the etails of each others data or code. It is sufficient to know the type of message
accepted, and the type of message accepted and the type of response returned by the objects.

Classes:
The entire set of data and code of an object can made a user defined data type with the
help of a class. In fact the objects are variable of the type class. Once class has been defined we
can create any number of objects belonging to that class. In short, a class serves as a blueprint or
a plan or a template. It specifies what data and what functions will be included in objects of that
class. Defining the class doesn’t create any objects, just as the mere existence of a type int
doesn’t create any variables.

Data Abstraction and Encapsulation:


The wrapping up of data and functions into a single unit is known as encapsulation. It is
the most striking feature of the class. The data is not accessible to the outside world and only
those functions which are wrapped in the class can access it. These functions provide interface
b/n the object’s data and the program. This insulation of the data from direct access by the
program is called data hiding or information hiding.
Abstraction represents the act of representing the essential features without including the
background details or explanations. Classes use the concept of abstraction and are defined as a
list of abstract attributes such as size, weight and cost and functions to operate on these
attributes. The attributes are called data members and functions are called member functions or
methods. Since the classes use the concept of data abstraction, they are known as Abstract Data
Types (ADT).

Inheritance:
It is the process by which objects of one class acquire the properties of objects of another
class. It supports the concept of hierarchical classification. For example, the bird ‘robin’ is a part
of the class ‘flying bird’ which is again a part of the class ‘bird’. This concept provides the idea
of reusability. This means that we can add additional features to an existing class without
modifying it. This is possible by a deriving a new class from an existing one. The new class will
have the combined features of both the classes.

Polymorphism:
It means the ability to take more than one form. An operation may exhibit different
behavior in different instances. The behavior depends upon the types of data used in the
117

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

operation. For example the operation addition will generate sum if the operands are numbers
whereas if the operands are strings then the operation would produce a third string by
concatenation. The process of making an operator to exhibit different behaviors in different
instances is known as operator overloading.
A single function name can be used to handle different types of tasks based on the
number and types of arguments. This is known as function overloading. It allows objects to have
different internal structures to share the same external interface.

Dynamic Binding:
Binding refers to the linking of a procedure call to the code to be executed in response to
the call. Dynamic Binding (late binding) means the code associated with the given procedure call
is not known until the time of the call at run-time. It is associated with the polymorphism and
inheritance.

Message Passing:
The process of programming in OOP involves the following basic steps:
 Creating classes that define objects and their behavior
 Creating objects from class definitions
 Establishing communication among objects
A message for an object is request for execution of a procedure and therefore will invoke a
function (procedure) in the receiving object that generates the desired result.
Message passing involves specifying the name of the object, the name of the function
(message) and the information to be sent.
E.g.: employee.salary(name);
Object: employee
Message: salary
Information: name

3. State the rules to be followed while overloading an operator.write a program to illustrate


overloading.

OPERATOR OVERLOADING:
• Operator overloading means giving additional meaning to existing operators
• By operator overloading an existing operator can be made to perform different operations
than the stipulated one.
• It doesn’t change the meaning and precedence of the original operator.
• Almost all the operators in c++ can be overloaded except the following

1) Sizeof ()
2) Conditional operator (?:)
3) Scope resolution operator (::)
4) Class member access operator (.,.*)
118

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

example the operation addition will generate sum if the operands are numbers whereas if the
operands are strings then the operation would produce a third string by concatenation. The
process of making an operator to exhibit different behaviors in different instances is known as
operator overloading.
A single function name can be used to handle different types of tasks based on the number and
types of arguments. This is known as function overloading.
It allows objects to have different internal structures to share the same external interface.

Dynamic Binding:
Binding refers to the linking of a procedure call to the code to be executed in response to the call.
Dynamic Binding (late binding) means the code associated with the given procedure call is not
known until the time of the call at run-time. It is associated with the polymorphism and
inheritance.

Message Passing:
The process of programming in OOP involves the following basic steps:
 Creating classes that define objects and their behavior
 Creating objects from class definitions
 Establishing communication among objects
A message for an object is request for execution of a procedure and therefore will invoke a
function (procedure) in the receiving object that generates the desired result. Message passing
involves specifying the name of the object, the name of the function (message) and the
information to be sent.
E.g.: employee.salary(name);
Object: employee
Message: salary
Information: name

3. State the rules to be followed while overloading an operator.write a program to illustrate


overloading.

OPERATOR OVERLOADING:

• Operator overloading means giving additional meaning to existing operators


• By operator overloading an existing operator can be made to perform different operations than
the stipulated one.
• It doesn’t change the meaning and precedence of the original operator.
• Almost all the operators in c++ can be overloaded except the following
o Sizeof ()
o Conditional operator (?:)
o Scope resolution operator (::)

119

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

o Class member access operator (.,.*)

SYNTAX:

Return-type operator op-symbol(argument list)


{
body of the function;
}
Generally there are three general classifications of operator namely
Operator
Unary binary ternary
Among the above unary and binary operators can be overloaded and ternary operator
cannot
be overloaded.
OVERLOADING UNARY OPERATOR:
Unary operators like unary + , Unary - ….. can be overloaded as follows
EXAMPLE:
#include
#include
class unary
{
int a;
public:
void get()
{
a=10;
}
void show()
{
cout <<"\n The value of the object is\n"<
}
void operator - ()
{
a=-a;
}
};
void main()
{
clrscr();
unary u;
u.get();
u.show();
-u; //unary operator - called
u.show();
getch();
}
120

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

O/P:

The value of the object is


10
The value of the object is
-10

• In the above program, the unary operator minus is overloaded.


• In this an object ‘u’ is created and given the value 10
• When the unary operator – is called the operator function is invoked and the value of the
member data of the object ‘u’ is negated.
• The negated value is then displayed.

4. (i) Describe the application of oop technology

Applications of OOP:
 Real time systems
 Simulation and modeling
 AI and expert systems
 Neural networks and parallel programming.
 Decision support and office automation systems

(ii) What is an inline function?

INLINE FUNCTIONS :

An inline function is a function that is expanded in line when it is invoked. That is , the
complier replaces the function call with the corresponding function code.The inline function are
defined as follows:
inline function header

{
function body
}

Example :

inline int cube(int a)


{
return (a*a*a);
}
Program :
#include
121

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

inline float mul(float x, float y)


{
return (x*y);
}
inline float div(double p, double q)
{
return( p / q )
}
int main()
{
float a=12.35;
float b=9.32;
cout<< mul(a,b);
cout<
return 0;
}

5. (i). Explain copy constructor with suitable c++ coding.

Constructor:
A constructor is a special member function whose task is to initialize the objects of its class. It is
special because its name is the same as the class name. The constructor is invoked whenever an
object of its associated class it’s created.
A constructor is declared and defined as follows:

class sample
{
int m,n;
public:
sample()
{
m = n = 0;
}
// some code
};
void main()
{
sample s; // object is created
// some code
}

During the object creation, it also initializes the data member’s m and n to 0.
Default Constructor:
A constructor that accepts no parameters is called the default constructor.

122

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Characteristics of a constructor:

 They should be declared in the public section


 They are invoked automatically when the objects are created
 They do not have return types
 They cannot be inherited
 They can have default arguments
 It cannot be virtual
 We cannot refer to their addresses
 An object with a constructor or destructor can not be used as a member of a union
 They make ‘implicit calls’ to the operators new and delete when memory allocation is
required

Copy constructor:
A copy constructor takes a reference to an object of the same class as itself as an
argument.
Example:
It sets the value of every data element of s3 to the value of the corresponding data element of
s2

Constructors with default arguments:

It is possible to define constructors with default arguments.


Example:
Inside the class definition:
sample(int a, int b = 10)
Inside the main():
sample s2(20);
assigns 20 to x and 10 to y.
where as
sample s5(25,35) ; assigns 25 to x and 35 to y

(ii).List out the rules for overloading operator.

OPERATOR OVERLOADING:

• Operator overloading means giving additional meaning to existing operators


• By operator overloading an existing operator can be made to perform different operations
than the stipulated one.
• It doesn’t change the meaning and precedence of the original operator.
• Almost all the operators in c++ can be overloaded except the following

o Sizeof ()
o Conditional operator (?:)
o Scope resolution operator (::)
123

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

o Class member access operator (.,.*)

SYNTAX:

Return-type operator op-symbol(argument list)


{
body of the function;
}

Generally there are three general classifications of operator namely


Operator
Unary binary ternary
Among the above unary and binary operators can be overloaded and ternary operator cannot
be overloaded.

6. Compare and contrast the following control structure with example:


(i). break statement & continue statement.

Break continue
a) Used to terminate the loops or to Used to transfer the control to the Exit loop from a switch.
Start of loop
b) The break statement when executed The continue statement when causes Immediate
termination of loop executed caused immediate containing it. termination of the current iteration
of the loop.

(ii). Do – while and the while statement.

While loop do-while loop


c) The while loop tests the condition The do-while loop tests the condition before each iteration
after the first iteration
d) If the condition fails initially the loop Even if the condition fails initially Is skipped entirely
even in the first the loop is executed once in a Iteration.

124

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

UNIT – II
PAERT – A

1. What are templates? (AUC DEC 2009)

 Template is one of the features added to C++.


 It is new concepts which enable us to define generic classes and functions and thus
provides support for generic programming.
 A template can be used to create a family of classes or functions.

2. Illustrate the exception handling mechanism. (AUC DEC 2009)


C++ exception handling mechanism is basically upon three keywords, namely,
try, throw, and catch.
try – is used to preface a block of statements which may generate exceptions.
throw - When an exception is detected, it is thrown using a throw statement in the try block.
Catch- A catch block defined by the keyword catch ‘catches’ the exception ‘thrown’ by the
thrown statement in the try block and handles its appropriately.

3. What happens when a raised exception is not caught by catch block? (AUC MAY 2010)

 If the type of object thrown matches the arg type in the catch statement, then catch block
is executed for handling the exception.
 If they do not match the program is aborted with the help of abort() function which is
invoked by default.
 When no exception is detected and thrown, the control goes to the statement immediately
after the catch block. That is the catch block is skipped.

4. Give the syntax of a pointer to a function which returns an integer and takes arguments
one of integer type and 2 of float type. What is the difference between a class and a
structure? (AUC MAY 2010)
int (X_fun) (int, float);

5. What is a template? What are their advantages (AUC DEC 2010/JUN 2012/DEC
2012)

 A template can be used to create a family of classes or functions.


 A template is defined with a parameter that would be replaced by a specified data type at
the time of actual use of the class or functions. Supporting different data types in a single
framework.
 The template are sometimes called parameterized classes or functions.

6. How is an exception handled in C++? (AUC DEC 2010)

125

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Exceptions are run time anomalies or unusual conditions that a program may encounter
while executing.
1. Find the problem (Hit the exception)
2. Inform that an error has occurred.( Throw the exception)
3. Receive the error information. ( Catch the exception)
4. Take corrective actions.( Handle the exception)

7. What is function template? Explain. (AUC MAY 2011)

 Like class template, we can also define function templates that could be used to create a
family of functions with different argument types.
 The general format of a function template is:
template<class T>
returntypefunctionname ( arguments of type T)
{
// ………
//……….Body of function with type T wherever appropriate
//………..
}

8. List five common examples of exceptions. (AUC MAY 2011)

 Division by zero
 Access to an array outside of its bounds
 Running out of memory or disk space.

9. What is class template? (AUC DEC 2011)

 Templates allows to define generic classes. It is a simple process to create a generic class
using a template with an anonymous type.
 The general format of class template is:
template<class T>
classclassname
{
// ………class member specification.
//………. with anonymous type T wherever appropriate
//……,…..
}

10. What are 3 basic keywords of exception handling mechanism? (AUC DEC 2011)
C++ exception handling mechanism is basically built upon three keywords
 try
 throw
 catch
126

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

11. What are the c++ operators that cannot be overloaded?


Size operator (sizeof)
Scope resolution operator (::)
member access operators(. , .*)
Conditional operator (?:)

12. What is a virtual base class?


When a class is declared as virtual c++ takes care to see that only copy of that class is inherited,
regardless of how many inheritance paths exist between the virtual base class and a derived class.

13. What is the difference between base class and derived class?
The biggest difference between the base class and the derived class is that the derived
class contains the data members of both the base and its own data members. The other difference
is based on the visibility modes of the data members.

14. What are the rules governing the declaration of a class of multiple inheritance?
• More than one class name should be specified after the : symbol.
• Visibility modes must be taken care of.
If several inheritance paths are employed for a single derived class the base class must be
appropriately declared

15. Mention the types of inheritance.


1.Single inheritance.
2. Multiple inheritance.
3. Hierarchical inheritance.
4. Multilevel inheritance.
5. Hybrid inheritance.

6. Define dynamic binding. APRIL AMY 2010


Dynamic binding means that the code associated with a given procedure call is not
known until the time of the call at run-time.

17. What do u mean by pure virtual functions?


A pure virtual function is a function declared in a base class that has no definition relative to the
base class. In such cases, the compiler requires each derived class to either define the function or
redeclare it as a pure virtual function. A class containing pure virtual functions cannot be used to
declare any objects of its own.

18. Mention the key words used in exception handling.

127

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

The keywords used in exception handling are


• throw
• try
• catch

19. List the ios format function.


The ios format functions are as follows:
• width()
• precision()
• fill()
• setf()
• unsetf()

20. List the manipulators.


The manipulators are:
• setw()
• setprecision()
• setfill()
• setiosflags()
• resetiosflags()

21. Mention the equicalent ios function for manipulators.


Manipulator Equivalent ios function
setw(int w) width()
setprecision(int d) precision()
setfill(int c) fill()
setiosflags(long f) setf()
resetiosflags(long f) unsetf()
endl “\n”

22. Define fill functions.


The fill( ) function can be used to fill the unused positions of the field by any desired
character rather than by white spaces (by default). It is used in the following form:
cout.fill(ch);
where ch represents the character which is used for filling the unused positions. For example,
the statements
cout.fill(‘*’);
cout.width(10);
cout<<5250<<”\n”;

23 .Give the syntax of exception handling mechanism.


The syntax of exception handling mechanism is as follows:
128

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

try
{
---------------------
throw exception
---------------------
}
catch(type arguments)
{
---------------------
---------------------
}
----------------------
----------------------

UNIT – II
PART – B
1. Explain hybrid inheritance with suitable C++ coding.
2. Explain multiple inheritances with suitable c++ coding.
INHERITANCE:
• Inheritance is a mechanism of deriving a new class from a old class.
• It provides the concept of reusability
• By inheritance some or all the properties of a class can be derived in to another class.
• The class which provides the properties is called as base class and the class which derives
the properties is called as derived class.
Multiple inheritance:
It is a type of inheritance in which a class can inherit properties from more than one class.
Syntax:
Class derivedclass name : visibility mode baseclass1,visibilitmode baseclass2
{
body of the derived class;
};
visibility mode can be either private or public.
Example :
#include
#include
class bc1
{
protected:
int a;
public :
void get()
129

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

{
cout <<”\n enter the value for a\n”;
cin >>a;
}
};
class bc2
{
protected:
int b;
public;
void get1()
{
cout <<” \n enter the value for b \n”;
cin>>b;
}
};
class dc : public bc1, public bc2
{
public :
void show()
{
cout <<”The values of a and b are” ;
cout <<<”\n”<
}
};

void main()
{
clrscr();
dc d;
d.get();
d.get1();
d.show();
}

3. Define polymorphism. Explain the different types of polymorphism.


Polymorphism

Run time compile time


Or
Dynamic binding static binding
Or
late binding early binding
130

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Virtual fuctions operator overloading function overloading


• Linking a function call to the function definition is called as binding.
• If the binding is done at compile time then it is called as compile time polymorphism.
•Compile time polymorphism is achieved by using function overloading and operator
overloading.
• If the binding is done at run time then it is called as run time polymorphism. It is
achieved by using virtual functions.

VIRTUAL FUNCTIONS:
• When the same function is used in both the base class and derived class and if a pointer of
base class is used to access the members of the derived class , then the members appropriate to
the base class is called.
Example:
#include
#include
class base
{
public:

void show()
{
cout <<”\n This is a base class\n”;
}
};
class derived : public base
{
public:

void show()
{
cout <<”\n This is a derived class\n”;
}
};

void main()
{
base b,*bptr;
bptr=&b;
bptr->show();
derived d;
bptr=&d;
bptr->show();
getch();
}
131

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

o/p
This is a base class
This is a base class
• This can be overcome by using virtual functions.
• The base class member functions should be preceeded by a keyword virtual.
Example:
#include
#include
class base
{
public:

virtual void show()


{
cout <<”\n This is a base class\n”;
}
};

class derived : public base


{
public:

void show()
{
cout <<”\n This is a derived class\n”;
}
};

void main()
{
base b,*bptr;
bptr=&b;
bptr->show();
derived d;
bptr=&d;
bptr->show();
getch();
}

o/p
This is a base class
This is a derived class
132

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

133

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

4. Explain multiple catch statement with help of suitable C++ coding.


EXCEPTION HANDLING
Exceptions are run time anomalies or unusual conditions that a program may encounter
while executing. Anomalies might include conditions such as division by zero, access to an array
outside of its bounds, or running out of memory or disk space. When a program encounters an
exceptional condition, it is important that it is identified and dealt with effectively. Exceptions
are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as
“out-of-range index” and “over flow” belong to the synchronous exceptions. The errors that are
caused by events beyond the control of the program are called asynchronous exceptions. The
proposed exception handling mechanism is designed to handle only synchronous exceptions.

The mechanism performs following tasks:


 Find the problem (Hit the exception).
 Inform that an error has occurred (Throw the exception).
 Receive the error information (Catch the expression).
 Take corrective actions (Handle the exceptions).
 The error handling code basically consists of two segments one to detect errors and to
throw exceptions, and other to catch the exceptions and to take appropriate actions.

Exception Handling Mechanism:


It is built upon three keywords, namely, try, throw and catch. The keyword try is used to
preface a block of statements which may generate exceptions. This block of statements is known
as try block.
When an exception is detected, it is thrown using a throw statement in the try block. A
catch block is defined by the keyword catch ‘catches’ the exception ‘thrown’ by the throw
statement in the try block, and handles it appropriately. If the type of object thrown matches the
arg type in the catch statement, then catch block is executed for handling the exception. If they
do not match the program is aborted with the help of the abort() function is invoked by default.
When no exception is detected and thrown, the control goes to the statement immediately after
the catch block. Most often exceptions are thrown by the functions that are invoked from within
the try blocks. The point at which the throw is executed is called the throw point. The general
format of code for this kind of relationship is shown below

Multiple catch statements:


It is possible that a program segment has more than one condition to throw an exception. In
such cases, we can associate more than one catch statement with a try as shown below:
try
{ // try block
}
catch(type1 arg)
134

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

{ // catch block1 }
catch(type2 arg)
{ // catch block2 }


catch(typeN arg)
{ // catch blockN }
Catch All Exceptions:
In some situations we may not be able to anticipate all possible types of exceptions we can force
a catch statement to catch all exceptions instead of a certain type alone. This is achieved as
follows:
catch (…)
{
// statement of processing all exceptions
}

5. Describe the various file modes and its syntax.


FILE
To handle large volumes of data, we need to use some devices such as floppy disk or hard
disk to store the data. The data is stored in these devices using the concept of files. A file is a
collection of related data stores in a particular area on the disk. Programs can be designed to
perform the read and write operations on these files.
A program involves either or both of the following kinds of data communication:
1. Data transfer between the console unit and the program.
2. Data transfer between the program and a disk file.
The I/O system of C++ uses file stream as an interface b/n the programs and the files.
The stream that supplies data to the program is known as input stream. The stream that receives
data from the program is known as output stream. In other words, the input stream reads data
from the file and the output stream writes data to the file.
Classes for File Stream Operations:
The I/O system of C++ contains a set of classes that define the file handling methods. These
include ifstream, ofstream and fstream. These classes are contained in the header file fstream.
This file should be included for performing file operations.
The filename is a string of characters that make up a valid filename for the operating system. It
may contain two parts, a primary name and an optional period with extension.

Examples:
Input.txt
Student
For opening a file, we must create a file stream and then link it to the filename. There are two
ways of opening a file:
 Using the constructor function of the class.
 Using the member function open() of the class.
135

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

The first method is useful when only one file in the stream is used. The second method is useful
when multiple files are managed using one stream.

File Modes:
The two methods that we discussed can also take two arguments instead of one. The second
argument will specify the file-mode. The general form of the function open() with two arguments
is:
stream-object.open (“filename”, mode);
Parameter Meaning
ios::app Append to end of file
ios::ate Go to end of file on opening
ios::binary Binary file
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
ios::out Open file for writing only
ios::trunc Delete the contents of the file if it exists
File pointers and their manipulations:
Each file has two associated pointers known as the file pointers. They are input pointer and
output pointer. The input pointer is used for reading the contents of a given file location and the
output pointer is used for writing to a given file location.

Read only mode:


Input pointer is automatically set at the beginning so that we can read the file from start.

Write only mode:


Existing contents are deleted and the output pointer is set at the beginning.

Append mode:
The output pointer is set to the end of file.
Functions for manipulations of file pointers:
seekg() – Moves input pointer to a specified location
seekp() – Moves output pointer to a specified location
tellg() – Gives the current position of the input pointer
tellp() – Gives the current position of the output pointer
Example:
infile.seekg(10);
moves file pointer to the byte number 10. The bytes are numbered from zero hence it points to
the 11th byte in the file.
ofstream out
out.open(“filename”,ios::app);
int p = out.tellp();
The above program will give the number of bytes in the file, since the file is opened in the
append mode.
136

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

6. Discuss the need for exception with try, catch and throw keywords.
Exceptions are run time anomalies or unusual conditions that a program may encounter
while executing. Anomalies might include conditions such as division by zero, access to an array
outside of its bounds, or running out of memory or disk space. When a program encounters an
exceptional condition, it is important that it is identified and dealt with effectively.
Exceptions are of two kinds, namely, synchronous exceptions and asynchronous
exceptions. Errors such as “out-of-range index” and “over flow” belong to the synchronous
exceptions. The errors that are caused by events beyond the control of the program are called
asynchronous exceptions. The proposed exception handling mechanism is designed to handle
only synchronous exceptions.
The mechanism performs following tasks:
 Find the problem (Hit the exception).
 Inform that an error has occurred (Throw the exception).
 Receive the error information (Catch the expression).
 Take corrective actions (Handle the exceptions).

The error handling code basically consists of two segments one to detect errors and to throw
exceptions, and other to catch the exceptions and to take appropriate actions.

Exception Handling Mechanism:


 It is built upon three keywords, namely, try, throw and catch.
 The keyword try is used to preface a block of statements which may generate exceptions.
 This block of statements is known as try block.
When an exception is detected, it is thrown using a throw statement in the try block.

A catch block is defined by the keyword catch ‘catches’ the exception ‘thrown’ by the throw
statement in the try block, and handles it appropriately. If the type of object thrown matches the
arg type in the catch statement, then catch block is executed for handling the exception. If they
do not match the program is aborted with the help of the abort() function is invoked by default.
When no exception is detected and thrown, the control goes to the statement immediately after
the catch block. Most often exceptions are thrown by the functions that are invoked from within
the try blocks.

The point at which the throw is executed is called the throw point.
The general format of code for this kind of relationship is shown below:
Example:
#include
void divide(int a, int b)
137

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

{
if(b!=0)
cout << “Result = “ << a/b;
else
throw(b);
}
void main()
{
cout << “Enter two numbers “;
int x,y;
cin >> x >> y;
try
{
divide(x,y);
}
catch(int i)
{
cout << “Error! Dividing by Zero “;
}

7. Explain the various forms of inheritance in C++ with necessary coding.


INHERITANCE:

• Inheritance is a mechanism of deriving a new class from a old class.


• It provides the concept of reusability
• By inheritance some or all the properties of a class can be derived in to another class.
• The class which provides the properties is called as base class and the class which
derives the properties is called as derived class.

Types:
There are five types of inheritance viz
1. Single level inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hybrid inheritance and
5. Hierarchical inheritance

Multiple inheritance:
It is a type of inheritance in which a class can inherit properties from more than one class.

Syntax:
Class derivedclass name : visibility mode baseclass1,visibilitmode baseclass2
{
body of the derived class;
};
138

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

visibility mode can be either private or public.

Example :
#include
#include
class bc1
{
protected:
int a;
public :

void get()
{
cout <<”\n enter the value for a\n”;
cin >>a;
}
};
class bc2
{
protected:

int b;
public;
void get1()
{
cout <<” \n enter the value for b \n”;
cin>>b;
}
};
class dc : public bc1, public bc2
{
public :

void show()
{
cout <<”The values of a and b are” ;
cout <<<”\n”<
}
};

void main()
{
clrscr();
dc d;
d.get();
139

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

d.get1();
d.show();
}

output:
Enter the value for a
20
Enter the value for b
30
The values of a and b are
20
30

140

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

UNIT-III

PART-A

1. Write down the definition of data structures? NOV DEC 2012


A data structure is a mathematical or logical way of organizing data in the memory that
consider not only the items stored but also the relationship to each other and also it is
characterized by accessing functions.

2. Binary Heap NOV DEC 2012 ,APRIL MAY 2009


The implementation we will use is known as a binary heap. Its use is so common for priority
queue implementations that when the word heap is used without a qualifier
Structure Property
A heap is a binary tree that is completely filled, with the possible exception of the bottom level,
which is filled from left to right. Such a tree is known as a complete binary tree

3. Define Algorithm?
Algorithm is a solution to a problem independent of programming language. It consist of set of
finite steps which, when carried out for a given set of inputs, produce the corresponding output
and terminate in a finite time.

4. What are the features of an efficient algorithm?


• Free of ambiguity
• Efficient in execution time
• Concise and compact
• Completeness
• Definiteness
• Finiteness

5. List down any four applications of data structures?

 Compiler design
 Operating System
 Database Management system
 Network analysis

6. What is meant by an abstract data type (ADT)?

141

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

An ADT is a set of operation. A useful tool for specifying the logical properties of a datatype is
the abstract data type.ADT refers to the basic mathematical concept that defines the datatype.
Eg.Objects such as list, set and graph along their operations can be viewed as ADT's.

7. What are the operations of ADT?

Union, Intersection, size, complement and find are the various operations of ADT.

8. What is meant by list ADT?

List ADT is a sequential storage structure. General list of the form a1, a2, a3.…., an and
the size of the list is 'n'. Any element in the list at the position I is defined to be ai, ai+1 the
successor of ai and ai-1 is the predecessor of ai.

9. What are the two parts of ADT?

• Value definition
• Operator definition

10. What is a Sequence?

A sequence is simply an ordered set of elements.A sequence S is sometimes written as


the enumeration of its elements,such as S =If S contains n elements,then length of S is n.

11. Define len(S),first(S),last(S),nilseq ?

len(S) is the length of the sequence S.


first(S) returns the value of the first element of S
last(S) returns the value of the last element of S
nilseq :Sequence of length 0 is nilseq .ie., contains no element.

12. What are the two basic operations that access an array?

Extraction:
Extraction operation is a function that accepts an array, a ,an index,i,and
returns an element of the array.
Storing:
Storing operation accepts an array , a ,an index i , and an element x.

13. Define Structure?

A Structure is a group of items in which each item is identified by its own identifier ,each of
which is known as a member of the structure.

14. Define Union ?

142

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Union is collection of Structures ,which permits a variable to be interpreted in several


different ways.

15. Define Automatic and External variables?

Automatic variables are variables that are allocated storage when the function is invoked.
External variables are variables that are declared outside any function and are allocated storage at
the point at which they are first encountered for the remeinder of the program’s execution.

16. What is a Stack? NOV DEC 2008

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.
The other name of stack is
Last-in -First-out list.

17. What are the two operations of Stack?

• _ PUSH
• _ POP

18. What is a Queue?

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 tems may be inserted at the other end called rear of
the queue.Queue is called as First–in-First-Out(FIFO).

19. What is a Priority Queue? NOV DEC 2010

Priority queue is a data structure in which the intrinsic ordering of the elements does
determine the results of its basic operations. Ascending and Descending priority queue are the
two types of Priority queue.

20. What is a linked list?

Linked list is a kind of series of data structures, which are not necessarily adjacent in
memory. Each structure contain the element and a pointer to a record containing its successor.

21. What is a doubly linked list?

In a simple linked list, there will be one pointer named as 'NEXT POINTER' to point the
next element, where as in a doubly linked list, there will be two pointers one to point the next
element and the other to point the previous element location.

143

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

22. Define double circularly linked list?

In a doubly linked list, if the last node or pointer of the list, point to the first element of
the list,then it is a circularly linked list.

23. What is a circular queue?

The queue, which wraps around upon reaching the end of the array is called as circular
queue.

24. Define max and min heap?


 A heap in which the parent has a larger key than the child's is called a max heap.
 A heap in which the parent has a smaller key than the child is called a min heap.

25. Write postfix from of the expression –A+B-C+D?


A-B+C-D+

26. Define Recursion?


Recursion is a function calling itself again and again.

27. What is a Fibonacci sequence?

Fibonacci sequence is the number of integers


0,1,1,2,3,5,8,13,21,34,……….
Each element in this sequence is the sum of the two preceding elements.

28.Name the two fields of Linked list?


• Info field
• Next field

29. What is a doubly linked list?


In a simple linked list, there will be one pointer named as 'NEXT POINTER' to point the
next element, where as in a doubly linked list, there will be two pointers one to point the next
element and the other to point the previous element location.

30. Name the three fields of Doubly Linked list?


• Info field
• Left field
• Right field

31. Define double circularly linked list?


In a doubly linked list, if the last node or pointer of the list, point to the first element of
the list,then it is a circularly linked list.

32. What is the need for the header?

144

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Header of the linked list is the first element in the list and it stores the number of
elements in the list. It points to the first data element of the list.

33. List three examples that uses linked list?


• Polynomial ADT
• Radix sort
• Multi lists

34. Give some examples for linear data structures?


• Stack
• Queue

35. How do you test for an empty queue?


To test for an empty queue, we have to check whether READ=HEAD where REAR is a pointer
pointing to the last node in a queue and HEAD is a pointer that pointer to the dummy header. In
the case of array implementation of queue, the condition to be checked for an empty queue is
READ

36. What are the postfix and prefix forms of the expression?
A+B*(C-D)/(P-R)
Postfix form: ABCD-*PR-/+
Prefix form: +A/*B-CD-PR

37. Explain the usage of stack in recursive algorithm implementation?


In recursive algorithms, stack data structures is used to store the return address when a recursive
call is encountered and also to store the values of all the parameters essential to the
current state of the procedure.

38. Write down the operations that can be done with queue data structure?
Queue is a first - in -first out list. The operations that can be done with queue are insert and
remove.

39. What is a circular queue?


The queue, which wraps around upon reaching the end of the array is called as circular queue.

40. Define max heap?


A heap in which the parent has a larger key than the child's is called a max heap.

41. Define min heap?


A heap in which the parent has a smaller key than the child is called a min heap.

UNIT-III

PART - B
145

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

1. How do you analyses an algorithm?

Algorithm analysis refers to the process of determining how much computing time and
storage that algorithms will require. In other words, it’s a process of predicting the resource
requirement of algorithms in a given environment. In order to solve a problem, there are many
possible algorithms. One has to be able to choose the best algorithm for the problem at hand
using some scientific method. To classify some data structures and algorithms as good, we need
precise ways of analyzing them in terms of resource requirement. The main resources are:
• Running Time
• Memory Usage
• Communication Bandwidth

Running time is usually treated as the most important since computational time is the most
precious resource in most problem domains.
There are two approaches to measure the efficiency of algorithms:
• Empirical: Programming competing algorithms and trying them on different instances.
• Theoretical: Determining the quantity of resources required mathematically (Execution
time, memory space, etc.) needed by each algorithm.

However, it is difficult to use actual clock-time as a consistent measure of an algorithm’s


efficiency, because clock-time can vary based on many things. For example,
• Specific processor speed
• Current processor load
• Specific data for a particular run of the program
o Input Size
o Input Properties
• Operating Environment

Accordingly, we can analyze an algorithm according to the number of operations required,


rather than according to an absolute amount of time involved. This can show how an algorithm’s
efficiency changes according to the size of the input.

Complexity Analysis
Complexity Analysis is the systematic study of the cost of computation, measured either in time
units or in operations performed, or in the amount of storage space required.
The goal is to have a meaningful measure that permits comparison of algorithms independent of
operating platform.
There are two things to consider:
• Time Complexity: Determine the approximate number of operations required to solve a
problem of size n.
• Space Complexity: Determine the approximate memory required to solve a problem of
size n. Complexity analysis involves two distinct phases:
• Algorithm Analysis: Analysis of the algorithm or data structure to produce a function T
(n) that describes the algorithm in terms of the operations performed in order to measure the
complexity of the algorithm.
146

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

• Order of Magnitude Analysis: Analysis of the function T (n) to determine the general
complexity category to which it belongs. There is no generally accepted set of rules for algorithm
analysis. However, an exact count of operations is commonly used.

Analysis Rules:
1. We assume an arbitrary time unit.
2. Execution of one of the following operations takes time 1:
• Assignment Operation
• Single Input/Output Operation
• Single Boolean Operations
• Single Arithmetic Operations
• Function Return
3. Running time of a selection statement (if, switch) is the time for the condition evaluation +
the maximum of the running times for the individual clauses in the selection.
4. Loops: Running time for a loop is equal to the running time for the statements inside the loop
* number of iterations.
The total running time of a statement inside a group of nested loops is the running time of the
statements multiplied by the product of the sizes of all the loops.
For nested loops, analyze inside out.
• Always assume that the loop executes the maximum number of iterations possible.
5. Running time of a function call is 1 for setup + the time for any parameter calculations + the
time required for the execution of the function body.

Examples:

1. int count(){
int k=0;
cout<< “Enter an integer”;
cin>>n;
for (i=0;i
k=k+1;
return 0;}
Time Units to Compute
-------------------------------------------------
1 for the assignment statement: int k=0
1 for the output statement.
1 for the input statement.
In the for loop:
1 assignment, n+1 tests, and n increments.
n loops of 2 units for an assignment, and an addition.
1 for the return statement.
-------------------------------------------------------------------

147

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

T (n)= 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)


2. int total(int n)
{
int sum=0;
for (int i=1;i<=n;i++)
sum=sum+1;
return sum;
}
Time Units to Compute
-------------------------------------------------
1 for the assignment statement: int sum=0
In the for loop:
1 assignment, n+1 tests, and n increments.
n loops of 2 units for an assignment, and an addition.
1 for the return statement.
-------------------------------------------------------------------
T (n)= 1+ (1+n+1+n)+2n+1 = 4n+4 = O(n)
3. void func()
{
int x=0;
int i=0;
int j=1;
cout<< “Enter an Integer value”;
cin>>n;
while (i
x++;
i++;
}
while (j
{
j++;
}
}
Time Units to Compute
-------------------------------------------------
1 for the first assignment statement: x=0;
1 for the second assignment statement: i=0;
1 for the third assignment statement: j=1;
1 for the output statement.
1 for the input statement.
In the first while loop:
n+1 tests
n loops of 2 units for the two increment (addition) operations
In the second while loop:
148

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

n tests
n-1 increments
-------------------------------------------------------------------
T (n)= 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5 = O(n)
4. int sum (int n)
{
int partial_sum = 0;
for (int i = 1; i <= n; i++)
partial_sum = partial_sum +(i * i * i);
return partial_sum;
}
Time Units to Compute
-------------------------------------------------
1 for the assignment.
1 assignment, n+1 tests, and n increments.
n loops of 4 units for an assignment, an addition, and two multiplications.
1 for the return statement.
-------------------------------------------------------------------
T (n)= 1+(1+n+1+n)+4n+1 = 6n+4 = O(n)

Formal Approach to Analysis


In the above examples we have seen that analysis so complex. However, it can be simplified by
using some formal approach in which case we can ignore initializations, loop control, and book
keeping.

For Loops: Formally


• In general, a for loop translates to a summation. The index and bounds of the summation are
the same as the index and bounds of the for loop.
• Suppose we count the number of additions that are done. There is 1 addition per iteration of
the loop, hence N additions in total.
Nested Loops: Formally
• Nested for loops translate into multiple summations, one for each for loop.
• Again, count the number of additions. The outer summation is for the outer for loop.

Consecutive Statements: Formally


• Add the running times of the separate blocks of your code

Conditionals: Formally
• If (test) s1 else s2: Compute the maximum of the running time for s1 and s2.

2. Explain how pointer are used to implement linked list structure.

A linked list is a data structure that is built from structures and pointers. It forms a chain
of"nodes" with pointers representing the links of the chain and holding the entire thing together.
149

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

A linked list can be represented by a diagram like this one: This linked list has four nodes in it,
each with a link to the next node in the series. The last node has a link to the special value
NULL, which any pointer (whatever its type) can point to, to show that it is the last link in the
chain. There is also another special pointer, called Start (also called head), which points to the
first link in the chain so that we can keep track of it.
Defining the data structure for a linked list
The key part of a linked list is a structure, which holds the data for each node (the name,
address, age or whatever for the items in the list), and, most importantly, a pointer to the next
node. Here we have given the structure of a typical node:
struct node
{ char name[20]; // Name of up to 20 letters
int age
float height; // In metres
node *nxt;// Pointer to next node
};
struct node *start_ptr = NULL;
The important part of the structure is the line before the closing curly brackets. This gives a
pointer to the next node in the list. This is the only case in C++ where you are allowed to refer to
a data type (in this case node) before you have even finished defining it!
We have also declared a pointer called start_ptr that will permanently point to the start of the
list. To start with, there are no nodes in the list, which is why start_ptr is set to NULL.

3. Explain various operation performed on the doubly linked list.


Doubly Linked Lists
That sounds even harder than a linked list! Well, if you've mastered how to do singly
linked lists, then it shouldn't be much of a leap to doubly linked lists A doubly linked list is one
where there are links from each node in both directions: You will notice that each node in the list
has two pointers, one to the next node and one to the previous one - again, the ends of the list are
defined by NULL pointers. Also there is no pointer to the start of the list. Instead, there is simply
a pointer to some position in the list that can be moved left or right. The reason we needed a start
pointer in the ordinary linked list is because, having moved on from one node to another, we
can't easily move back, so without the start pointer, we would lose track of all the nodes in the
list that we have already passed. With the doubly linked list, we can move the current pointer
backwards and forwards at will.

Creating Doubly Linked Lists

The nodes for a doubly linked list would be defined as follows:


struct node{
char name[20];
node *nxt; // Pointer to next node
node *prv; // Pointer to previous node
};
node *current;
150

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

current = new node;


current->name = "Fred";
current->nxt = NULL;
current->prv = NULL;
We have also included some code to declare the first node and set its pointers to NULL. It gives
the following situation:
We still need to consider the directions 'forward' and 'backward', so in this case, we will need to
define functions to add a node to the start of the list (left-most position) and the end of the list
(right-most position).
Adding a Node to a Doubly Linked List
void add_node_at_start (string new_name)
{ // Declare a temporary pointer and move it to the start
node *temp = current;
while (temp->prv != NULL)
temp = temp->prv;
// Declare a new node and link it in
node *temp2;
temp2 = new node;
temp2->name = new_name; // Store the new name in the node
temp2->prv = NULL; // This is the new start of the list
temp2->nxt = temp; // Links to current list
temp->prv = temp2;
}
void add_node_at_end ()
{ // Declare a temporary pointer and move it to the end
node *temp = current;
while (temp->nxt != NULL)
temp = temp->nxt;
// Declare a new node and link it in
node *temp2;
temp2 = new node;
temp2->name = new_name; // Store the new name in the node
temp2->nxt = NULL; // This is the new start of the list
temp2->prv = temp; // Links to current list
temp->nxt = temp2;
}

Here, the new name is passed to the appropriate function as a parameter. We'll go through the
function for adding a node to the right-most end of the list. The method is similar for adding a
node at the other end. Firstly, a temporary pointer is set up and is made to march along the list
until it points to last node in the list.
After that, a new node is declared, and the name is copied into it. The nxt pointer of this new
node is set to NULL to indicate that this node will be the new end of the list.
The prv pointer of the new node is linked into the last node of the existing list.
The nxt pointer of the current end of the list is set to the new node.
151

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

4. Give linked list implementation of stack operation.

Linked List Implementation of Stacks: the PUSH operation It’s very similar to the
insertion operation in a dynamic singly linked list. The only difference is that here you'll add the
new element only at the end of the list, which means addition can happen only from the TOP.
Since a dynamic list is used for the stack, the Stack is also dynamic, means it has no prior upper
limit set. So, we don't have to check for the Overflow condition at all!
In Step [1] we create the new element to be pushed to the Stack.
In Step [2] the TOP most element is made to point to our newly created element.
In Step [3] the TOP is moved and made to point to the last element in the stack, which is our
newly added element.

Linked List Implementation of Stacks: the POP Operation


This is again very similar to the deletion operation in any Linked List, but you can only delete
from the end of the list and only one at a time; and that makes it a stack. Here, we'll have a list
pointer, "target", which will be pointing to the last but one element in the List (stack). Every time
we POP, the TOP most element will be deleted and "target" will be made as the TOP most
element.
In step[1] we got the "target" pointing to the last but one node.
In step[2] we freed the TOP most element.
In step[3] we made the "target" node as our TOP most element.
Supposing you have only one element left in the Stack, then we won't make use of "target" rather
we'll take help of our "bottom" pointer. See how...
Algorithm:
 Step-1: If the Stack is empty then give an alert message "Stack Underflow" and quit; or
else proceed
 Step-2: If there is only one element left go to step-3 or else step-4
 Step-3: Free that element and make the "stack", "top" and "bottom" pointers point to
NULL and quit
 Step-4: Make "target" point to just one element before the TOP; free the TOP most
element;
make "target" as your TOP most element
Implementation:
struct node
{
int nodeval;
struct node *next;
}
struct node *stack = NULL; /*stack is initially empty*/
struct node *top = stack;
main()
152

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

{
int newvalue, delval;
..
push(newvalue);
..
delval = pop(); /*POP returns the deleted value from the stack*/
}
int pop( )
{
int pop_val = 0;
struct node *target = stack;
if(stack == NULL) /*step-1*/
cout<<"Stack Underflow";
else
{
if(top == bottom) /*step-2*/
{
pop_val = top -> nodeval; /*step-3*/
delete top;
stack = NULL;
top = bottom = stack;
}
else /*step-4*/
{
while(target->next != top) target = target ->next;
pop_val = top->nodeval;
delete top;
top = target;
target ->next = NULL;
}
}
return(pop_val);
}

5. What is a stack? Explain any two operations performed on a stack with required
algorithm.
Stacks
A simple data structure in which insertion and deletion occur at the same end, is termed (called)
a stack. It is a LIFO (Last In First Out) structure.
The operations of insertion and deletion are called PUSH and POP
Push - push (put) item onto stack
153

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Pop - pop (get) item from stack

Initial Stack Push(8) Pop


TOS=>
4
1
3
6
TOS=>
8
4
1
3
6
TOS=>
4
1
3
6
Our Purpose:
To develop a stack implementation that does not tie us to a particular data type or to a
particular implementation.
Implementation:
Stacks can be implemented both as an array (contiguous list) and as a linked list. We want a set
of operations that will work with either type of implementation: i.e. the method of
implementation is hidden and can be changed without affecting the programs that use them.

The Basic Operations:


Push()
{
if there is room {
put an item on the top of the stack
else
give an error message
}
}
Pop()
{
if stack not empty {
return the value of the top item
remove the top item from the stack
}
else {
give an error message
}
154

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

}
CreateStack()
{
remove existing items from the stack
initialise the stack to empty
}
Array Implementation of Stacks: The PUSH operation
Here, as you might have noticed, addition of an element is known as the PUSH operation. So, if
an array is given to you, which is supposed to act as a STACK, you know that it has to be a
STATIC
Stack; meaning, data will overflow if you cross the upper limit of the array. So, keep this in
mind.

Algorithm:
Step-1: Increment the Stack TOP by 1. Check whether it is always less than the Upper Limit of
the stack. If it is less than the Upper Limit go to step-2 else report -"Stack Overflow"
Step-2: Put the new element at the position pointed by the TOP
Implementation:
static int stack[UPPERLIMIT];
int top= -1; /*stack is empty*/
..
..
main()
{
..
..
push(item);
..
..
}
push(int item)
{
top = top + 1;
if(top < UPPERLIMIT)
stack[top] = item; /*step-1 & 2*/
else
cout<<"Stack Overflow";
}
Note:- In array implementation,we have taken TOP = -1 to signify the empty stack, as this
simplifies the implementation.
Array Implementation of Stacks: the POP operation
POP is the synonym for delete when it comes to Stack. So, if you're taking an array as the stack,
remember that you'll return an error message, "Stack underflow", if an attempt is made to Pop
an item from an empty Stack. OK.
Algorithm
155

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Step-1: If the Stack is empty then give the alert "Stack underflow" and quit; or else go to step-2
Step-2: a) Hold the value for the element pointed by the TOP
b) Put a NULL value instead
c) Decrement the TOP by 1
Implementation:
static int stack[UPPPERLIMIT];
int top=-1;
..
..
main()
{
..
..
poped_val = pop();
..
..
}
int pop()
{
int del_val = 0;
if(top == -1)
cout<<"Stack underflow"; /*step-1*/
else
{
del_val = stack[top]; /*step-2*/
stack[top] = NULL;
top = top -1;
}
return(del_val);
}
Note: - Step-2:(b) signifies that the respective element has been deleted.
Algorithm
Step-1: If the Stack is empty go to step-2 or else go to step-3
Step-2: Create the new element and make your "stack" and "top" pointers point to it and quit.
Step-3: Create the new element and make the last (top most) element of the stack to point to it
Step-4: Make that new element your TOP most element by making the "top" pointer point to it.
Implementation:
struct node{
int item;
struct node *next;
}
struct node *stack = NULL; /*stack is initially empty*/
struct node *top = stack;
main()
{
156

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

..
..
push(item);
..
}
push(int item)
{
if(stack == NULL) /*step-1*/
{
newnode = new node /*step-2*/
newnode -> item = item;
newnode -> next = NULL;
stack = newnode;
top = stack;
}
else
{
newnode = new node; /*step-3*/
newnode -> item = item;
newnode -> next = NULL;
top ->next = newnode;
top = newnode; /*step-4*/
}
}

6. State and explain the priority queue with example.


Priority Queue
- is a queue where each data has an associated key that is provided at the time of insertion.
- Dequeue operation deletes data having highest priority in the list
- One of the previously used dequeue or enqueue operations has to be modified
Example: Consider the following queue of persons where females have higher priority than
males (gender is the key to give priority).
Dequeue()- deletes Aster
Dequeue()- deletes Meron
Now the queue has data having equal priority and dequeue operation deletes the front element
like in the case of ordinary queues.
Dequeue()- deletes Abebe
Dequeue()- deletes Alemu
Thus, in the above example the implementation of the dequeue operation need to be modified.

7. Explain, with example the basic heap operations and write the algorithms for the same.
Heap Sort
Heap sort operates by first converting the list in to a heap tree. Heap tree is a binary tree
in which each node has a value greater than both its children (if any). It uses a process called
"adjust to accomplish its task (building a heap tree) whenever a value is larger than its parent.
157

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

The time complexity of heap sort is O(nlogn).


Algorithm:
1. Construct a binary tree
• The root node corresponds to Data[0].
• If we consider the index associated with a particular node to be i, then the left child of this node
corresponds to the element with index 2*i+1 and the right child corresponds to the element with
index 2*i+2. If any or both of these elements do not exist in the array, then the corresponding
child node does not exist either.
2. Construct the heap tree from initial binary tree using "adjust" process.
3. Sort by swapping the root value with the lowest, right most value and deleting the lowest,
right most value and inserting the deleted value in the array in it proper position.
Example: Sort the following list using heap sort algorithm.
5 8 2 4 1 3 9 7 6 0

158

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

UNIT - IV

PART – A
1. Define non-linear data structure?

Data structure which is capable of expressing more complex relationship than that of
physical adjacency is called non-linear data structure.

2. Define tree?

A tree is a data structure, which represents hierarchical relationship between


individual Data items.

3. Define child and parent of a tree.

The root of each subtree is said to be a child of ‘r’ and ‘r’ is the parent of each subtree
root.

4. Define leaf?

In a directed tree any node which has out degree o is called a terminal node or a leaf.

5. What is a Binary tree?

A Binary tree is a finite set of elements that is either empty or is partitioned into three
disjoint subsets. The first subset contains a single element called the root of the tree. The other
two subsets are themselves binary trees called the left and right sub trees.

6. What are the applications of binary tree?

Binary tree is used in data processing.


a. File index schemes
b. Hierarchical database management system

7. What is meant by traversing?

Traversing a tree means processing it in such a way, that each node is visited only once.

8. What are the different types of traversing?


The different types of traversing are
a.Pre-order traversal-yields prefix form of expression.
b. In-order traversal-yields infix form of expression.
c. Post-order traversal-yields postfix form of expression.

159

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

9. What are the two methods of binary tree implementation?

Two methods to implement a binary tree are,


a. Linear representation.
b. Linked representation

10. Define Graph?

A graph G consist of a nonempty set V which is a set of nodes of the graph, a set E which is the
set of edges of the graph, and a mapping from the set for edge E to a set of pairs of elements of
V.
It can also be represented as G=(V, E).

11. Define adjacent nodes?

Any two nodes which are connected by an edge in a graph are called adjacent nodes. For
Example, if and edge xÎE is associated with a pair of nodes (u,v) where u, v Î V, then we say that
the edge x connects the nodes u and v.

12.Name the different ways of representing a graph?

a. Adjacency matrix
b. Adjacency list

13. What are the two traversal strategies used in traversing a graph?

a. Breadth first search


b. Depth first search

14. What is an acyclic graph?

A simple diagram which does not have any cycles is called an acyclic graph.

15. Give some example of NP complete problems.

Hamiltonian circuit.
Travelling salesmen problems
16. What are AVL trees?

 An AVL tree is a binary search tree with a balancing condition. For every node in the tree
the heigh of the left and right subtrees can differ at most by 1.

160

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 The height of an empty tree is defined to be -1.It ensures that the depth of the tree is
O(log N)1

7. What is topological sort?

A topological sort is an ordering of vertices in a directed acyclic graph, such that if there
is a path from vi then vj appears after vi in the ordering.

18. What is single source shortest path problem?

Given as an input a weighted graph, G=(V,E) and a distinguished vertex, ’s’ find the shortest
weighted path from ‘s’ to every other vertex in G.

19. Mention some shortest –path problems.

 Unweighted shortest paths


 Dijikstra’s algorithm
 All-pairs shortest paths

20. What are the algorithms used to find the minimum spanning tree?

 Prim’s algorithm
 Kruskal’s algorit

21. Define complete binary tree.

It is a complete binary tree only if all levels, except possibly the last level have the
maximum number of nodes maximum. A complete binary tree of height ‘h’ has between 2h and
2h+1 – 1 node

22. Define binary search tree. Why it is preferred rather than the sorted linear array
and linked list?

Binary search tree is a binary tree in which key values of the left sub trees are lesser than
the root value and the key values of the right sub tree are always greater than the root value.
In linear array or linked list the values are arranged in the form of increasing or decreasing order.
If we want to access any element means, we have to traverse the entire list. But if we use BST,
the element to be accessed is greater or smaller than the root element means we can traverse
either the right or left sub tree and can access the element irrespective of searching the entire
tree.

23. Give various implementations of trees.

 Linear implementation
161

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 Linked list implementation

24. List out the application of trees.

 Ordered tree
 “C” – representation of tree

25. What is the difference between binary tree and binary search tree?

 Binary Tree Binary Search Tree


 A tree is said to be a binary tree if it A BST is binary tree in which the key values in the
left sub tree is lesser than the has at most two children root and key values of the right
sub tree is greater than the root value.
 It doesn’t have any order. It should be in order.

26. Show the maximum number of nodes in a binary tree of height H is 2H+1 – 1.

Consider H = 3
No. of nodes in a full binary tree = 2H+1 – 1
= 23+1 – 1
= 24 – 1
= 15 nodes
We know that a full binary tree with height h=3 has maximum 15 nodes.
Hence proved.

27. List out the cases involved in deleting a node from a binary search tree.

Case 1: Node to be deleted is a Leaf node


Case 2: Node to be deleted has one child
Case:3: Node to be deleted has two children.

28. A + (B-C)*D+(E*F), if the above arithmetic expression is represented using a


binary tree, Find the number of non-leaf nodes in the tree.

Expression tree is a binary tree in which non – leaf nodes are operators and the leaf nodes are
operands.
In the above example, we have 5 operators. Therefore the number of non-leaf nodes in the tree is
5.

29. What is a BST – binary search tree? (AUC NOV/ DEC 2009)

162

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Binary search tree is a binary tree in which key values of the left sub trees are lesser than
the root value and the key values of the right sub tree are always greater than the root value.

30. Define threaded Binary Tree. (AUC NOV/ DEC 2010)

A binary tree is threaded by making all right child pointers that would normally be null
point to the inorder successor of the node, and all left child pointers that would normally be null
point to the inorder predecessor of the node.

31 Write the advantages of threaded binary tree. (AUC NOV/ DEC 2009)
1. By doing threading we avoid the recursive method of traversing a Tree ,
which makes use of stack and consumes a lot of memory and time.
2. The node can keep record of its root

32. Write an algorithm to declare nodes of a tree structure. (AUC APR / MAY 2010)

Struct tree node


{
int element;
Struct tree *left;
Struct tree *right;
}

UNIT - IV

PART – B

1. Describe in detail about the implementation of trees

One way to implement a tree would be to have in each node, besides its data, a link to
each child of the node. However, since the number of children per node can vary so greatly and
is not known in advance, it might be infeasible to make the children direct links in the data

Node declarations for trees:

1 struct TreeNode
2{
3 Object element;
4 TreeNode *firstChild;
5 TreeNode *nextSibling;
6 };
163

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

structure, because there would be too much wasted space. The solution is simple: Keep the
children of each node in a linked list of tree nodes. The declaration is typical. Figure 4.4 shows
how a tree might be represented in this implementation. Horizontal arrows that point downward
are firstChild links. Arrows that go left to right are nextSibling links. Null links are not drawn,
because there are too many. In the tree of Figure 4.4, node E has both a link to a sibling (F) and a
link to a child (I), while some nodes have neither.

Tree Traversals with an Application


There are many applications for trees. One of the popular uses is the directory structure in many
common operating systems, including UNIX and DOS. Figure 4.5 is a typical directory in the
UNIX file system. The root of this directory is /usr. (The asterisk next to the name indicates that
/usr is itself a directory.) /usr has three children, mark, alex, and bill, which are themselves
directories. Thus, /usr contains three directories and no regular files. The filename
/usr/mark/book/ch1.r is obtained by following the leftmost child three times. Each / after the first
indicates an edge; the result is the full pathname. This hierarchical file system is very popular
because it allows users to organize their data logically. Furthermore, two files in different
directories can share the same name, because they must have different paths from the root and
thus have different pathnames. A directory in the UNIX file system is just a file with a list of all
its children, so the directories are structured almost exactly in accordance

164

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

with the type declaration above.1 Indeed, on some versions of UNIX, if the normal command to
print a file is applied to a directory, then the names of the files in the directory can be seen in the
output (along with other non-ASCII information). Suppose we would like to list the names of all
of the files in the directory. Our output format will be that files that are depth di will have their
names indented by di tabs. Our algorithm is given in Figure 4.6 as pseudocode. The recursive
function listAll needs to be started with a depth of 0 to signify no indenting for the root. This
depth is an internal bookkeeping variable, and is hardly a parameter that a calling routine should
be expected to know about. Thus, the default value of 0 is provided for depth. The logic of the
algorithm is simple to follow. The name of the file object is printed out with the appropriate
number of tabs. If the entry is a directory, then we process all children recursively, one by one.
These children are one level deeper, and thus need to be indented an extra space. This traversal
strategy is known as a preorder traversal. In a preorder traversal, work at a node is performed
before (pre) its children are processed. When this program is run, it is clear that line 1 is
executed exactly once per node, since each name is output once. Since line 1 is executed at most
once per node, line 2 must also be executed once per node. Furthermore, line 4 can be executed
at most once for each child of each node. But the number of children is exactly one less than the
number of nodes. Finally, the for loop iterates once per execution of line 4 plus once each time
the loop ends. Thus, the total amount of work is constant per node. If there are N file names to be
output, then the running time is O(N). Another common method of traversing a tree is the
postorder traversal. In a postorder traversal, the work at a node is performed after (post) its
children are evaluated. As an example, Figure 4.8 represents the same directory structure as
before, with the numbers in parentheses representing the number of disk blocks taken up by each
165

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

file. Since the directories are themselves files, they have sizes too. Suppose we would like to
calculate the total number of blocks used by all the files in the tree. The most natural way to do
this would be to find the number of blocks contained in the subdirectories /usr/mark (30),
/usr/alex (9), and /usr/bill (32). The total number of blocks is then the total in

the
subdirectories plus the one block used by /usr, for a total of 72. The pseudocode method size in
Figure 4.9 implements this strategy. If the current object is not a directory, then size merely
returns the number of blocks it uses in the current object. Otherwise, the number of blocks used
by the directory is added to the number of blocks (recursively) found in all the children.
2.What is a Binary Tree? Explain the implementation of a Binary Tree with an expression
tree
A binary tree is a tree in which no node can have more than two children. Figure 4.11
shows that a binary tree consists of a root and two subtrees, TL and TR, both of which could
possibly be empty. A property of a binary tree that is sometimes important is that the depth of an
average binary tree is considerably smaller than N. An analysis shows that the average depth is
O(√N), and that for a special type of binary tree, namely the binary search tree, the average
value of the depth is O(logN). Unfortunately, the depth can be as large as N − 1, as the example
in Figure 4.12 shows.

ch1.r 3
166

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

ch2.r 2
ch3.r 4
book 10
syl.r 1
fall 2
syl.r 5
spr 6
syl.r 2
sum 3
cop3530 12
course 13
junk 6
mark 30
junk 8
alex 9
work 1
grades 3
prog1.r 4
prog2.r 1
fall 9
prog2.r 2
prog1.r 7
grades 9
fall 19
cop3212 29
course 30
bill 32
/usr 72
Figure 4.10 Trace of the size function

167

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Implementation

Because a binary tree node has at most two children, we can keep direct links to them.
The declaration of tree nodes is similar in structure to that for doubly linked lists, in that a node
is a structure consisting of the element information plus two pointers (left and right) to other
nodes(Figure 4.13). We could draw the binary trees using the rectangular boxes that are
customary for linked lists, but trees are generally drawn as circles connected by lines, because
they are actually graphs. We also do not explicitly draw nullptr links when referring to trees,
because every binary tree with N nodes would require N + 1 nullptr links. Binary trees have
many important uses not associated with searching. One of the principal uses of binary trees is in
the area of compiler design, which we will now explore.

Example: Expression Trees

Figure 4.14 shows an example of an expression tree. The leaves of an expression tree are
operands, such as constants or variable names, and the other nodes contain operators. This
particular tree happens to be binary, because all the operators are binary, and although this is the
simplest case, it is possible for nodes to have more than two children. It is also possible for a
node to have only one child, as is the case with the unary minus operator. We can evaluate an
expression tree, T, by applying the operator at the root to the values struct BinaryNode

{
Object element; // The data in the node
BinaryNode *left; // Left child
BinaryNode *right; // Right child
};

168

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Figure 4.13 Binary tree node class (pseudocode)

obtained by recursively evaluating the left and right subtrees. In our example, the left subtree
evaluates to a + (b * c) and the right subtree evaluates to ((d * e) + f) * g. The entire tree
therefore represents (a + (b * c)) + (((d * e) + f) * g). We can produce an (overly parenthesized)
infix expression by recursively producing a parenthesized left expression, then printing out the
operator at the root, and finally recursively producing a parenthesized right expression. This
general strategy (left, node, right) is known as an inorder traversal; it is easy to remember
because of the type of expression it produces. An alternate traversal strategy is to recursively
print out the left subtree, the right subtree, and then the operator. If we apply this strategy to our
tree above, the output is a b c * + d e * f + g * +, which is easily seen to be the postfix
representation. This traversal strategy is generally known as a postorder traversal. A third
traversal strategy is to print out the operator first and then recursively print out the left and right
subtrees. The resulting expression, + + a * b c * + * d e f g, is the less useful prefix notation, and
the traversal strategy is a preorder traversal

3. Explain in detail about Breadth First Search

This strategy for searching a graph is known as breadth-first search. It operates by


processing vertices in layers: The vertices closest to the start are evaluated first, and the most
distant vertices are evaluated last. This is much the same as a level-order traversal for trees.
Given this strategy, we must translate it into code. Figure 9.15 shows the initial
configuration of the table that our algorithm will use to keep track of its progress. For each
vertex, we will keep track of three pieces of information. First, we will keep its distance from s
in the entry dv. Initially all vertices are unreachable except for s, whose path length is 0. The
entry in pv is the bookkeeping variable, which will allow us to print the actual paths. The entry
known is set to true after a vertex is processed. Initially, all entries are not known, including the
start vertex. When a vertex is marked known, we have

169

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Figure 9.14 Final Shortest Path


a guarantee that no cheaper path will ever be found, and so processing for that vertex is
essentially complete. The basic algorithm can be described in Figure 9.16. The algorithm n
below mimics the diagrams by declaring as known the vertices at distance d = 0, then d = 1,
then d = 2, and so on, and setting all the adjacent vertices w that still have dw = ∞to a distance
dw = d + 1.

Pseudocode for unweighted shortest-path algorithm

void Graph::unweighted( Vertex s )


{
for each Vertex v
{
v.dist = INFINITY;
v.known = false;
}
s.dist = 0;
for( int currDist = 0; currDist < NUM_VERTICES; currDist++ )
for each Vertex v
if( !v.known && v.dist == currDist )
{
v.known = true;
for each Vertex w adjacent to v
if( w.dist == INFINITY )
{
w.dist = currDist + 1;
w.path = v;
170

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

}
}
}

Figure 9.15 Initial configuration of table used in unweighted shortest-path computation

Figure 9.17 A bad case for unweighted shortest-path algorithm

using the above algorithmBy tracing back through the pv variable, the actual path can be
printed. We will seehow when we discuss the weighted case. The running time of the algorithm
is O(|V|2), because of the doubly nested for loops. An obvious inefficiency is that the outside
loop continues until NUM_VERTICES-1, even if all the vertices become known much earlier.
Although an extra test could be made to avoid this, it does not affect the worst-case running time,
as can be seen by generalizing what happens when the input is the graph in Figure 9.17 with start
vertex v9. We can remove the inefficiency in much the same way as was done for topological
sort. At any point in time, there are only two types of unknown vertices that have dv _=∞. Some
have dv = currDist, and the rest have dv = currDist + 1. Because of this extra structure, it is very
wasteful to search through the entire table to find a proper vertex. A very simple but abstract
solution is to keep two boxes. Box #1 will have the unknown vertices with dv = currDist, and
box #2 will have dv = currDist + 1.
The test to find an appropriate vertex v can be replaced by finding any vertex in box #1.
After updating w (inside the innermost if block), we can add w to box #2. After the outermost for
loop erminates, box #1 is empty, and box #2 can be transferred to box #1 for the next pass of the
for loop. We can refine this idea even further by using just one queue. At the start of the pass, the
171

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

queue contains only vertices of distance currDist. When we add adjacent vertices of distance
currDist + 1, since they enqueue at the rear, we are guaranteed that they will not be processed
until after all the vertices of distance currDist have been processed. After the last vertex at
distance currDist dequeues and is processed, the queue only contains vertices of distance
currDist + 1, so this process perpetuates. We merely need to begin the process by placing the
start node on the queue by itself.
The refined algorithm is shown in Figure 9.18. In the pseudocode, we have assumed that
the start vertex, s, is passed as a parameter. Also, it is possible that the queue might empty
prematurely, if some vertices are unreachable from the start node. In this case, a distance of
INFINITY will be reported for these nodes, which is perfectly reasonable. Finally, the known
data member is not used; once a vertex is processed it can never enter the queue again, so the fact
that it need not be reprocessed is implicitly marked. Thus, the known data member can be
discarded. Figure 9.19 shows how the values on the graph we have been using are changed
during the algorithm (it includes the changes that would occur to known if we had kept it). Using
the same analysis as was performed for topological sort, we see that the running time is O(|E| +
|V|), as long as adjacency lists are used.

4. Give the application of Depth First Search

Depth-first search is a generalization of preorder traversal. Starting at some vertex, v, we


process v and then recursively traverse all vertices adjacent to v. If this process is performed on a
tree, then all tree vertices are systematically visited in a total of O(|E|) time, since |E| = _(|V|). If
we perform this process on an arbitrary graph, we need to be careful to avoid cycles. To do this,
when we visit a vertex, v, we mark it visited, since now we have been there, and recursively call
depth-first search on all adjacent vertices that are not already marked. We implicitly assume that
for undirected graphs every edge (v, w) appears twice in the adjacency lists: once as (v, w) and
once as (w, v). The procedure in Figure 9.61 performs a depth-first search (and does absolutely
nothing else) and is a template for the general style. For each vertex, the data member visited is
initialized to false. By recursively calling the procedures only on nodes that have not been
visited, we guarantee that we do not loop indefinitely. If the graph is undirected and not
connected, or directed and not strongly connected, this strategy might fail to visit some nodes.
We then search for an unmarked node,

void Graph::dfs( Vertex v )


{
v.visited = true;
for each Vertex w adjacent to v
if( !w.visited )
dfs( w );
}
Template for depth-first search (pseudocode)

apply a depth-first traversal there, and continue this process until there are no unmarked nodes.2
Because this strategy guarantees that each edge is encountered only once, the total time to
perform the traversal is O(|E| + |V|), as long as adjacency lists are used.

172

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Implementation: Undirected Graphs

An undirected graph is connected if and only if a depth-first search starting from any
node visits every node. Because this test is so easy to apply, we will assume that the graphs we
deal with are connected. If they are not, then we can find all the connected components and
apply our algorithm on each of these in turn. As an example of depth-first search, suppose in the
graph of Figure 9.62 we start at vertex A. Then we mark A as visited and call dfs(B) recursively.
dfs(B) marks B as visited and calls dfs(C) recursively. dfs(C) marks C as visited and calls dfs(D)
recursively. dfs(D) sees both A and B, but both of these are marked, so no recursive calls are
made. dfs(D) also sees that C is adjacent but marked, so no recursive call is made there, and
dfs(D) returns back to dfs(C). dfs(C) sees B adjacent, ignores it, finds a previously unseen vertex
E adjacent, and thus calls dfs(E). dfs(E) marks E, ignores A and C, and returns to dfs(C). dfs(C)
returns to dfs(B). dfs(B) ignores both A and D and returns. dfs(A) ignores both D and E and
returns. (We have actually touched every edge twice, once as (v, w) and again as (w, v), but this
is really once per adjacency list entry.)
We graphically illustrate these steps with a depth-first spanning tree. The root of the
tree is A, the first vertex visited. Each edge (v, w) in the graph is present in the tree. If, when we
process (v, w), we find that w is unmarked, or if, when we process (w, v), we find that v is
unmarked, we indicate this with a tree edge. If, when we process (v, w), we find that w is already
marked, and when processing (w, v), we find that v is already marked, we draw a dashed line,
which we will call a back edge, to indicate that this “edge” is not really part of the tree. The
depth-first search of the graph in Figure 9.62 is shown in Figure 9.63. The tree will simulate the
traversal we performed. A preorder numbering of the tree, using only tree edges, tells us the
order in which the vertices were marked. If the graph is not connected, then processing all nodes
(and edges) requires several calls to dfs, and each generates a tree. This entire collection is a
depth-first spanning forest.

173

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

174

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

UNIT - V

PART A
1. What is maxheap? Apr/May,2010

If we want the elements in the more typical increasing sorted order, we can change the
ordering property so that the parent has a larger key than the child. it is called max heap.

2. What is divide and conquer strategy?

In divide and conquer strategy the given problem is divided into smaller problems and
solved recursively. The conquering phase consists of patching together the answers. Divide and
conquer is a very powerful use of recursion that we will see many times.

3. Differentiate between merge sort and quick sort? Apr/May,2011

Mergesort
1.Divide and conquer strategy
2.Partition by position
Quicksort
1. Divide and conquer strategy
2. Partition by value

4. Mention some methods for choosing the pivot element in quicksort?

1. Choosing first element


2. Generate random number
3. Median of three

5. What are the three cases that arise during the left to right scan in quicksort?

I and j cross each other


I and j do not cross each other
I and j points the same position

6. 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. It is a basic external sorting in which there
are two inputs and two outputs tapes.

7. Define multi way merge? Apr/May,2011

If we have extra tapes then we can expect to reduce the number of passes required to sort
175

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

8. Define polyphase merge?

The k-way merging strategy requires the use of 2 k tapes. This could be prohibitive for
some applications. It is possible to get

9. What is replacement selection?

We read as many records as possible and sort them. Writing the result to some tapes. This
seems like the best approach possible until one realizes that as soon as the first record is written
to a output tape the memory it used becomes available for another record. If the next record on
the input tape is larger than the record we have just output then it can be included in the item.
Using this we can give algorithm. This is called replacement selection.

10. What is sorting?

Sorting is the process of arranging the given items in a logical order. Sorting is an
example where the analysis can be precisely performed.

11. What is mergesort?

The mergesort algorithm is a classic divide and conquer strategy. The problem is divided
into two arrays and merged into single array

12. What are the properties involved in heapsort? Apr/May,2010

1.Structure property
2.Heap order property

13. Define articulation points.

If a graph is not biconnected, the vertices whose removal would disconnect the graph are
known as articulation points. An expression tree is a binary tree in which the operands are
attached as leaf nodes and operators become the internal nodes.

14. What is an acyclic graph?

A simple diagram which does not have any cycles is called an acyclic graph.

15. Give some example of NP complete problems.

176

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

 Hamiltonian circuit.
 Travelling salesmen problems

16. What are AVL trees?

An AVL tree is a binary search tree with a balancing condition.For every node in the tree
the heigh of the left and right subtrees can differ at most by 1.The height of an empty tree is
defined to be -1.It ensures that the depth of the tree is O(log N)

17. What is topological sort?

A topological sort is an ordering of vertices in a directed acyclic graph, such that if there
is a path from vi then vj appears after vi in the ordering.

18.What is single source shortest path problem?

Given as an input a weighted graph, G=(V,E) and a distinguished vertex,’s’ find the
shortest weighted path from ‘s’ to every other vertex in G.

19.Mention some shortest –path problems.

 Unweighted shortest paths


 Dijikstra’s algorithm
 All-pairs shortest paths

20. Define threaded Binary Tree. (AUC NOV/ DEC 2010)

A binary tree is threaded by making all right child pointers that would normally be null
point to the inorder successor of the node, and all left child pointers that would normally be null
point to the inorder predecessor of the node.

UNIT – V

PART – B

1. Discuss in detail about Insertion Sort

One of the simplest sorting algorithms is the insertion sort. Insertion sort consists of N−1
passes. For pass p=1 through N−1, insertion sort ensures that the elements in positions 0 through
p are in sorted order. Insertion sort makes use of the fact that elements in positions 0 through p−1
are already known to be in sorted order. Figure below shows a sample array after each pass of
insertion sort. Figure belowshows the general strategy. In pass p, we move the element in
position p left until its correct place is found among the first p+1 elements. The code implements
this strategy. Lines 11 to 14 implement that data movement without the explicit use of swaps.
177

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

The element in position p is moved to tmp, and all larger elements (prior to position p) are
moved one spot to the right. Then tmp is moved to the correct spot. This is the same technique
that was used in the implementation of binary heaps.

Insertion Sort After each Pass


1 /**
2 * Simple insertion sort.
3 */
4 template <typename Comparable>
5 void insertionSort( vector<Comparable> & a )
6{
7 for( int p = 1; p < a.size( ); ++p )
8{
9 Comparable tmp = std::move( a[ p ] );
10
11 int j;
12 for( j = p; j > 0 && tmp < a[ j - 1 ]; --j )
13 a[ j ] = std::move( a[ j - 1 ] );
14 a[ j ] = std::move( tmp );
15 }
16 }

Insertion sort routine

Implementation of Insertion Sort


In the STL, instead of having the sort routines take an array of comparable items as a
single parameter, the sort routines receive a pair of iterators that represent the start and
endmarker of a range. A two-parameter sort routine uses just that pair of iterators and presumes
that the items can be ordered, while a three-parameter sort routine has a function object as a third
parameter. Converting the algorithm in Figure 7.2 to use the STL introduces several issues. The
obvious issues are

1. We must write a two-parameter sort and a three-parameter sort. Presumably, the two
parameter sort invokes the three-parameter sort, with less<Object>{ } as the third parameter.

2. Array access must be converted to iterator access.


178

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

3. Line 11 of the original code requires that we create tmp, which in the new code will have type
Object.

The first issue is the trickiest because the template type parameters (i.e., the generic types) for
the two-parameter sort are both Iterator; however, Object is not one of the generic type
parameters. Prior to C++11, one had to write extra routines to solve this problem. As shown in
Figure 7.3, C++11 introduces decltype which cleanly expresses the intent. Algorithm shows the
main sorting code that replaces array indexing with use of the iterator, and that replaces calls to
operator< with calls to the lessThan function object. Observe that once we actually code the
insertionSort algorithm, every statement in the original code is replaced with a corresponding
statement in the new code that makes

1 /*
2 * The two-parameter version calls the three-parameter version,
3 * using C++11 decltype
4 */
5 template <typename Iterator>
6 void insertionSort( const Iterator & begin, const Iterator & end )
7{
8 insertionSort( begin, end, less<decltype(*begin)>{ } );
9}

Two-parameter sort invokes three-parameter sort via C++11 decltype


1 template <typename Iterator, typename Comparator>
2 void insertionSort( const Iterator & begin, const Iterator & end,
3 Comparator lessThan )
4{
5 if( begin == end )
6 return;
7
8 Iterator j;
9
10 for( Iterator p = begin+1; p != end; ++p )
11 {
12 auto tmp = std::move( *p );
13 for( j = p; j != begin && lessThan( tmp, *( j-1 ) ); --j )
14 *j = std::move( *(j-1) );
15 *j = std::move( tmp );
16 }
17 }

Three-parameter sort using iterators

179

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

straightforward use of iterators and the function object. The original code is arguably much
simpler to read, which is why we use our simpler interface rather than the STL interface when
coding our sorting algorithms.

Analysis of Insertion Sort

Because of the nested loops, each of which can take N iterations, insertion sort is O(N2).
Furthermore, this bound is tight, because input in reverse order can achieve this bound. A precise
calculation shows that the number of tests in the inner loop in is at most p + 1 for each value of
p.

Summing over all p gives a total of On the other hand, if the input is presorted, the running time
is O(N), because the test in the inner for loop always fails immediately. Indeed, if the input is
almost sorted (this term will be more rigorously defined in the next section), insertion sort will
run quickly. Because of this wide variation, it is worth analyzing the average-case behavior of
this algorithm. It turns out that the average case is _(N2) for insertion sort, as well as for a variety
of other sorting algorithms.

2. Give a brief note on Merge Sort

We now turn our attention to mergesort. Mergesort runs in O(N logN) worst-case
running time, and the number of comparisons used is nearly optimal. It is a fine example of a
recursive algorithm. The fundamental operation in this algorithm is merging two sorted lists.
Because the lists are sorted, this can be done in one pass through the input, if the output is put in
a third list. The basic merging algorithm takes two input arrays A and B, an output array C, and
three counters, Actr, Bctr, and Cctr, which are initially set to the beginning of their respective
arrays. The smaller of A[Actr] and B[Bctr] is copied to the next entry in C, and the appropriate
counters are advanced. When either input list is exhausted, the remainder of the other list is
copied to C. An example of how the merge routine works is provided for the following input.

If the array A contains 1, 13, 24, 26, and B contains 2, 15, 27, 38, then the algorithm proceeds as
follows: First, a comparison is done between 1 and 2. 1 is added to C, and then 13 and 2 are
compared.

180

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

2 is added to C, and then 13 and 15 are compared.

13 is added to C, and then 24 and 15 are compared. This proceeds until 26 and 27 are compared.

26 is added to C, and the A array is exhausted.

The remainder of the B array is then copied to C.

The time to merge two sorted lists is clearly linear, because at most N−1 comparisons are made,
where N is the total number of elements. To see this, note that every comparison adds an element
to C, except the last comparison, which adds at least two. The mergesort algorithm is therefore
easy to describe. If N = 1, there is only one element to sort, and the answer is at hand. Otherwise,
recursively mergesort the first half and the second half. This gives two sorted halves, which can
then be merged together using the merging algorithm described above. For instance, to sort the
eight-element array 24, 13, 26, 1, 2, 27, 38, 15, we recursively sort the first four and last four
elements, obtaining 1, 13, 24, 26, 2, 15, 27, 38. Then we merge the two halves as above,
obtaining the final list 1, 2, 13, 15, 24, 26, 27, 38. This algorithm is a classic divide-and-conquer
strategy. The problem is divided into smaller problems and solved recursively. The conquering
phase consists of patching together the answers. Divide-and-conquer is a very powerful use of
181

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

recursion that we will see many times. An implementation of mergesort is provided. The one-
parameter mergeSort is just a driver for the four-parameter recursive mergeSort. The merge
routine is subtle. If a temporary array is declared locally for each recursive call of merge, then
there could be logN temporary arrays active at any point. A close examination shows that since
merge is the last line of mergeSort, there only needs to be one

1 /**
2 * Mergesort algorithm (driver).
3 */
4 template <typename Comparable>
5 void mergeSort( vector<Comparable> & a )
6{
7 vector<Comparable> tmpArray( a.size( ) );
8
9 mergeSort( a, tmpArray, 0, a.size( ) - 1 );
10 }
11
12 /**
13 * Internal method that makes recursive calls.
14 * a is an array of Comparable items.
15 * tmpArray is an array to place the merged result.
16 * left is the left-most index of the subarray.
17 * right is the right-most index of the subarray.
18 */
19 template <typename Comparable>
20 void mergeSort( vector<Comparable> & a,
21 vector<Comparable> & tmpArray, int left, int right )
22 {
23 if( left < right )
24 {
25 int center = ( left + right ) / 2;
26 mergeSort( a, tmpArray, left, center );
27 mergeSort( a, tmpArray, center + 1, right );
28 merge( a, tmpArray, left, center + 1, right );
29 }
30 }
Mergesort routines

temporary array active at any point, and that the temporary array can be created in the public
mergeSort driver. Further, we can use any part of the temporary array; we will use the same
portion as the input array a.

Analysis of Mergesort

Mergesort is a classic example of the techniques used to analyze recursive routines: We


have to write a recurrence relation for the running time. We will assume that N is a power of 2 so
182

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

that we always split into even halves. For N = 1, the time to mergesort is constant, which we will
denote by 1. Otherwise, the time to mergesort N numbers is equal to the
1 /**
2 * Internal method that merges two sorted halves of a subarray.
3 * a is an array of Comparable items.
4 * tmpArray is an array to place the merged result.
5 * leftPos is the left-most index of the subarray.
6 * rightPos is the index of the start of the second half.
7 * rightEnd is the right-most index of the subarray.
8 */
9 template <typename Comparable>
10 void merge( vector<Comparable> & a, vector<Comparable> & tmpArray,
11 int leftPos, int rightPos, int rightEnd )
12 {
13 int leftEnd = rightPos - 1;
14 int tmpPos = leftPos;
15 int numElements = rightEnd - leftPos + 1;
16
17 // Main loop
18 while( leftPos <= leftEnd && rightPos <= rightEnd )
19 if( a[ leftPos ] <= a[ rightPos ] )
20 tmpArray[ tmpPos++ ] = std::move( a[ leftPos++ ] );
21 else
22 tmpArray[ tmpPos++ ] = std::move( a[ rightPos++ ] );
23
24 while( leftPos <= leftEnd ) // Copy rest of first half
25 tmpArray[ tmpPos++ ] = std::move( a[ leftPos++ ] );
26
27 while( rightPos <= rightEnd ) // Copy rest of right half
28 tmpArray[ tmpPos++ ] = std::move( a[ rightPos++ ] );
29
30 // Copy tmpArray back
31 for( int i = 0; i < numElements; ++i, --rightEnd )
32 a[ rightEnd ] = std::move( tmpArray[ rightEnd ] );
33 }

merge routine

time to do two recursive mergesorts of size N/2, plus the time to merge, which is linear. The
following equations say this exactly:
T(1) = 1
T(N) = 2T(N/2) + N
This is a standard recurrence relation, which can be solved several ways.We will show two
methods. The first idea is to divide the recurrence relation through by N. The reason for doing
this will become apparent soon. This yields

183

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Now add up all the equations. This means that we add all of the terms on the left-hand
side and set the result equal to the sum of all of the terms on the right-hand side. Observe that the
term T(N/2)/(N/2) appears on both sides and thus cancels. In fact, virtually all the terms appear
on both sides and cancel. This is called telescoping a sum. After everything is added, the final
result is

because all of the other terms cancel and there are logN equations, and so all the 1s at the
end of these equations add up to logN. Multiplying through by N gives the final answer.

Notice that if we did not divide through by N at the start of the solutions, the sum would
not telescope. This is why it was necessary to divide through by N. An alternative method is to
substitute the recurrence relation continually on the righthand side. We have

184

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

The choice of which method to use is a matter of taste. The first method tends to produce scrap
work that fits better on a standard 81/2 × 11 sheet of paper leading to fewer mathematical errors,
but it requires a certain amount of experience to apply. The second method is more of a brute-
force approach. Recall that we have assumed N = 2k. The analysis can be refined to handle cases
when N is not a power of 2. The answer turns out to be almost identical (this is usually the case).
Although mergesort’s running time is O(N logN), it has the significant problem that merging two
sorted lists uses linear extra memory. The additional work involved in copying to the temporary
array and back, throughout the algorithm, slows the sort considerably. This copying can be
avoided by judiciously switching the roles of a and tmpArray at alternate levels of the recursion.
A variant of mergesort can also be implemented nonrecursively.

The running time of mergesort, when compared with other O(N logN) alternatives,
depends heavily on the relative costs of comparing elements and moving elements in the array
(and the temporary array). These costs are language dependent. For instance, in Java, when
performing a generic sort (using a Comparator), an element comparison can be expensive
(because comparisons might not be easily inlined, and thus the overhead of dynamic dispatch
could slow things down), but moving elements is cheap (because they are reference assignments,
rather than copies of large objects). Mergesort uses the lowest number of comparisons of all the
popular sorting algorithms, and thus is a good candidate for general-purpose sorting in Java. In
fact, it is the algorithm used in the standard Java library for generic sorting. On the other hand, in
classic C++, in a generic sort, copying objects can be expensive if the objects are large, while
comparing objects often is relatively cheap because of the ability of the compiler to aggressively
perform inline optimization. In this scenario, it might be reasonable to have an algorithm use a
few more comparisons, if we can also use significantly fewer data movements

3. Explain in detail about Quick Sort

quicksort has been the fastest known generic sorting algorithm in practice. Its average
running time is O(N logN). It is very fast, mainly due to a very tight and highly optimized inner
loop. It has O(N2) worst-case performance, but this can be made exponentially unlikely with a
little effort. By combining quicksort quicksort has historically been the fastest known generic
sorting algorithm in practice. Its average running time is O(N logN). It is very fast, mainly due to
a very tight and highly optimized inner loop. It has O(N2) worst-case performance, but this can
be made exponentially unlikely with a little effort. By combining quicksort
1 template <typename Comparable>
2 void SORT( vector<Comparable> & items )
3{
185

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

4 if( items.size( ) > 1 )


5{
6 vector<Comparable> smaller;
7 vector<Comparable> same;
8 vector<Comparable> larger;
9
10 auto chosenItem = items[ items.size( ) / 2 ];
11
12 for( auto & i : items )
13 {
14 if( i < chosenItem )
15 smaller.push_back( std::move( i ) );
16 else if( chosenItem < i )
17 larger.push_back( std::move( i ) );
18 else
19 same.push_back( std::move( i ) );
20 }
21
22 SORT( smaller ); // Recursive call!
23 SORT( larger ); // Recursive call!
24
25 std::move( begin( smaller ), end( smaller ), begin( items ) );
26 std::move( begin( same ), end( same ), begin( items ) + smaller.size( ) );
27 std::move( begin( larger ), end( larger ), end( items ) - larger.size( ) );
28 }
29 }

Simple recursive sorting algorithm

respectable on most inputs. In fact, if the list contains large numbers of duplicates with relatively
few distinct items, as is sometimes the case, then the performance is extremely good. The
algorithm we have described forms the basis of the quicksort. However, by making the extra
lists, and doing so recursively, it is hard to see how we have improved upon mergesort. In fact,
so far, we really haven’t. In order to do better, we must avoid using significant extra memory and
have inner loops that are clean. Thus quicksort is commonly written in a manner that avoids
creating the second group (the equal items), and the algorithm has numerous subtle details that
affect the performance; therein lies the complications. We now describe the most common
implementation of quicksort—“classic quicksort,” in which the input is an array, and in which no
extra arrays are created by the algorithm. The classic quicksort algorithm to sort an array S
consists of the following four easy steps:

1. If the number of elements in S is 0 or 1, then return.


2. Pick any element v in S. This is called the pivot.
3. Partition S − {v} (the remaining elements in S) into two disjoint groups: S1 = {x}
S − {v}|x ≤ v}, and S2 = {x ∈ S − {v}|x ≥ v}.
4. Return {quicksort(S1) followed by v followed by quicksort(S2)}.
186

Visit : www.EasyEngineeering.net
Visit : www.EasyEngineeering.net

Since the partition step ambiguously describes what to do with elements equal to the pivot, this
becomes a design decision. Part of a good implementation is handling this case as efficiently as
possible. Intuitively, we would hope that about half the elements that are equal to the pivot go
into S1 and the other half into S2, much as we like binary search trees to be
balanced.Thefigureshows the action of quicksort on a set of numbers. The pivot is chosen (by
chance) to be 65. The remaining elements in the set are partitioned into two smaller sets.
Recursively sorting the set of smaller numbers yields 0, 13, 26, 31, 43, 57 (by rule 3of
recursion). The set of large numbers is similarly sorted. The sorted arrangement of theentire set
is then trivially obtained.It should be clear that this algorithm works, but it is not clear why it is
any fasterthan mergesort. Like mergesort, it recursively solves two subproblems and requires
linear additional work (step 3), but, unlike mergesort, the subproblems are not guaranteed to be
of equal size, which is potentially bad. The reason that quicksort is faster is that the partitioning
step can actually be performed in place and very efficiently. This efficiency more than makes up
for the lack of equal-sized recursive calls. The algorithm as described so far lacks quite a few
details, which we now fill in. There are many ways to implement steps 2 and 3; the method
presented here is the result
of extensive analysis and empirical study and represents a very efficient way to implement
quicksort. Even the slightest deviations from this method can cause surprisingly bad
results.

-----------------------------------)))000000000000((((((-----------------------------------------

QUESTION BANK

UNIT – I
DATA ABSTRACTION & OVERLOADING

PART A
1.What are the features of Object Oriented P rogramming?
2.List out some of the benefits of OOP.
3.What is the use of scope resolution operator?
4.List out the advantages of new operator over malloc ().
5.What are the control structures used in C++?
6.State the difference between structures and class.
7.Define a class.
8.List the access modes used within a class.
9.What are the characteristics of member functions?
10.What are the properties of a static data member?
11.What are the properties of a static member function?
12.How can objects be used as function arguments?
187

Visit : www.EasyEngineeering.net

You might also like