
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert a BST to a Binary Tree such that sum of all greater keys is added to every key in C++
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 <bits/stdc++.h> using namespace std; //node structure of BST struct node{ int key; struct node* left; struct node* right; }; //creating new node with no child struct node* newNode(int key){ struct node* node = (struct node*)malloc(sizeof(struct node)); node->key = key; node->left = NULL; node->right = NULL; return (node); } //traversing BST in reverse inorder and adding sum void reverse_BST(struct node *root, int *sum_ptr){ if (root == NULL) return; reverse_BST(root->right, sum_ptr); //adding elements along the way *sum_ptr = *sum_ptr + root->key; root->key = *sum_ptr; reverse_BST(root->left, sum_ptr); } //Using sum and updating the values void change_greater(struct node *root){ int sum = 0; reverse_BST(root, &sum); } //printing inorder traversal void printInorder(struct node* node){ if (node == NULL) return; printInorder(node->left); cout << node->key << " " ; printInorder(node->right); } int main(){ node *root = newNode(5); root->left = newNode(2); root->right = newNode(13); cout << "Given Tree :" << endl; printInorder(root); change_greater(root); cout << endl; cout << "Modified Tree :" << endl; printInorder(root); return 0; }
Output
Given Tree : 2 5 13 Modified Tree : 20 18 13
- Related Articles
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Convert BST to Greater Tree in C++
- Add all greater values to every node in a given BST?
- Add all greater values to every node in a given BST in C++ ?
- Binary Search Tree to Greater Sum Tree in C++
- Add all greater values to every node in the given BST
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property in C++
- Maximum Sum BST in Binary Tree in C++
- C++ Program to Check if a Binary Tree is a BST
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Largest BST in a Binary Tree in C++
- A program to check if a binary tree is BST or not in C ?
- Program to check whether a binary tree is BST or not in Python

Advertisements