|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +// Definition for a Node in the Binary Tree |
| 6 | +class Node { |
| 7 | +public: |
| 8 | + int data; |
| 9 | + Node* left; |
| 10 | + Node* right; |
| 11 | + |
| 12 | + // Constructor to create a new node with given value |
| 13 | + Node(int val) { |
| 14 | + this->data = val; |
| 15 | + this->left = NULL; |
| 16 | + this->right = NULL; |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +// Function to perform inorder traversal on the BST |
| 21 | +// This collects all node values in sorted order |
| 22 | +void inOrder(Node* root, vector<int>& nodes) { |
| 23 | + if (root == NULL) return; // Base case for recursion |
| 24 | + |
| 25 | + inOrder(root->left, nodes); // Traverse left subtree |
| 26 | + nodes.push_back(root->data); // Visit node and add its value to the vector |
| 27 | + inOrder(root->right, nodes); // Traverse right subtree |
| 28 | +} |
| 29 | + |
| 30 | +// Function to transform the BST to a Min Heap using preorder traversal |
| 31 | +// It assigns values from the sorted list to each node |
| 32 | +void inOrderToMinHeap(Node* root, vector<int>& nodes, int& index) { |
| 33 | + if (root == NULL) return; // Base case for recursion |
| 34 | + |
| 35 | + // Assign the current node with the next value in sorted order |
| 36 | + root->data = nodes[index++]; |
| 37 | + |
| 38 | + // Continue assigning values to left and right children |
| 39 | + inOrderToMinHeap(root->left, nodes, index); |
| 40 | + inOrderToMinHeap(root->right, nodes, index); |
| 41 | +} |
| 42 | + |
| 43 | +// Main function to convert BST to Min Heap |
| 44 | +void BST_to_MinHeap(Node* root) { |
| 45 | + if (root == NULL) { // Check for an empty tree |
| 46 | + cout << "Empty Tree" << endl; |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + vector<int> nodes; // Vector to store nodes in sorted order |
| 51 | + |
| 52 | + // Step 1: Store elements of BST in sorted order |
| 53 | + inOrder(root, nodes); |
| 54 | + |
| 55 | + int index = 0; |
| 56 | + // Step 2: Assign values from sorted list to nodes in preorder traversal |
| 57 | + inOrderToMinHeap(root, nodes, index); |
| 58 | +} |
| 59 | + |
| 60 | +// Function to print the tree in preorder to check Min Heap property |
| 61 | +void print(Node* root) { |
| 62 | + if (root == NULL) return; // Base case for recursion |
| 63 | + |
| 64 | + cout << root->data << " "; // Print current node |
| 65 | + print(root->left); // Print left subtree |
| 66 | + print(root->right); // Print right subtree |
| 67 | +} |
| 68 | + |
| 69 | +int main() { |
| 70 | + // Constructing a simple BST |
| 71 | + Node* root = new Node(4); |
| 72 | + root->left = new Node(2); |
| 73 | + root->right = new Node(6); |
| 74 | + root->left->left = new Node(1); |
| 75 | + root->left->right = new Node(3); |
| 76 | + root->right->left = new Node(5); |
| 77 | + root->right->right = new Node(7); |
| 78 | + |
| 79 | + // Convert BST to Min Heap |
| 80 | + BST_to_MinHeap(root); |
| 81 | + |
| 82 | + // Print the tree in preorder to verify Min Heap property |
| 83 | + cout << "Preorder of Min Heap : "; |
| 84 | + print(root); |
| 85 | + cout << endl; |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
0 commit comments