Found 349 Articles for Data Structure Algorithms

Level Order Tree Traversal in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 11:34:58

299 Views

In this section we will see the level-order traversal technique for binary search tree.Suppose we have one tree like this −The traversal sequence will be like: 10, 5, 16, 8, 15, 20, 23AlgorithmlevelOrderTraverse(root): Begin    define queue que to store nodes    insert root into the que.    while que is not empty, do       item := item present at front position of queue       print the value of item       if left of the item is not null, then          insert left of item into que       end ... Read More

Binary Tree Traversals in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 11:23:47

736 Views

In this section we will see different traversal algorithms to traverse keys present in binary search tree. These traversals are Inorder traversal, Preorder traversal, Postorder traversal and the level order traversal.Suppose we have one tree like this −The Inorder traversal sequence will be like − 5 8 10 15 16 20 23The Preorder traversal sequence will be like − 10 5 8 16 15 20 23The Postorder traversal sequence will be like − 8 5 15 23 20 16 10The Level-order traversal sequence will be like − 10, 5, 16, 8, 15, 20, 23AlgorithminorderTraverse(root): Begin    if root is not ... Read More

Binary Trees and Properties in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 11:04:14

4K+ Views

In this section we will see some important properties of one binary tree data structure. Suppose we have a binary tree like this.Some properties are −The maximum number of nodes at level ‘l’ will be $2^{l-1}$ . Here level is the number of nodes on path from root to the node, including the root itself. We are considering the level of root is 1.Maximum number of nodes present in binary tree of height h is $2^{h}-1$ . Here height is the max number of nodes on root to leaf path. Here we are considering height of a tree with one ... Read More

Binary Tree Representation in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 10:47:48

13K+ Views

Here we will see how to represent a binary tree in computers memory. There are two different methods for representing. These are using array and using linked list.Suppose we have one tree like this −The array representation stores the tree data by scanning elements using level order fashion. So it stores nodes level by level. If some element is missing, it left blank spaces for it. The representation of the above tree is like below −12345678910111213141510516-81520-------23The index 1 is holding the root, it has two children 5 and 16, they are placed at location 2 and 3. Some children are ... Read More

Operations on an Array in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 10:38:25

526 Views

Here we will see some basic operations of array data structure. These operations are −TraverseInsertionDeletionSearchUpdateThe traverse is scanning all elements of an array. The insert operation is adding some elements at given position in an array, delete is deleting element from an array and update the respective positions of other elements after deleting. The searching is to find some element that is present in an array, and update is updating the value of element at given position. Let us see one C++ example code to get better idea.Example Live Demo#include #include using namespace std; main(){    vector arr;    //insert elements ... Read More

Amortized time complexity in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 09:15:10

735 Views

Amortize AnalysisThis analysis is used when the occasional operation is very slow, but most of the operations which are executing very frequently are faster. In Data structures we need amortized analysis for Hash Tables, Disjoint Sets etc.In the Hash-table, the most of the time the searching time complexity is O(1), but sometimes it executes O(n) operations. When we want to search or insert an element in a hash table for most of the cases it is constant time taking the task, but when a collision occurs, it needs O(n) times operations for collision resolution.Aggregate MethodThe aggregate method is used to ... Read More

Matrix multiplication algorithm

Arnab Chakraborty
Updated on 27-Aug-2019 08:14:18

17K+ Views

In this section we will see how to multiply two matrices. The matrix multiplication can only be performed, if it satisfies this condition. Suppose two matrices are A and B, and their dimensions are A (m x n) and B (p x q) the resultant matrix can be found if and only if n = p. Then the order of the resultant matrix C will be (m x q).AlgorithmmatrixMultiply(A, B): Assume dimension of A is (m x n), dimension of B is (p x q) Begin    if n is not same as p, then exit    otherwise define C ... Read More

Step Count Method in Algorithm

Arnab Chakraborty
Updated on 27-Aug-2019 08:02:11

8K+ Views

The step count method is one of the method to analyze the algorithm. In this method, we count number of times one instruction is executing. From that we will try to find the complexity of the algorithm.Suppose we have one algorithm to perform sequential search. Suppose each instruction will take c1, c2, …. amount of time to execute, then we will try to find out the time complexity of this algorithmAlgorithmNumber of timesCostseqSearch(arr, n, key)i := 0while i < n, do   if arr[i] = key, then      break   end ifdonereturn i1n+1n0/11c1c2c3c4c5Now if we add the cost by multiplying the ... Read More

Operations on Queue in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 07:51:59

488 Views

Queue is First In First Out data structure. The queue is used in different area for graph traversal algorithms Breadth First Search etc. The queue has some primitive operations. Here we will see those operations of queue, and see one example using the queue ADT.The ADT (abstract datatype) is special kind of datatype, whose behavior is defined by a set of values and set of operations. The keyword “Abstract” is used as we can use these datatypes, we can perform different operations. But how those operations are working that is totally hidden from the user. The ADT is made of ... Read More

Tail Recursion in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 07:45:44

3K+ Views

Here we will see what is tail recursion. The tail recursion is basically using the recursive function as the last statement of the function. So when nothing is left to do after coming back from the recursive call, that is called tail recursion. We will see one example of tail recursion.Example Live Demo#include using namespace std; void printN(int n){    if(n < 0){       return;    }    cout

Advertisements