Skip to content

Commit 748aab8

Browse files
authored
Create main.cpp
1 parent fd29ec0 commit 748aab8

File tree

1 file changed

+83
-0
lines changed
  • 19 - Heap Data Structure Problems/09 - Convert BST to Max Heap

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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; // Value of the node
9+
Node* left; // Left child
10+
Node* right; // Right child
11+
12+
// Constructor to create a new node with a given value
13+
Node(int val) {
14+
this->data = val;
15+
this->left = NULL; // Initially, left child is NULL
16+
this->right = NULL; // Initially, right child is NULL
17+
}
18+
};
19+
20+
// Function to perform in-order traversal of the BST
21+
// This will store the values of the tree nodes in an array
22+
void inOrder(Node* root, vector<int> &nodes) {
23+
if (root == NULL) return; // Base case: if node is NULL, return
24+
25+
inOrder(root->left, nodes); // Recur on the left child
26+
nodes.push_back(root->data); // Store the current node's data
27+
inOrder(root->right, nodes); // Recur on the right child
28+
}
29+
30+
// Function to convert the BST to a Max Heap
31+
// It uses the in-order traversal values stored in 'nodes' and sets the tree
32+
// values in a way that satisfies the Max Heap property
33+
void inOrderToMaxHeap(Node* root, vector<int>& nodes, int &index) {
34+
if (root == NULL) return; // Base case: if node is NULL, return
35+
36+
// Assign the current node's data from the largest remaining value
37+
root->data = nodes[index--]; // Decrement index after assigning to ensure we take the largest value first
38+
39+
// Recur on the left and right subtrees
40+
inOrderToMaxHeap(root->left, nodes, index);
41+
inOrderToMaxHeap(root->right, nodes, index);
42+
}
43+
44+
// Function to convert the entire BST to Max Heap
45+
void BST_to_MaxHeap(Node* root) {
46+
if (root == NULL) return; // Base case: if root is NULL, return
47+
48+
vector<int> nodes; // Vector to store the node values in in-order
49+
inOrder(root, nodes); // Perform in-order traversal and store node values in 'nodes'
50+
51+
int index = nodes.size() - 1; // Initialize the index to the last element (largest value)
52+
inOrderToMaxHeap(root, nodes, index); // Assign values from largest to smallest to the tree nodes
53+
}
54+
55+
// Function to print the tree in preorder (root, left, right) to check the Max Heap property
56+
void print(Node* root) {
57+
if (root == NULL) return; // Base case: if node is NULL, return
58+
59+
cout << root->data << " "; // Print the current node's data
60+
print(root->left); // Recursively print the left subtree
61+
print(root->right); // Recursively print the right subtree
62+
}
63+
64+
int main() {
65+
// Constructing a simple BST
66+
Node* root = new Node(4); // Root node with value 4
67+
root->left = new Node(2); // Left child of root with value 2
68+
root->right = new Node(6); // Right child of root with value 6
69+
root->left->left = new Node(1); // Left child of node 2 with value 1
70+
root->left->right = new Node(3); // Right child of node 2 with value 3
71+
root->right->left = new Node(5); // Left child of node 6 with value 5
72+
root->right->right = new Node(7); // Right child of node 6 with value 7
73+
74+
// Convert BST to Max Heap
75+
BST_to_MaxHeap(root);
76+
77+
// Print the tree in preorder to verify the Max Heap property
78+
cout << "Preorder of Max Heap: ";
79+
print(root); // Print the tree's preorder traversal (should satisfy Max Heap property)
80+
cout << endl;
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)