-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskew.h
More file actions
77 lines (71 loc) · 1.34 KB
/
skew.h
File metadata and controls
77 lines (71 loc) · 1.34 KB
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
#ifndef SKEW_H
#define SKEW_H
#include <iostream>
using namespace std;
class Skew{
private:
struct Node{
Node* left;
Node* right;
int data;
Node(int da):left(nullptr), right(nullptr), data(da){}
};
public:
Node* root;
Skew(): root(nullptr){}
void insert(int data){
Node* newNode = new Node(data);
root = merge(newNode, root);
}
void deleteMin(){
if(!root){
cout << "Heap Empty" << endl;
}else{
root = merge(root->left, root->right);
}
}
void printHeap(Node* t, int depth = 0){
if(t){
printHeap(t->right, depth+5);
for(int i = 0; i < depth; i++){
cout << " ";
}
cout << t->data << endl;
printHeap(t->left, depth+5);
}
}
void printHeap(){
printHeap(root);
}
Node* merge1(Node* h1, Node* h2){
if(h1->left == nullptr) h1->left = h2;
else{
h1->right = merge(h1->right, h2);
swapChildren(h1);
}
return h1;
}
Node* merge(Node* h1, Node* h2){
if(h1 == nullptr) return h2;
if(h2 == nullptr) return h1;
if(h1->data > h2->data) return merge1(h1, h2);
else return merge1(h2, h1);
}
void swapChildren(Node* h1){
Node* temp = h1->left;
h1->left = h1->right;
h1->right = temp;
}
int findMin(){
if(!root){
cout << "Heap Empty" << endl;
}else{
return root->data;
}
}
int FindMin(){
if(!root)
return root->data;
}
};
#endif