Introduction To Data Structures
Introduction To Data Structures
preencoded.png
Arrays: The Building Blocks of Data Organization
Definition and Characteristics Usage Examples Definition of Array
Queue
A queue is a First-In-First-Out (FIFO) data structure with enqueue and dequeue operations. It is
useful for scheduling, buffering, and breadth-first traversal.
preencoded.png
Why & When stack or queue is used instead of
array/lists:
Because they assist you in managing your data in a more specific manner than arrays and lists. It means you won’t have to wonder if
someone placed an element in the midst of your list at random, messing up certain invariants when troubleshooting an issue. Random
access is the nature of arrays and lists. They’re incredibly adaptable, but they’re also easily corruptible. If you wish to keep track of
your data, It’s recommended to use those, previously implemented, collections when storing data as FIFO or LIFO.
•We use stack or queue instead of arrays/lists when we want the elements in a specific order i.e. in the order, we put them (queue) or
in the reverse order (stack).
•Queues and stacks are dynamic while arrays are static. So, when we require dynamic memory, we use queue or stack over arrays.
•Stacks and queues are used over arrays when sequential access is required.
•To efficiently remove any data from the start (queue) or the end (stack) of a data structure.
•When you want to get items out in the same order that you put them in, use a queue (FIFO)
•When you need to bring things out in the opposite order that they were put in, use a stack (LIFO)
preencoded.png
Linked Lists: Dynamic Sequential Access
Singly Linked List Doubly Linked List
Each node contains data and a Nodes hold pointers to both
pointer to the next node. Allows previous and next nodes,
dynamic size with efficient enabling bidirectional traversal
insertions and deletions at head and easier node removal.
or middle.
A linked list is a linear data structure consisting of a sequence of nodes, where each node contains two parts: data and a pointer to the
next node in the sequence. Unlike arrays, linked lists do not require contiguous memory allocation; each node is dynamically allocated
its own memory space, allowing for efficient insertions and deletions. The first node is often referred to as the head, and linked lists
can be singly linked (each node points to the next) or doubly linked (each node points to both the next and the previous node). Linked
lists are widely used in various applications, including implementing stacks, queues, and dynamic memory allocation.
Bing Videos Introduction to Linked List preencoded.png
Graphs: Modeling Complex Relationships
A graph is a non-linear data structure. It consists of:
•Vertices (nodes): Represent entities within the graph.
•Edges: Define relationships or connections between vertices.
•Weight (in weighted graphs): Assign numerical values to edges.
•Degree: Refers to the number of edges incident to a vertex.
Binary Tree 1
A tree where each node has at most two children. Enables fast
searching, insertion, and deletion.
2 Binary Search Tree (BST)
Left child nodes contain smaller values, right child nodes contain
larger, facilitating ordered data retrieval.
Balanced Trees 3
Structure is maintained to keep height minimal, improving operation
efficiency.
4 Other Variants
Includes AVL trees, red-black trees, and B-trees used in databases
and file systems.
•Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast
retrieval of information based on its key.
•The great thing about hashing is, we can achieve all three operations (search, insert and delete) in O(1) time on average.
•Hashing is mainly used to implement a set of distinct items (only keys) and dictionaries (key value pairs).
Components of Hashing
There are majorly three components of hashing:
1.Key: A Key can be anything string or integer which is fed as input in the hash function
the technique that determines an index or location for storage of an item in a data
structure.
2.Hash Function: Receives the input key and returns the index of an element in an
array called a hash table. The index is known as the hash index .
3.Hash Table: Hash table is typically an array of lists. It stores values corresponding to
the keys. Hash stores the data in an associative manner in an array where each data
value has its own unique index.
preencoded.png
Hashing: Fast Data Lookup
How does Hashing work?
Suppose we have a set of strings {“ab”, “cd”, “efg”} and we would
like to store it in a table. What is Collision in Hashing?
•Step 1: We know that hash functions (which is some mathematical When two or more keys have the same hash value,
formula) are used to calculate the hash value which acts as the index a collision happens. If we consider the above example, the hash
of the data structure where the value will be stored. function we used is the sum of the letters, but if we examined the
•Step 2: So, let’s assign hash function closely then the problem can be easily visualized
•“a” = 1, that for different strings same hash value is being generated by
•“b”=2, .. etc, to all alphabetical characters. the hash function.
•Step 3: Therefore, the numerical value by summation of all For example: {“ab”, “ba”} both have the same hash value, and
characters of the string: string {“cd”,”be”} also generate the same hash value, etc. This is
•“ab” = 1 + 2 = 3, known as collision and it creates problem in searching, insertion,
•“cd” = 3 + 4 = 7 , deletion, and updating of value.
•“efg” = 5 + 6 + 7 = 18
•Step 4: Now, assume that we have a table of size 7 to store these
strings. The hash function that is used here is the sum of the
characters in key mod Table size . We can compute the location of
the string in the array by taking the sum(string) mod 7 .
•Step 5: So we will then store
•“ab” in 3 mod 7 = 3,
•“cd” in 7 mod 7 = 0, and
•“efg” in 18 mod 7 = 4.
Common Algorithms
• Bubble Sort
• Merge Sort
• Quick Sort
• Heap Sort
Performance Considerations
Time complexity varies; merge and quick sorts are efficient for large data sets.
Stability and space complexity also matter.
Use Cases
Sorting is fundamental for searching, data organization, and algorithm optimization.
preencoded.png
Searching Algorithms:
Searching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to
focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input array.
Linear Search : It is used for an unsorted array. It mainly does one by one comparison of the item to be search with array elements. It takes linear or O(n) Time.
Binary Search : It is used for a sorted array. It mainly compares the array's middle element first and if the middle element is same as input, then it returns. Otherwise it
searches in either left half or right half based on comparison result (Whether the mid element is smaller or greater). This algorithm is faster than linear search and
takes O(Log n) time.
Data Requirement Works on any list (unsorted or sorted). Works only on a sorted list.
Time Complexity O(n) O(log n)
Space Complexity O(1) O(1)
Speed Slower for large datasets. Much faster for large datasets.
Large, sorted datasets (like phonebooks,
Example Use Small or unsorted data sets.
databases).
preencoded.png
Searching Algorithms:
Example
Example List: List: [3, 7, 9, 13, 18, 21, 27, 31]
Search for: 18
preencoded.png
Searching Algorithms: Locating Data Quickly
3 Graph Search
Includes depth-first and breadth-first search for traversing graph
structures and finding paths.
preencoded.png
Key Takeaways and Best Practices
Choose Data Structures Wisely
Match the structure to your application's needs to optimize performance and memory
use.
Practice Implementation
Hands-on coding deepens understanding and improves problem-solving skills.
Stay Updated
New structures and algorithms continue to evolve; keep learning to enhance efficiency.
preencoded.png