rbtree.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package rbtree
  2. // color of node
  3. const (
  4. RED = 0
  5. BLACK = 1
  6. )
  7. type Keytype interface {
  8. LessThan(interface{}) bool
  9. }
  10. type valuetype interface{}
  11. type node struct {
  12. left, right, parent *node
  13. color int
  14. Key Keytype
  15. Value valuetype
  16. }
  17. // Tree is a struct of red-black tree.
  18. type Tree struct {
  19. root *node
  20. size int
  21. }
  22. // NewTree creates a new rbtree.
  23. func NewTree() *Tree {
  24. return &Tree{}
  25. }
  26. // Find finds the node and return its value.
  27. func (t *Tree) Find(key Keytype) interface{} {
  28. n := t.findnode(key)
  29. if n != nil {
  30. return n.Value
  31. }
  32. return nil
  33. }
  34. // FindIt finds the node and return it as an iterator.
  35. func (t *Tree) FindIt(key Keytype) *node {
  36. return t.findnode(key)
  37. }
  38. // Empty checks whether the rbtree is empty.
  39. func (t *Tree) Empty() bool {
  40. if t.root == nil {
  41. return true
  42. }
  43. return false
  44. }
  45. // Iterator creates the rbtree's iterator that points to the minmum node.
  46. func (t *Tree) Iterator() *node {
  47. return minimum(t.root)
  48. }
  49. // Size returns the size of the rbtree.
  50. func (t *Tree) Size() int {
  51. return t.size
  52. }
  53. // Clear destroys the rbtree.
  54. func (t *Tree) Clear() {
  55. t.root = nil
  56. t.size = 0
  57. }
  58. // Insert inserts the key-value pair into the rbtree.
  59. func (t *Tree) Insert(key Keytype, value valuetype) {
  60. x := t.root
  61. var y *node
  62. for x != nil {
  63. y = x
  64. if key.LessThan(x.Key) {
  65. x = x.left
  66. } else {
  67. x = x.right
  68. }
  69. }
  70. z := &node{parent: y, color: RED, Key: key, Value: value}
  71. t.size++
  72. if y == nil {
  73. z.color = BLACK
  74. t.root = z
  75. return
  76. } else if z.Key.LessThan(y.Key) {
  77. y.left = z
  78. } else {
  79. y.right = z
  80. }
  81. t.rbInsertFixup(z)
  82. }
  83. // Delete deletes the node by key
  84. func (t *Tree) Delete(key Keytype) {
  85. z := t.findnode(key)
  86. if z == nil {
  87. return
  88. }
  89. var x, y *node
  90. if z.left != nil && z.right != nil {
  91. y = successor(z)
  92. } else {
  93. y = z
  94. }
  95. if y.left != nil {
  96. x = y.left
  97. } else {
  98. x = y.right
  99. }
  100. xparent := y.parent
  101. if x != nil {
  102. x.parent = xparent
  103. }
  104. if y.parent == nil {
  105. t.root = x
  106. } else if y == y.parent.left {
  107. y.parent.left = x
  108. } else {
  109. y.parent.right = x
  110. }
  111. if y != z {
  112. z.Key = y.Key
  113. z.Value = y.Value
  114. }
  115. if y.color == BLACK {
  116. t.rbDeleteFixup(x, xparent)
  117. }
  118. t.size--
  119. }
  120. func (t *Tree) 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. t.leftRotate(z)
  134. }
  135. z.parent.color = BLACK
  136. z.parent.parent.color = RED
  137. t.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. t.rightRotate(z)
  150. }
  151. z.parent.color = BLACK
  152. z.parent.parent.color = RED
  153. t.leftRotate(z.parent.parent)
  154. }
  155. }
  156. }
  157. t.root.color = BLACK
  158. }
  159. func (t *Tree) rbDeleteFixup(x, parent *node) {
  160. var w *node
  161. for x != t.root && getColor(x) == BLACK {
  162. if x != nil {
  163. parent = x.parent
  164. }
  165. if x == parent.left {
  166. w = parent.right
  167. if w.color == RED {
  168. w.color = BLACK
  169. parent.color = RED
  170. t.leftRotate(parent)
  171. w = parent.right
  172. }
  173. if getColor(w.left) == BLACK && getColor(w.right) == BLACK {
  174. w.color = RED
  175. x = parent
  176. } else {
  177. if getColor(w.right) == BLACK {
  178. if w.left != nil {
  179. w.left.color = BLACK
  180. }
  181. w.color = RED
  182. t.rightRotate(w)
  183. w = parent.right
  184. }
  185. w.color = parent.color
  186. parent.color = BLACK
  187. if w.right != nil {
  188. w.right.color = BLACK
  189. }
  190. t.leftRotate(parent)
  191. x = t.root
  192. }
  193. } else {
  194. w = parent.left
  195. if w.color == RED {
  196. w.color = BLACK
  197. parent.color = RED
  198. t.rightRotate(parent)
  199. w = parent.left
  200. }
  201. if getColor(w.left) == BLACK && getColor(w.right) == BLACK {
  202. w.color = RED
  203. x = parent
  204. } else {
  205. if getColor(w.left) == BLACK {
  206. if w.right != nil {
  207. w.right.color = BLACK
  208. }
  209. w.color = RED
  210. t.leftRotate(w)
  211. w = parent.left
  212. }
  213. w.color = parent.color
  214. parent.color = BLACK
  215. if w.left != nil {
  216. w.left.color = BLACK
  217. }
  218. t.rightRotate(parent)
  219. x = t.root
  220. }
  221. }
  222. }
  223. if x != nil {
  224. x.color = BLACK
  225. }
  226. }
  227. func (t *Tree) leftRotate(x *node) {
  228. y := x.right
  229. x.right = y.left
  230. if y.left != nil {
  231. y.left.parent = x
  232. }
  233. y.parent = x.parent
  234. if x.parent == nil {
  235. t.root = y
  236. } else if x == x.parent.left {
  237. x.parent.left = y
  238. } else {
  239. x.parent.right = y
  240. }
  241. y.left = x
  242. x.parent = y
  243. }
  244. func (t *Tree) rightRotate(x *node) {
  245. y := x.left
  246. x.left = y.right
  247. if y.right != nil {
  248. y.right.parent = x
  249. }
  250. y.parent = x.parent
  251. if x.parent == nil {
  252. t.root = y
  253. } else if x == x.parent.right {
  254. x.parent.right = y
  255. } else {
  256. x.parent.left = y
  257. }
  258. y.right = x
  259. x.parent = y
  260. }
  261. // findnode finds the node by key and return it, if not exists return nil.
  262. func (t *Tree) findnode(key Keytype) *node {
  263. x := t.root
  264. for x != nil {
  265. if key.LessThan(x.Key) {
  266. x = x.left
  267. } else {
  268. if key == x.Key {
  269. return x
  270. }
  271. x = x.right
  272. }
  273. }
  274. return nil
  275. }
  276. // Next returns the node's successor as an iterator.
  277. func (n *node) Next() *node {
  278. return successor(n)
  279. }
  280. // successor returns the successor of the node
  281. func successor(x *node) *node {
  282. if x.right != nil {
  283. return minimum(x.right)
  284. }
  285. y := x.parent
  286. for y != nil && x == y.right {
  287. x = y
  288. y = x.parent
  289. }
  290. return y
  291. }
  292. // getColor gets color of the node.
  293. func getColor(n *node) int {
  294. if n == nil {
  295. return BLACK
  296. }
  297. return n.color
  298. }
  299. // minimum finds the minimum node of subtree n.
  300. func minimum(n *node) *node {
  301. for n.left != nil {
  302. n = n.left
  303. }
  304. return n
  305. }