カテゴリー
Visual Basic

BinaryTreeを基本からやってみる。VBで。

Public Class Node

    Public Property LeftNode As Node = Nothing
    Dim a As Node = _LeftNode

    Public Property RightNode As Node = Nothing
    Dim b As Node = _RightNode

    Public Property Value As Int32 = 0
    Dim c As Int32 = _Value

    Public Sub New(ByVal value As Int32)
        '-- new nodes don't have children yet, just a value
        _LeftNode = Nothing
        _RightNode = Nothing
        _Value = value

    End Sub
    Public Sub insert(data As Node)
        If data.Value <> 0 Then
            If data.Value < _Value Then
                If _LeftNode Is Nothing Then
                    _LeftNode = data
                Else
                    _LeftNode.insert(data)

                End If
            ElseIf data.Value > _Value Then
                If _RightNode Is Nothing Then
                    _RightNode = data
                Else
                    _RightNode.insert(data)
                End If
            End If
        Else
            _Value = data.Value
        End If

    End Sub
    Public Sub PrintTree()
        If Not (_LeftNode Is Nothing) Then
            _LeftNode.PrintTree()
        End If

        Console.WriteLine(_Value)

        If Not (_RightNode Is Nothing) Then
            _RightNode.PrintTree()
        End If
    End Sub

Imports System
Module Program
    Public Function Change2Node(ByVal v) As Node
        Dim w As Node = New Node(v)
        Return w
    End Function

    Sub Main(args As String())
        Dim root As Node = New Node(10)

        root.insert(Change2Node(6))
        root.insert(Change2Node(14))
        root.insert(Change2Node(3))
        root.PrintTree()
        Console.ReadLine()
    End Sub
End Module

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

inserted by FC2 system