| 1 | #Region "Microsoft.VisualBasic::1bc33bc63a33826b475605df12c3e2ec, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\BinaryTree\TreeNode.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class BinaryTree |
| 35 | ' |
| 36 | ' Properties: Key, Left, QualifiedName, Right, Value |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' |
| 40 | ' Function: ToString, viewQualifiedName |
| 41 | ' |
| 42 | ' Sub: Copy, SetValue |
| 43 | ' |
| 44 | ' |
| 45 | ' /********************************************************************************/ |
| 46 | |
| 47 | #End Region |
| 48 | |
| 49 | Imports System.Runtime.CompilerServices |
| 50 | Imports Microsoft.VisualBasic.Language |
| 51 | Imports Microsoft.VisualBasic.Language.Default |
| 52 | |
| 53 | Namespace ComponentModel.Algorithm.BinaryTree |
| 54 | |
| 55 | ''' <summary> |
| 56 | ''' The binary tree node. |
| 57 | ''' |
| 58 | ''' (如果执行的是聚类操作的话,可以通过!values字典属性来获取簇的结果) |
| 59 | ''' |
| 60 | ''' 在这里只是二叉树的节点实现,具体的二叉树构建可以通过使用下面的算法来完成: |
| 61 | ''' |
| 62 | ''' + <see cref="AVLTree(Of K, V)"/> |
| 63 | ''' |
| 64 | ''' </summary> |
| 65 | ''' <typeparam name="K"></typeparam> |
| 66 | ''' <typeparam name="V"></typeparam> |
| 67 | Public Class BinaryTree(Of K, V) : Implements Value(Of V).IValueOf |
| 68 | |
| 69 | ''' <summary> |
| 70 | ''' 键名是唯一的,赋值之后就不可以改变了 |
| 71 | ''' </summary> |
| 72 | ''' <returns></returns> |
| 73 | Public ReadOnly Property Key As K |
| 74 | ''' <summary> |
| 75 | ''' 与当前的这个键名相对应的键值可以根据需求发生改变,即可以被任意赋值 |
| 76 | ''' |
| 77 | ''' 如果是进行聚类操作的话,可以通过``!values`` as <see cref="List(Of V)"/> |
| 78 | ''' 来添加簇成员 |
| 79 | ''' </summary> |
| 80 | ''' <returns></returns> |
| 81 | Public Property Value As V Implements Value(Of V).IValueOf.Value |
| 82 | Public Property Left As BinaryTree(Of K, V) |
| 83 | Public Property Right As BinaryTree(Of K, V) |
| 84 | |
| 85 | ''' <summary> |
| 86 | ''' Additional values that using for the binary tree algorithm. |
| 87 | ''' </summary> |
| 88 | ReadOnly additionals As New Dictionary(Of String, Object) |
| 89 | |
| 90 | ''' <summary> |
| 91 | ''' Full name of current node |
| 92 | ''' </summary> |
| 93 | ''' <returns></returns> |
| 94 | Public ReadOnly Property QualifiedName As String |
| 95 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 96 | Get |
| 97 | Return viewQualifiedName(Me) |
| 98 | End Get |
| 99 | End Property |
| 100 | |
| 101 | Default Public ReadOnly Property Item(key As String) As Object |
| 102 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 103 | Get |
| 104 | Return additionals.TryGetValue(key) |
| 105 | End Get |
| 106 | End Property |
| 107 | |
| 108 | ReadOnly defaultView As New DefaultValue(Of Func(Of K, String))(Function(key) Scripting.ToString(key)) |
| 109 | |
| 110 | ''' <summary> |
| 111 | ''' |
| 112 | ''' </summary> |
| 113 | ''' <param name="key"></param> |
| 114 | ''' <param name="value"></param> |
| 115 | ''' <param name="parent"></param> |
| 116 | ''' <param name="toString">Default debug view is <see cref="Scripting.ToString(Object, String)"/></param> |
| 117 | Sub New(key As K, value As V, |
| 118 | Optional parent As BinaryTree(Of K, V) = Nothing, |
| 119 | Optional toString As Func(Of K, String) = Nothing) |
| 120 | |
| 121 | Me.Key = key |
| 122 | Me.Value = value |
| 123 | |
| 124 | Call SetValue("name", (toString Or defaultView)(key)) |
| 125 | Call SetValue("parent", parent) |
| 126 | Call SetValue("values", New List(Of V)) |
| 127 | |
| 128 | DirectCast(Me!values, List(Of V)).Add(value) |
| 129 | End Sub |
| 130 | |
| 131 | ''' <summary> |
| 132 | ''' Set <see cref="additionals"/> value by using a key value tuple. |
| 133 | ''' </summary> |
| 134 | ''' <param name="key$"></param> |
| 135 | ''' <param name="value"></param> |
| 136 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 137 | Public Sub SetValue(key$, value As Object) |
| 138 | additionals(key) = value |
| 139 | End Sub |
| 140 | |
| 141 | Public Sub Copy(source As BinaryTree(Of K, V)) |
| 142 | _Key = source.Key |
| 143 | _Value = source.Value |
| 144 | additionals.Clear() |
| 145 | additionals.AddRange(source.additionals) |
| 146 | End Sub |
| 147 | |
| 148 | Private Shared Function viewQualifiedName(node As BinaryTree(Of K, V)) As String |
| 149 | Dim additionals = node.additionals |
| 150 | Dim parent = TryCast(additionals!parent, BinaryTree(Of K, V)) |
| 151 | Dim name$ = additionals!name |
| 152 | |
| 153 | If parent Is Nothing Then |
| 154 | Return "/" |
| 155 | Else |
| 156 | Return parent.QualifiedName & "/" & name |
| 157 | End If |
| 158 | End Function |
| 159 | |
| 160 | ''' <summary> |
| 161 | ''' Display debug view as: ``[key, value]`` |
| 162 | ''' </summary> |
| 163 | ''' <returns></returns> |
| 164 | Public Overrides Function ToString() As String |
| 165 | Return $"[{additionals!name}, {Value}]" |
| 166 | End Function |
| 167 | End Class |
| 168 | End Namespace |