Question is to convert bst to minheap.
MY ATTEMPT:-
#include
#include
using namespace std;
class Node{
public:
int data;
Nodeleft;
Noderight;
Node(int d){
Noden=new Node(d);
left=NULL;
right=NULL;
}
};
Node constructTree(){
cout<<“Enter”<<endl;
int d;
cin>>d;
if(d==-1){
return NULL;
}
Node* n=new Node(d);
n->left=constructTree();
n->right=constructTree();
return n;
}
void inorder(vector &v,Node*root){
if(root==NULL){
return;
}
inorder(v,root->left);
v.push_back(root->data);
inorder(v,root->right);
}
void solve(vector v,Node* &root){
static int i=0;
if(root==NULL) return;
if(i<v.size()){
root->data=v[i];
i++;
}
solve(v,root->left);
solve(v,root->right);
}
void bfs(Node *root)
{
if (root == NULL) return;
// Create an empty queue for
// level order traversal
queue<Node *> q;
// to store front element of
// queue.
Node *curr;
// Enqueue Root and NULL node.
q.push(root);
q.push(NULL);
while (q.size() > 1)
{
curr = q.front();
q.pop();
// condition to check
// occurrence of next
// level.
if (curr == NULL)
{
q.push(NULL);
cout << "\n";
}
else {
// pushing left child of
// current node.
if(curr->left)
q.push(curr->left);
// pushing right child of
// current node.
if(curr->right)
q.push(curr->right);
cout << curr->data << " ";
}
}
}
int main(){
Node*root=NULL;
vector v;
root=constructTree();
inorder(v,root);
solve(v,root);
bfs(root);
return 0;
}