Guest User

Untitled

a guest
Feb 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package main
  2.  
  3. type Node struct {
  4. Value int
  5. Parent *Node
  6. Left *Node
  7. Right *Node
  8. }
  9.  
  10. func NewNode(v int) *Node {
  11. return &Node{Value: v}
  12. }
  13.  
  14. func (n *Node) Compare(m *Node) int {
  15. if n.Value < m.Value {
  16. return -1
  17. } else if n.Value > m.Value {
  18. return 1
  19. }
  20. return 0
  21. }
  22.  
  23. type BST struct {
  24. Root *Node
  25. Size int
  26. }
  27.  
  28. func NewBST(n *Node) *BST {
  29. if n == nil {
  30. return &BST{}
  31. }
  32. return &BST{Root: n, Size: 1}
  33. }
  34.  
  35. func (t *BST) Search(v int) *Node {
  36. n := NewNode(v)
  37. h := t.Root
  38. for h != nil {
  39. switch n.Compare(h) {
  40. case -1:
  41. h = h.Left
  42. case 1:
  43. h = h.Right
  44. default:
  45. return h
  46. }
  47. }
  48. return nil
  49. }
  50.  
  51. func (t *BST) Max() *Node {
  52. if t.Root == nil {
  53. return nil
  54. }
  55. max := t.Root
  56. for max.Right != nil {
  57. max = max.Right
  58. }
  59. return max
  60. }
  61.  
  62. func (t *BST) Min() *Node {
  63. if t.Root == nil {
  64. return nil
  65. }
  66. min := t.Root
  67. for min.Left != nil {
  68. min = min.Left
  69. }
  70. return min
  71. }
  72.  
  73. func (t *BST) Insert(v int) {
  74. n := NewNode(v)
  75. if t.Root == nil {
  76. t.Root = n
  77. t.Size++
  78. return
  79. }
  80. h := t.Root
  81. for {
  82. switch n.Compare(h) {
  83. case -1:
  84. if h.Left == nil {
  85. h.Left = n
  86. t.Size++
  87. return
  88. } else {
  89. h = h.Left
  90. }
  91. case 1:
  92. if h.Right == nil {
  93. h.Right = n
  94. t.Size++
  95. return
  96. } else {
  97. h = h.Right
  98. }
  99. default:
  100. return
  101. }
  102. }
  103. }
  104.  
  105. func (t *BST) Traverse(n *Node, f func(*Node)) {
  106. if n == nil {
  107. return
  108. }
  109. t.Traverse(n.Right, f)
  110. f(n)
  111. t.Traverse(n.Left, f)
  112. }
  113.  
  114. func (t *BST) Delete(v int) {
  115. var parent *Node
  116. h := t.Root
  117. n := NewNode(v)
  118. for h != nil {
  119. switch n.Compare(h) {
  120. case -1:
  121. parent = h
  122. h = h.Left
  123. case 1:
  124. parent = h
  125. h = h.Right
  126. default:
  127. if h.Left != nil {
  128. right := h.Right
  129. h.Value = h.Left.Value
  130. h.Right = h.Left.Right
  131. h.Left = h.Left.Left
  132. if right != nil {
  133. st := &BST{Root: h}
  134. t.Traverse(right, func(n *Node) {
  135. st.Insert(n.Value)
  136. })
  137. }
  138. t.Size--
  139. return
  140. }
  141.  
  142. if h.Right != nil {
  143. h.Value = h.Right.Value
  144. h.Left = h.Right.Left
  145. h.Right = h.Right.Right
  146. t.Size--
  147. return
  148. }
  149.  
  150. if parent == nil {
  151. t.Root = nil
  152. t.Size--
  153. return
  154. }
  155. if parent.Left.Value == n.Value {
  156. parent.Left = nil
  157. } else {
  158. parent.Right = nil
  159. }
  160. t.Size--
  161. return
  162. }
  163. }
  164. }
Add Comment
Please, Sign In to add comment