|
| 1 | +// Copyright 2021 gotomicro |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tree |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + |
| 20 | + "github.com/gotomicro/ekit" |
| 21 | +) |
| 22 | + |
| 23 | +type color bool |
| 24 | + |
| 25 | +const ( |
| 26 | + Red color = false |
| 27 | + Black color = true |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + ErrRBTreeSameRBNode = errors.New("ekit: RBTree不能添加重复节点Key") |
| 32 | + ErrRBTreeNotRBNode = errors.New("ekit: RBTree不存在节点Key") |
| 33 | + // errRBTreeCantRepaceNil = errors.New("ekit: RBTree不能将节点替换为nil") |
| 34 | +) |
| 35 | + |
| 36 | +type RBTree[K any, V any] struct { |
| 37 | + root *rbNode[K, V] |
| 38 | + compare ekit.Comparator[K] |
| 39 | + size int |
| 40 | +} |
| 41 | + |
| 42 | +func (rb *RBTree[K, V]) Size() int { |
| 43 | + if rb == nil { |
| 44 | + return 0 |
| 45 | + } |
| 46 | + return rb.size |
| 47 | +} |
| 48 | + |
| 49 | +type rbNode[K any, V any] struct { |
| 50 | + color color |
| 51 | + key K |
| 52 | + value V |
| 53 | + left, right, parent *rbNode[K, V] |
| 54 | +} |
| 55 | + |
| 56 | +func (node *rbNode[K, V]) setNode(v V) { |
| 57 | + if node == nil { |
| 58 | + return |
| 59 | + } |
| 60 | + node.value = v |
| 61 | +} |
| 62 | + |
| 63 | +// NewRBTree 构建红黑树 |
| 64 | +func NewRBTree[K any, V any](compare ekit.Comparator[K]) *RBTree[K, V] { |
| 65 | + return &RBTree[K, V]{ |
| 66 | + compare: compare, |
| 67 | + root: nil, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func newRBNode[K any, V any](key K, value V) *rbNode[K, V] { |
| 72 | + return &rbNode[K, V]{ |
| 73 | + key: key, |
| 74 | + value: value, |
| 75 | + color: Red, |
| 76 | + left: nil, |
| 77 | + right: nil, |
| 78 | + parent: nil, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Add 增加节点 |
| 83 | +func (rb *RBTree[K, V]) Add(key K, value V) error { |
| 84 | + return rb.addNode(newRBNode(key, value)) |
| 85 | +} |
| 86 | + |
| 87 | +// Delete 删除节点 |
| 88 | +func (rb *RBTree[K, V]) Delete(key K) { |
| 89 | + if node := rb.findNode(key); node != nil { |
| 90 | + rb.deleteNode(node) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Find 查找节点 |
| 95 | +func (rb *RBTree[K, V]) Find(key K) (V, error) { |
| 96 | + var v V |
| 97 | + if node := rb.findNode(key); node != nil { |
| 98 | + return node.value, nil |
| 99 | + } |
| 100 | + return v, ErrRBTreeNotRBNode |
| 101 | +} |
| 102 | +func (rb *RBTree[K, V]) Set(key K, value V) error { |
| 103 | + if node := rb.findNode(key); node != nil { |
| 104 | + node.setNode(value) |
| 105 | + return nil |
| 106 | + } |
| 107 | + return ErrRBTreeNotRBNode |
| 108 | +} |
| 109 | + |
| 110 | +// addNode 插入新节点 |
| 111 | +func (rb *RBTree[K, V]) addNode(node *rbNode[K, V]) error { |
| 112 | + var fixNode *rbNode[K, V] |
| 113 | + if rb.root == nil { |
| 114 | + rb.root = newRBNode[K, V](node.key, node.value) |
| 115 | + fixNode = rb.root |
| 116 | + } else { |
| 117 | + t := rb.root |
| 118 | + cmp := 0 |
| 119 | + parent := &rbNode[K, V]{} |
| 120 | + for t != nil { |
| 121 | + parent = t |
| 122 | + cmp = rb.compare(node.key, t.key) |
| 123 | + if cmp < 0 { |
| 124 | + t = t.left |
| 125 | + } else if cmp > 0 { |
| 126 | + t = t.right |
| 127 | + } else if cmp == 0 { |
| 128 | + return ErrRBTreeSameRBNode |
| 129 | + } |
| 130 | + } |
| 131 | + fixNode = &rbNode[K, V]{ |
| 132 | + key: node.key, |
| 133 | + parent: parent, |
| 134 | + value: node.value, |
| 135 | + color: Red, |
| 136 | + } |
| 137 | + if cmp < 0 { |
| 138 | + parent.left = fixNode |
| 139 | + } else { |
| 140 | + parent.right = fixNode |
| 141 | + } |
| 142 | + } |
| 143 | + rb.size++ |
| 144 | + rb.fixAfterAdd(fixNode) |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// deleteNode 红黑树删除方法 |
| 149 | +// 删除分两步,第一步取出后继节点,第二部着色旋转 |
| 150 | +// 取后继节点 |
| 151 | +// case1:node左右非空子节点,通过getSuccessor获取后继节点 |
| 152 | +// case2:node左右只有一个非空子节点 |
| 153 | +// case3:node左右均为空节点 |
| 154 | +// 着色旋转 |
| 155 | +// case1:当删除节点非空且为黑色时,会违反红黑树任何路径黑节点个数相同的约束,所以需要重新平衡 |
| 156 | +// case2:当删除红色节点时,不会破坏任何约束,所以不需要平衡 |
| 157 | +func (rb *RBTree[K, V]) deleteNode(node *rbNode[K, V]) { |
| 158 | + // node左右非空,取后继节点 |
| 159 | + if node.left != nil && node.right != nil { |
| 160 | + s := rb.findSuccessor(node) |
| 161 | + node.key = s.key |
| 162 | + node.value = s.value |
| 163 | + node = s |
| 164 | + } |
| 165 | + var replacement *rbNode[K, V] |
| 166 | + // node节点只有一个非空子节点 |
| 167 | + if node.left != nil { |
| 168 | + replacement = node.left |
| 169 | + } else { |
| 170 | + replacement = node.right |
| 171 | + } |
| 172 | + if replacement != nil { |
| 173 | + replacement.parent = node.parent |
| 174 | + if node.parent == nil { |
| 175 | + rb.root = replacement |
| 176 | + } else if node == node.parent.left { |
| 177 | + node.parent.left = replacement |
| 178 | + } else { |
| 179 | + node.parent.right = replacement |
| 180 | + } |
| 181 | + node.left = nil |
| 182 | + node.right = nil |
| 183 | + node.parent = nil |
| 184 | + if node.getColor() { |
| 185 | + rb.fixAfterDelete(replacement) |
| 186 | + } |
| 187 | + } else if node.parent == nil { |
| 188 | + // 如果node节点无父节点,说明node为root节点 |
| 189 | + rb.root = nil |
| 190 | + } else { |
| 191 | + // node子节点均为空 |
| 192 | + if node.getColor() { |
| 193 | + rb.fixAfterDelete(node) |
| 194 | + } |
| 195 | + if node.parent != nil { |
| 196 | + if node == node.parent.left { |
| 197 | + node.parent.left = nil |
| 198 | + } else if node == node.parent.right { |
| 199 | + node.parent.right = nil |
| 200 | + } |
| 201 | + node.parent = nil |
| 202 | + } |
| 203 | + } |
| 204 | + rb.size-- |
| 205 | +} |
| 206 | + |
| 207 | +// findSuccessor 寻找后继节点 |
| 208 | +// case1: node节点存在右子节点,则右子树的最小节点是node的后继节点 |
| 209 | +// case2: node节点不存在右子节点,则其第一个为左节点的祖先的父节点为node的后继节点 |
| 210 | +func (rb *RBTree[K, V]) findSuccessor(node *rbNode[K, V]) *rbNode[K, V] { |
| 211 | + if node == nil { |
| 212 | + return nil |
| 213 | + } else if node.right != nil { |
| 214 | + p := node.right |
| 215 | + for p.left != nil { |
| 216 | + p = p.left |
| 217 | + } |
| 218 | + return p |
| 219 | + } else { |
| 220 | + p := node.parent |
| 221 | + ch := node |
| 222 | + for p != nil && ch == p.right { |
| 223 | + ch = p |
| 224 | + p = p.parent |
| 225 | + } |
| 226 | + return p |
| 227 | + } |
| 228 | + |
| 229 | +} |
| 230 | + |
| 231 | +func (rb *RBTree[K, V]) findNode(key K) *rbNode[K, V] { |
| 232 | + node := rb.root |
| 233 | + for node != nil { |
| 234 | + cmp := rb.compare(key, node.key) |
| 235 | + if cmp < 0 { |
| 236 | + node = node.left |
| 237 | + } else if cmp > 0 { |
| 238 | + node = node.right |
| 239 | + } else { |
| 240 | + return node |
| 241 | + } |
| 242 | + } |
| 243 | + return nil |
| 244 | +} |
| 245 | + |
| 246 | +// fixAfterAdd 插入时着色旋转 |
| 247 | +// 如果是空节点、root节点、父节点是黑无需构建 |
| 248 | +// 可分为3种情况 |
| 249 | +// fixUncleRed 叔叔节点是红色右节点 |
| 250 | +// fixAddLeftBlack 叔叔节点是黑色右节点 |
| 251 | +// fixAddRightBlack 叔叔节点是黑色左节点 |
| 252 | +func (rb *RBTree[K, V]) fixAfterAdd(x *rbNode[K, V]) { |
| 253 | + x.color = Red |
| 254 | + for x != nil && x != rb.root && x.getParent().getColor() == Red { |
| 255 | + uncle := x.getUncle() |
| 256 | + if uncle.getColor() == Red { |
| 257 | + x = rb.fixUncleRed(x, uncle) |
| 258 | + continue |
| 259 | + } |
| 260 | + if x.getParent() == x.getGrandParent().getLeft() { |
| 261 | + x = rb.fixAddLeftBlack(x) |
| 262 | + continue |
| 263 | + } |
| 264 | + x = rb.fixAddRightBlack(x) |
| 265 | + } |
| 266 | + rb.root.setColor(Black) |
| 267 | +} |
| 268 | + |
| 269 | +// fixAddLeftRed 叔叔节点是红色右节点,由于不能存在连续红色节点,此时祖父节点x.getParent().getParent()必为黑。另x为红所以叔父节点需要变黑,祖父变红,此时红黑树完成 |
| 270 | +// |
| 271 | +// b(b) b(r) |
| 272 | +// / \ / \ |
| 273 | +// a(r) y(r) -> a(b) y(b) |
| 274 | +// / \ / \ / \ / \ |
| 275 | +// x(r) nil nil nil x (r) nil nil nil |
| 276 | +// / \ / \ |
| 277 | +// nil nil nil nil |
| 278 | +func (rb *RBTree[K, V]) fixUncleRed(x *rbNode[K, V], y *rbNode[K, V]) *rbNode[K, V] { |
| 279 | + x.getParent().setColor(Black) |
| 280 | + y.setColor(Black) |
| 281 | + x.getGrandParent().setColor(Red) |
| 282 | + x = x.getGrandParent() |
| 283 | + return x |
| 284 | +} |
| 285 | + |
| 286 | +// fixAddLeftBlack 叔叔节点是黑色右节点.x节点是父节点左节点,执行左旋,此时x节点变为原x节点的父节点a,也就是左子节点。的接着将x的父节点和爷爷节点的颜色对换。然后对爷爷节点进行右旋转,此时红黑树完成 |
| 287 | +// 如果x为左节点则跳过左旋操作 |
| 288 | +// |
| 289 | +// b(b) b(b) b(r) |
| 290 | +// / \ / \ / \ |
| 291 | +// a(r) y(b) -> a(r) y(b) -> a(b) y(b) |
| 292 | +// / \ / \ / \ / \ / \ / \ |
| 293 | +// nil x (r) nil nil x(r) nil nil nil x(r) nil nil nil |
| 294 | +// / \ / \ / \ |
| 295 | +// nil nil nil nil nil nil |
| 296 | +func (rb *RBTree[K, V]) fixAddLeftBlack(x *rbNode[K, V]) *rbNode[K, V] { |
| 297 | + if x == x.getParent().getRight() { |
| 298 | + x = x.getParent() |
| 299 | + rb.rotateLeft(x) |
| 300 | + } |
| 301 | + x.getParent().setColor(Black) |
| 302 | + x.getGrandParent().setColor(Red) |
| 303 | + rb.rotateRight(x.getGrandParent()) |
| 304 | + return x |
| 305 | +} |
| 306 | + |
| 307 | +// fixAddRightBlack 叔叔节点是黑色左节点.x节点是父节点右节点,执行右旋,此时x节点变为原x节点的父节点a,也就是右子节点。接着将x的父节点和爷爷节点的颜色对换。然后对爷爷节点进行右旋转,此时红黑树完成 |
| 308 | +// 如果x为右节点则跳过右旋操作 |
| 309 | +// |
| 310 | +// b(b) b(b) b(r) |
| 311 | +// / \ / \ / \ |
| 312 | +// y(b) a(r) -> y(b) a(r) -> y(b) a(b) |
| 313 | +// / \ / \ / \ / \ / \ / \ |
| 314 | +// nil nil x(r) nil nil nil nil x(r) nil nil nil x(r) |
| 315 | +// / \ / \ / \ |
| 316 | +// nil nil nil nil nil nil |
| 317 | +func (rb *RBTree[K, V]) fixAddRightBlack(x *rbNode[K, V]) *rbNode[K, V] { |
| 318 | + if x == x.getParent().getLeft() { |
| 319 | + x = x.getParent() |
| 320 | + rb.rotateRight(x) |
| 321 | + } |
| 322 | + x.getParent().setColor(Black) |
| 323 | + x.getGrandParent().setColor(Red) |
| 324 | + rb.rotateLeft(x.getGrandParent()) |
| 325 | + return x |
| 326 | +} |
| 327 | + |
| 328 | +// fixAfterDelete 删除时着色旋转 |
| 329 | +// 根据x是节点位置分为fixAfterDeleteLeft,fixAfterDeleteRight两种情况 |
| 330 | +func (rb *RBTree[K, V]) fixAfterDelete(x *rbNode[K, V]) { |
| 331 | + for x != rb.root && x.getColor() == Black { |
| 332 | + if x == x.parent.getLeft() { |
| 333 | + x = rb.fixAfterDeleteLeft(x) |
| 334 | + } else { |
| 335 | + x = rb.fixAfterDeleteRight(x) |
| 336 | + } |
| 337 | + } |
| 338 | + x.setColor(Black) |
| 339 | +} |
| 340 | + |
| 341 | +// fixAfterDeleteLeft 处理x为左子节点时的平衡处理 |
| 342 | +func (rb *RBTree[K, V]) fixAfterDeleteLeft(x *rbNode[K, V]) *rbNode[K, V] { |
| 343 | + sib := x.getParent().getRight() |
| 344 | + if sib.getColor() == Red { |
| 345 | + sib.setColor(Black) |
| 346 | + sib.getParent().setColor(Red) |
| 347 | + rb.rotateLeft(x.getParent()) |
| 348 | + sib = x.getParent().getRight() |
| 349 | + } |
| 350 | + if sib.getLeft().getColor() == Black && sib.getRight().getColor() == Black { |
| 351 | + sib.setColor(Red) |
| 352 | + x = x.getParent() |
| 353 | + } else { |
| 354 | + if sib.getRight().getColor() == Black { |
| 355 | + sib.getLeft().setColor(Black) |
| 356 | + sib.setColor(Red) |
| 357 | + rb.rotateRight(sib) |
| 358 | + sib = x.getParent().getRight() |
| 359 | + } |
| 360 | + sib.setColor(x.getParent().getColor()) |
| 361 | + x.getParent().setColor(Black) |
| 362 | + sib.getRight().setColor(Black) |
| 363 | + rb.rotateLeft(x.getParent()) |
| 364 | + x = rb.root |
| 365 | + } |
| 366 | + return x |
| 367 | +} |
| 368 | + |
| 369 | +// fixAfterDeleteRight 处理x为右子节点时的平衡处理 |
| 370 | +func (rb *RBTree[K, V]) fixAfterDeleteRight(x *rbNode[K, V]) *rbNode[K, V] { |
| 371 | + sib := x.getParent().getLeft() |
| 372 | + if sib.getColor() == Red { |
| 373 | + sib.setColor(Black) |
| 374 | + x.getParent().setColor(Red) |
| 375 | + rb.rotateRight(x.getParent()) |
| 376 | + sib = x.getBrother() |
| 377 | + } |
| 378 | + if sib.getRight().getColor() == Black && sib.getLeft().getColor() == Black { |
| 379 | + sib.setColor(Red) |
| 380 | + x = x.getParent() |
| 381 | + } else { |
| 382 | + if sib.getLeft().getColor() == Black { |
| 383 | + sib.getRight().setColor(Black) |
| 384 | + sib.setColor(Red) |
| 385 | + rb.rotateLeft(sib) |
| 386 | + sib = x.getParent().getLeft() |
| 387 | + } |
| 388 | + sib.setColor(x.getParent().getColor()) |
| 389 | + x.getParent().setColor(Black) |
| 390 | + sib.getLeft().setColor(Black) |
| 391 | + rb.rotateRight(x.getParent()) |
| 392 | + x = rb.root |
| 393 | + } |
| 394 | + return x |
| 395 | +} |
| 396 | + |
| 397 | +// rotateLeft 左旋转 |
| 398 | +// |
| 399 | +// b a |
| 400 | +// / \ / \ |
| 401 | +// c a -> b y |
| 402 | +// / \ / \ |
| 403 | +// x y c x |
| 404 | + |
| 405 | +func (rb *RBTree[K, V]) rotateLeft(node *rbNode[K, V]) { |
| 406 | + if node == nil || node.getRight() == nil { |
| 407 | + return |
| 408 | + } |
| 409 | + r := node.right |
| 410 | + node.right = r.left |
| 411 | + if r.left != nil { |
| 412 | + r.left.parent = node |
| 413 | + } |
| 414 | + r.parent = node.parent |
| 415 | + if node.parent == nil { |
| 416 | + rb.root = r |
| 417 | + } else if node.parent.left == node { |
| 418 | + node.parent.left = r |
| 419 | + } else { |
| 420 | + node.parent.right = r |
| 421 | + } |
| 422 | + r.left = node |
| 423 | + node.parent = r |
| 424 | + |
| 425 | +} |
| 426 | + |
| 427 | +// rotateRight 右旋转 |
| 428 | +// |
| 429 | +// b c |
| 430 | +// / \ / \ |
| 431 | +// c a -> x b |
| 432 | +// / \ / \ |
| 433 | +// x y y a |
| 434 | +func (rb *RBTree[K, V]) rotateRight(node *rbNode[K, V]) { |
| 435 | + if node == nil || node.getLeft() == nil { |
| 436 | + return |
| 437 | + } |
| 438 | + l := node.left |
| 439 | + node.left = l.right |
| 440 | + if l.right != nil { |
| 441 | + l.right.parent = node |
| 442 | + } |
| 443 | + l.parent = node.parent |
| 444 | + if node.parent == nil { |
| 445 | + rb.root = l |
| 446 | + } else if node.parent.right == node { |
| 447 | + node.parent.right = l |
| 448 | + } else { |
| 449 | + node.parent.left = l |
| 450 | + } |
| 451 | + l.right = node |
| 452 | + node.parent = l |
| 453 | + |
| 454 | +} |
| 455 | + |
| 456 | +func (node *rbNode[K, V]) getColor() color { |
| 457 | + if node == nil { |
| 458 | + return Black |
| 459 | + } |
| 460 | + return node.color |
| 461 | +} |
| 462 | + |
| 463 | +func (node *rbNode[K, V]) setColor(color color) { |
| 464 | + if node == nil { |
| 465 | + return |
| 466 | + } |
| 467 | + node.color = color |
| 468 | +} |
| 469 | + |
| 470 | +func (node *rbNode[K, V]) getParent() *rbNode[K, V] { |
| 471 | + if node == nil { |
| 472 | + return nil |
| 473 | + } |
| 474 | + return node.parent |
| 475 | +} |
| 476 | + |
| 477 | +func (node *rbNode[K, V]) getLeft() *rbNode[K, V] { |
| 478 | + if node == nil { |
| 479 | + return nil |
| 480 | + } |
| 481 | + return node.left |
| 482 | +} |
| 483 | + |
| 484 | +func (node *rbNode[K, V]) getRight() *rbNode[K, V] { |
| 485 | + if node == nil { |
| 486 | + return nil |
| 487 | + } |
| 488 | + return node.right |
| 489 | +} |
| 490 | + |
| 491 | +func (node *rbNode[K, V]) getUncle() *rbNode[K, V] { |
| 492 | + if node == nil { |
| 493 | + return nil |
| 494 | + } |
| 495 | + return node.getParent().getBrother() |
| 496 | +} |
| 497 | +func (node *rbNode[K, V]) getGrandParent() *rbNode[K, V] { |
| 498 | + if node == nil { |
| 499 | + return nil |
| 500 | + } |
| 501 | + return node.getParent().getParent() |
| 502 | +} |
| 503 | +func (node *rbNode[K, V]) getBrother() *rbNode[K, V] { |
| 504 | + if node == nil { |
| 505 | + return nil |
| 506 | + } |
| 507 | + if node == node.getParent().getLeft() { |
| 508 | + return node.getParent().getRight() |
| 509 | + } |
| 510 | + return node.getParent().getLeft() |
| 511 | +} |
0 commit comments