0% found this document useful (0 votes)
24 views11 pages

281707lecture Notes 1-An Overview of Data Structures-1718434382231

Notes on data structure in detailed

Uploaded by

praksh6377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

281707lecture Notes 1-An Overview of Data Structures-1718434382231

Notes on data structure in detailed

Uploaded by

praksh6377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structures and Algorithms

LECTURE 1 NOTES

An Overview of Data Structures


● What is Data?

■ Data is the collection of different numbers, symbols, and alphabets to represent information.

● What is a Data Structure?

■ 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 exactly is a "data structure"?

○ 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].

○ D: Set of the domain

○ F: the set of operations

○ A: the set of axioms

● Need of Data Structure

■ 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.

■ Data structure contributes to efficiency, reusability, and abstraction. It is important in


improving a program's performance because the program's main function is to store and
retrieve the user's data as quickly as possible.

■ 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.

■ Here is a list of the needs for data.

I. Data structure modification is easy

II. It requires less time

III. Save storage or memory space

IV. Data representation is easy

V. Easy access to the large database

■ 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

Data Type Data Structure

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.

The implementation of a data type is known as The process of implementing a data


an "abstract implementation." structure is known as "concrete
implementation."

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.

● Types of Data Structures

■ There are two types of data structures:

○ Primitive data structures

○ Non-primitive data structures

○ Linear data structures

★ Static

★ Dynamic

○ Non-linear data structures


● Primitive Data Structures

■ 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

■ 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.

○ An example of this data structure is an array.

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.

i. Stack – A stack is a linear data structure in which operations are performed in a


specific order. The order could be FILO (First In, Last Out) or LIFO (Last In, First
Out) (Last In, First Out).

The basic operations performed on the stack are as follows:

★ Push – Adds an item within the stack.

★ Pop – Deletes or removes an item from the stack.

★ Top – Returns the topmost element of the stack.

★ IsEmpty – Returns true if the stack is empty.

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.

★ Deque – Removes an item from the front of the queue.

★ Enqueue – Adds an item from the back of the queue.

★ IsFull – Returns true if the queue is full.

★ IsEmpty – Returns true if the queue is empty.


iii. Linked List – A linked list is a linear data structure that is used in computer
memory to maintain a list-like structure. It is a collection of nodes that are not
all stored in the same place. Pointers are used to connect each node in the list
to its neighboring node.

○ 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.

● Examples of non-linear data structures are trees and graphs.

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.

Types of Trees in Data Structure

★ General Tree

★ Binary Tree

★ Binary Search Tree

★ AVL Tree

★ Red Black Tree

★ N-ary Tree

ii. Graph – A graph is a graphical representation of a collection of objects linked


together by edges. The points that connect the interconnected nodes are called
vertices, and the links that connect the vertices are called edges.

○ 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

★ Connected or Disconnected Graph

★ Cyclic Graph

★ Vertex Labelled Graph

★ Directed Acyclic Graph

● Data Structure Operations

The common operations that can be performed on the data structures


are as follows:

■ Searching – We can easily search for any data element in a data structure.

■ Sorting – We can sort the elements either in ascending or descending order.

■ 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.

● Steps to Select 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

a. Data insertion into the data structure

b. Remove the data items.

c. Looking for specific values in the data

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.

● How is data arranged and accessed in the computer’s memory?

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.

Consider the structure as shown below:

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:

Size of 1 block = 1 byte


Size of 1 row = 4 byte
a b b c

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.

Size of 1 block = 1 byte


Size of 1 row = 4 byte

a padding b b

c c c c

d padding padding padding

The processor will require a total of 12 bytes for the above structure to maintain the data
alignment.

Look at the below C++ program:

// CPP program to test


// size of struct
#include <iostream>
using namespace std;

// 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:

size of struct test1 is 12


size of struct test2 is 8

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.

The padding for both structures is shown below:

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.

You might also like