-
Notifications
You must be signed in to change notification settings - Fork 1
/
RB.cpp
153 lines (139 loc) · 2.68 KB
/
RB.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
bool red;
struct node *left, *right;
};
struct node* newNode(int value)
{
struct node *newN = new struct node;
newN->data = value;
newN->red = true;
newN->left = NULL;
newN->right = NULL;
return newN;
}
bool color(struct node *n)
{
if(n == NULL)
return false;
return n->red;
}
void rightRotate(struct node **n)
{
struct node *temp;
struct node *temp1;
temp = (*n)->left;
temp1 = *n;
temp1->left = temp->right;
temp->right = temp1;
bool t;
t = temp->red;
temp->red = temp1->red;
temp1->red = t;
*n = temp;
}
void leftRotate(struct node **n)
{
struct node *temp;
struct node *temp1;
temp = (*n)->right;
temp1 = *n;
temp1->right = temp->left;
temp->left = temp1;
bool t;
t = temp->red;
temp->red = temp1->red;
temp1->red = t;
*n = temp;
}
void insert(int value, struct node **node)
{
//if((*node)->NIL == true)
if(*node == NULL)
{
*node = newNode(value);
return;
}
if(value < (*node)->data)
{
insert(value, &(*node)->left);
if(color((*node)->left))
{
if(color((*node)->right) && (color((*node)->left->left) || color((*node)->left->right)))
{
(*node)->red = true;
(*node)->left->red = false;
(*node)->right->red = false;
}
else if(value < (*node)->left->data && color((*node)->left->left)) // LEFT red > LEFT red
{
rightRotate(node);
}
else if(value >= (*node)->left->data && color((*node)->left->right)) // LEFT red > RIGHT red
{
leftRotate(&(*node)->left);
rightRotate(node);
}
}
}
else
{
insert(value, &(*node)->right);
if(color((*node)->right))
{
if(color((*node)->left) && (color((*node)->right->left) || color((*node)->right->right)))
{
(*node)->red = true;
(*node)->left->red = false;
(*node)->right->red = false;
}
else if(value >= (*node)->right->data && color((*node)->right->right)) // RIGHT red > RIGHT red
{
leftRotate(node);
}
else if(value < (*node)->right->data && color((*node)->right->left)) // RIGHT red > LEFT red
{
rightRotate(&(*node)->right);
leftRotate(node);
}
}
}
}
void insertTree(int value, struct node **node)
{
insert(value, node);
(*node)->red = false;
}
/*
void inorder(struct node *n)
{
if(n->NIL)
return;
inorder(n->left);
cout << n->data << " ";
inorder(n->right);
}*/
int main()
{
ios::sync_with_stdio(false);
int MAX = 5000000;
double elapsed_secs;
clock_t begin;
clock_t end;
struct node *root = NULL;
int temp;
begin = clock();
for(int i=1;i<=MAX;i++)
{
cin >> temp;
insertTree(temp, &root);
}
end = clock();
cout << root->data << endl;
elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << elapsed_secs << endl;
return 0;
}