Professional Documents
Culture Documents
Data StructureT1
Data StructureT1
Data Structure
In linear data structure, single level is Whereas in non-linear data structure, multiple
2.
involved. levels are involved.
In a linear data structure, memory is not While in a non-linear data structure, memory
5.
utilized in an efficient way. is utilized in an efficient way.
Introduction
In computer science, the time complexity of an algorithm quantifies the amount of time taken by
an algorithm to run as a function of the length of the string representing the input.
Big O notation
The time complexity of an algorithm is commonly expressed using big O notation, which
excludes coefficients and lower order terms. When expressed this way, the time complexity
is said to be described asymptotically, i.e as the input size goes to infinity.
For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n,
the asymptotic time complexity is O(n3). More on that later.
Few more Examples:
1 = O(n)
n = O(n2)
log(n) = O(n)
2 n + 1 = O(n)
O(1) Constant Time:
An algorithm is said to run in constant time if it requires the same amount of time regardless
of the input size.
Examples array: accessing any element
fixed-size stack: push and pop methods
fixed-size queue: enqueue and dequeue methods
A Short Guide to Written Exam 4 Data Structure
O(n) Linear Time
An algorithm is said to run in linear time if its time execution is directly proportional to the
input size, i.e. time grows linearly as input size increases.Consider the following examples,
below I am linearly searching for an element, this has a time complexity of O(n).
More Examples:
Array: Linear Search, Traversing, Find minimum etc
ArrayList: contains method
Queue: contains method
O(log n) Logarithmic Time:
An algorithm is said to run in logarithmic time if it’s time execution is proportional to the
logarithm of the input size.
Example: Binary Search
Recall the twenty question game - the task is to guess the value of a hidden number in an
interval. Each time you make a guess, you are told whether your guess is too high or too low.
Twenty questions game implies a strategy that uses your guess number to halve the interval
size. This is an example of the general problem-solving method known as binary search
O(n2) Quadratic Time
An algorithm is said to run in quadratic time if it’s time execution is proportional to the
square of the input size.
Examples:
Bubble Sort
Selection Sort
Insertion Sort
[see more details cloud it book data structure chapter]
Stack:
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure
that allows adding and removing elements in a particular order. 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.
push(2) 2 -
push(1) 2,1 -
pop 2 1
push(3) 2,3 1
push(2) 2,3,2 1
pop 2 1,2,3
pop - 1,2,3,2
push(2) 2 1,2,3,2
pop - 1,2,3,2,2
A Short Guide to Written Exam 7 Data Structure
11. What are Infix, prefix, Postfix notations?
Infix notation: X + Y – Operators are written in-between their operands. This is the
usual way we write expressions. An expression such as A*(B+C)/D
Postfix notation (also known as “Reverse Polish notation”): X Y + Operators are
written after their operands. The infix expression given above is equivalent to A B C +
*D/
Prefix notation (also known as “Polish notation”): + X YOperators are written before
their operands. The expressions given above are equivalent to / * A+ B C D
12. What is the data structures used to perform recursion?
Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows
whom to return when the function has to return. Recursion makes use of system stack
for storing the return addresses of the function calls.
Every recursive function has its equivalent iterative (non-recursive) function. Even when
such equivalent iterative procedures are written, explicit stack is to be used.
14. What is a Queue, how it is different from stack and how is it implemented?
Queue is a linear structure which follows the order is First In First Out (FIFO) to access
elements. Mainly the following are basic operations on queue: Enqueue, Dequeue, Front,
Rear
The difference between stacks and queues is in removing. In a stack we remove the item the
most recently added; in a queue, we remove the item the least recently added. Both Queues
and Stacks can be implemented using Arrays and Linked Lists.
Application and uses of queue: Queue, as the name suggests is used whenever we need to
manage any group of objects in an order in which the first one coming in also gets out first
while the others wait for their turn
Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
In real life scenario, Call Center phone systems uses Queues to hold people calling
them in an order, until a service representative is free.
Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive i.e First come first served.
Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
16. Write down the different between stack and queue?
A Short Guide to Written Exam 8 Data Structure
Stack Queue
It represents the collection of It represents the collection of elements
elements in Last In First Out order. in First In First Out order.
Operations includes testing null Operations include testing null queue,
stack, finding the top element in the finding the next element, removal of
stack, removal of top most element elements and inserting the elements
and adding elements on the top of the from the queue.
stack.
element inserted first will be element inserted first will be served
accessed last in stack as it has only first
one open end
Push, pop is use for insert and delete Front and rear is use for insert and
items. delete items.
Example is plates placed one above Example is queue for bus tickets.
other.
17. Write down the details on tree?
TREE
Tree is non-linear data structure designated at a special node calledroot and elements are arranged
in levels without containing cycles.
Or
The tree is
Rooted at one vertex
Contains no cycles
There is a sequence of edges from any vertex to any other
Any number of elements may connect to any node (includingroot)
A unique path traverses from root to any node of tree
The elements are arranged in layers
Appications of tree
1. Binary trees can represent arithmetic expressions.
An infix expression will have a parent operator andtwo children operands.
Consider the expression ((3 + (7*2))-1)
Each parenthesised expression becomes a tree.
Each operand is a leaf, each operator is an internal node.
A Short Guide to Written Exam 10 Data
Dat Structure
Basics of Graph
A graph is a set of nodes (vertic
(vertices) and edges. The node that holds data iscalled
lled vertex
ve and the
line connecting two vertices
es is ca
called edge. If G denotesa graph, G= (V, E) wherehere V denotes set
of vertices and E denotes set
et ofed
ofedges.
Undirected Graph: If eachh edge of a graph is undirected (without direction), the graphgr is called
undirected graph. The undirected
irected edge is alsocalled unordered edge.
Directed Graph: If each edge oof a graph is directed (with direction), thegraph h is called
ca directed
graph.
Weighted Graph: If the value
alue (c
(cost) of each edge is given, the graph iscalled weighted
weight graph.
Path: A path is a sequencee of vevertices where each pair of successive verticesisis connected
con by an
edge
Connected Graph: A graphh is ca called connected, if there is a path between eachh pair of vertices.
Cycle: A cycle is a path where
here fi
first and last vertices are the same.
= +
=O(n)
24. Write an algorithm which search a value divide and conquer process?
Or
Write a algorithm to find a node in a binary search tree.
Algorithm for binary searching:[divide and conquer process]