Posts

Showing posts with the label Data Structures

Tree Data Structure

 Tree Definition: A tree is a widely used data structure in computer science. It is a hierarchical structure that consists of nodes, where each node can have one or more child nodes. In C++, a tree can be implemented using classes, objects, and pointers. Binary Tree: One of the most common types of trees is the binary tree, where each node has at most two child nodes. In C++, a binary tree can be represented by a class that has a left and right pointer to represent its child nodes, and a data variable to store the node's value. Here is an example of a binary tree class in C++: class BinaryTree {     public:         int data;         BinaryTree* left;         BinaryTree* right; }; Explanation: This class contains three variables: data, left, and right. The data variable stores the value of the node, and the left and right pointers point to the left and right child nodes respectively. A new node can be created by c...

Recursion

  Recursion Definition : Recursion is a powerful technique in computer programming where a function calls itself in order to solve a problem. It is a way to break down a complex problem into smaller, manageable parts. One of the most common examples of recursion is the calculation of factorials, which is the product of all numbers from 1 to a given number. In C++, a recursive function for calculating factorials would look like this: Example : int factorial(int n) {     if (n == 0) {         return 1;     } else {         return n * factorial(n - 1);     } } Explanation : In this example, the function checks if the input number is 0. If it is, the function returns 1, as 0! = 1 by definition. If the input number is not 0, the function calls itself with an input of n-1. This process continues until the input number is 0, at which point the function returns the product of all the numbers that have been multiplied tog...

Stack and Queues

Image
What is Stack? The basis of stack is first in last out(FILO) Or last in first out (LIFO). To understand stack, consider example of plats. We place plats one over another. Let say we have 5 plats, named as p1, p2, p3, p4, and p5. Assume they are arranged in such a way that p2 is placed on p1, p3 is placed on p2, p4 on p3, and p5 on p4. As shown in the diagram.   Now you are clear with, what is stack. Now we will see where it is used and how we implement stack in c plus plus programming language. Why Stack? We see the functionality of forward and backward operations in our browsers. Similarly, we see functionalities like undo, redo in our text editor or any application like photo editors. The functionality of undo, redo is very common. It is used in many applications like in word, excel, photoshop, browsers, paint, and many other applications. Now if we think about what will be the best way to implement undo, redo functionality in our application, we will see the best data structure ...

Singly, Doubly and Circular Linked List

Image
So far, we have seen the implementation of Linked List. The Link List we implemented in the Lined List article was actually Singly Lined List. There is slightly difference between Singly, Doubly and Circular Linked List. We will see the difference and the importance of each in detail in this article.  Singly Linked List: In singly linked list, we keep head node in our linked list class. This head node is actually the starting point of our linked list. As shown: The above Figure shows the representation of Singly Linked List. We have a head node in our linked list class, it is the starting point of our linked list. Every node has some data and next pointer of type Node, that point to the next node of the list. Now I will tell you what is doubly linked list an why we need it. Doubly Linked List: In node of singly linked list, we have only one pointer of type node but in doubly linked list we have two pointers of type node. They are generally named as next and previous. The next point...

Linked List

Linked List: So far, we have studied arrays and pointers. We see, whenever we make an arrays we have to declare its size. i.e my_array[10] or some thing like my_array[10][10] (2D array) . We solve this problem by using pointers. We used to ask size of array from user and then, make dynamic memory of that size. We did something like int* my_array = new int[size] . Where size is given by user. Now let discuss what is its demerit of using this process of creating dynamic memory. Let say, user want to make array of size 20. Using dynamic memory we will create array of size 20 easily, but now let say, user wants to add one more item to this array. i.e he/ she wants to grow this array. What we can do using this strategy, we will make an array of size 21 or more then, transfer the data of previous array the delete the previous array. What's wrong with using this type of implementation? Lets consider, we have made an array of size 10,000,000. Now we want to grow this array to 10,000,001....