DSA  The Logic Builder

DSA The Logic Builder

Introduction

Data Structure

Data Structure and Algorithms are a great way to improve problem-solving skills. As the name suggests there are some called data structures and algorithms. When Data is processed i.e organized & presented in a meeting context it becomes useful for decision-making. Data Structures may seem complex but let us break it down, it is how the Data have been stored in which format( i.e Structure) and how we can access it. So basically, Data Structure is used to access and work with data in appropriate ways.

ALGORITHMS

The algorithm is a procedure that is used to solve a problem or perform some mathematical operations. It contains a set of instructions(code) that is executed by the computer. There are some basic, popular and useful Algorithms developed by developers for a specific purpose. For example, there are Searching algorithms like linear search & binary search. There are some Sorting Algorithms like Quick Sort, Insertion sort, Bubble sort etc.

Basics of Data Structure

As you can see the classification of Data Structure in the above picture let us understand it clearly.

Data Structures are classified into Linear and Non-Linear structures. Linear Data Structure means where data elements are connected so they can be accessed sequentially. Whereas Non-Linear Data Structure means the data elements are not arranged sequentially.

The Linear Data Structure is further classified into Static and Dynamic Data Structures. Static means the number of elements is fixed and cannot be changed at run time. Dynamic means we don't know the size we will take elements at run time and the size can vary.

Array

The array is the most basic, simple and easy-to-use data structure. It stores the same array of elements of the same data type in a contiguous and adjacent memory location. Arrays can be one-dimensional or multidimensional as required. Array index starts from 0 and can go up to a number of elements - 1.

Linked List

A linked List is a data structure that consists of Nodes. Nodes are nothing but collections of two elements i.e

  1. data &

  2. next

Now, what is data & next, data is the actual data that you want to store and next is the pointer variable which holds the address of the next Node. When two or more Nodes are linked together to form a chain it is called Linked List. Now, what is HEAD & NULL doing in the picture, so the head is the starting point of the Linked List & null is where the Linked List ends. There are 4 types of Linked Lists

a)Singly Linked List(Normal Linked list),

b)Doubly Linked List,

c)Circular Linked List,

d)Circular Doubly Linked List.

A doubly Linked list is a link where a node contains three elements

1. previous: to store the address of the previous node,

2. data: the actual data,

3. next: to store the address of the next node

In a doubly linked list, we can access the previous node and next node as required.

Circular Linked List is where the next or last node is pointing towards the first node while playing music if we play it in the loop it gets played again and again.

A Circular Doubly Linked list is a combination of both circular and doubly linked lists.

Is Linked List better than Array?

YES, it is better than Array in terms of Memory usage. In an Array where we have to know the number of elements before runtime, we cannot increase or decrease it but we can do it in Linked List.

Real-Life Example: In your music playlist each song is linked to its previous and next song.

Stack

A stack data structure is a collection of elements or items that can be accessed in a specific order, called Last-In-First-Out (LIFO). In other words, the element that is most recently added to the stack is the first one to be removed.

Explaining Stack without using the golden example of "Plates in a wedding buffet is impossible"

A good comparison for the stack data structure is a pile of plates at a wedding buffet. Imagine that you are at a wedding and you want to grab a plate. You notice that the plates are stacked on top of each other, with the most recent plate added to the top of the stack.

When you take a plate, you take it from the top of the stack. The next person who wants a plate also takes it from the top of the stack. This continues until there are no more plates on the top of the stack. If someone wants to add a plate to the stack, they simply place it on top of the existing stack, becoming the new top element.

Similarly, when we use a stack data structure, we add new elements to the top of the stack and remove them from the top as well. This is useful when we need to keep track of the order in which elements were added or when we need to perform a set of operations in reverse order of their occurrence.

Queue

The queue is similar to Stack but it follows the First-In-First-Out (FIFO) principle it has two ends the front is used to remove the element and the other end is to insert the element. The example for the queue is very simple just imagine you are at a mall and standing in queue for billing the person who came first will get the billing done first and the person who came last will have to wait till his turn come.

Tree

As the name says tree imagine this data structure as the tree it is the same but has cool names like node, siblings, parent, child, root, leaf etc. Most of the things must be clear to you just by taking a look at the picture above and let me clear the rest of the things for you. Now to understand the diagram just imagine a tree a normal tree and see the roots come first then the stem and branches and then the leaf. Also, one parent can have a maximum of 2 children and a minimum of 1 child.

A tree data structure is a way of organizing data hierarchically, like a family tree. The structure is made up of nodes.

The topmost node in a tree is called the root node, and it has no parent node. All other nodes in the tree have exactly one parent node, except for leaf nodes, which have no children nodes.

Each node in a tree can have zero or more child nodes. A child node is a node that is directly connected to another node through an edge that originates from the parent node.

The advantage of a tree data structure is that it provides an efficient way to search and retrieve data. By organizing data hierarchically, we can quickly narrow down our search to a specific subtree and eliminate irrelevant data.

For example, consider a file directory on a computer. The root directory is the topmost node, and it contains all the other files and directories. Each subdirectory is a child node of the root node.

Graph

A graph data structure is a way of representing relationships between objects or data points. It consists of a set of vertices, or nodes, that are connected by edges. Each edge represents a relationship or connection between two vertices. It is a complex data structure.

Graphs can be used to model a wide variety of real-world scenarios, such as social networks, transportation systems, and computer networks. For example, in a social network, each person would be represented as a vertex, and the edges would represent connections between friends.

There are two main types of graphs: directed and undirected. In a directed graph, the edges have a direction, and they represent a one-way relationship between two vertices. In an undirected graph, the edges do not have a direction, and they represent a two-way relationship between two vertices.

Graphs can also be weighted or unweighted. A weighted graph assigns a value, or weight, to each edge, representing the strength or cost of the relationship between two vertices. An unweighted graph does not assign any values to the edges.

Graph algorithms, such as shortest path algorithms and minimum spanning tree algorithms, can be used to analyze and manipulate graph data structures, making them a powerful tool in computer science and data analysis.

The shortest path algorithm is used in applications like google maps to find the shortest path to your destination but it also suggests a path that has less traffic. A minimum spanning tree is used to save resources it is a tree with the least total cost.

Did you like the blog? answer that in the comment section and let me know your views about it!