-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BSTtoMinHeap.cpp
137 lines (133 loc) · 3.44 KB
/
BSTtoMinHeap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Program to convert the given BST into a Min Heap.
Conditions are that all the values in the left subtree of a node should be less than all the values in the right subtree of the node.*/
#include <bits/stdc++.h>
using namespace std;
/* Data structure to store a binary tree node*/
struct Node
{
int key;
Node *left, *right;
};
/* Function to create a new binary tree node having a given key*/
Node* newNode(int key)
{
Node* node = new Node;
node->key = key;
node->left = node->right = nullptr;
return node;
}
/* Recursive function to insert a key into a BST*/
Node* insert(Node* root, int key)
{
/* if the root is null, create a new node and return it*/
if (root == nullptr) {
return newNode(key);
}
/* if the given key is less than the root node, recur for the left subtree*/
if (key < root->key) {
root->left = insert(root->left, key);
}
/* if the given key is more than the root node, recur for the right subtree*/
else {
root->right = insert(root->right, key);
}
return root;
}
/* Helper function to perform level order traversal on a binary tree*/
void printLevelOrderTraversal(Node* root)
{
/* base case: empty tree*/
if (root == nullptr) {
return;
}
queue<Node*> q;
q.push(root);
while (!q.empty())
{
int n = q.size();
while (n--)
{
Node* front = q.front();
q.pop();
cout << front->key << ' ';
if (front->left) {
q.push(front->left);
}
if (front->right) {
q.push(front->right);
}
}
cout << endl;
}
}
/* Function to perform inorder traversal on a given binary tree and
enqueue all nodes (in encountered order)*/
void inorder(Node* root, queue<int> &keys)
{
if (root == NULL) {
return;
}
inorder(root->left, keys);
keys.push(root->key);
inorder(root->right, keys);
}
/* Function to perform preorder traversal on a given binary tree.
Assign each encountered node with the next key from the queue*/
void preorder(Node* root, queue<int> &keys)
{
/* base case: empty tree*/
if (root == nullptr) {
return;
}
/* replace the root's key value with the next key from the queue*/
root->key = keys.front();
keys.pop();
/* process left subtree*/
preorder(root->left, keys);
/* process right subtree*/
preorder(root->right, keys);
}
/* Function to convert a BST into a min-heap*/
void convert(Node* root)
{
/* maintain a queue to store inorder traversal on the tree*/
queue<int> keys;
/* fill the queue in an inorder fashion*/
inorder(root, keys);
/* traverse tree in preorder fashion, and for each encountered node,
dequeue a key and assign it to the node*/
preorder(root, keys);
}
int main()
{
vector<int> keys;
int n;
cout<<"Enter the no. of nodes"<<endl;
cin>>n;
cout<<"Enter the keys to construct a binary tree:"<<endl;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
keys.push_back(a);
}
Node* root = nullptr;
for (int key: keys) {
root = insert(root, key);
}
convert(root);
printLevelOrderTraversal(root);
return 0;
}
/*
Sample Input/Output:
Output: Enter the no. of nodes
Input: 7
Output: Enter the keys to construct a binary tree:
Input: 4 2 6 1 3 5 7
Output: 1
2 5
3 4 6 7
Time complexity:O(n)
Space complexity:O(n)
*/