
- 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 BST to Min Heap in C++
In this tutorial, we will be discussing a program to convert a binary search tree to a min heap.
For this we will be provided with a binary search tree. Our task is to convert the given binary search tree into a min heap such that following the condition of the binary search tree when elements are compared with themselves.
Example
#include <bits/stdc++.h> using namespace std; //node structure of BST struct Node { int data; Node *left, *right; }; //node creation struct Node* getNode(int data) { struct Node *newNode = new Node; newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } //performing preorder traversal void preorderTraversal(Node*); //storing values in sorted fashion //with inorder traversal void inorderTraversal(Node *root, vector<int>& arr) { if (root == NULL) return; inorderTraversal(root->left, arr); arr.push_back(root->data); inorderTraversal(root->right, arr); } //converting BST to min heap void convert_BSPheap(Node *root, vector<int> arr, int *i) { if (root == NULL) return; root->data = arr[++*i]; convert_BSPheap(root->left, arr, i); convert_BSPheap(root->right, arr, i); } //converting to min heap void convert_minheap(Node *root) { //vector storing the values of nodes vector<int> arr; int i = -1; //moving via inorder traversal inorderTraversal(root, arr); convert_BSPheap(root, arr, &i); } //performing preorder traversal void preorderTraversal(Node *root) { if (!root) return; cout << root->data << " "; preorderTraversal(root->left); preorderTraversal(root->right); } int main() { struct Node *root = getNode(4); root->left = getNode(2); root->right = getNode(6); root->left->left = getNode(1); root->left->right = getNode(3); root->right->left = getNode(5); root->right->right = getNode(7); convert_minheap(root); cout << "Preorder Traversal:" << endl; preorderTraversal(root); return 0; }
output
Preorder Traversal: 1 2 3 4 5 6 7
- Related Articles
- Convert min Heap to max Heap in C++
- Convert BST to Max Heap in C++
- C++ Program to Implement Min Heap
- Maximum element in min heap in C++
- Convert a normal BST to Balanced BST in C++
- K’th Least Element in a Min-Heap in C++
- Convert BST to Greater Tree in C++
- Print all nodes less than a value x in a Min Heap in C++
- Program to make almost BST to exact BST in python
- Program to check heap is forming max heap or not in Python
- MySQL query to convert a string like “1h 15 min” into 75 minutes?
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- Heap Sort in C#
- Binomial Heap in C++?
- Max Heap in Java

Advertisements