microsoft-visualbasic-runtime/ComponentModel/Algorithm/BinaryTree/AVLTree/AVLSupports.vb

106 lines
3.2 KiB
VB.net
Raw Normal View History

#Region "Microsoft.VisualBasic::fa8b3cc7757e3eebf57da0d645f14bb2, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\BinaryTree\AVLTree\AVLSupports.vb"
2018-08-02 20:14:48 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Module AVLSupports
'
' Function: height, RotateLL, RotateLR, RotateRL, RotateRR
'
' Sub: PutHeight
'
'
' /********************************************************************************/
#End Region
Imports System.Runtime.CompilerServices
Imports stdNum = System.Math
2018-08-02 20:14:48 +08:00
Namespace ComponentModel.Algorithm.BinaryTree
''' <summary>
''' Binary tree balance helper
''' </summary>
Public Module AVLSupports
<Extension>
Public Function RotateLL(Of K, V)(node As BinaryTree(Of K, V)) As BinaryTree(Of K, V)
Dim top = node.Left
node.Left = top.Right
top.Right = node
node.PutHeight
top.PutHeight
Return top
End Function
<Extension>
Public Function RotateRR(Of K, V)(node As BinaryTree(Of K, V)) As BinaryTree(Of K, V)
Dim top = node.Right
node.Right = top.Left
top.Left = node
Call node.PutHeight
Call top.PutHeight
Return top
End Function
<Extension>
Public Function RotateLR(Of K, V)(node As BinaryTree(Of K, V)) As BinaryTree(Of K, V)
node.Left = node.Left.RotateRR
Return node.RotateLL
End Function
<Extension>
Public Function RotateRL(Of K, V)(node As BinaryTree(Of K, V)) As BinaryTree(Of K, V)
node.Right = node.Right.RotateLL
Return node.RotateRR
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Friend Function height(Of K, V)(node As BinaryTree(Of K, V)) As Double
Return If(node Is Nothing, -1, CDbl(node!height))
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Friend Sub PutHeight(Of K, V)(node As BinaryTree(Of K, V))
node.SetValue("height", stdNum.Max(node.Left.height, node.Right.height) + 1)
2018-08-02 20:14:48 +08:00
End Sub
End Module
End Namespace