You are on page 1of 20

# Data Structure and Algorithm

## Introduction to Data Structures

Data Structure:
A data structure is a specialized format for organizing and
storing data. General data structure types include the array, the file,
the record, the table, the tree, and so on. Any data structure is
designed to organize data to suit a specific purpose so that it can be
accessed and worked with in appropriate ways. In computer
programming, a data structure may be selected or designed to store
data for the purpose of working on it with various algorithms.
A data structure is a way of organizing data that considers not only
the items stored, but also their relationship to each other. Advance
knowledge about the relationship between data items allows
designing of efficient algorithms for the manipulation of data.
A data structure is a particular way of organizing data in a
computer so that it can be used efficiently.
A data structure is a collection of data items stored in memory; in
addition a number of operations are provided by the software to
manipulate that data structure.
A data structure means there is a relationship of some kind
between the data items. Exactly what the relationships are
determine what type of data structure is being used.
A data structure is a specialized format for organizing and storing
data.

1|Page

## Data Structure and Algorithm

What is Algorithm?
An algorithm is an effective method expressed as a finite list of well-defined
instructions for calculating a function. Starting from an initial state and initial input
(perhaps empty),the instructions describe a computation that, whenexecuted,
proceeds through a finite number of well-defined successive states, eventually
producing "output" and terminating at a final ending state. The transition from one
state to the next is not necessarily deterministic; some algorithms, known
as randomized algorithms, incorporate random input.
A process or set of rules to be followed in calculations or other problemsolving operations, especially by a computer.

Data Structure
Data structure is the way of organizing the data along with the
relationship among the data. The study of data structure includes

## Defining The operations that can be performed on that data.

Representing the data in the memory.
Determining the amount of memory required to store the
structure and the amount of time needed to process the
structure.

## Data Structure Operations

The data appearing in our data structure is processed by means of
certain operations. Infect, the particular data structure that one chooses
for a given situation depends largely on the frequency with which
specific operations are performed. The following four operations play a
major role:
Traversing
Accessing each record exactly once so that certain items in the record
may be processed.(This accessing or processing is sometimes called
'visiting" the records.)
2|Page

## Data Structure and Algorithm

Searching:
Finding the location of the record with a given key value, or finding the
locations of all records, which satisfy one or more conditions.
Inserting:
Adding new records to the structure.
Deleting:
Removing a record from the structure.
Sometimes two or more data structure of operations may be used in a
given situation; e.g., we may want to delete the record with a given key,
which may mean we first need to search for the location of the record.
Difference between Primitive and Non-Primitive Data
Type.
Primitive Data Type:

A primitive data type is one that fits the base architecture of the
underlying computer such as int, float, and pointer, and all of the
variations, thereof such as char short long unsigned float double
and etc, are primitive data type.
Primitive data are only single values, they have not special
capabilities.
The examples of Primitive data type s are given byte, short, int,
long, float, double, char etc.
The integer reals, logic data character data pointer and reference
are primitive data structures data structure that normally are
directly operated upon by machine level instructions are known as
primitive structure and data type.

3|Page

## A non-primitive data type is something else such as an array

structure or class is known as the non-primitive data type.
The data type that are derived from primary data types are known
as non-primitive data type.
The non-primitive data types are used to store the group of
values.
Examples of non-primitive data types are Array, structure, union,

Data Structures:
A data structure is an arrangement of data in a computer's memory or even disk storage.
Data structures can be classified into two types

4|Page

## Data Structure and Algorithm

Linear data structures are those data structures in which data elements are accessed (read and
written) in sequential fashion ( one by one)
Eg: Stacks , Queues, Lists, Arrays
Non Linear Data Structures:
Non Linear Data Structures are those in which data elements are not accessed in sequential
fashion.
Eg: trees, graphs

Algorithm:
Step by Step process of representing solution to a problem in words is called an Algorithm.
Characteristics of an Algorithm:

5|Page

## Characteristics of Data Structures

Array

Slow search
Slow deletes
Fixed size

Quick inserts
Fast access if index known

Slow inserts
Slow deletes
Fixed size

Stack

Queue

## First-in, first-out access

Quick inserts
Quick deletes

Slow search

Binary Tree

Quick search
Quick inserts
Quick deletes
(If the tree remains balanced)

## Red-Black Tree Quick search

Quick inserts
Quick deletes
(Tree always remains balanced)

Complex to implement

2-3-4 Tree

Quick search
Complex to implement
Quick inserts
Quick deletes
(Tree always remains balanced)
(Similar trees good for disk storage)

Hash Table

## Very fast access if key is known

Quick inserts

Slow deletes
Access slow if key is not known
Inefficient memory usage

Heap

Quick inserts
Quick deletes

Graph

complex

6|Page

## Data Structure and Algorithm

Stack :
Stack is a Linear Data Structure which follows Last in First Out mechanism.
It means: the first element inserted is the last one to be removed
Stack uses a variable called top which points topmost element in the stack. top is incremented
while pushing (inserting) an element in to the stack and decremented while poping (deleting) an
element from the stack

top

Push(A)

B
A
Push(B)

C
B
A

top

top

Push(C)

D
C
B
A
Push(D)

top

top

C
BA
Pop()

## Displaying the elements in the queue (Display)

Note:
While pushing an element into the stack, stack is full condition should be checked
While deleting an element from the stack, stack is empty condition should be checked

Applications of Stack:

7|Page

## Data Structure and Algorithm

Queue:
Queue is a Linear Data Structure which follows First in First out mechanism.
It means: the first element inserted is the first one to be removed
Queue uses two variables rear and front. Rear is incremented while inserting an element into the
queue and front is incremented while deleting element from the queue

rear
front

A
Insert(A)

B
A
Insert(B)

rear
front

C
B
A
Insert(C)

rear
front

D
C
B
A
Insert(D)

rear

D
C
B

front

Delete()

## Displaying the elements in the queue

Note:
While inserting an element into the queue, queue is full condition should be checked
While deleting an element from the queue, queue is empty condition should be checked
Applications of Queues:
Real life examples
Waiting in line
Waiting on hold for tech support
Applications related to Computer Science
Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

8|Page

rear
front

## Data Structure and Algorithm

To overcome the disadvantage of fixed size arrays linked list were introduced.
A linked list consists of nodes of data which are connected with each other. Every node consist of
two parts data and the link to other nodes. The nodes are created dynamically.
NODE

bat
Data

bat

cat

9|Page

sat

vat

NULL

## Data Structure and Algorithm

Trees :
A tree is a Non-Linear Data Structure which consists of set of nodes called vertices and set of

Terminology:

Root Node: The starting node of a tree is called Root node of that tree

Terminal Nodes: The node which has no children is said to be terminal node or leaf .

Non-Terminal Node: The nodes which have children is said to be Non-Terminal Nodes

## Degree: The degree of a node is number of sub trees of that node

Depth: The length of largest path from root to terminals is said to be depth or height of
the tree

## Siblings: The children of same parent are said to be siblings

Ancestors: The ancestors of a node are all the nodes along the path from the root to the
node

Property

G
H

10 | P a g e

Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E

Value
:
:
:
:
:
:
:
:
:

9
4
A
ED, H, I, F, C
D, E, G
5
I
D,E, F
D, F

## Data Structure and Algorithm

Binary Trees:
Binary trees are special class of trees in which max degree for each node is 2
Recursive definition:
A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint
binary trees called the left subtree and the right subtree.
Any tree can be transformed into binary tree. By left child-right sibling representation.

A
B
C

E
K

## Binary Tree Traversal Techniques:

There are three binary tree traversing techniques
Inorder
Preorder
Postorder
Inorder: In inorder traversing first left subtree is visited followed by root and right subtree
Preorder: In preorder traversing first root is visited followed by left subtree and right subtree.
Postorder: In post order traversing first left tree is visited followed by right subtree and root.

11 | P a g e

## Binary Search Tree:

A Binary Search Tree (BST) is a binary tree which follows the following conditons

## Every element has a unique key.

The keys in a nonempty left subtree are smaller than the key in the root of subtree.

The keys in a nonempty right subtree are grater than the key in the root of subtree.

The left and right subtrees are also binary search trees.

63

89

41

34

56

## Valid Operations on Binary Search Tree:

Inserting an element

Deleting an element

Traversing

12 | P a g e

72

95

## Data Structure and Algorithm

Avl Tree:
If in a binary search tree, the elements are inserted in sorted order then the height will be n,
where n is number of elements. To overcome this disadvantage balanced trees were introduced.

## Balanced binary search trees

An AVL Tree is a binary search tree such that for every internal node v of T, the
heights of the children of v can differ by at most 1.

44

2
17

78
1

32

88

50
1
48

## Operations of Avl tree:

Inserting an element

Deleting an element

Traversing

Height balancing

13 | P a g e

62

## Data Structure and Algorithm

Graphs
A graph is a Non-Linear Data Structure which consists of set of nodes called vertices V and set
of edges E which links vertices
Note: A tree is a graph with out loops

0
1

3
Graph

2
5

Tree

Graph Traversal:
Problem: Search for a certain node or traverse all nodes in the graph
Depth First Search
Once a possible path is found, continue the search until the end of the path
Start several paths at a time, and advance in each one step at a time

14 | P a g e

## Introduction to Object Oriented Programming

You've heard it a lot in the past several years. Everybody is saying it.

What is all the fuss about objects and object-oriented technology? Is it real? Or is it hype? Well,
the truth is--it's a little bit of both. Object-oriented technology does, in fact, provide many
benefits to software developers and their products. However, historically a lot of hype has
surrounded this technology, causing confusion in both managers and programmers alike. Many
companies fell victim to this hardship (or took advantage of it) and claimed that their software
products were object-oriented when, in fact, they weren't. These false claims confused
consumers, causing widespread misinformation and mistrust of object-oriented technology.

Object:
As the name object-oriented implies, objects are key to understanding object-oriented
technology. You can look around you now and see many examples of real-world objects: your
Definition: An object is a software bundle of variables and related methods
15 | P a g e

## Data Structure and Algorithm

Class:
In the real world, you often have many objects of the same kind. For example, your bicycle is
just one of many bicycles in the world. Using object-oriented terminology, we say that your
bicycle object is an instance of the class of objects known as bicycles. Bicycles have some state
(current gear, current cadence, two wheels) and behavior (change gears, brake) in common.
However, each bicycle's state is independent of and can be different from other bicycles.
Definition: A class is a blueprint or prototype that defines the variables and methods common to
all objects of a certain kind.

Inheritance:
Acquiring the properties of one class in another class is called inheritance

## The Benefits of Inheritance

Subclasses provide specialized behaviors from the basis of common elements provided
by the super class. Through the use of inheritance, programmers can reuse the code in the
superclass many times.

Programmers can implement superclasses called abstract classes that define "generic"
behaviors. The abstract superclass defines and may partially implement the behavior but
much of the class is undefined and unimplemented. Other programmers fill in the details
with specialized subclasses.

Data Abstraction:
The essential element of object oriented programming in abstraction. The complexity of
programming in object oriented programming is maintained through abstraction.
For example, the program consist of data and code which work over data. While executing a
program we dont thing in which location that data is being stored how the input device is
transferring the input to the memory etc. this abstraction allows us to execute the program
without thinking deeply about the complexity of execution of program.

16 | P a g e

## Data Structure and Algorithm

Encapsulation:
Encapsulation is the mechanism that binds together code and the data and keeps them safe from
outside world. In the sense it is a protective wrapper that prevents the code and data from being
accessed by other code defied outside the wrapper. Access is controlled through a well defined
interface.

Polymorphism:
Existing in more that one form is called polymorphism.
Polymorphism means the ability to take more that one form. For example an operation may
exhibit different behavior in different behavior in different instances.
For example consider operation of addition. For two numbers the operation will generate a sum.
If the operands are string the operation would produces a third string by concatenation.

if the same method name used for different procedures that the method is said to be overloaded.

Dynamic Binding:
Binding refer to the linking of a procedure call to the code to be executed in response to the call.
Dynamic binding means that the code associated with a given procedure call is not know until
the time of the call at runtime. It is associated with a polymorphism reference depends on the
dynamic type of that reference.

Message communication:
An object oriented program consists of objects that communicate with each other. The process
of programming in an object oriented language therefore involves the following basic steps:
1. creating classes that define objects and their behaviors.
2. creating objects from class definitions.
3. establishing communication among objects.

17 | P a g e

18 | P a g e

## Abstract Data Types:

An Abstract Data Type (ADT) is more a way of looking at a data structure: focusing on what it
does and ignoring how it does its job. A stack or a queue is an example of an ADT. It is important
to understand that both stacks and queues can be implemented using an array. It is also possible
to implement stacks and queues using a linked list. This demonstrates the "abstract" nature of
stacks and queues: how they can be considered separately from their implementation.
To best describe the term Abstract Data Type, it is best to break the term down into "data type"
and then "abstract".
Data type:
When we consider a primitive type we are actually referring to two things: a data item with
certain characteristics and the permissible operations on that data. An int in Java, for example,
can contain any whole-number value from -2,147,483,648 to +2,147,483,647. It can also be used
with the operators +, -, *, and /. The data type's permissible operations are an inseparable part of
its identity; understanding the type means understanding what operations can be performed on it.
In C++, any class represents a data type, in the sense that a class is made up of data (fields) and
permissible operations on that data (methods). By extension, when a data storage structure like a
stack or queue is represented by a class, it too can be referred to as a data type. A stack is
different in many ways from an int, but they are both defined as a certain arrangement of data
and a set of operations on that data.
abstract
Now lets look at the "abstract" portion of the phrase. The word abstract in our context stands for
"considered apart from the detailed specifications or implementation".
In C++, an Abstract Data Type is a class considered without regard to its implementation. It can
be thought of as a "description" of the data in the class and a list of operations that can be carried
out on that data and instructions on how to use these operations. What is excluded though, is the
19 | P a g e

## Data Structure and Algorithm

details of how the methods carry out their tasks. An end user (or class user), you should be told
what methods to call, how to call them, and the results that should be expected, but not HOW
they work.
We can further extend the meaning of the ADT when applying it to data structures such as a stack
and queue. In Java, as with any class, it means the data and the operations that can be performed
on it. In this context, although, even the fundamentals of how the data is stored should be
invisible to the user. Users not only should not know how the methods work, they should also not
know what structures are being used to store the data.
Consider for example the stack class. The end user knows that push() and pop() (amoung other
similar methods) exist and how they work. The user doesn't and shouldn't have to know how
push() and pop() work, or whether data is stored in an array, a linked list, or some other data
structure like a tree.

20 | P a g e