You are on page 1of 10

FACULTY OF COMPUTER SCIENCE & MULTIMEDIA

Program : Bachelor of Information Technology (Hons.)

Course : Data Structure and Algorithm


Course Code : BIT 243

Submitted by : Anish Nepal

Year/
Semester : II Year /III Semester

Assessment : Assignment

Weightage : 20 Marks

Date : 11thMay2020–Monday

LCID : LC00017000505
Course ID: 243 DATA STRUCTURE AND ALGORITHM

1. Define In-order, post-order and pre-order using suitable example.


Differentiate between algorithm and pseudo code with suitable
example.

A tree traversal is a method of visiting every node in the tree by visit, we mean that some
type of operation is perform for example; we may wish to point the content of the node.
There are three ways which we use to traverse a tree –
I. In-order Traversal
II. Pre-order Traversal
III. Post-order Traversal
They are described below:

I. In-order Traversal (Left, Root, and Right): In this case, the root of each sub trees
has been traversed but before the traversal of its right sub trees begins.
Algorithm:
Step 1: Visit the left.
Step 2: Visit the root.
Step 3: Visit the right sub tree

For example:

We start from A, and following


in-order traversal, we move to its
left sub tree B. B is also traversed
in-order. The process goes on
until all the nodes are visited. The
output of In-order traversal of this
tree will be:
D→B→E→A→F→C→G

II. Pre-Order Traversal (Root, Left and Right): In this case, each root node is visited
before its left and right sub trees are traversed. It is also called back tracking.
Algorithm:
Step 1: Visit root.
Step 2: Visit left sub trees.
Step 3: Visit right sub trees

Anish Nepal P a g e 2 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

For example:

We start from A, and following


pre-order traversal, we first visit a
itself and then move to its left sub
tree B. B is also traversed pre-
order. The process goes on until all
the nodes are visited. The output of
pre-order traversal of this tree will
be −

A→B→D→E→C→F→G

III. Post-Order Traversal (Left, right and root): In this case, each root is visited after
its left and right sub trees.
Algorithm:
Step 1: Visit left sub trees.
Step 2: Visit right sub trees.
Step 3: Visit root.

For Example:

We start from A, and following


Post-order traversal, we first visit
the left sub tree B. B is also
traversed post-order. The process
goes on until all the nodes are
visited. The output of post-order
traversal of this tree will be −

D→E→B→F→G→C→A

Anish Nepal P a g e 3 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

Differences Between algorithm and pseudo code with example are as follows:

ALGORITHM PSEUDO CODE


An algorithm is defined as a well-defined A pseudo code is one of the methods that
sequence of steps that provides a solution can be used to represent an algorithm.
for a given problem.

It is quite hard to understand. It is easy to interpret.

Algorithm has no specific restriction. Pseudo code has constructs similar to


programming languages and follows
similar semantics.

Algorithms can be written in natural Pseudo code is written in a format that is


language. closely related to high level programming
language structures.

Algorithm are base for writing Pseudo code can be said as the base for
programming logic. writing algorithms.

Mathematical notations are rarely used, It is written in natural language and


mathematical notations.

For example: For example: If student's grade is greater


Step 1: Start than or equal to 60
Step 2: add num1 and num2. Print "passed"
Step 3: Display sum. else
Step 4: Stop. Print "failed"

Anish Nepal P a g e 4 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

2. What do you mean by Stack? Define Stack as ADT.

A stack is an ordered list in which all insertions and deletions are made at one end, called
the top. Or Stack is abstract data type with a bounded (predefined) capacity.
Stack is an ordered list of similar data type. Stack is said to be in overflow state when it is
completely full and is said to be in underflow state if it is completely empty.
Example: In computer programming processing of functions calls and their termination
uses stack. Stack is used to remember the place where the call was made, so that it can
return there after the function is complete.
The following diagram depicts a stack and its operations –

Stack as ADT

ADT is a data type, just like integers and Booleans primitive data types. An ADT consist
not only of operations, but also of values of the underlying data and of constraints on the
operations.
A constrains for a stack would be that each pop always returns the most recently pushed
item that has not been popped yet.
The actual implementation for an ADT is abstracted away, and we don’t have to care
about. So, how a stack is actually implemented is not that important. A stack could be
and often is implemented behind the scenes using a dynamic array, but it could be
implemented with a linked list instead. It really doesn’t matter.

Anish Nepal P a g e 5 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

The bottom line is, when we say the stack data structure, we refer to how stacks are
implemented and arrangement in the memory. But, when we say the stack ADT, we refer
to the stack data type which has set of defined operations, the operations constrains, and
the possible values.
Stack operates in LIFO order. It has its own attributes. It has its own functions for
operations. The representations of those attributes are hidden from, and of no concern to
the application code. For e.g.: PUSH(S,x) is enough to push an element x to the top of
stack S, without the actual function being defined inside the main function. This is the
main concept of ADTs. Hence stack is an abstract data type.

Anish Nepal P a g e 6 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

3. Justify the statement: “To write an efficient program, we should know


about data structures and algorithms”.
Well starting off, Data Structure is a way of collecting and organizing data in a way that
we can perform operations on these data in an effective way.
It’s about rendering data elements in terms of some relationship, for better organization
and storage.
Similarly, an algorithm is a well-defined collection of clear and simple instruction of
definite and effectively computable operation that well executed produce a result and stop
executing at same points in a finite amount of time rather than just going on and on
infinite.

Well, I agree with the statement that, “To write an efficient program, we should know
about data structures and algorithms”. Every program or software are built on the basis of
any algorithm and data structure. Data structure helps to organize and collects data which
helps any program to be more efficient and reliable. Whereas, an algorithm is a step by
step process which is regardless the base of any program. For example, for any project
either building houses or computer application, first there must be plan and vision. First
the blue print (which is an algorithm) is prepared which helps in building the project
efficiently and all the data and information regarding that project is collected (which is
data structure) and then the further process is proceed. Any program coded in any
programming language(c++, c, python, JAVA) used algorithm to encode and decode
codes and either those codes are integers or character, that is classified by Data structure.

Data structure and algorithms help in understanding the nature of the problem at a deeper
level and thereby a better understanding of the world. What makes any program efficient?
A program without problems, user friendly, dynamic in nature and so on. Data Structure
and algorithm solve the problem more efficiently. Every program have problems and
bugs, and these problems can make programmer hectic to solve. So, data structure and
algorithm helps to prevent those problems in first place and help to easily solve that
problems in second place. Similarly, data structure and algorithm Use right tool to solve
the problems. Many data structures like stack, queue, functions, array, etc. are used in
every programming language and with the help of these data types, a program becomes
efficient. Whenever the coding goes wrong in middle of programming, then an algorithm
helps to short out that problem. We can say that, every program and every programming
language to build that program uses data structures and algorithm. Without these,
programming become dull and worthless.

So, to wrap up with the fact that, data structure and algorithm are the two pillars of any
program. They helps to build program efficient and reliable. Not only, it simplifies
problems but also it provides surety for future.

Anish Nepal P a g e 7 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

4. Define stack as an ADT. Explain the condition that is to be checked


for Pop operation.

A stack is an ordered list in which all insertions and deletions are made at one end, called
the top. Or Stack is abstract data type with a bounded (predefined) capacity.

ADT (Abstract Data Type) is a data type, just like integers and Booleans primitive data
types. An ADT consist not only of operations, but also of values of the underlying data
and of constraints on the operations.
A constrains for a stack would be that each pop always returns the most recently pushed
item that has not been popped yet.
The actual implementation for an ADT is abstracted away, and we don’t have to care
about. So, how a stack is actually implemented is not that important. A stack could be
and often is implemented behind the scenes using a dynamic array, but it could be
implemented with a linked list instead. It really doesn’t matter.

The bottom line is, when we say the stack data structure, we refer to how stacks are
implemented and arrangement in the memory. But, when we say the stack ADT, we refer
to the stack data type which has set of defined operations, the operations constrains, and
the possible values.
Stack operates in LIFO order. It has its own attributes. It has its own functions for
operations. The representations of those attributes are hidden from, and of no concern to
the application code. For e.g.: PUSH(S,x) is enough to push an element x to the top of
stack S, without the actual function being defined inside the main function. This is the
main concept of ADTs. Hence stack is an abstract data type.

Every time an element is added, it goes on the top of the stack and the only element that
can be removed is the element that is at the top of the stack, just like a pile of objects.

Pop operation:

Anish Nepal P a g e 8 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

Accessing the content while removing it from the stack, is known as a pop operation. In
array implementation of pop () operation the data element is not actually removed,
instead top is decremented to lower position in the stack to point to next value.

A Pop operation may involve the following steps −

 Step 1 − Checks if the stack is empty.

 Step 2 − If the stack is empty, produces an error and exit.

 Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.

 Step 4 − Decreases the value of top by 1.

 Step 5 − Returns success.

Anish Nepal P a g e 9 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM

Algorithm for Pop Operation

A simple algorithm for Pop operation can be derived as follows –

begin procedure pop: stack

if stack is empty
return null
endif

data ← stack[top]
top ← top - 1
return data

end procedure

sev

Anish Nepal P a g e 10 | 10

You might also like