@@ -25,6 +25,7 @@ namespace red_black {
25
25
red_black_tree () {
26
26
nil = new node<Key, Value>(Key (), Value (), BLACK);
27
27
root = nil;
28
+ root->parent = root->left = root->right = nil;
28
29
sz = 0 ;
29
30
}
30
31
// dtor: destroy the red black tree
@@ -57,9 +58,9 @@ namespace red_black {
57
58
node<Key, Value>* root;
58
59
node<Key, Value>* nil;
59
60
size_t sz;
60
- void left_rotate (node<Key, Value>* x);
61
- void right_rotate (node<Key, Value>* x);
62
- void insert_fixup (node<Key, Value>* z);
61
+ void left_rotate (node<Key, Value>* & x);
62
+ void right_rotate (node<Key, Value>* & x);
63
+ void insert_fixup (node<Key, Value>* & z);
63
64
void delete_fixup (node<Key, Value>* x);
64
65
node<Key, Value>* minimum (node<Key, Value>* x);
65
66
node<Key, Value>* maximum (node<Key, Value>* x);
@@ -70,84 +71,90 @@ namespace red_black {
70
71
71
72
// left_rotate: rotate the tree to the left
72
73
template <class Key , class Value >
73
- inline void red_black_tree<Key, Value>::left_rotate(node<Key, Value>* x)
74
+ inline void red_black_tree<Key, Value>::left_rotate(node<Key, Value>* & x)
74
75
{
75
- auto y = x->right ; // set y
76
- x->right = y->left ; // turn y's left subtree into x's right subtree
77
- if (y->left != nil)
78
- y->left ->parent = x;
79
- y->parent = x->parent ; // link x's parent to y
80
- if (x->parent == nil)
81
- root = y;
82
- else if (x == x->parent ->left )
83
- x->parent ->left = y;
84
- else
85
- x->parent ->right = y;
86
- y->left = x; // put x on y's left
87
- x->parent = y;
76
+ if (x->right != nil) // performing left_rotate need node at the right of x
77
+ {
78
+ auto y = x->right ; // set y
79
+ x->right = y->left ; // turn y's left subtree into x's right subtree
80
+ if (y->left != nil)
81
+ y->left ->parent = x;
82
+ y->parent = x->parent ; // link x's parent to y
83
+ if (x->parent == nil)
84
+ root = y;
85
+ else if ((x->parent != nil) && (x == x->parent ->left ))
86
+ x->parent ->left = y;
87
+ else
88
+ x->parent ->right = y;
89
+ y->left = x; // put x on y's left
90
+ x->parent = y;
91
+ }
88
92
}
89
93
90
94
// right_rotate: rotate the tree to the right
91
95
template <class Key , class Value >
92
- inline void red_black_tree<Key, Value>::right_rotate(node<Key, Value>* x)
96
+ inline void red_black_tree<Key, Value>::right_rotate(node<Key, Value>* & x)
93
97
{
94
- auto y = x->left ; // set y
95
- x->left = y->right ; // turn y's right subtree into x's left subtree
96
- if (y->right != nil)
97
- y->right ->parent = x;
98
- y->parent = x->parent ; // link x's parent to y
99
- if (x->parent == nil)
100
- root = y;
101
- else if (x == x->parent ->right )
102
- x->parent ->right = y;
103
- else
104
- x->parent ->left = y;
105
- y->right = x; // put x on y's right
106
- x->parent = y;
98
+ if (x->left != nil){ // performing right rotate need node at the left of x
99
+ auto y = x->left ; // set y
100
+ x->left = y->right ; // turn y's right subtree into x's left subtree
101
+ if (y->right != nil)
102
+ y->right ->parent = x;
103
+ y->parent = x->parent ; // link x's parent to y
104
+ if (x->parent == nil)
105
+ root = y;
106
+ else if ((x->parent != nil) && (x == x->parent ->right ))
107
+ x->parent ->right = y;
108
+ else
109
+ x->parent ->left = y;
110
+ y->right = x; // put x on y's right
111
+ x->parent = y;
112
+ }
107
113
}
108
114
109
115
// insert_fixup: fix the tree after insertion
110
116
template <class Key , class Value >
111
- inline void red_black_tree<Key, Value>::insert_fixup(node<Key, Value>* z)
117
+ inline void red_black_tree<Key, Value>::insert_fixup(node<Key, Value>* & z)
112
118
{
113
119
while (z->parent ->color == RED) {
114
120
if (z->parent == z->parent ->parent ->left ) {
115
121
auto y = z->parent ->parent ->right ;
116
- if (y->color == RED) { // case 1
122
+ if (y!=nil && y ->color == RED) { // case 1
117
123
z->parent ->color = BLACK;
118
124
y->color = BLACK;
119
125
z->parent ->parent ->color = RED;
120
126
z = z->parent ->parent ;
121
127
}
122
- else if (z == z-> parent -> right ) { // case 2
123
- z = z->parent ;
124
- left_rotate (z) ;
125
- }
126
- else { // case 3
127
- z->parent ->color = BLACK;
128
+ else {
129
+ if ( z == z->parent -> right ) { // case 2
130
+ z = z-> parent ;
131
+ left_rotate (z);
132
+ }
133
+ z->parent ->color = BLACK; // case 3
128
134
z->parent ->parent ->color = RED;
129
135
right_rotate (z->parent ->parent );
130
136
}
131
137
}
132
138
else {
133
139
auto y = z->parent ->parent ->left ;
134
- if (y->color == RED) { // case 1
140
+ if (y!= nil && y ->color == RED) { // case 1
135
141
z->parent ->color = BLACK;
136
142
y->color = BLACK;
137
143
z->parent ->parent ->color = RED;
138
144
z = z->parent ->parent ;
139
145
}
140
- else if (z == z-> parent -> left ) { // case 2
141
- z = z->parent ;
142
- right_rotate (z) ;
143
- }
144
- else { // case 3
145
- z->parent ->color = BLACK;
146
+ else {
147
+ if ( z == z->parent -> left ) { // case 2
148
+ z = z-> parent ;
149
+ right_rotate (z);
150
+ }
151
+ z->parent ->color = BLACK; // case 3
146
152
z->parent ->parent ->color = RED;
147
153
left_rotate (z->parent ->parent );
148
- }
154
+ }
149
155
}
150
156
}
157
+ root->color = BLACK;
151
158
}
152
159
153
160
// minimum: find the minimum node in the tree
@@ -175,8 +182,13 @@ namespace red_black {
175
182
y = x;
176
183
if (z->key < x->key )
177
184
x = x->left ;
178
- else
185
+ else if (z-> key > x-> key )
179
186
x = x->right ;
187
+ else {
188
+ x->value = value;
189
+ --sz;
190
+ return ;
191
+ }
180
192
}
181
193
z->parent = y;
182
194
if (y==nil)
0 commit comments