-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred-black-tree.cpp
More file actions
138 lines (125 loc) · 4.1 KB
/
Copy pathred-black-tree.cpp
File metadata and controls
138 lines (125 loc) · 4.1 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
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
#include <bits/stdc++.h>
using namespace std;
#define TOUPPER(str) transform(str.begin(), str.end(),str.begin(), ::toupper)
#define TOLOWER(str) transform(str.begin(), str.end(),str.begin(), ::tolower)
template < class T > T MAX( T a, T b ){return ( a > b ? a : b );}
template < class T > T MIN( T a, T b ){return ( a < b ? a : b );}
string getline(){string inputstr; while (inputstr.length()==0 ){ getline(cin, inputstr);} return inputstr;}
template <typename T> string to_str(const T str){stringstream stream; stream << str; return stream.str();}
string to_str(const vector<int> &v, const char delim){string s; for(int i = 0; i < v.size(); i++){s += to_string(v[i]); if(i != v.size() - 1) { s += delim;}} return s;}
template <typename T>int to_int(const T num){int val; stringstream stream; stream<<num; stream>>val; return val;}
vector<string> split(const string &s,const char delim){vector<string> elems;stringstream ss(s); string item;while(getline(ss,item,delim)){elems.push_back(item);}return elems;}
const bool RED = true, BLACK = false;
class Node {
public:
int key;
Node *left, *right;
bool color;
Node(int key, Node *left, Node *right, bool color) {
this->key = key;
this->left = left;
this->right = right;
this->color = color;
}
};
class BST {
Node *root = NULL;
private:
bool isRed(Node *node) {
return node == NULL ? BLACK : node->color;
}
Node* search(int key, Node *node) {
if(node == NULL) return NULL;
if(key == node->key){
return node;
} else if(key < node->key) {
return search(key, node->left);
} else{
return search(key, node->right);
}
};
Node* insert(int key, Node *node) {
if(node == NULL){
node = new Node(key, NULL, NULL, RED);
}
if(key < node->key) {
node->left = insert(key, node->left);
} else if(key > node->key) {
node->right = insert(key, node->right);
}
if(isRed(node->right) && !isRed(node->left)) {
node = rotateLeft(node);
}
if(isRed(node->left) && node->left != NULL && isRed(node->left->left)){
node = rotateRight(node);
}
if(isRed(node->left) && isRed(node->right)) {
node = flipColors(node);
}
return node;
};
Node* rotateLeft(Node *node) {
Node *x = node->right;
node->right = x->left;
x->left = node;
x->color = node->color;
node->color = RED;
return x;
};
Node* rotateRight(Node *node) {
Node *x = node->left;
node->left = x->right;
x->right = node;
x->color = node->color;
node->color = RED;
return x;
};
Node* flipColors(Node *node) {
node->left->color = BLACK;
node->right->color = BLACK;
node->color = RED;
return node;
};
public:
void insert(int key) {
root = insert(key, root);
}
Node* search(int key) {
return search(key, root);
}
BST(){
}
};
int solve(vector<int> &v, int searchKey){
BST *bst = new BST();
for(int i = 0; i < v.size(); i++) {
bst->insert(v[i]);
}
return bst->search(searchKey)->key;
}
int solve(){
int l, s;
scanf("%d %d", &l, &s);
vector<int> v(l);
for(int i = 0; i < l; i++){
scanf("%d", &v[i]);
}
return solve(v, s);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
freopen("1-input", "r", stdin);
freopen("2-output", "w", stdout);
int t;
scanf("%d", &t);
while(t--){
int output = solve();
int expected;
cin>>expected;
printf("==============testcase: %d===========\n", t);
cout<<"===>output:"<<output<<" expected:"<<expected<<" result:"<<(output==expected ? "true" : "false")<<endl;
}
return 0;
}