llrb.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // Copyright 2010 Petar Maymounkov. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // A Left-Leaning Red-Black (LLRB) implementation of 2-3 balanced binary search trees,
  5. // based on the following work:
  6. //
  7. // http://www.cs.princeton.edu/~rs/talks/LLRB/08Penn.pdf
  8. // http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
  9. // http://www.cs.princeton.edu/~rs/talks/LLRB/Java/RedBlackBST.java
  10. //
  11. // 2-3 trees (and the run-time equivalent 2-3-4 trees) are the de facto standard BST
  12. // algoritms found in implementations of Python, Java, and other libraries. The LLRB
  13. // implementation of 2-3 trees is a recent improvement on the traditional implementation,
  14. // observed and documented by Robert Sedgewick.
  15. //
  16. package llrb
  17. // Tree is a Left-Leaning Red-Black (LLRB) implementation of 2-3 trees
  18. type LLRB struct {
  19. count int
  20. root *Node
  21. }
  22. type Node struct {
  23. Item
  24. Left, Right *Node // Pointers to left and right child nodes
  25. Black bool // If set, the color of the link (incoming from the parent) is black
  26. // In the LLRB, new nodes are always red, hence the zero-value for node
  27. }
  28. type Item interface {
  29. Less(than Item) bool
  30. }
  31. //
  32. func less(x, y Item) bool {
  33. if x == pinf {
  34. return false
  35. }
  36. if x == ninf {
  37. return true
  38. }
  39. return x.Less(y)
  40. }
  41. // Inf returns an Item that is "bigger than" any other item, if sign is positive.
  42. // Otherwise it returns an Item that is "smaller than" any other item.
  43. func Inf(sign int) Item {
  44. if sign == 0 {
  45. panic("sign")
  46. }
  47. if sign > 0 {
  48. return pinf
  49. }
  50. return ninf
  51. }
  52. var (
  53. ninf = nInf{}
  54. pinf = pInf{}
  55. )
  56. type nInf struct{}
  57. func (nInf) Less(Item) bool {
  58. return true
  59. }
  60. type pInf struct{}
  61. func (pInf) Less(Item) bool {
  62. return false
  63. }
  64. // New() allocates a new tree
  65. func New() *LLRB {
  66. return &LLRB{}
  67. }
  68. // SetRoot sets the root node of the tree.
  69. // It is intended to be used by functions that deserialize the tree.
  70. func (t *LLRB) SetRoot(r *Node) {
  71. t.root = r
  72. }
  73. // Root returns the root node of the tree.
  74. // It is intended to be used by functions that serialize the tree.
  75. func (t *LLRB) Root() *Node {
  76. return t.root
  77. }
  78. // Len returns the number of nodes in the tree.
  79. func (t *LLRB) Len() int { return t.count }
  80. // Has returns true if the tree contains an element whose order is the same as that of key.
  81. func (t *LLRB) Has(key Item) bool {
  82. return t.Get(key) != nil
  83. }
  84. // Get retrieves an element from the tree whose order is the same as that of key.
  85. func (t *LLRB) Get(key Item) Item {
  86. h := t.root
  87. for h != nil {
  88. switch {
  89. case less(key, h.Item):
  90. h = h.Left
  91. case less(h.Item, key):
  92. h = h.Right
  93. default:
  94. return h.Item
  95. }
  96. }
  97. return nil
  98. }
  99. // Min returns the minimum element in the tree.
  100. func (t *LLRB) Min() Item {
  101. h := t.root
  102. if h == nil {
  103. return nil
  104. }
  105. for h.Left != nil {
  106. h = h.Left
  107. }
  108. return h.Item
  109. }
  110. // Max returns the maximum element in the tree.
  111. func (t *LLRB) Max() Item {
  112. h := t.root
  113. if h == nil {
  114. return nil
  115. }
  116. for h.Right != nil {
  117. h = h.Right
  118. }
  119. return h.Item
  120. }
  121. func (t *LLRB) ReplaceOrInsertBulk(items ...Item) {
  122. for _, i := range items {
  123. t.ReplaceOrInsert(i)
  124. }
  125. }
  126. func (t *LLRB) InsertNoReplaceBulk(items ...Item) {
  127. for _, i := range items {
  128. t.InsertNoReplace(i)
  129. }
  130. }
  131. // ReplaceOrInsert inserts item into the tree. If an existing
  132. // element has the same order, it is removed from the tree and returned.
  133. func (t *LLRB) ReplaceOrInsert(item Item) Item {
  134. if item == nil {
  135. panic("inserting nil item")
  136. }
  137. var replaced Item
  138. t.root, replaced = t.replaceOrInsert(t.root, item)
  139. t.root.Black = true
  140. if replaced == nil {
  141. t.count++
  142. }
  143. return replaced
  144. }
  145. func (t *LLRB) replaceOrInsert(h *Node, item Item) (*Node, Item) {
  146. if h == nil {
  147. return newNode(item), nil
  148. }
  149. h = walkDownRot23(h)
  150. var replaced Item
  151. if less(item, h.Item) { // BUG
  152. h.Left, replaced = t.replaceOrInsert(h.Left, item)
  153. } else if less(h.Item, item) {
  154. h.Right, replaced = t.replaceOrInsert(h.Right, item)
  155. } else {
  156. replaced, h.Item = h.Item, item
  157. }
  158. h = walkUpRot23(h)
  159. return h, replaced
  160. }
  161. // InsertNoReplace inserts item into the tree. If an existing
  162. // element has the same order, both elements remain in the tree.
  163. func (t *LLRB) InsertNoReplace(item Item) {
  164. if item == nil {
  165. panic("inserting nil item")
  166. }
  167. t.root = t.insertNoReplace(t.root, item)
  168. t.root.Black = true
  169. t.count++
  170. }
  171. func (t *LLRB) insertNoReplace(h *Node, item Item) *Node {
  172. if h == nil {
  173. return newNode(item)
  174. }
  175. h = walkDownRot23(h)
  176. if less(item, h.Item) {
  177. h.Left = t.insertNoReplace(h.Left, item)
  178. } else {
  179. h.Right = t.insertNoReplace(h.Right, item)
  180. }
  181. return walkUpRot23(h)
  182. }
  183. // Rotation driver routines for 2-3 algorithm
  184. func walkDownRot23(h *Node) *Node { return h }
  185. func walkUpRot23(h *Node) *Node {
  186. if isRed(h.Right) && !isRed(h.Left) {
  187. h = rotateLeft(h)
  188. }
  189. if isRed(h.Left) && isRed(h.Left.Left) {
  190. h = rotateRight(h)
  191. }
  192. if isRed(h.Left) && isRed(h.Right) {
  193. flip(h)
  194. }
  195. return h
  196. }
  197. // Rotation driver routines for 2-3-4 algorithm
  198. func walkDownRot234(h *Node) *Node {
  199. if isRed(h.Left) && isRed(h.Right) {
  200. flip(h)
  201. }
  202. return h
  203. }
  204. func walkUpRot234(h *Node) *Node {
  205. if isRed(h.Right) && !isRed(h.Left) {
  206. h = rotateLeft(h)
  207. }
  208. if isRed(h.Left) && isRed(h.Left.Left) {
  209. h = rotateRight(h)
  210. }
  211. return h
  212. }
  213. // DeleteMin deletes the minimum element in the tree and returns the
  214. // deleted item or nil otherwise.
  215. func (t *LLRB) DeleteMin() Item {
  216. var deleted Item
  217. t.root, deleted = deleteMin(t.root)
  218. if t.root != nil {
  219. t.root.Black = true
  220. }
  221. if deleted != nil {
  222. t.count--
  223. }
  224. return deleted
  225. }
  226. // deleteMin code for LLRB 2-3 trees
  227. func deleteMin(h *Node) (*Node, Item) {
  228. if h == nil {
  229. return nil, nil
  230. }
  231. if h.Left == nil {
  232. return nil, h.Item
  233. }
  234. if !isRed(h.Left) && !isRed(h.Left.Left) {
  235. h = moveRedLeft(h)
  236. }
  237. var deleted Item
  238. h.Left, deleted = deleteMin(h.Left)
  239. return fixUp(h), deleted
  240. }
  241. // DeleteMax deletes the maximum element in the tree and returns
  242. // the deleted item or nil otherwise
  243. func (t *LLRB) DeleteMax() Item {
  244. var deleted Item
  245. t.root, deleted = deleteMax(t.root)
  246. if t.root != nil {
  247. t.root.Black = true
  248. }
  249. if deleted != nil {
  250. t.count--
  251. }
  252. return deleted
  253. }
  254. func deleteMax(h *Node) (*Node, Item) {
  255. if h == nil {
  256. return nil, nil
  257. }
  258. if isRed(h.Left) {
  259. h = rotateRight(h)
  260. }
  261. if h.Right == nil {
  262. return nil, h.Item
  263. }
  264. if !isRed(h.Right) && !isRed(h.Right.Left) {
  265. h = moveRedRight(h)
  266. }
  267. var deleted Item
  268. h.Right, deleted = deleteMax(h.Right)
  269. return fixUp(h), deleted
  270. }
  271. // Delete deletes an item from the tree whose key equals key.
  272. // The deleted item is return, otherwise nil is returned.
  273. func (t *LLRB) Delete(key Item) Item {
  274. var deleted Item
  275. t.root, deleted = t.delete(t.root, key)
  276. if t.root != nil {
  277. t.root.Black = true
  278. }
  279. if deleted != nil {
  280. t.count--
  281. }
  282. return deleted
  283. }
  284. func (t *LLRB) delete(h *Node, item Item) (*Node, Item) {
  285. var deleted Item
  286. if h == nil {
  287. return nil, nil
  288. }
  289. if less(item, h.Item) {
  290. if h.Left == nil { // item not present. Nothing to delete
  291. return h, nil
  292. }
  293. if !isRed(h.Left) && !isRed(h.Left.Left) {
  294. h = moveRedLeft(h)
  295. }
  296. h.Left, deleted = t.delete(h.Left, item)
  297. } else {
  298. if isRed(h.Left) {
  299. h = rotateRight(h)
  300. }
  301. // If @item equals @h.Item and no right children at @h
  302. if !less(h.Item, item) && h.Right == nil {
  303. return nil, h.Item
  304. }
  305. // PETAR: Added 'h.Right != nil' below
  306. if h.Right != nil && !isRed(h.Right) && !isRed(h.Right.Left) {
  307. h = moveRedRight(h)
  308. }
  309. // If @item equals @h.Item, and (from above) 'h.Right != nil'
  310. if !less(h.Item, item) {
  311. var subDeleted Item
  312. h.Right, subDeleted = deleteMin(h.Right)
  313. if subDeleted == nil {
  314. panic("logic")
  315. }
  316. deleted, h.Item = h.Item, subDeleted
  317. } else { // Else, @item is bigger than @h.Item
  318. h.Right, deleted = t.delete(h.Right, item)
  319. }
  320. }
  321. return fixUp(h), deleted
  322. }
  323. // Internal node manipulation routines
  324. func newNode(item Item) *Node { return &Node{Item: item} }
  325. func isRed(h *Node) bool {
  326. if h == nil {
  327. return false
  328. }
  329. return !h.Black
  330. }
  331. func rotateLeft(h *Node) *Node {
  332. x := h.Right
  333. if x.Black {
  334. panic("rotating a black link")
  335. }
  336. h.Right = x.Left
  337. x.Left = h
  338. x.Black = h.Black
  339. h.Black = false
  340. return x
  341. }
  342. func rotateRight(h *Node) *Node {
  343. x := h.Left
  344. if x.Black {
  345. panic("rotating a black link")
  346. }
  347. h.Left = x.Right
  348. x.Right = h
  349. x.Black = h.Black
  350. h.Black = false
  351. return x
  352. }
  353. // REQUIRE: Left and Right children must be present
  354. func flip(h *Node) {
  355. h.Black = !h.Black
  356. h.Left.Black = !h.Left.Black
  357. h.Right.Black = !h.Right.Black
  358. }
  359. // REQUIRE: Left and Right children must be present
  360. func moveRedLeft(h *Node) *Node {
  361. flip(h)
  362. if isRed(h.Right.Left) {
  363. h.Right = rotateRight(h.Right)
  364. h = rotateLeft(h)
  365. flip(h)
  366. }
  367. return h
  368. }
  369. // REQUIRE: Left and Right children must be present
  370. func moveRedRight(h *Node) *Node {
  371. flip(h)
  372. if isRed(h.Left.Left) {
  373. h = rotateRight(h)
  374. flip(h)
  375. }
  376. return h
  377. }
  378. func fixUp(h *Node) *Node {
  379. if isRed(h.Right) {
  380. h = rotateLeft(h)
  381. }
  382. if isRed(h.Left) && isRed(h.Left.Left) {
  383. h = rotateRight(h)
  384. }
  385. if isRed(h.Left) && isRed(h.Right) {
  386. flip(h)
  387. }
  388. return h
  389. }