Skip to content

Commit 547d33e

Browse files
add implementations for checking max heap and converting BST to min/max heap
1 parent 513467e commit 547d33e

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

26Heap/11maxheap_from_bst.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
class Node{
4+
public:
5+
int val;
6+
Node* left;
7+
Node* right;
8+
Node(int val){
9+
this->val = val;
10+
this->left = nullptr;
11+
this->right = nullptr;
12+
}
13+
};
14+
15+
void revInorder(Node *root, vector<int> &dec){//make a decreasing inorder array
16+
if (root==nullptr) return;
17+
revInorder(root->right, dec);
18+
dec.push_back(root->val);
19+
revInorder(root->left, dec);
20+
}
21+
22+
void printTree(Node *root){
23+
queue<Node*> q;
24+
q.push(root);
25+
while(q.size()>0){
26+
Node* temp = q.front();
27+
q.pop();
28+
cout<<temp->val<<" ";
29+
if(temp->left!=nullptr) q.push(temp->left);
30+
if(temp->right!=nullptr) q.push(temp->right);
31+
}
32+
cout<<endl;
33+
}
34+
//modify the to have preorde using the reverse sorted array
35+
void preorder(Node *root, vector<int> &dec, int& i){
36+
if (root==nullptr) return;
37+
root->val = dec[i++];
38+
preorder(root->left, dec, i);
39+
preorder(root->right, dec, i);
40+
}
41+
42+
int main(){
43+
Node* a = new Node(10);
44+
Node* b = new Node(5);
45+
Node* c = new Node(16);
46+
Node* d = new Node(1);
47+
Node* e = new Node(8);
48+
Node* f = new Node(12);
49+
Node* g = new Node(20);
50+
a->left = b; a->right = c;
51+
b->left = d; b->right = e;
52+
c->left = f; c->right = g;
53+
vector<int> dec;//reverse inorder
54+
revInorder(a, dec);
55+
cout<<"before converting to maxheap:"<<endl;
56+
printTree(a);
57+
int i = 0;
58+
preorder(a, dec, i);
59+
cout<<"after converting to maxheap:"<<endl;
60+
printTree(a);
61+
return 0;
62+
}
63+
/*
64+
before converting to maxheap:
65+
10 5 16 1 8 12 20
66+
after converting to maxheap:
67+
20 16 8 12 10 5 1
68+
*/

26Heap/11maxheap_from_bst.exe

177 KB
Binary file not shown.

26Heap/12minheap_from_bst.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
class Node{
4+
public:
5+
int val;
6+
Node* left;
7+
Node* right;
8+
Node(int val){
9+
this->val = val;
10+
this->left = nullptr;
11+
this->right = nullptr;
12+
}
13+
};
14+
15+
void Inorder(Node *root, vector<int> &dec){//make a decreasing inorder array
16+
if (root==nullptr) return;
17+
Inorder(root->left, dec);
18+
dec.push_back(root->val);
19+
Inorder(root->right, dec);
20+
}
21+
22+
void printTree(Node *root){
23+
queue<Node*> q;
24+
q.push(root);
25+
while(q.size()>0){
26+
Node* temp = q.front();
27+
q.pop();
28+
cout<<temp->val<<" ";
29+
if(temp->left!=nullptr) q.push(temp->left);
30+
if(temp->right!=nullptr) q.push(temp->right);
31+
}
32+
cout<<endl;
33+
}
34+
//modify the to have preorder using the sorted array
35+
void preorder(Node *root, vector<int> &dec, int& i){
36+
if (root==nullptr) return;
37+
root->val = dec[i++];
38+
preorder(root->left, dec, i);
39+
preorder(root->right, dec, i);
40+
}
41+
42+
int main(){
43+
Node* a = new Node(10);
44+
Node* b = new Node(5);
45+
Node* c = new Node(16);
46+
Node* d = new Node(1);
47+
Node* e = new Node(8);
48+
Node* f = new Node(12);
49+
Node* g = new Node(20);
50+
a->left = b; a->right = c;
51+
b->left = d; b->right = e;
52+
c->left = f; c->right = g;
53+
vector<int> dec;//reverse inorder
54+
Inorder(a, dec);
55+
cout<<"before converting to minheap:"<<endl;
56+
printTree(a);
57+
int i = 0;
58+
preorder(a, dec, i);
59+
cout<<"after converting to minheap:"<<endl;
60+
printTree(a);
61+
return 0;
62+
}
63+
/*
64+
before converting to minheap:
65+
10 5 16 1 8 12 20
66+
after converting to minheap:
67+
1 5 12 8 10 16 20
68+
*/

26Heap/12minheap_from_bst.exe

177 KB
Binary file not shown.

26Heap/13check_maxheap.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
/*
4+
Check whether the binary tree is a maxheap or not
5+
*/
6+
class Node{
7+
public:
8+
int val;
9+
Node* left;
10+
Node* right;
11+
Node(int val){
12+
this->val = val;
13+
this->left = nullptr;
14+
this->right = nullptr;
15+
}
16+
};
17+
bool ismax(Node *root){
18+
if(root==nullptr) return true;
19+
if(root->left && root->val<root->left->val) return false;
20+
if(root->right && root->val<root->right->val) return false;
21+
return ismax(root->left) && ismax(root->right);
22+
}
23+
bool iscbt(Node* root){
24+
if(root==nullptr) return true;
25+
//if either child is null
26+
if(root->left==nullptr and root->right) return false;
27+
if(root->left and root->right==nullptr) return false;
28+
return iscbt(root->left) && iscbt(root->right);
29+
}
30+
31+
int main(){
32+
Node* a = new Node(20);
33+
Node* b = new Node(15);
34+
Node* c = new Node(10);
35+
Node* d = new Node(8);
36+
Node* e = new Node(11);
37+
// Node* f = nullptr; //with this it is not maxheap
38+
Node* f = new Node(4); //with this it is a maxheap
39+
Node* g = new Node(6);
40+
a->left = b; a->right = c;
41+
b->left = d; b->right = e;
42+
c->left = f; c->right = g;
43+
// cout<<iscbt(a);
44+
// cout<<ismax(a);
45+
if(ismax(a) && iscbt(a)) cout<<"Binary tree is a maxheap"<<endl;
46+
else cout<<"Binary tree is not a maxheap"<<endl;
47+
return 0;
48+
}

26Heap/13check_maxheap.exe

129 KB
Binary file not shown.

0 commit comments

Comments
 (0)