-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst.cpp
139 lines (113 loc) · 3.05 KB
/
bst.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
<iostream>
#include <queue>
#include <cmath>
using namespace std;
struct Details{
string name;
double ratio;
int position;
};
struct Compare{
bool operator ()( Details & el1, Details & el2){
if(el1.ratio < el2.ratio)
return true;
if(el1.ratio > el2.ratio)
return false;
if(el1.position < el2.position)
return true;
if(el1.position > el2.position)
return false;
return false;
}
};
struct Node{
int price;
Node *left, *right;
priority_queue<Details, vector<Details>, Compare> p_que;
Node(int price, Details new_el){
left = right = NULL;
this->price = price;
p_que.push(new_el);
}
void print(){
cout << p_que.top().name << endl;
}
};
class BST{
public:
Node *root;
BST(){
root = NULL;
}
Node * search(int key){
Node *v = root;
while(v != NULL && v->price != key){
if(v->price < key)
v = v->right;
else
v = v->left;
}
return v;
}
void insert(int price, Details new_el) {
if(!root)
root = new Node(price, new_el);
else{
Node *current = root;
Node *parent = NULL;
while(current){
if (current->price == price){
current->p_que.push(new_el);
return;
}
if (current->price > price) {
parent = current;
current = current->left;
}
else {
parent = current;
current = current->right;
}
}
current = new Node(price, new_el);
if (parent->price > current->price)
parent->left = current;
else
parent->right = current;
}
}
void remove(Node *remove_el){
if (!remove_el->p_que.empty())
remove_el->p_que.pop();
}
};
int main(){
int operations_num, voices, price;
float avg, ratio;
string name;
char oper_type;
BST tree;
Details new_element;
cin >> operations_num;
for(int i = 0; i < operations_num; i++){
cin >> oper_type;
if(oper_type == 'A'){
cin >> avg >> voices >> price >> name;
new_element.name = name;
new_element.ratio = avg / 5 * floor(voices / 1000);
new_element.position = i;
tree.insert(price, new_element);
}
else if(oper_type == 'S'){
cin >> price >> ratio;
Node *temp = tree.search(price);
if(!temp || temp->p_que.empty() || temp->p_que.top().ratio < ratio)
cout << "-" << endl;
else{
temp->print();
tree.remove(temp);
}
}
}
return 0;
}