rbtree.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //@File rbtree.go
  2. //@Time 2022/05/12
  3. //@Author #Suyghur,
  4. package rbtree
  5. import (
  6. "fmt"
  7. "ylink/comm/utils/comparator"
  8. "ylink/comm/utils/visitor"
  9. )
  10. var defaultKeyComparator = comparator.BuiltinTypeComparator
  11. type Options struct {
  12. keyCmp comparator.Comparator
  13. }
  14. // Option is a function type used to set Options
  15. type Option func(option *Options)
  16. //WithKeyComparator is used to set RbTree's key comparator
  17. func WithKeyComparator(cmp comparator.Comparator) Option {
  18. return func(option *Options) {
  19. option.keyCmp = cmp
  20. }
  21. }
  22. type RbTree struct {
  23. root *Node
  24. size int
  25. keyCmp comparator.Comparator
  26. }
  27. // New creates a new RbTree
  28. func New(opts ...Option) *RbTree {
  29. option := Options{
  30. keyCmp: defaultKeyComparator,
  31. }
  32. for _, opt := range opts {
  33. opt(&option)
  34. }
  35. return &RbTree{keyCmp: option.keyCmp}
  36. }
  37. // Clear clears the RbTree
  38. func (tree *RbTree) Clear() {
  39. tree.root = nil
  40. tree.size = 0
  41. }
  42. // Find finds the first node that the key is equal to the passed key, and returns its value
  43. func (tree *RbTree) Find(key interface{}) interface{} {
  44. n := tree.findFirstNode(key)
  45. if n != nil {
  46. return n.value
  47. }
  48. return nil
  49. }
  50. // FindNode the first node that the key is equal to the passed key and return it
  51. func (tree *RbTree) FindNode(key interface{}) *Node {
  52. return tree.findFirstNode(key)
  53. }
  54. // Begin returns the node with minimum key in the RbTree
  55. func (tree *RbTree) Begin() *Node {
  56. return tree.First()
  57. }
  58. // First returns the node with minimum key in the RbTree
  59. func (tree *RbTree) First() *Node {
  60. if tree.root == nil {
  61. return nil
  62. }
  63. return minimum(tree.root)
  64. }
  65. // RBegin returns the Node with maximum key in the RbTree
  66. func (tree *RbTree) RBegin() *Node {
  67. return tree.Last()
  68. }
  69. // Last returns the Node with maximum key in the RbTree
  70. func (tree *RbTree) Last() *Node {
  71. if tree.root == nil {
  72. return nil
  73. }
  74. return maximum(tree.root)
  75. }
  76. // IterFirst returns the iterator of first node
  77. func (tree *RbTree) IterFirst() *RbTreeIterator {
  78. return NewIterator(tree.First())
  79. }
  80. // IterLast returns the iterator of first node
  81. func (tree *RbTree) IterLast() *RbTreeIterator {
  82. return NewIterator(tree.Last())
  83. }
  84. // Empty returns true if Tree is empty,otherwise returns false.
  85. func (tree *RbTree) Empty() bool {
  86. if tree.size == 0 {
  87. return true
  88. }
  89. return false
  90. }
  91. // Size returns the size of the rbtree.
  92. func (tree *RbTree) Size() int {
  93. return tree.size
  94. }
  95. // Insert inserts a key-value pair into the RbTree.
  96. func (tree *RbTree) Insert(key, value interface{}) {
  97. x := tree.root
  98. var y *Node
  99. for x != nil {
  100. y = x
  101. if tree.keyCmp(key, x.key) < 0 {
  102. x = x.left
  103. } else {
  104. x = x.right
  105. }
  106. }
  107. z := &Node{parent: y, color: RED, key: key, value: value}
  108. tree.size++
  109. if y == nil {
  110. z.color = BLACK
  111. tree.root = z
  112. return
  113. } else if tree.keyCmp(z.key, y.key) < 0 {
  114. y.left = z
  115. } else {
  116. y.right = z
  117. }
  118. tree.rbInsertFixup(z)
  119. }
  120. func (tree *RbTree) rbInsertFixup(z *Node) {
  121. var y *Node
  122. for z.parent != nil && z.parent.color == RED {
  123. if z.parent == z.parent.parent.left {
  124. y = z.parent.parent.right
  125. if y != nil && y.color == RED {
  126. z.parent.color = BLACK
  127. y.color = BLACK
  128. z.parent.parent.color = RED
  129. z = z.parent.parent
  130. } else {
  131. if z == z.parent.right {
  132. z = z.parent
  133. tree.leftRotate(z)
  134. }
  135. z.parent.color = BLACK
  136. z.parent.parent.color = RED
  137. tree.rightRotate(z.parent.parent)
  138. }
  139. } else {
  140. y = z.parent.parent.left
  141. if y != nil && y.color == RED {
  142. z.parent.color = BLACK
  143. y.color = BLACK
  144. z.parent.parent.color = RED
  145. z = z.parent.parent
  146. } else {
  147. if z == z.parent.left {
  148. z = z.parent
  149. tree.rightRotate(z)
  150. }
  151. z.parent.color = BLACK
  152. z.parent.parent.color = RED
  153. tree.leftRotate(z.parent.parent)
  154. }
  155. }
  156. }
  157. tree.root.color = BLACK
  158. }
  159. // Delete deletes node from the RbTree
  160. func (tree *RbTree) Delete(node *Node) {
  161. z := node
  162. if z == nil {
  163. return
  164. }
  165. var x, y *Node
  166. if z.left != nil && z.right != nil {
  167. y = successor(z)
  168. } else {
  169. y = z
  170. }
  171. if y.left != nil {
  172. x = y.left
  173. } else {
  174. x = y.right
  175. }
  176. xparent := y.parent
  177. if x != nil {
  178. x.parent = xparent
  179. }
  180. if y.parent == nil {
  181. tree.root = x
  182. } else if y == y.parent.left {
  183. y.parent.left = x
  184. } else {
  185. y.parent.right = x
  186. }
  187. if y != z {
  188. z.key = y.key
  189. z.value = y.value
  190. }
  191. if y.color == BLACK {
  192. tree.rbDeleteFixup(x, xparent)
  193. }
  194. tree.size--
  195. }
  196. func (tree *RbTree) rbDeleteFixup(x, parent *Node) {
  197. var w *Node
  198. for x != tree.root && getColor(x) == BLACK {
  199. if x != nil {
  200. parent = x.parent
  201. }
  202. if x == parent.left {
  203. x, w = tree.rbFixupLeft(x, parent, w)
  204. } else {
  205. x, w = tree.rbFixupRight(x, parent, w)
  206. }
  207. }
  208. if x != nil {
  209. x.color = BLACK
  210. }
  211. }
  212. func (tree *RbTree) rbFixupLeft(x, parent, w *Node) (*Node, *Node) {
  213. w = parent.right
  214. if w.color == RED {
  215. w.color = BLACK
  216. parent.color = RED
  217. tree.leftRotate(parent)
  218. w = parent.right
  219. }
  220. if getColor(w.left) == BLACK && getColor(w.right) == BLACK {
  221. w.color = RED
  222. x = parent
  223. } else {
  224. if getColor(w.right) == BLACK {
  225. if w.left != nil {
  226. w.left.color = BLACK
  227. }
  228. w.color = RED
  229. tree.rightRotate(w)
  230. w = parent.right
  231. }
  232. w.color = parent.color
  233. parent.color = BLACK
  234. if w.right != nil {
  235. w.right.color = BLACK
  236. }
  237. tree.leftRotate(parent)
  238. x = tree.root
  239. }
  240. return x, w
  241. }
  242. func (tree *RbTree) rbFixupRight(x, parent, w *Node) (*Node, *Node) {
  243. w = parent.left
  244. if w.color == RED {
  245. w.color = BLACK
  246. parent.color = RED
  247. tree.rightRotate(parent)
  248. w = parent.left
  249. }
  250. if getColor(w.left) == BLACK && getColor(w.right) == BLACK {
  251. w.color = RED
  252. x = parent
  253. } else {
  254. if getColor(w.left) == BLACK {
  255. if w.right != nil {
  256. w.right.color = BLACK
  257. }
  258. w.color = RED
  259. tree.leftRotate(w)
  260. w = parent.left
  261. }
  262. w.color = parent.color
  263. parent.color = BLACK
  264. if w.left != nil {
  265. w.left.color = BLACK
  266. }
  267. tree.rightRotate(parent)
  268. x = tree.root
  269. }
  270. return x, w
  271. }
  272. func (tree *RbTree) leftRotate(x *Node) {
  273. y := x.right
  274. x.right = y.left
  275. if y.left != nil {
  276. y.left.parent = x
  277. }
  278. y.parent = x.parent
  279. if x.parent == nil {
  280. tree.root = y
  281. } else if x == x.parent.left {
  282. x.parent.left = y
  283. } else {
  284. x.parent.right = y
  285. }
  286. y.left = x
  287. x.parent = y
  288. }
  289. func (tree *RbTree) rightRotate(x *Node) {
  290. y := x.left
  291. x.left = y.right
  292. if y.right != nil {
  293. y.right.parent = x
  294. }
  295. y.parent = x.parent
  296. if x.parent == nil {
  297. tree.root = y
  298. } else if x == x.parent.right {
  299. x.parent.right = y
  300. } else {
  301. x.parent.left = y
  302. }
  303. y.right = x
  304. x.parent = y
  305. }
  306. // findNode finds the node that its key is equal to the passed key, and returns it.
  307. func (tree *RbTree) findNode(key interface{}) *Node {
  308. x := tree.root
  309. for x != nil {
  310. if tree.keyCmp(key, x.key) < 0 {
  311. x = x.left
  312. } else {
  313. if tree.keyCmp(key, x.key) == 0 {
  314. return x
  315. }
  316. x = x.right
  317. }
  318. }
  319. return nil
  320. }
  321. // findNode finds the first node that its key is equal to the passed key, and returns it
  322. func (tree *RbTree) findFirstNode(key interface{}) *Node {
  323. node := tree.FindLowerBoundNode(key)
  324. if node == nil {
  325. return nil
  326. }
  327. if tree.keyCmp(node.key, key) == 0 {
  328. return node
  329. }
  330. return nil
  331. }
  332. // FindLowerBoundNode finds the first node that its key is equal or greater than the passed key, and returns it
  333. func (tree *RbTree) FindLowerBoundNode(key interface{}) *Node {
  334. return tree.findLowerBoundNode(tree.root, key)
  335. }
  336. func (tree *RbTree) findLowerBoundNode(x *Node, key interface{}) *Node {
  337. if x == nil {
  338. return nil
  339. }
  340. if tree.keyCmp(key, x.key) <= 0 {
  341. ret := tree.findLowerBoundNode(x.left, key)
  342. if ret == nil {
  343. return x
  344. }
  345. if tree.keyCmp(ret.key, x.key) <= 0 {
  346. return ret
  347. }
  348. return x
  349. }
  350. return tree.findLowerBoundNode(x.right, key)
  351. }
  352. // FindUpperBoundNode finds the first node that its key is greater than the passed key, and returns it
  353. func (tree *RbTree) FindUpperBoundNode(key interface{}) *Node {
  354. return tree.findUpperBoundNode(tree.root, key)
  355. }
  356. func (tree *RbTree) findUpperBoundNode(x *Node, key interface{}) *Node {
  357. if x == nil {
  358. return nil
  359. }
  360. if tree.keyCmp(key, x.key) >= 0 {
  361. return tree.findUpperBoundNode(x.right, key)
  362. }
  363. ret := tree.findUpperBoundNode(x.left, key)
  364. if ret == nil {
  365. return x
  366. }
  367. if tree.keyCmp(ret.key, x.key) <= 0 {
  368. return ret
  369. }
  370. return x
  371. }
  372. // Traversal traversals elements in the RbTree, it will not stop until to the end of RbTree or the visitor returns false
  373. func (tree *RbTree) Traversal(visitor visitor.KvVisitor) {
  374. for node := tree.First(); node != nil; node = node.Next() {
  375. if !visitor(node.key, node.value) {
  376. break
  377. }
  378. }
  379. }
  380. // IsRbTree is a function use to test whether t is a RbTree or not
  381. func (tree *RbTree) IsRbTree() (bool, error) {
  382. // Properties:
  383. // 1. Each node is either red or black.
  384. // 2. The root is black.
  385. // 3. All leaves (NIL) are black.
  386. // 4. If a node is red, then both its children are black.
  387. // 5. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
  388. _, property, ok := tree.test(tree.root)
  389. if !ok {
  390. return false, fmt.Errorf("violate property %v", property)
  391. }
  392. return true, nil
  393. }
  394. func (tree *RbTree) test(n *Node) (int, int, bool) {
  395. if n == nil { // property 3:
  396. return 1, 0, true
  397. }
  398. if n == tree.root && n.color != BLACK { // property 2:
  399. return 1, 2, false
  400. }
  401. leftBlackCount, property, ok := tree.test(n.left)
  402. if !ok {
  403. return leftBlackCount, property, ok
  404. }
  405. rightBlackCount, property, ok := tree.test(n.right)
  406. if !ok {
  407. return rightBlackCount, property, ok
  408. }
  409. if rightBlackCount != leftBlackCount { // property 5:
  410. return leftBlackCount, 5, false
  411. }
  412. blackCount := leftBlackCount
  413. if n.color == RED {
  414. if getColor(n.left) != BLACK || getColor(n.right) != BLACK { // property 4:
  415. return 0, 4, false
  416. }
  417. } else {
  418. blackCount++
  419. }
  420. if n == tree.root {
  421. //fmt.Printf("blackCount:%v \n", blackCount)
  422. }
  423. return blackCount, 0, true
  424. }
  425. // getColor returns the node's color
  426. func getColor(n *Node) Color {
  427. if n == nil {
  428. return BLACK
  429. }
  430. return n.color
  431. }