-
Notifications
You must be signed in to change notification settings - Fork 3
/
TESTt1.cpp
111 lines (96 loc) · 2.07 KB
/
TESTt1.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node{
int x; //Data
struct node *left;
struct node *right;
struct node *parent;
int y; //Priority
};
//Priority is Descending, Highest with Root
node* root;
void rotateleft(struct node*){
node* temp = node->left->parent;
node->left->parent = node->parent;
node->parent = temp;
}
void rotateright(struct node*){
node* temp = node->right->parent;
node->right->parent = node->parent;
node->parent = temp;
}
void heapify(struct node*){
while(node->left!=node->right==NULL){
int *MINl;
if(node->left<node->right){
MINl=node->left;
}else{
if(node->left>node->right){
MINl=node->right;
}else{
MINl=node->right;
}
}
if(node->y>MINl->y && MINl=node->left){
rotateright(MINl);
}else{
if(node->y>MINl->y && MINl=node->right){
rotateleft(MINl);
}
}
heapify(node->left);
heapify(node->right);
node=node->left;
}
node=root;
while(node->left!=node->right==NULL){
int *MINr;
if(node->left<node->right){
MINr=node->left;
}else{
if(node->left>node->right){
MINr=node->right;
}else{
MINr=node->right;
}
}
if(node->y>MINr->y && MINr=node->left){
rotateright(MINr);
}else{
if(node->y>MINr->y && MINr=node->right){
rotateleft(MINr);
}
}
heapify(node->left);
heapify(node->right);
node=node->right;
}
}
void insert(struct node*, int x, int y){
if(node->data==NULL){
node->x=x;
}
if(x<node->data){
node=node->left;
node->x=x;
}else{
if(x>node->data){
node=node->right;
node->x=x;
}
}
node->y=y;
heapify(node);
}
int main(){
int n,i=0,x,y;
cout<<"No of Nodes?";
cin>>n;
while(i<n){
cin>>x>>" ">>y;
insert(node,x,y);
i++;
}
}