Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 40 of 44

Program to print the DFS traversal step-wise using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 490 Views

In this tutorial, we will be discussing a program to print the steps of the traversal using Depth First Search in a given binary tree.This would include every step that occurs in the depth-first search including the backtracking procedure as well.During DFS, we will be traversing each node and simultaneously storing the parent node and the edge used. During the traversal, if the adjacent edge has been visited, then the exact node can be printed as a step in the depth-first search.Example#include using namespace std; const int N = 1000; vector adj[N]; //printing the steps in DFS traversal void ...

Read More

Program to print the first shortest root to leaf path in a Binary Tree using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 329 Views

In this tutorial, we will be discussing a program to print the first shortest root to leaf path in a Binary Tree.In this, we will be given a binary tree with distinct values, and we have to find the shortest path from the root node to the leaf node in a given binary tree.To solve this, we can use a queue to perform level order traversal in a binary tree and store the nodes in the shortest path. For this, once we reach the leftmost node for the final level, we retrieve the values from the queue and print the ...

Read More

Program to print the longest common substring using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 712 Views

In this tutorial, we will be discussing a program to print the longest common substring.For this we will be given two strings say A and B. We have to print the longest substring common to the two input strings A and B.For example, if we are given with “HelloWorld” and “world book”. Then the longest common substring, in this case, will be the “world”.Example#include #include #include using namespace std; void print_lstring(char* X, char* Y, int m, int n){    int longest[m + 1][n + 1];    int len = 0;    int row, col;    for (int i = 0; i

Read More

Program to print root to leaf paths without using recursion using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 215 Views

In this tutorial, we will be discussing a program to print the path from the root node to all the leaf nodes in a given binary tree.For example, let us say we have the following binary treeIn this binary tree, we have 4 leaf nodes. Therefore we can have 4 paths from the root node to the leaf node.To solve this, we will use the iterative approach. While doing preorder traversal of the binary tree we can store parent pointers in a map. Whenever during traversal we encounter a leaf node we can easily print its path from the root ...

Read More

Program to print prime numbers in a given range using C++ STL

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 350 Views

In this tutorial, we will be discussing a program to print prime numbers for a given range of numbers using the C++ Standard Template Library.In this, we will be given two numbers say a and b. The task is to print all the coming prime numbers in this range. For this, we will be using the Sieve of Eratosthenes method by running it as a subroutine. Simultaneously we will be storing all the prime numbers in a vector and finally printing them all.Example#include using namespace std; typedef unsigned long long int unll; vector eratosthemes(unll n){    vector prime_num(n+1, true);   ...

Read More

Program to print path from root to a given node in a binary tree using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 348 Views

In this tutorial, we will be discussing a program to print the path from root to a given node in a binary tree.For a given binary tree having distinct nodes, we have to print the complete path to reach a particularly given node from the root node of the binary tree.To solve this problem, we will use recursion. While traversing the binary tree, we will recursively search for the particular element to be found. Also alongside we will be storing the path to reach the element to be searched.Example#include using namespace std; struct Node{    int data;    Node ...

Read More

Program to print path from root to all nodes in a Complete Binary Tree using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 250 Views

In this tutorial, we will be discussing a program to print the path from the root node of a binary tree to all the other nodes present in the given binary tree.In this program, we will be given a number N, denoting the number of elements present in the binary tree from 1 to N; 1 being the root node of the binary tree. Therefore, our task is to print all the possible paths from the root node to the various other elements present in the binary tree.To solve this program, we know that for given node I, its left ...

Read More

Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 205 Views

In this tutorial, we will be discussing a program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are coprime.In this, we will be given an integer N, we have to print N integers less than 109 such that no two consecutive numbers are coprime but a pair of 3 consecutive integers must be coprime.For example, let us say we have the integer 4. Then the numbers that follow both of the above-provided conditions are6 15 35 14Example#include using namespace std; #define limit 1000000000 #define MAX_PRIME 2000000 #define MAX 1000000 #define I_MAX ...

Read More

Program to print nodes in the Top View of Binary Tree using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 289 Views

In this tutorial, we will be discussing a program to print all the nodes that appear in the top view of a given binary tree.For a particular binary tree, a node appears in its top view if it is the very first node at its horizontal distance. Horizontal distance for the left node of a node x is x-1 and for the right node of node x is x+1.To solve this, we will do the level order traversal so that we get the topmost node for a particular level before the other nodes present at that level. Further, we will ...

Read More

Program to print nodes between two given level numbers of a binary tree using C++

Ayush Gupta
Ayush Gupta
Updated on 01-Nov-2019 202 Views

In this tutorial, we will be discussing a program to print nodes between the two given level numbers of a binary tree.In this, we will be given a low level and a high level for a particular binary tree and we have to print all the elements between the given levels.To solve this we can use queue-based level traversal. While moving through inorder traversal we can have a marking node at the end of each level. Then we can go to each level and print its nodes if the marking node exists between the given levels.Example#include #include using ...

Read More
Showing 391–400 of 433 articles
« Prev 1 38 39 40 41 42 44 Next »
Advertisements