Btech AIML Unit 1 Sem 2
Btech AIML Unit 1 Sem 2
A data structure is a specialized format for organizing, storing, and managing data. It provides a
way to efficiently access and modify data. In essence, it's a logical or mathematical model of
how data is organized and how it can be manipulated.
● Organization: How data elements are arranged and related to each other.
● Storage: How data is physically stored in memory.
● Operations: The set of operations that can be performed on the data (e.g., insertion,
deletion, searching, sorting).
● Efficiency: The performance characteristics of the data structure in terms of time and
space complexity.
○ These are the basic data structures that are directly supported by the
programming language.
○ Examples:
■ Integers (int)
■ Floating-point numbers (float, double)
■ Characters (char)
■ Booleans (bool)
2. Non-Primitive Data Structures:
○ These are more complex data structures that are derived from primitive data
structures.
Key Considerations:
● The choice of data structure depends on the specific requirements of the application,
such as the type of data, the operations to be performed, and the performance
requirements.
● Understanding the characteristics and trade-offs of different data structures is essential
for efficient software development.
● Definition:
○ These are the most basic data structures that are directly supported by the
programming language.
○ They represent fundamental data types that the compiler already understands.
○ They are the building blocks for more complex data structures.
● Characteristics:
○ Directly Supported: The language provides built-in mechanisms for creating
and manipulating these data types.
○ Fixed Size: Typically, they have a fixed size in memory.
○ Simple Operations: Operations on primitive data types are usually simple and
efficient.
● Examples:
○ Integer (int): Represents whole numbers (e.g., 10, -5, 0).
○ Floating-Point (float, double): Represents numbers with decimal points (e.g.,
3.14, -2.5). double offers higher precision.
○ Character (char): Represents single characters (e.g., 'A', '7', '$').
○ Boolean (bool): Represents logical values (true or false).
● Purpose:
○ They are used to store basic data values.
○ They are used as the foundation for creating more complex data structures.
● Definition:
○ These are data structures that are derived from primitive data structures.
○ They are designed to handle larger and more complex datasets.
○ They are created by programmers to organize collections of primitive or other
non-primitive data.
● Characteristics:
○ Derived: They are built using primitive data types or other non-primitive data
structures.
○ Flexible Size: They can have varying sizes, and their size can change during
program execution.
○ Complex Operations: Operations on non-primitive data structures can be more
complex and involve algorithms.
● Types:
○ Linear Data Structures:
■ Data elements are arranged in a sequential or linear order.
■ Examples:
■ Arrays: A collection of elements of the same data type stored in
contiguous memory locations.
■ Linked Lists: A sequence of nodes, where each node contains
data and a pointer to the next node.
■ Stacks: A LIFO (Last-In, First-Out) data structure.
■ Queues: A FIFO (First-In, First-Out) data structure.
○ Non-Linear Data Structures:
■ Data elements are not arranged in a linear order.
■ They represent hierarchical or network-like relationships between data
elements.
■ Examples:
■ Trees: A hierarchical data structure consisting of nodes connected
by edges.
■ Graphs: A collection of nodes (vertices) and edges that connect
them.
■ Heaps: specialized tree based data structure.
● Purpose:
○ They are used to organize and manage large and complex datasets.
○ They are used to implement complex algorithms and data processing tasks.
○ They are used to model real world relationships between data.
Data structures are not just about storing data; they're also about performing operations on that
data efficiently. Common operations include:
1. Traversal:
○ Visiting each element in the data structure exactly once.
○ Example: Printing all elements of an array or linked list.
2. Insertion:
○ Adding a new element to the data structure.
○ Example: Adding a node to a linked list or pushing an element onto a stack.
3. Deletion:
○ Removing an element from the data structure.
○ Example: Removing a node from a linked list or popping an element from a stack.
4. Searching:
○ Finding a specific element in the data structure.
○ Example: Searching for a value in an array or a node in a tree.
5. Sorting:
○ Arranging the elements in a specific order (e.g., ascending or descending).
○ Example: Sorting an array of numbers.
6. Merging:
○ Combining two or more data structures into a single data structure.
○ Example: Merging two sorted linked lists.
7. Splitting:
○ Dividing a data structure into multiple data structures.
○ Example: Splitting a linked list into two separate lists.
Algorithm Analysis:
● Choosing the Right Algorithm: It helps in selecting the most efficient algorithm for a
given task.
● Predicting Performance: It allows us to predict how an algorithm will perform with large
inputs.
● Optimizing Code: It helps identify bottlenecks and areas for optimization.
● Resource Management: It provides insights into the resource requirements of an
algorithm.
Example:
● Linear Search:
○ Time complexity: O(n) (in the worst case).
○ Space complexity: O(1) (assuming the input array is already in memory).
● Binary Search:
○ Time complexity: O(log n) (for a sorted array).
○ Space complexity: O(1) (iterative) or O(log n) (recursive).