| 1 |
#Region "Microsoft.VisualBasic::9d8242f71ef8d4ebc659b093346e8109, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\BinaryTree\AVLTree.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#End Region
|
| 47 |
|
| 48 |
Imports System.Runtime.CompilerServices
|
| 49 |
|
| 50 |
Namespace ComponentModel.Algorithm.BinaryTree
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
Public Class AVLTree(Of K, V) : Inherits TreeBase(Of K, V)
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Sub New(compares As Comparison(Of K), Optional views As Func(Of K, String) = Nothing)
|
| 68 |
MyBase.New(compares, views)
|
| 69 |
End Sub
|
| 70 |
|
| 71 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 72 |
Public Sub Add(key As K, value As V, Optional valueReplace As Boolean = True)
|
| 73 |
_root = Add(key, value, _root, valueReplace)
|
| 74 |
End Sub
|
| 75 |
|
| 76 |
Public Function Add(key As K, value As V, tree As BinaryTree(Of K, V), valueReplace As Boolean) As BinaryTree(Of K, V)
|
| 77 |
If tree Is Nothing Then
|
| 78 |
|
| 79 |
tree = New BinaryTree(Of K, V)(key, value, Nothing, views)
|
| 80 |
stack.Add(tree)
|
| 81 |
End If
|
| 82 |
|
| 83 |
Select Case compares(key, tree.Key)
|
| 84 |
Case < 0 : Call appendLeft(tree, key, value, valueReplace)
|
| 85 |
Case > 0 : Call appendRight(tree, key, value, valueReplace)
|
| 86 |
Case = 0
|
| 87 |
|
| 88 |
|
| 89 |
If valueReplace Then
|
| 90 |
tree.Value = value
|
| 91 |
End If
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
Call DirectCast(tree!values, List(Of V)).Add(value)
|
| 97 |
|
| 98 |
Case Else
|
| 99 |
|
| 100 |
Throw New Exception("????")
|
| 101 |
End Select
|
| 102 |
|
| 103 |
tree.PutHeight
|
| 104 |
|
| 105 |
Return tree
|
| 106 |
End Function
|
| 107 |
|
| 108 |
Private Sub appendRight(ByRef tree As BinaryTree(Of K, V), key As K, value As V, replace As Boolean)
|
| 109 |
tree.Right = Add(key, value, tree.Right, replace)
|
| 110 |
|
| 111 |
If tree.Right.height - tree.Left.height = 2 Then
|
| 112 |
If compares(key, tree.Right.Key) > 0 Then
|
| 113 |
tree = tree.RotateRR
|
| 114 |
Else
|
| 115 |
tree = tree.RotateRL
|
| 116 |
End If
|
| 117 |
End If
|
| 118 |
End Sub
|
| 119 |
|
| 120 |
Private Sub appendLeft(ByRef tree As BinaryTree(Of K, V), key As K, value As V, replace As Boolean)
|
| 121 |
tree.Left = Add(key, value, tree.Left, replace)
|
| 122 |
|
| 123 |
If tree.Left.height - tree.Right.height = 2 Then
|
| 124 |
If compares(key, tree.Left.Key) < 0 Then
|
| 125 |
tree = tree.RotateLL
|
| 126 |
Else
|
| 127 |
tree = tree.RotateLR
|
| 128 |
End If
|
| 129 |
End If
|
| 130 |
End Sub
|
| 131 |
|
| 132 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 133 |
Public Sub Remove(key As K)
|
| 134 |
_root = Remove(key, _root)
|
| 135 |
End Sub
|
| 136 |
|
| 137 |
Public Function Remove(key As K, tree As BinaryTree(Of K, V)) As BinaryTree(Of K, V)
|
| 138 |
If tree Is Nothing Then
|
| 139 |
Return Nothing
|
| 140 |
End If
|
| 141 |
|
| 142 |
Select Case compares(key, tree.Key)
|
| 143 |
Case < 0 : Call removeLeft(tree, key)
|
| 144 |
Case > 0 : Call removeRight(tree, key)
|
| 145 |
Case = 0 : Call removeCurrent(tree)
|
| 146 |
Case Else
|
| 147 |
|
| 148 |
Throw New Exception("This will never happed!")
|
| 149 |
End Select
|
| 150 |
|
| 151 |
If Not tree Is Nothing Then
|
| 152 |
Call tree.PutHeight
|
| 153 |
End If
|
| 154 |
|
| 155 |
Return tree
|
| 156 |
End Function
|
| 157 |
|
| 158 |
Private Sub removeLeft(ByRef tree As BinaryTree(Of K, V), key As K)
|
| 159 |
tree.Left = Remove(key, tree.Left)
|
| 160 |
|
| 161 |
If tree.Left.height - tree.Right.height = 2 Then
|
| 162 |
If compares(key, tree.Left.Key) < 0 Then
|
| 163 |
tree = tree.RotateLL
|
| 164 |
Else
|
| 165 |
tree = tree.RotateLR
|
| 166 |
End If
|
| 167 |
End If
|
| 168 |
End Sub
|
| 169 |
|
| 170 |
Private Sub removeRight(ByRef tree As BinaryTree(Of K, V), key As K)
|
| 171 |
tree.Right = Remove(key, tree.Right)
|
| 172 |
|
| 173 |
If tree.Right.height - tree.Left.height = 2 Then
|
| 174 |
If compares(key, tree.Right.Key) > 0 Then
|
| 175 |
tree = tree.RotateRR
|
| 176 |
Else
|
| 177 |
tree = tree.RotateRL
|
| 178 |
End If
|
| 179 |
End If
|
| 180 |
End Sub
|
| 181 |
|
| 182 |
Private Sub removeCurrent(ByRef tree As BinaryTree(Of K, V))
|
| 183 |
If Not tree.Left Is Nothing AndAlso Not tree.Right Is Nothing Then
|
| 184 |
|
| 185 |
tree = New BinaryTree(Of K, V)(tree.Right.MinKey, tree.Value) With {
|
| 186 |
.Left = tree.Left,
|
| 187 |
.Right = tree.Right
|
| 188 |
}
|
| 189 |
tree.Right = Remove(tree.Key, tree.Right)
|
| 190 |
|
| 191 |
Else
|
| 192 |
tree = If(tree.Left Is Nothing, tree.Right, tree.Left)
|
| 193 |
|
| 194 |
If tree Is Nothing Then
|
| 195 |
tree = Nothing
|
| 196 |
End If
|
| 197 |
End If
|
| 198 |
End Sub
|
| 199 |
End Class
|
| 200 |
End Namespace
|