| 1 | #Region "Microsoft.VisualBasic::64c325c878d9b56b8ccbac4578582073, Microsoft.VisualBasic.Core\ComponentModel\DataStructures\Tree\BinaryTree\TreeNode(Of T).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 TreeNode |
| 35 | ' |
| 36 | ' Properties: AllChilds, ChainPosition, Count, DisplayQualifiedName, IsLeaf |
| 37 | ' Left, Name, Parent, QualifiedName, Right |
| 38 | ' Value |
| 39 | ' |
| 40 | ' Constructor: (+2 Overloads) Sub New |
| 41 | ' Function: GetEnumerator, ToString |
| 42 | ' Operators: -, + |
| 43 | ' |
| 44 | ' |
| 45 | ' /********************************************************************************/ |
| 46 | |
| 47 | #End Region |
| 48 | |
| 49 | Imports System.Runtime.CompilerServices |
| 50 | Imports System.Web.Script.Serialization |
| 51 | Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic |
| 52 | Imports Microsoft.VisualBasic.Language |
| 53 | |
| 54 | Namespace ComponentModel.DataStructures.BinaryTree |
| 55 | |
| 56 | ''' <summary> |
| 57 | ''' Define tree nodes |
| 58 | ''' </summary> |
| 59 | ''' <remarks></remarks> |
| 60 | Public Class TreeNode(Of T) : Implements INamedValue |
| 61 | Implements Value(Of T).IValueOf |
| 62 | |
| 63 | Public Property Name As String Implements INamedValue.Key |
| 64 | Public Property Value As T Implements Value(Of T).IValueOf.Value |
| 65 | Public Property Left As TreeNode(Of T) |
| 66 | Public Property Right As TreeNode(Of T) |
| 67 | Public Property Parent As TreeNode(Of T) |
| 68 | |
| 69 | Public ReadOnly Property QualifiedName As String |
| 70 | Get |
| 71 | If Parent Is Nothing Then |
| 72 | Return "/" |
| 73 | Else |
| 74 | Return Parent.QualifiedName & "/" & Name |
| 75 | End If |
| 76 | End Get |
| 77 | End Property |
| 78 | |
| 79 | Public ReadOnly Property ChainPosition As String |
| 80 | Get |
| 81 | If Parent Is Nothing Then |
| 82 | Return "/" |
| 83 | Else |
| 84 | If Parent.Left Is Nothing Then |
| 85 | Return Parent.ChainPosition & "/+" |
| 86 | ElseIf Parent.Right Is Nothing Then |
| 87 | Return Parent.ChainPosition & "/-" |
| 88 | Else |
| 89 | If Me Is Parent.Right Then |
| 90 | Return Parent.ChainPosition & "/+" |
| 91 | Else |
| 92 | Return Parent.ChainPosition & "/-" |
| 93 | End If |
| 94 | End If |
| 95 | End If |
| 96 | End Get |
| 97 | End Property |
| 98 | |
| 99 | <ScriptIgnore> Public ReadOnly Property IsLeaf As Boolean |
| 100 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 101 | Get |
| 102 | Return Left Is Nothing AndAlso |
| 103 | Right Is Nothing |
| 104 | End Get |
| 105 | End Property |
| 106 | |
| 107 | <ScriptIgnore> Public ReadOnly Property AllChilds As List(Of TreeNode(Of T)) |
| 108 | Get |
| 109 | Dim list As New List(Of TreeNode(Of T)) |
| 110 | |
| 111 | For Each x In Me.GetEnumerator |
| 112 | Call list.Add(x) |
| 113 | Call list.AddRange(x.AllChilds) |
| 114 | Next |
| 115 | |
| 116 | Return list |
| 117 | End Get |
| 118 | End Property |
| 119 | |
| 120 | ''' <summary> |
| 121 | ''' 递归的得到子节点的数目 |
| 122 | ''' </summary> |
| 123 | ''' <returns></returns> |
| 124 | <ScriptIgnore> Public ReadOnly Property Count As Integer |
| 125 | Get |
| 126 | Dim n As Integer |
| 127 | |
| 128 | If Not Left Is Nothing Then |
| 129 | n += 1 |
| 130 | n += Left.Count |
| 131 | End If |
| 132 | |
| 133 | If Not Right Is Nothing Then |
| 134 | n += 1 |
| 135 | n += Right.Count |
| 136 | End If |
| 137 | |
| 138 | Return n |
| 139 | End Get |
| 140 | End Property |
| 141 | |
| 142 | ''' <summary> |
| 143 | ''' Constructor to create a single node |
| 144 | ''' </summary> |
| 145 | ''' <param name="name"></param> |
| 146 | ''' <param name="obj"></param> |
| 147 | Public Sub New(name As String, obj As T) |
| 148 | With Me |
| 149 | .Name = name |
| 150 | .Value = obj |
| 151 | End With |
| 152 | End Sub |
| 153 | |
| 154 | Sub New() |
| 155 | End Sub |
| 156 | |
| 157 | Public Shared Property DisplayQualifiedName As Boolean = True |
| 158 | |
| 159 | Public Overrides Function ToString() As String |
| 160 | If DisplayQualifiedName Then |
| 161 | Return QualifiedName |
| 162 | Else |
| 163 | If Value Is Nothing Then |
| 164 | Return Name |
| 165 | Else |
| 166 | Return $"[{Name}] {Value}" |
| 167 | End If |
| 168 | End If |
| 169 | End Function |
| 170 | |
| 171 | ''' <summary> |
| 172 | ''' 最多只有两个元素 |
| 173 | ''' </summary> |
| 174 | ''' <returns></returns> |
| 175 | Public Iterator Function GetEnumerator() As IEnumerable(Of TreeNode(Of T)) |
| 176 | If Not Left Is Nothing Then |
| 177 | Yield Left |
| 178 | End If |
| 179 | If Not Right Is Nothing Then |
| 180 | Yield Right |
| 181 | End If |
| 182 | End Function |
| 183 | |
| 184 | Public Shared Operator +(parent As TreeNode(Of T), child As TreeNode(Of T)) As TreeNode(Of T) |
| 185 | If parent.Left Is Nothing Then |
| 186 | parent.Left = child |
| 187 | Return parent |
| 188 | End If |
| 189 | If parent.Right Is Nothing Then |
| 190 | parent.Right = child |
| 191 | Return parent |
| 192 | End If |
| 193 | |
| 194 | Throw New Exception("TreeNode is full, can not append any more!") |
| 195 | End Operator |
| 196 | |
| 197 | Public Shared Operator -(parent As TreeNode(Of T), child As TreeNode(Of T)) As TreeNode(Of T) |
| 198 | If Not parent.Left Is Nothing Then |
| 199 | If parent.Left.Equals(child) Then |
| 200 | parent.Left = Nothing |
| 201 | Return parent |
| 202 | End If |
| 203 | End If |
| 204 | If Not parent.Right Is Nothing Then |
| 205 | If parent.Right.Equals(child) Then |
| 206 | parent.Right = Nothing |
| 207 | Return parent |
| 208 | End If |
| 209 | End If |
| 210 | Return parent |
| 211 | End Operator |
| 212 | End Class |
| 213 | End Namespace |