avl.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package ctn
  2. type AVL[T any] struct {
  3. Value T
  4. Left *AVL[T]
  5. Right *AVL[T]
  6. Size uint64
  7. Height uint64
  8. }
  9. func AvlNode[T any] (v T, left *AVL[T], right *AVL[T]) *AVL[T] {
  10. var node = &AVL[T] {
  11. Value: v,
  12. Left: left,
  13. Right: right,
  14. Size: 1 + left.GetSize() + right.GetSize(),
  15. Height: 1 + max(left.GetHeight(), right.GetHeight()),
  16. }
  17. return node.balanced()
  18. }
  19. func AvlLeaf[T any] (v T) *AVL[T] {
  20. return &AVL[T] {
  21. Value: v,
  22. Left: nil,
  23. Right: nil,
  24. Size: 1,
  25. Height: 1,
  26. }
  27. }
  28. func (node *AVL[T]) IsLeaf() bool {
  29. return (node.Left == nil && node.Right == nil)
  30. }
  31. func (node *AVL[T]) GetSize() uint64 {
  32. if node == nil {
  33. return 0
  34. } else {
  35. return node.Size
  36. }
  37. }
  38. func (node *AVL[T]) GetHeight() uint64 {
  39. if node == nil {
  40. return 0
  41. } else {
  42. return node.Height
  43. }
  44. }
  45. func (node *AVL[T]) Walk(f func(T)) {
  46. if node == nil { return }
  47. node.Left.Walk(f)
  48. f(node.Value)
  49. node.Right.Walk(f)
  50. }
  51. func (node *AVL[T]) Lookup(target T, cmp Compare[T]) (T, bool) {
  52. if node == nil {
  53. return zero[T](), false
  54. } else {
  55. switch cmp(target, node.Value) {
  56. case Smaller:
  57. return node.Left.Lookup(target, cmp)
  58. case Bigger:
  59. return node.Right.Lookup(target, cmp)
  60. case Equal:
  61. return node.Value, true
  62. default:
  63. panic("impossible branch")
  64. }
  65. }
  66. }
  67. func (node *AVL[T]) Inserted(inserted T, cmp Compare[T]) (*AVL[T], bool) {
  68. if node == nil {
  69. return AvlLeaf(inserted), false
  70. } else {
  71. var value = node.Value
  72. var left = node.Left
  73. var right = node.Right
  74. switch cmp(inserted, value) {
  75. case Smaller:
  76. var left_inserted, override = left.Inserted(inserted, cmp)
  77. return AvlNode(value, left_inserted, right), override
  78. case Bigger:
  79. var right_inserted, override = right.Inserted(inserted, cmp)
  80. return AvlNode(value, left, right_inserted), override
  81. case Equal:
  82. return AvlNode(inserted, left, right), true
  83. default:
  84. panic("impossible branch")
  85. }
  86. }
  87. }
  88. func (node *AVL[T]) Deleted(target T, cmp Compare[T]) (T, *AVL[T], bool) {
  89. if node == nil {
  90. return zero[T](), nil, false
  91. } else {
  92. var value = node.Value
  93. var left = node.Left
  94. var right = node.Right
  95. switch cmp(target, value) {
  96. case Smaller:
  97. var deleted, rest, found = left.Deleted(target, cmp)
  98. if found {
  99. return deleted, AvlNode(value, rest, right), true
  100. } else {
  101. return zero[T](), node, false
  102. }
  103. case Bigger:
  104. var deleted, rest, found = right.Deleted(target, cmp)
  105. if found {
  106. return deleted, AvlNode(value, left, rest), true
  107. } else {
  108. return zero[T](), node, false
  109. }
  110. case Equal:
  111. if left == nil {
  112. return value, right, true
  113. } else if right == nil {
  114. return value, left, true
  115. } else {
  116. var node_state, _ = node.GetBalanceState()
  117. if node_state == RightTaller {
  118. var successor, rest_right, found = right.DeleteMin()
  119. assert(found, "right subtree should not be empty")
  120. return value, AvlNode(successor, left, rest_right), true
  121. } else {
  122. var prior, rest_left, found = left.DeletedMax()
  123. assert(found, "left subtree should not be empty")
  124. return value, AvlNode(prior, rest_left, right), true
  125. }
  126. }
  127. default:
  128. panic("impossible branch")
  129. }
  130. }
  131. }
  132. func (node *AVL[T]) DeleteMin() (T, *AVL[T], bool) {
  133. if node == nil {
  134. return zero[T](), nil, false
  135. } else {
  136. var value = node.Value
  137. var left = node.Left
  138. var right = node.Right
  139. var deleted, rest, found = left.DeleteMin()
  140. if found {
  141. return deleted, AvlNode(value, rest, right), true
  142. } else {
  143. return value, right, true
  144. }
  145. }
  146. }
  147. func (node *AVL[T]) DeletedMax() (T, *AVL[T], bool) {
  148. if node == nil {
  149. return zero[T](), nil, false
  150. } else {
  151. var value = node.Value
  152. var left = node.Left
  153. var right = node.Right
  154. var deleted, rest, found = right.DeletedMax()
  155. if found {
  156. return deleted, AvlNode(value, left, rest), true
  157. } else {
  158. return value, left, true
  159. }
  160. }
  161. }
  162. type BalanceState int
  163. const (
  164. LeftTaller BalanceState = iota
  165. RightTaller
  166. NeitherTaller
  167. )
  168. func (node *AVL[T]) GetBalanceState() (BalanceState, uint) {
  169. var L = node.Left.GetHeight()
  170. var R = node.Right.GetHeight()
  171. if L > R {
  172. return LeftTaller, uint(L - R)
  173. } else if L < R {
  174. return RightTaller, uint(R - L)
  175. } else {
  176. return NeitherTaller, 0
  177. }
  178. }
  179. func (node *AVL[T]) balanced() *AVL[T] {
  180. var current = node
  181. var current_state, diff = current.GetBalanceState()
  182. if current_state == NeitherTaller || diff == 1 {
  183. return current
  184. } else {
  185. assert(diff == 2, "invalid usage of balanced()")
  186. switch current_state {
  187. case LeftTaller:
  188. var left = current.Left
  189. var left_state, _ = left.GetBalanceState()
  190. switch left_state {
  191. case LeftTaller, NeitherTaller:
  192. var new_right = AvlNode(current.Value, left.Right, current.Right)
  193. var new_current = AvlNode(left.Value, left.Left, new_right)
  194. return new_current
  195. case RightTaller:
  196. var middle = left.Right
  197. var new_left = AvlNode(left.Value, left.Left, middle.Left)
  198. var new_right = AvlNode(current.Value, middle.Right, current.Right)
  199. var new_current = AvlNode(middle.Value, new_left, new_right)
  200. return new_current
  201. default:
  202. panic("impossible branch")
  203. }
  204. case RightTaller:
  205. var right = current.Right
  206. var right_state, _ = right.GetBalanceState()
  207. switch right_state {
  208. case LeftTaller:
  209. var middle = right.Left
  210. var new_left = AvlNode(current.Value, current.Left, middle.Left)
  211. var new_right = AvlNode(right.Value, middle.Right, right.Right)
  212. var new_current = AvlNode(middle.Value, new_left, new_right)
  213. return new_current
  214. case RightTaller, NeitherTaller:
  215. var new_left = AvlNode(current.Value, current.Left, right.Left)
  216. var new_current = AvlNode(right.Value, new_left, right.Right)
  217. return new_current
  218. default:
  219. panic("impossible branch")
  220. }
  221. default:
  222. panic("impossible branch")
  223. }
  224. }
  225. }