节点和儿童结构混乱,令人困惑

时间:2013-07-28 23:47:58

标签: vb.net b-tree

以下是它应该如何显示的示例(这与代码使用的示例相同) enter image description here

当从1级变为2级时,代码开始变得混乱,发生了巨大的混乱,我根本不理解它。

这是代码

   Public Class Node
        Public label As Byte
        Public visited As Boolean = False
        Public level As Integer

        Public Sub New(ByVal label As Byte, ByVal level As Integer)
            Me.label = label
            Me.level = level
        End Sub
    End Class

    Public rootNodes As New List(Of Node)
    Public nodes As New List(Of Node)
    Dim adjMatrix As Byte(,)
    Dim setDimensions As Boolean = False

    Public Sub connectNode(ByVal start As Node, ByVal endd As Node)
        'This method will be called to make connect two nodes
        If setDimensions = False Then
            ReDim Preserve adjMatrix(999 - 1, 999)
            setDimensions = True
        End If
        Dim startIndex As Integer = nodes.IndexOf(start)
        Dim endIndex As Integer = nodes.IndexOf(endd)
        adjMatrix(startIndex, endIndex) = 1
        adjMatrix(endIndex, startIndex) = 1
    End Sub

    Private Function getUnvisitedChildNode(ByVal n As Node) As Node
        Dim index As Integer = nodes.IndexOf(n)
        Dim j As Integer = 0

        Do While j < nodes.Count
            If adjMatrix(index, j) = 1 AndAlso nodes(j).visited = False Then
                Return nodes(j)
            End If
            j += 1
        Loop
        Return Nothing
    End Function

    Private Sub clearNodes()
        For Each n As Node In nodes
            n.visited = False
        Next
    End Sub

    Private Sub printNode(ByVal n As Node)
        Debug.Print(n.label & " ")
    End Sub

    Public Enum Operation
        LessThan = 0
        GreaterThan = 1
    End Enum

    Public Function getPossibleNumbersForFirstByte(ByVal op As Operation, ByVal operationOf As Byte, ByVal numbersAlreadyUsed As List(Of Byte), ByVal totalSize As Long) As List(Of Byte)
        Dim possibleNumbers As New List(Of Byte)
        Dim tempNumber As Integer = -1

        While True
            If op = Operation.LessThan Then
                tempNumber = IIf(tempNumber = -1, operationOf - 1, tempNumber - 1)
            ElseIf op = Operation.GreaterThan Then
                tempNumber = IIf(tempNumber = -1, operationOf + 1, tempNumber + 1)
            End If

            If tempNumber < 1 OrElse tempNumber > totalSize Then
                Exit While
            End If
            If numbersAlreadyUsed Is Nothing OrElse numbersAlreadyUsed.Contains(tempNumber) = False Then
                possibleNumbers.Add(tempNumber)
            End If
        End While
        Return possibleNumbers
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        test()
    End Sub

   Sub test()
        'Dim actualNumbers() As Byte = {5, 6, 1, 9, 7, 3, 8, 2, 4, 10}

        Dim actualNumberList As New List(Of Byte)
        actualNumberList.AddRange({5, 6, 1, 9, 7, 3, 8, 2, 4, 10})
        Dim ListofGTLT() As Byte = {1, 0, 1, 0, 0, 1, 0, 1, 1}

        Dim firstNumber As Byte = actualNumberList(0)
        Dim possibleNumbersToUse As New List(Of Byte)
        Dim numbersAlreadyUsed As New List(Of Byte)

        For currentLevel = 0 To UBound(ListofGTLT)
            'clear visited nodes.
            clearNodes()

            If currentLevel = 0 Then
                'Generates Level 0 of all possible values in the first offset.
                possibleNumbersToUse = getPossibleNumbersForFirstByte(ListofGTLT(currentLevel), firstNumber, Nothing, 10)
                Dim n As Node
                'Generates parent nodes of all possible values in the first offset.
                For Each possibleNumber As Byte In possibleNumbersToUse
                    n = New Node(possibleNumber, 0)
                    rootNodes.Add(n)
                    nodes.Add(n)
                Next
            Else
                Dim rootNode As Node
                For Each rootNode In rootNodes
                    'Clear numbers already used.
                    numbersAlreadyUsed.Clear()
                    'Set already exists first value (known value).
                    numbersAlreadyUsed.Add(firstNumber)

                    If currentLevel = 1 Then
                        'Gets all the numbers which are possible connections.
                        possibleNumbersToUse = getPossibleNumbersForFirstByte(ListofGTLT(currentLevel), rootNode.label, numbersAlreadyUsed, 10)

                        Dim n As Node
                        'Setup connections with new nodes to root node.
                        For Each value As Byte In possibleNumbersToUse
                            n = New Node(value, 1)
                            nodes.Add(n)
                            'Debug.Print("[Connection]: " & rootNode.label & " = " & n.label)
                            connectNode(rootNode, n)
                        Next
                    Else
                        numbersAlreadyUsed.Add(rootNode.label)

                        Dim child = getUnvisitedChildNode(rootNode)
                        Do While child IsNot Nothing
                            Debug.Print("------------------------------------------")
                            Debug.Print("| Root = " & rootNode.label)
                            Debug.Print("| Child Level = " & child.level)
                            Debug.Print("| Child = " & child.label)
                            Debug.Print("------------------------------------------")

                            'Add to already existing values the child connection node.
                            If numbersAlreadyUsed.Contains(child.label) = False Then
                                If child.level = (currentLevel - 1) Then
                                    'Gets next possible numbers which are possible connections.
                                    possibleNumbersToUse = getPossibleNumbersForFirstByte(ListofGTLT(currentLevel), child.label, numbersAlreadyUsed, 10)

                                    Dim n As Node
                                    'Setup connections with new nodes to child node.
                                    For Each value As Byte In possibleNumbersToUse
                                        n = New Node(value, currentLevel)
                                        nodes.Add(n)
                                        connectNode(child, n)
                                    Next

                                    If child.level = 1 Then
                                        'Finish all the level 1 childs for the current Root Node.
                                        child.visited = True
                                        child = getUnvisitedChildNode(rootNode)
                                        Continue Do
                                    Else
                                        numbersAlreadyUsed.Add(child.label)
                                        child.visited = True
                                        child = getUnvisitedChildNode(child)
                                        Continue Do
                                    End If

                                Else
                                    numbersAlreadyUsed.Add(child.label)
                                    child.visited = True
                                    child = getUnvisitedChildNode(child)
                                    Continue Do
                                End If
                            Else
                                numbersAlreadyUsed.Add(child.label)
                                child.visited = True
                                child = getUnvisitedChildNode(child)
                                Continue Do
                            End If
                        Loop
                    End If
                Next
            End If
        Next
    End Sub

1 个答案:

答案 0 :(得分:1)

使用专为此设计的TreeNode类,而不是实现自己的类,使您的代码更容易理解,并且具有TreeView本机的附加优势,这意味着我们所要做的就是添加基本节点并调用ExpandAll,我们有一个树的图形表示。您想要做的也非常适合递归例程。这是一种构建树结构的方法。无论何时添加子节点,它都会跳过树中已有值的任何节点。

    Private Sub btnAnswers_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Declare initial treenode with the value we want.
        Dim Tree As New TreeNode("5")
        'Declare the string of bits.
        Dim input As String = "1001001001"
        'Start the recursive routine
        CalculateTreeNodes(Tree, input)
        'Show the tree
        TreeView1.Nodes.Add(Tree)
        TreeView1.ExpandAll()h
    End Sub
    Private Sub CalculateTreeNodes(CurrentNode As TreeNode, directions As String)
        If directions = "" Then Return
        'Parse the text of the current node as byte
        Dim value As Byte = Byte.Parse(CurrentNode.Text)
        'Get the direction we want and truncate the string
        Dim direction As Directions = CType([Enum].Parse(GetType(Directions), directions(0).ToString), Directions)
        If directions.Length > 0 Then
            directions = directions.Substring(1)
        End If
        'Get a list of the values of all the parent nodes above this one.
        Dim Values As New List(Of Byte)
        GetPrevValues(CurrentNode, Values)
            'build our child nodes based on the direction we want.
            Select Case direction
                Case WindowsApplication7.Directions.Higher
                    For i As Byte = value + 1 To 10
                    If Not Values.Contains(i) Then
                        Dim node As New TreeNode(i.ToString)
                        CurrentNode.Nodes.Add(node)
                        'Calculate the children for each node
                        CalculateTreeNodes(node, directions)
                    End If
                    Next
                Case WindowsApplication7.Directions.Lower
                    For i As Byte = 1 To value - 1
                    If Not Values.Contains(i) Then
                        Dim node As New TreeNode(i.ToString)
                        CurrentNode.Nodes.Add(node)
                        CalculateTreeNodes(node, directions)
                    End If
                    Next
            End Select
    End Sub
    'recursive routine to find the parent values.
    Private Sub GetPrevValues(CurrentNode As TreeNode, Values As List(Of Byte))
        If Not CurrentNode Is Nothing Then
            Values.Add(Byte.Parse(CurrentNode.Text))
            GetPrevValues(CurrentNode.Parent, Values)
        End If
    End Sub
End Class
Public Enum Directions
    Higher = 1
    Lower = 0
End Enum

要查找最长的分支,您可以使用条件来检查directions是否为一个字符,那么任何子项都将位于最长的分支中。