C++ program to replace every element with the least greater element on its right














































C++ program to replace every element with the least greater element on its right



Description:
This Program is to replace every element with the least greater element on its right of binary search tree.If the element is found it will replace it with the least greater element otherwise it will replace it by -1.

For Example:
Input :
The Elements are : 5 , 55 , 60 , 12 , 24
Output :
Elements entered : 12 60 -1 24 -1 (after replace every element with the least greater element on its right)

Program :
//C++ program to replace every element with the least greater element on its right #include <bits/stdc++.h> using namespace std; // tree node struct Node { int data; Node *left, *right; }; // returns a new tree Node Node* newNode(int data) { Node* temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp; } // A function to create binary search tree and find successor. void Tree(Node*& temp, int data ,Node*& succ ) { // If the tree is empty, return a new node if (temp == NULL) temp = newNode(data); // Otherwise, recur down the tree if (data < temp->data) { succ = temp; Tree(temp->left, data, succ); } else if (data > temp->data) Tree(temp->right, data, succ); } // Function to replace every element with the least greater element on its right void replace(int arr[], int n) { Node* temp = NULL; // start from right to left for (int i = n - 1; i >= 0; i--) { Node* succ = NULL; // insert current element into BST and find its inorder successor Tree(temp, arr[i], succ); // replace element by its inorder successor in BST if (succ) arr[i] = succ->data; else //if no inorder successor is found arr[i] = -1; } } int main() { int n=0,size; cout<<"Enter the size of array : "; cin>>size; int arr[size]; cout<<"Enter the elements in array : "; for(int i=0;i<size;i++) { cin>>arr[i]; } n = sizeof(arr)/sizeof(arr[0]); cout<<"\nElement are : "; replace(arr, n); for(int i=0;i<n;i++) { cout<<arr[i]<<" "; } return 0; }

More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 6240 19
C++ program to find an element into binary tree 772 16
C++ std::is_void 572 15
C++ program to find the closest element in binary search tree 916 19
C++ program to replace every element with the least greater element on its right 574 12
C++ program to delete an element into binary tree 785 24
C++ program to find maximum element between two nodes of binary search tree 700 20
C++ std::remove_copy_if with std::vectors 548 11
C++ program to print duplicate elements from the binary search tree 2937 15
C++ program to find depth of the deepest odd level node in binary tree 575 23
C++ program to remove duplicate elements from the binary search tree 1555 20
C++ std::rotate_copy with std::vector 530 14
C++ std::copy_n with std::vector 638 22
C++ std::copy_if with std::vector 1552 18
C++ program to print all the elements of binary search tree 7107 22
C++ std::reverse_copy with std::list 599 18
C++ program to print all the elements of binary tree 1158 18
C++ program to print all full nodes in a binary tree 573 25
C++ program to find sink odd nodes in binary tree 580 15
C++ std::is_copy_assignable 596 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 609 19
C++ std::is_copy_constructible 618 27
C++ program to delete an element into binary search tree 2873 18
C++ std::reverse_copy with std::vector 488 18
C++ std::rotate with std::vector 641 15
C++ program to check for symmetric binary tree using recursion 599 25
C++ program to maximum sum from a tree with adjacent levels not allowed 552 15
C++ std::copy_n with std::list 595 21
C++ program to check if two trees are identical using recursion 546 15
C++ std::copy_n 874 21
C++ std::copy_if with std::list 998 19
C++ program to print the nodes at odd levels of a tree 562 13
C++ program to find lowest common ancestor in a binary tree 690 29
C++ program to find depth of the deepest odd level leaf node 494 13
C++ std::remove_copy_if with std::list 667 20
C++ program to add all greater values to every node in a given binary search tree 638 15

Comments