281707lecture Notes 1-An Overview of Data Structures-1718434382231
281707lecture Notes 1-An Overview of Data Structures-1718434382231
LECTURE 1 NOTES
■ Data is the collection of different numbers, symbols, and alphabets to represent information.
■ A data structure is a storage type used to organize and store data. It is a way of collecting
data on a computer that can be easily accessed and updated.
■ A data structure is a collection of data elements that provides the simplest way to store and
perform various actions on computer data. A data structure is a method of organizing data in
a computer that allows it to be used effectively. The goal is to reduce the spatial and time
complexities of various tasks.
■ A good data structure allows you to perform a variety of critical operations efficiently. An
efficient data structure uses as little memory space and execution time as possible to process
the structure.
■ Data structures are essential components of computers that are used to organize data in
memory. They are essential for efficiently organizing, processing, accessing, and storing data.
But wait, there's more. Different types of data structures have unique characteristics,
features, applications, benefits, and drawbacks.
○ So, how do you find a data structure that is appropriate for a specific task?
○ What are the different types of data structures and what are they used for?
■ The data structure is not any programming language like C, C++, Java, etc.
■ It is a set of algorithms that we can use in any programming language to structure the data in
the memory.
■ To structure the data in memory, n number of algorithms were proposed, and all of these
algorithms are known as "abstract data types". These abstract data types are a set of rules.
■ A data structure has also defined an instance of ADT. ADT means Abstract Data Type. It is
formally defined as a triplet [ D, F, A].
■ As applications become more complex and the amount of data increases, this may cause
issues with processing speed, data searching, handling multiple requests, and so on. A data
structure is a method of efficiently organizing, managing, and storing data. The data items
can be easily traversed with the help of a data structure.
■ The structure of the data and the synthesis of the algorithm are relative to each other. Data
presentation must be easy to understand so the developer, as well as the user, can make an
efficient implementation of the operation.
■ Data Search − Consider a store that has a 1 million(106) item inventory. If the application is to
search for an item, it must search through 1 million(106) items each time, which slows down
the search. Search will become slower as data grows.
■ Processor speed − Although the processor speed is very high, it is limited when the data
grows to a billion records.
■ Multiple requests − Multiple requests because thousands of users can search for data on a
web server at the same time, even the fastest server can fail while searching for the data.
● Data Structure vs Data Type
The data type is the form of a variable that can A data structure is a collection of various
be assigned a value. It specifies that the types of data. That entire set of data can be
variable will only be assigned values of the represented as an object and used
specified data type. throughout the program.
It is capable of storing value but not data. As a It can store various types of data in a single
result, it is dataless. object.
In the case of data types, there is no time Time complexity is important in data
complexity. structure objects.
The value of data is not stored in the case of In the case of data structures, the data and
data types because it only represents the type their values take up space in the main
of data that can be stored. memory of the computer. A data structure
can also hold various types of data within a
single object.
Data type examples are int, float, double, etc. Data structure examples are stack, queue,
tree, etc.
★ Static
★ Dynamic
■ The primitive data structures are primitive data types. Primitive Data Structures operate
directly according to the machine instructions. The int, char, float, double, and pointer are
primitive data structures that can hold a single value.
■ Non-primitive data structures are complex data structures that are derived from primitive
data structures. Primitive data types are further divided into two categories.
○ Linear data structures: A data structure in which data elements are arranged sequentially
or linearly, where each element is attached to its previous and next adjacent elements, is
called a linear data structure. In these data structures, one element is linearly connected
to only one other element.
○ Examples of linear data structures are an array, a stack, a queue, a linked list, etc.
○ Static data structure: They are a type of data structure where the size is allocated at
compile time. A static data structure has a fixed memory size. Therefore, the maximum
size is fixed. It is easier to access the elements in a static data structure.
i. Array - An array is a collection of data elements that are similar and are stored in
contiguous memory locations. It is the most basic data structure because each data
element can be accessed directly by using only its index number.
■ Dynamic data structures: They are a type of data structure where the size is
allocated at run time. In a dynamic data structure, the size is not fixed. Therefore,
the maximum size is flexible. It can be randomly updated during the runtime, which
may be considered efficient concerning the memory (space) complexity of the code.
★ Examples of this data structure are stacks, queues, linked lists, etc.
ii. Queue – A queue is a linear data structure in which elements can be inserted
from only one end (the rear) and deleted from the other end (the front). It adheres
to the FIFO (First In, First Out) principle.
○ Non-linear data structures: data structures where data elements are not placed
sequentially or linearly are called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single run only. Elements are
arranged in one-many, many-one, and many-many dimensions.
i. Tree – A tree is a multilevel data structure that is defined as a set of nodes. The
topmost node is known as the root node, while the bottommost nodes are known
as leaf nodes. Each node has a single parent but multiple children.
★ General Tree
★ Binary Tree
★ AVL Tree
★ N-ary Tree
○ A graph is a pair of sets (V, E), where V is the set of vertices and E is the set of
edges.
Types of Graphs
★ Finite Graph
★ Infinite Graph
★ Trivial Graph
★ Simple Graph
★ Multi Graph
★ Null Graph
★ Complete Graph
★ Pseudo Graph
★ Regular Graph
★ Bipartite Graph
★ Labeled Graph
★ Digraph
★ Subgraph
★ Cyclic Graph
■ Searching – We can easily search for any data element in a data structure.
■ Insertion – We can insert new data elements into the data structure.
■ Deletion – We can delete the data elements from the data structure.
■ Updation – We can update or replace the existing elements in the data structure.
A four-step methodology selects the appropriate data structure while taking into account and
answering some of the fundamental concerns. The procedure is as follows:
1. Relevance of the data structure to the business problem: The objective of any program is to
solve a business problem, and hence, the data structure chosen must be suitable and
appropriate to the problem at hand, rather than based solely on familiarity with the data
structure. This has the additional effect of resulting in a slow program that will need to be
improved later.
2. Operations to be performed on the data: The next step is to select a data structure to analyze
the problem and determine which basic operations and functions must be supported by the
program. Some of the most important operations for a program are
3. Checking the computational complexity: Along with the functional use and appropriateness
of the data structure, the computational performance threshold must be considered. A
complex program may require improvements to achieve performance goals, whereas a
simpler program and design may be sufficient.
4. Implementation: Choose the data structures that best meet your needs.
The way data is organized and accessed in computer memory is referred to as ‘data structure
alignment.’ Data alignment and data structure padding are two distinct issues that are related
to one another and are referred to collectively as data structure alignment.
■ Data alignment: Data alignment means storing data in memory at an address that is a
multiple of the word size. This increases the performance of the system due to the way
the CPU handles memory.
■ Data Structure Padding: In order to align the data, it may be necessary to insert some
extra bytes between the end of the previous data structure and the beginning of the
next data structure because the data is stored in memory as multiples of fixed word
size. Data structure padding refers to the addition of extra bytes of memory to align
the data.
struct
{
char a;
short int b;
int c;
char d;
}
Now we may think that the processor will allocate memory to this structure, as shown
below:
c c c d
In this case, the total memory allocated is 8 bytes. However, this never occurs because the
processor can only access memory with a fixed word size of 4 bytes. As a result, the integer
variable c cannot be allocated memory in the manner shown above. An integer variable takes
up 4 bytes. The correct memory allocation method for this structure is shown below using
padding bytes.
a padding b b
c c c c
The processor will require a total of 12 bytes for the above structure to maintain the data
alignment.
// first structure
struct test1
{
short s;
int i;
char c;
};
// second structure
struct test2
{
int i;
char c;
short s;
};
// driver program
int main()
{
test1 t1;
test2 t2;
cout << "size of struct test1 is " << sizeof(t1) << "\n";
cout << "size of struct test2 is " << sizeof(t2) << "\n";
return 0;
}
Output:
The short variable requires 2 bytes for the first structure, test1. The next variable is int, which
takes up 4 bytes. As a result, two bytes of padding are appended after the short variable. The
char variable now requires one byte, but memory will be accessed in word sizes of four bytes,
so three bytes of padding are added once more. As a result, a total of 12 bytes of memory is
required. We can calculate the padding for the second structure in the same way.
struct test1
{
short s;
// 2 bytes
// 2 padding bytes
int i;
// 4 bytes
char c;
// 1 byte
// 3 padding bytes
};
struct test2
{
int i;
// 4 bytes
char c;
// 1 byte
// 1 padding byte
short s;
// 2 bytes
};
Note: You can minimize the size of memory allocated for a structure by sorting members by
alignment.