BCA 3rd Data Structure 1 20
BCA 3rd Data Structure 1 20
BCA 3rd Data Structure 1 20
Trees
Introduction to Tree, Tree Terminology Binary Tree, Binary Search Tree, Strictly
Binary Tree, Complete Binary Tree, Tree Traversal, Threaded Binary Tree, AVL
Tree B Tree, B+ Tree.
Unit -IV
Graphs, Searching, Sorting and Hashing Graphs:
Introduction, Representation to Graphs, Graph Traversals Shortest Path Algorithms.
Searching and Sorting:
Searching, Types of Searching, Sorting, Types of sorting like quick sort, bubble sort,
merge sort, selection sort.
Hashing:
Hash Function, Types of Hash Functions, Collision, Collision Resolution Technique
(CRT), Perfect Hashing
1
BCA 3rd Sem ( Data Structure)
INDEX
5 Trees 102-142
8 Hashing 171-179
2
BCA 3rd Sem ( Data Structure)
UNIT I
3
BCA 3rd Sem ( Data Structure)
Examples of data are weights, prices, costs, numbers of items sold, employee
names, product names, addresses, tax codes, registration marks etc
Information
Information is a set of data which is processed in a meaningful way according to
the given requirement. Information is processed, structured, or presented in a given
context to make it meaningful and useful.
It is processed data which includes data that possess context, relevance, and
purpose. It also involves manipulation of raw data.
Information assigns meaning and improves the reliability of the data. It helps to
ensure undesirability and reduces uncertainty. So, when the data is transformed
into information, it never has any useless details.
Example :- Information is data that has been converted into a more useful or
intelligible form.
4
BCA 3rd Sem ( Data Structure)
Data Information
It can be structured, tabular data, graph, Language, ideas, and thoughts based on
data tree, etc. the given data.
Data does not have any specific It carries meaning that has been
purpose. assigned by interpreting data.
Data that is collected Information that is processed.
Data is a single unit and is raw. It alone Information is the product and group of
doesn't have any meaning. data which jointly carry a logical
meaning.
It never depends on Information It depended on Data.
Measured in bits and bytes. Measured in meaningful units like time,
quantity, etc.
It can't be used for decision making It is widely used for decision making.
Problem Analysis
5
BCA 3rd Sem ( Data Structure)
Problem Analysis: Identify the issues. Be clear about what the problem is. ...
Algorithm Development
6
BCA 3rd Sem ( Data Structure)
Flow Chart
Program Coding
A Programming (or coding) language is a set of syntax rules that define how code
should be written and formatted. Thousands of different programming languages
make it possible for us to create computer software, apps and websites.
Compilation
First, the source ‘.java’ file is passed through the compiler, which then encodes the
source code into a machine independent encoding, known as Bytecode. The
content of each class contained in the source file is stored in a separate ‘.class’ file.
While converting the source code into the bytecode.
Execution
The class files generated by the compiler are independent of the machine or the
OS, which allows them to be run on any system. To run, the main class file (the
class that contains the method main) is passed to the JVM, and then goes through
three main stages before the final machine code is executed.
Testing means verifying correct behavior. Testing can be done at all stages of
module development: requirements analysis, interface design, algorithm design,
implementation, and integration with other modules. In the following, attention
7
BCA 3rd Sem ( Data Structure)
Debugging is a cyclic activity involving execution testing and code correction. The
testing that is done during debugging has a different aim than final module testing.
Final module testing aims to demonstrate correctness, whereas testing during
debugging is primarily aimed at locating errors. This difference has a significant
effect on the choice of testing strategies.
Documentation
The documentation section contains a set of comment including the name of the
program other necessary details. Comments are ignored by compiler and are used
to provide documentation to people who reads that code.
Algorithm
An algorithm is defined as a step-by-step procedure or method for solving a
problem by a computer in a finite number of steps. Steps of an algorithm definition
may include branching or repetition depending upon what problem the algorithm is
being developed for. While defining an algorithm steps are written in human
understandable language and independent of any programming language. We can
implement it in any programming language of our choice.
Besides merely being a finite set of rules which gives a sequence of operations for
solving a specific type of problem, a well defined algorithm has five important
Features:
Input. An algorithm has zero or more inputs, i.e, quantities which are given to it
initially before the algorithm begins.
8
BCA 3rd Sem ( Data Structure)
Output. An algorithm has one or more outputs i.e, quantities which have a
specified relation to the inputs.
Characteristics of an Algorithm
An algorithm must follow the mentioned below characteristics:
Output: An algorithm must have 1 or well defined outputs, and should match with
the desired output.
Step 1: Start
9
BCA 3rd Sem ( Data Structure)
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 6: Stop
Write an algorithm to find the largest among three different numbers entered
by user.
Step 1: Start
Step 4: If a>b
If a>c
Else
Else
If b>c
Else
Step 5: Stop
10
BCA 3rd Sem ( Data Structure)
Advantages of Algorithms:
Disadvantages of Algorithms:
Big O
For any monotonic functions f(n) and g(n) from the positive integers to the positive
integers, we say that f(n) = O(g(n)) when there exist constants c > 0 and n0 > 0
such that
Intuitively, this means that function f(n) does not grow faster than g(n), or that
function g(n) is an upper bound for f(n), for all sufficiently large n→∞
11
BCA 3rd Sem ( Data Structure)
Examples:
1 = O(n)
n = O(n2)
log(n) = O(n)
An algorithm is said to run in constant time if it requires the same amount of time
regardless of the input size. Examples:
Binary Search
Recall the "twenty questions" 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 iss too high or too low. Twenty questions game imploies 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:
locate the element a in a sorted (in ascending order) array by first comparing a with
the middle element and then (if they are not equal) dividing the array into two
subarrays; if a is less than the middle element you repeat the whole procedure in
the left subarray, otherwise - in the right subarray. The procedure repeats until a is
found or subarray is a zero dimension.
Note, log(n) < n, when n→∞. Algorithms that run in O(log n) does not use the
whole input.
Flowchart
Flowchart is a diagrammatic representation of sequence of logical steps of a
program. Flowcharts use simple geometric shapes to depict processes and arrows
to show relationships and process/data flow.
Flowchart Symbols
Here is a chart for some of the common symbols used in drawing flowcharts.
13
BCA 3rd Sem ( Data Structure)
program.
Flowchart can have only one start and one stop symbol
14
BCA 3rd Sem ( Data Structure)
Example Flowcharts
15
BCA 3rd Sem ( Data Structure)
Analysis of Algorithms
The term analysis of algorithms is used to describe approaches to the study of the
performance of algorithms. In this course we will perform the following types of
analysis:
the worst-case runtime complexity of the algorithm is the function defined by the
maximum number of steps taken on any instance of size a.
the best-case runtime complexity of the algorithm is the function defined by the
minimum number of steps taken on any instance of size a.
the average case runtime complexity of the algorithm is the function defined by an
average number of steps taken on any instance of size a.
Algorithm Complexity
The time complexity of algorithms is most commonly expressed using the big O
notation. It's an asymptotic notation to represent the time complexity.
16
BCA 3rd Sem ( Data Structure)
Worst case: An algorithm takes more time to execution is called the worst case, it
is mostly in cases of loops where execution time is depends upon the number of
input size.
Average case: An algorithm takes time between the best and worst case to
execution is called the Average case.
Best case: An algorithm takes expected to execution is called the best case.
Program Instruction
Execution
Space complexity is the amount of memory used by the algorithm (including the
input values to the algorithm) to execute and produce the result.
Instruction Space
It's the amount of memory used to save the compiled version of instructions.
Environmental Stack
17
BCA 3rd Sem ( Data Structure)
system stack, where they wait for further execution and then the call to the inside
algorithm(function) is made.
For example, If a function A() calls function B() inside it, then all th variables of
the function A() will get stored on the system stack temporarily, while the
function B() is called and executed inside the function A().
Data Space
For calculating the space complexity, we need to know the value of memory used
by different type of data type variables, which generally varies for different
operating systems, but the method for calculating the space complexity remains the
same.
Type Size
18
BCA 3rd Sem ( Data Structure)
Data Structures are the main part of many computer science algorithms as they
enable the programmers to handle the data in an efficient way. It plays a vitle role
in enhancing the performance of a software or a program as the main function of
the software is to store and retrieve the user's data as fast as possible
Basic Terminology
Data structures are the building blocks of any program or the software. Choosing
the appropriate data structure for a program is the most difficult task for a
programmer. Following terminology is used as far as data structures are concerned
Data: Data can be defined as an elementary value or the collection of values, for
example, student's name and its id are the data about the student.
Group Items: Data items which have subordinate data items are called Group
item, for example, name of a student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for
example, if we talk about the student entity, then its name, address, course and
marks can be grouped together to form the record for the student.
File: A File is a collection of various records of one type of entity, for example, if
there are 60 employees in the class, then there will be 20 records in the related file
where each record contains the data about each employee.
Attribute and Entity: An entity represents the class of certain objects. it contains
various attributes. Each attribute represents the particular property of that entity.
efficient here. There are better data structures which can make the search process
efficient like ordered array, binary search tree or hash tables.
Linear or non-linear: This characteristic describes whether the data items are
arranged in chronological sequence, such as with an array, or in an unordered
sequence, such as with a graph.
Static or dynamic: This characteristic describes how the data structures are
compiled. Static data structures have fixed sizes, structures and memory locations
at compile time. Dynamic data structures have sizes, structures and memory
locations that can shrink or expand depending on the use.
20