Professional Documents
Culture Documents
Year/
Semester : II Year /III Semester
Assessment : Assignment
Weightage : 20 Marks
Date : 11thMay2020–Monday
LCID : LC00017000505
Course ID: 243 DATA STRUCTURE AND ALGORITHM
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:
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:
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:
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 are base for writing Pseudo code can be said as the base for
programming logic. writing algorithms.
Anish Nepal P a g e 4 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM
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
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
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.
Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
Anish Nepal P a g e 9 | 10
Course ID: 243 DATA STRUCTURE AND ALGORITHM
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