VB.Net数据结构系列 第4章 树 4.3.1 二叉树的深度优先遍历

1、字符串(String)和字符数组(Char())的相互转换,

字符数组转字符串可以直接使用等号:

   Dim charsa() As Char = {"a", "b", "c"}

   Dim astring As String

   astring = charsa

字符串转字符数组可以直接使用等号:

   Dim astring As String

   Dim charsb() As Char

   charsb = astring

或者:

   charsb = astring.ToCharArray

2、数值(Byte或Integer)与字符(Char)的相互转换

数值转字符使用Convert.ToChar或者Chr()ChrW()

   Dim chara As Char = Convert.ToChar(65)

   Dim charb As Char = Chr(65)

   Dim charc As Char = Convert.ToChar(20013) '&H4E2D中”字

   Dim chard As Char = ChrW(20013)

字符转数值使用Convert.ToInt32 Convert.ToByte 或者Asc()AscW()

   Dim bytea As Byte = Convert.ToByte("A"c)

   Dim byteb As Byte = Asc("A"c)

   Dim inta As Integer = Convert.ToInt32(""c)

   Dim intb As Integer = AscW("")

注意:Convert.ToInt32的参数为字符串时,参数应该是一个数字形式的字符串。

Code4-1

Node.vb:

Public Class Node
    Private pdata As Object

    Private pright As Node

    Public ReadOnly Property [data] As Object
        Get
            Return pdata
        End Get
    End Property

    Public Property left As Node
    Public Property right As Node

    Public Sub New(ByVal data As Object)
        Me.pdata = data
    End Sub

    Public Function ToString() As String
        Return pdata.ToString
    End Function
End Class

BinaryTree.vb:

Public Class BinaryTree
    Private phead As Node
    Private Chars() As Char

    Public ReadOnly Property head As Node
        Get
            Return phead
        End Get
    End Property

    Public Sub New(ByVal constructStr As String)
        Chars = constructStr.ToCharArray
        phead = New Node(Chars(0))
        Add(phead, 0)
    End Sub

    '增加各个结点的值
    Private Sub Add(ByVal parent As Node, ByVal index As Integer)
        Dim leftindex As Integer = index * 2 + 1
        If leftindex < Chars.Length Then
            If Chars(leftindex) <> "#" Then
                parent.left = New Node(Chars(leftindex))
                Add(parent.left, leftindex)
            End If
        End If

        Dim rightindex As Integer = index * 2 + 2
        If rightindex < Chars.Length Then
            If Chars(rightindex) <> "#" Then
                parent.right = New Node(Chars(rightindex))
                Add(parent.right, rightindex)
            End If
        End If
    End Sub

    '调用递归先序遍历
    Public Sub PreOrder(ByVal srcNode As Node)
        If IsNothing(srcNode) = False Then
            Console.Write(srcNode.ToString)
            PreOrder(srcNode.left)
            PreOrder(srcNode.right)
        End If
    End Sub

    '调用递归中序遍历
    Public Sub MidOrder(ByVal srcNode As Node)
        If IsNothing(srcNode) = False Then
            MidOrder(srcNode.left)
            Console.Write(srcNode.ToString)
            MidOrder(srcNode.right)
        End If
    End Sub

    '调用递归后序遍历
    Public Sub AfterOrder(ByVal srcNode As Node)
        If IsNothing(srcNode) = False Then
            AfterOrder(srcNode.left)
            AfterOrder(srcNode.right)
            Console.Write(srcNode.ToString)
        End If
    End Sub

End Class

Module1.vb:

Module Module1

    Sub Main()
        Dim bTree As New BinaryTree("ABCDE#F")
        bTree.PreOrder(bTree.head)      '先序遍历
        Console.WriteLine()
        bTree.MidOrder(bTree.head)      '中序遍历
        Console.WriteLine()
        bTree.AfterOrder(bTree.head)    '后序遍历
        Console.ReadKey()
    End Sub

End Module

 

你可能感兴趣的:(数据结构)