Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. Option Explicit On
  2. Option Strict On
  3. Option Compare Binary
  4. Option Infer On
  5.  
  6. Imports System
  7. Imports System.Collections.Generic
  8.  
  9. Class BHItem(Of T)
  10. Public Node As BHNode(Of T)
  11. Public Value As T
  12. End Class
  13.  
  14. Class BHNode(Of T)
  15. Public Parent As BHNode(Of T)
  16. Public Sibling As BHNode(Of T)
  17. Public Child As BHNode(Of T)
  18. Public Item As BHItem(Of T)
  19. End Class
  20.  
  21. Class BinHeap(Of T)
  22. Dim cmp As Comparison(Of T)
  23. Dim Nodes As New List(Of BHNode(Of T))()
  24. Dim _Count As Integer
  25. Public Sub New(_cmp As Comparison(Of T))
  26. cmp = _cmp
  27. End Sub
  28. Public ReadOnly Property Count As Integer
  29. Get
  30. Return _Count
  31. End Get
  32. End Property
  33. Private Sub PushNode(ByVal node As BHNode(Of T), ByVal p As Integer)
  34. Do
  35. If p = Nodes.Count Then
  36. Nodes.Add(node)
  37. Exit Do
  38. ElseIf Nodes(p) Is Nothing Then
  39. Nodes(p) = node
  40. Exit Do
  41. Else
  42. If cmp(Nodes(p).Item.Value, node.Item.Value) < 0 Then
  43. node.Parent = Nodes(p)
  44. node.Sibling = Nodes(p).Child
  45. Nodes(p).Child = node
  46. node = Nodes(p)
  47. Else
  48. Nodes(p).Parent = node
  49. Nodes(p).Sibling = node.Child
  50. node.Child = Nodes(p)
  51. End If
  52. Nodes(p) = Nothing
  53. p += 1
  54. End If
  55. Loop
  56. End Sub
  57. Public Function Push(Value As T) As BHItem(Of T)
  58. Dim node As New BHNode(Of T)()
  59. Dim item As New BHItem(Of T)()
  60. item.Node = node
  61. item.Value = Value
  62. node.Item = item
  63. Dim p As Integer = 0
  64. Call PushNode(node, p)
  65. _Count += 1
  66. Push = item
  67. End Function
  68. Public Function Pop() As T
  69. Dim p As Integer = -1
  70. For i As Integer = 0 To Nodes.Count - 1
  71. If Nodes(i) Is Nothing Then Continue For
  72. If p >= 0 AndAlso cmp(Nodes(p).Item.Value, Nodes(i).Item.Value) < 0 Then
  73. Continue For
  74. End If
  75. p = i
  76. Next i
  77. Dim node As BHNode(Of T) = Nodes(p)
  78. Dim child As BHNode(Of T) = node.Child
  79. Dim item As BHItem(Of T) = node.Item
  80. node.Item = Nothing
  81. node.Child = Nothing
  82. item.Node = Nothing
  83. Nodes(p) = Nothing
  84. Do While child IsNot Nothing
  85. p -= 1
  86. node = child.Sibling
  87. child.Parent = Nothing
  88. child.Sibling = Nothing
  89. Call PushNode(child, p)
  90. child = node
  91. Loop
  92. _Count -= 1
  93. Pop = item.Value
  94. End Function
  95. Private Sub SwapChild(node As BHNode(Of T))
  96. Dim selChild As BHNode(Of T) = node.Child
  97. Do While selChild IsNot Nothing
  98. Dim child As BHNode(Of T) = selChild.Sibling
  99. Do While child IsNot Nothing
  100. If cmp(child.Item.Value, selChild.Item.Value) < 0 Then
  101. selChild = child
  102. End If
  103. child = child.Sibling
  104. Loop
  105. If cmp(node.Item.Value, selChild.Item.Value) <= 0 Then
  106. Exit Sub
  107. End If
  108. Dim item As BHItem(Of T) = node.Item
  109. node.Item = selChild.Item
  110. node.Item.Node = node
  111. selChild.Item = item
  112. item.Node = selChild
  113. node = selChild
  114. selChild = node.Child
  115. Loop
  116. End Sub
  117. Private Sub SwapParent(node As BHNode(Of T))
  118. Dim parent As BHNode(Of T) = node.Parent
  119. Do While parent IsNot Nothing
  120. If cmp(parent.Item.Value, node.Item.Value) < 0 Then
  121. Exit Do
  122. End If
  123. Dim item As BHItem(Of T) = node.Item
  124. node.Item = parent.Item
  125. node.Item.Node = node
  126. parent.Item = item
  127. item.Node = parent
  128. node = parent
  129. parent = node.parent
  130. Loop
  131. End Sub
  132. Public Function Update(item As BHItem(Of T), newValue As T) As BHItem(Of T)
  133. item = item.Node.Item
  134. Dim oldValue As T = item.Value
  135. item.Value = newValue
  136. If cmp(oldValue, newValue) < 0 Then
  137. Call SwapChild(item.Node)
  138. Else
  139. Call SwapParent(item.Node)
  140. End If
  141. Update = item
  142. End Function
  143. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement