C++ Articles

Page 229 of 597

Maximum element between two nodes of BST in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 321 Views

Problem statementGiven an array of N elements and two integers A, B which belongs to the given array. Create a Binary Search Tree by inserting element from arr[0] to arr[n-1]. The task is to find the maximum element in the path from A to B.ExampleIf array is {24, 23, 15, 36, 19, 41, 25, 35} the we can form BST as follows −If we consider A = 19 and B = 41 then maximum element between these two nodes is 41AlgorithmFind Lowest Common Ancestor(LCA) of node A and B.Find maximum node between LCA and A. Let us call it as ...

Read More

Program to print Hut in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 350 Views

In this tutorial, we will be discussing a program to print a Hut pattern.For this, we will be provided with the width of the hut to be printed (say N). Our task is to print a hut structure of the given width using stars and a gate inside the hut using line characters.Example#include using namespace std; //printing the given hut structure int print_hut(int n){    int i, j, t;       if (n % 2 == 0) {          n++;       }       for (i = 0; i = n / 5)             || (j >= n / 5 && j < n - n / 5 && i == 0)             || (j == 0 && i >= n / 5)             || (j == t && i > n / 5)             || (i

Read More

Program to print Inverse Diamond pattern on C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 491 Views

In this tutorial, we will be discussing a program to print given inverse diamond pattern.For this, we will be provided with the value of N. Our task is to print an inverse the diamond pattern according to the height of 2N-1.Example#include using namespace std; //printing the inverse diamond pattern void printDiamond(int n){    cout

Read More

Program to print Kite Pattern in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 1K+ Views

In this tutorial, we will be discussing a program to print the given Kite pattern.For this, we will be taking the input as N=5. Our task is to print the given Kite structure with the overall height of 2N+1 = 5. This includes 9 lines for the upper diamond structure and 2 for the lower incomplete diamond structure.Example#include #include using namespace std; int main(){    int i, j, k, sp, space = 4;    char prt = '$';    //printing the upper half of the first diamond    for (i = 1; i = 1; sp--){          cout

Read More

Convert a Binary Tree into its Mirror Tree in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 241 Views

In this tutorial, we will be discussing a program to convert a binary tree into its mirror tree.For this, we will be provided with a binary tree. Our task will be to swap the values on the left and the right end creating a mirror tree from the given binary tree.Example#include using namespace std; //binary tree node structure struct Node{    int data;    struct Node* left;    struct Node* right; }; //creation of a new node with no child nodes struct Node* newNode(int data){    struct Node* node = (struct Node*)malloc(sizeof(struct Node));    node->data = data;    node->left = ...

Read More

Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 207 Views

In this tutorial, we will be discussing a program to convert a binary tree such that every node stores the sum of all nodes in its right subtree.For this, we will be provided with a binary tree. Our task is to return another tree where every node must be equal to the sum of the node and its right subtree.Example#include using namespace std; //node structure of tree struct Node {    int data;    Node *left, *right; }; //creation of a new node struct Node* createNode(int item){    Node* temp = new Node;    temp->data = item;    temp->left ...

Read More

Convert a Binary Tree to a Circular Doubly Link List in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 197 Views

In this tutorial, we will be discussing a program to convert a binary tree to a circular doubly linked list.For this, we will be provided with a binary tree. Our task will be to convert the left and right nodes to the left and right elements respectively. And take the inorder of the binary tree to be the sequence order of the circular linked listExample#include using namespace std; //node structure of the binary tree struct Node{    struct Node *left, *right;    int data; }; //appending rightlist to the end of leftlist Node *concatenate(Node *leftList, Node *rightList){    //if one ...

Read More

Convert a BST to a Binary Tree such that sum of all greater keys is added to every key in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 158 Views

In this tutorial, we will be discussing a program to convert a BST to a binary tree such that the sum of all greater keys is added to every key.For this, we will be provided with a Binary Search tree. Our task is to convert that tree into a binary tree with the sum of all greater keys added to the current key. This will be done by the reverse in order of the given BST along with having the sum of all the previous elements and finally adding it to the current element.Example#include using namespace std; //node structure ...

Read More

Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 560 Views

In this tutorial, we will be discussing a program to convert a binary tree to a threaded binary tree using a queue data structure.For this, we will be provided with a binary tree. Our task is to convert that particular binary tree into a threaded binary tree by adding additional routes for quicker inorder traversal with the help of a queue data structure.Example#include #include using namespace std; //node structure for threaded tree struct Node {    int key;    Node *left, *right;    bool isThreaded; }; //putting the inorder pattern into a queue void convert_queue(Node* root, std::queue* q){ ...

Read More

Binary Search in C++ Standard Template Library (STL)

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 361 Views

A binary search known as logarithmic search is a search algorithm that searches for an element in a sorted array. The algorithm recursively divides the array into two halves, if the element is found at the mid position then return otherwise call the divide and check again until the element is found.WorkingThe algorithm works by comparing the middle element of the sorted array with the element that is to be searched.If the search element is equal to the middle element, then return the index of the element.If the search element is greater than the middle element, search in the left ...

Read More
Showing 2281–2290 of 5,962 articles
« Prev 1 227 228 229 230 231 597 Next »
Advertisements