-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BSTtoMinHeap.cpp
142 lines (106 loc) · 2.86 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
138
139
140
141
142
/*Convert the BST into a Min Heap with the condition 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;
struct Node
{
int data;
Node *left, *right;
};
/* Function to allocate a new node with the given data and NULL left and right pointers. */
struct Node* getNode(int data)
{
struct Node *newNode = new Node;
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
void preorderTraversal(Node*);
void inorderTraversal(Node *root, vector<int>& arr)
{
if (root == NULL)
return;
inorderTraversal(root->left, arr);
arr.push_back(root->data);
inorderTraversal(root->right, arr);
}
void BSTToMinHeap(Node *root, vector<int> arr, int *i)
{
if (root == NULL)
return;
root->data = arr[++*i];
//for left subtree
BSTToMinHeap(root->left, arr, i);
//for right subtree
BSTToMinHeap(root->right, arr, i);
}
// To convert the given BST to MIN HEAP
void convertToMinHeapUtil(Node *root)
{
// vector to store the data of all the
// nodes of the BST
vector<int> arr;
int i = -1;
// inorder traversal
inorderTraversal(root, arr);
// BST to MIN HEAP conversion
BSTToMinHeap(root, arr, &i);
}
// preorder traversal
void preorderTraversal(Node *root)
{
if (!root)
return;
// print the root's data first
cout << root->data << " ";
// recur on left subtree
preorderTraversal(root->left);
// recur on right subtree
preorderTraversal(root->right);
}
void convert(int keys[], int low, int high, Node* &root)
{
// base case
if (low > high) {
return;
}
// finding the middle element of the current range
int mid = (low + high) / 2;
// constructing a new node from the middle element and assign it to the root
root = getNode(keys[mid]);
convert(keys, low, mid - 1, root->left);
convert(keys, mid + 1, high, root->right);
}
Node* convert(int keys[], int n)
{
sort(keys, keys + n);
Node* root = nullptr;
convert(keys, 0, n - 1, root);
return root;
}
int main()
{
// BST formation
int n;
cout<<"Total no. of nodes:";
cin>>n;
int keys[n];
cout<<"Enter the elements of Tree";
for(int i=0;i<n;i++){
cin>>keys[i];
}
Node* root = convert(keys, n);
convertToMinHeapUtil(root);
cout << "Preorder Traversal:" << endl;
preorderTraversal(root);
return 0;
}
/*
OUTPUT:
Total no. of nodes:8
Enter the elements of Tree9 8 7 6 5 4 10 12
Preorder Traversal:
4 5 6 7 8 9 10 12
---------------------------------------------------------------------------------------------------------------------------------------------
Time Complexity: O(n) [n here is number of nodes in the tree]
Space Complexity: O(n)