Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27241

Tree Structure problem

$
0
0
I'm trying to make a application where you start from one number say in my example
5 then another that number is a bunch of bits 1 and 0's that represent weather the next number is greater then or less then the previous number.

What I'm trying to do is get all the possible combinations of valid fitting numbers.

To do all this I'm trying to generate a tree structure for all possible combinations and only collect those combinations which keep getting longer and longer while getting rid of all the combinations which hit dead-end's in the end I'd like to have all the combinations which are the same length as the number of greater then's and less then's.

I attempted to do the tree structure myself and started to get very confused with how I use it in relation to childerns.

Here is the code I got.

vb.net Code:
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.         makeTreeStruct()
  3.     End Sub
  4.  
  5.     Public Enum Operation
  6.         LessThan = 0
  7.         GreaterThan = 1
  8.     End Enum
  9.  
  10.     Public Class ParentNode
  11.         Public value As Byte
  12.         Public childern As New Dictionary(Of Integer, ParentNode)
  13.  
  14.         Public Sub New(ByVal value As Byte)
  15.             Me.value = value
  16.         End Sub
  17.     End Class
  18.  
  19.     Public Sub makeTreeStruct()
  20.         'Dim actualNumbers() As Byte = {5, 6, 1, 9, 7, 3, 8, 2, 4, 10}
  21.  
  22.         Dim actualNumberList As New List(Of Byte)
  23.         actualNumberList.AddRange({5, 6, 1, 9, 7, 3, 8, 2, 4, 10})
  24.         Dim ListofGTLT() As Byte = {1, 0, 1, 0, 0, 1, 0, 1, 1}
  25.  
  26.  
  27.         Dim parentNodes As New Dictionary(Of Integer, ParentNode)
  28.  
  29.         Dim nodeIdCount As Byte = 0
  30.  
  31.  
  32.         Dim tmpNumbersUsed As New List(Of Byte)
  33.         Dim tmpId As Integer = 0
  34.         Dim firstNumber As Byte = actualNumberList(0)
  35.         Dim possibleNumbersToUse As New List(Of Byte)
  36.         Dim numbersAlreadyUsed As New List(Of Byte)
  37.  
  38.         For currentLevel = 0 To UBound(ListofGTLT)
  39.             If currentLevel = 0 Then
  40.                 'Generates Level 0 of all possible values in the first offset.
  41.                 possibleNumbersToUse = getPossibleNumbersForFirstByte(ListofGTLT(currentLevel), firstNumber, Nothing, 10)
  42.                 Dim parentNode As ParentNode
  43.                 'Generates parent nodes of all possible values in the first offset.
  44.                 For Each possibleNumber As Byte In possibleNumbersToUse
  45.                     parentNode = New ParentNode(possibleNumber)
  46.                     parentNodes.Add(tmpId, parentNode)
  47.                     tmpId += 1
  48.                 Next
  49.  
  50.             Else
  51.                 possibleNumbersToUse.Clear()
  52.                 numbersAlreadyUsed.Clear()
  53.  
  54.                 Dim pair As KeyValuePair(Of Integer, ParentNode)
  55.                 For Each pair In parentNodes
  56.                     possibleNumbersToUse = getPossibleNumbersForNextByte(ListofGTLT(currentLevel), pair.Value, pair.Key, firstNumber, 10)
  57.                     tmpId = 0
  58.                     For Each possibleNumber As Byte In possibleNumbersToUse
  59.                         If currentLevel = 1 Then
  60.                             pair.Value.childern.Add(tmpId, New ParentNode(possibleNumber))
  61.                         ElseIf currentLevel > 1 Then
  62.                             Dim howMuchChildernIn As Long = currentLevel - 1
  63.                             Dim lastChild As ParentNode
  64.  
  65.                             If pair.Value.childern.Count > 0 Then
  66.                                 lastChild = pair.Value.childern(tmpId)
  67.                                 While howMuchChildernIn > 0
  68.                                     If lastChild.childern.Count > 0 Then
  69.                                         lastChild = lastChild.childern(tmpId)
  70.                                     End If
  71.                                     howMuchChildernIn -= 1
  72.                                 End While
  73.                                 lastChild.childern.Add(tmpId, New ParentNode(possibleNumber))
  74.                             End If
  75.                         End If
  76.                         tmpId += 1
  77.                     Next
  78.                 Next
  79.             End If
  80.             tmpId = 0
  81.         Next
  82.  
  83.     End Sub
  84.  
  85.  
  86.     Public Function getPossibleNumbersForNextByte(ByVal op As Operation, ByVal aParent As ParentNode, ByVal index As Integer, ByVal firstNumber As Byte, ByVal totalSize As Long) As List(Of Byte)
  87.         Dim numbersAlreadyUsed As New List(Of Byte)
  88.         numbersAlreadyUsed.Add(firstNumber)
  89.         numbersAlreadyUsed.Add(aParent.value)
  90.  
  91.         Dim lastOperationNumber As Byte = 0
  92.  
  93.         Dim lastChild As ParentNode
  94.  
  95.         While True
  96.             If aParent.childern.Count > 0 Then
  97.                 lastChild = aParent.childern(index)
  98.                 numbersAlreadyUsed.Add(lastChild.value)
  99. moreChildernCheck:
  100.                 If lastChild.childern.Count > 0 Then
  101.                     lastChild = lastChild.childern(index)
  102.                     numbersAlreadyUsed.Add(lastChild.value)
  103.                     GoTo moreChildernCheck
  104.                 End If
  105.  
  106.                 Exit While
  107.             Else
  108.                 Exit While
  109.             End If
  110.         End While
  111.  
  112.         lastOperationNumber = numbersAlreadyUsed(numbersAlreadyUsed.Count - 1)
  113.  
  114.         Return getPossibleNumbersForFirstByte(op, lastOperationNumber, numbersAlreadyUsed, totalSize)
  115.     End Function
  116.  
  117.     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)
  118.         Dim possibleNumbers As New List(Of Byte)
  119.         Dim tempNumber As Integer = -1
  120.  
  121.         While True
  122.             If op = Operation.LessThan Then
  123.                 tempNumber = IIf(tempNumber = -1, operationOf - 1, tempNumber - 1)
  124.             ElseIf op = Operation.GreaterThan Then
  125.                 tempNumber = IIf(tempNumber = -1, operationOf + 1, tempNumber + 1)
  126.             End If
  127.  
  128.             If tempNumber < 1 OrElse tempNumber > totalSize Then
  129.                 Exit While
  130.             End If
  131.             If numbersAlreadyUsed Is Nothing OrElse numbersAlreadyUsed.Contains(tempNumber) = False Then
  132.                 possibleNumbers.Add(tempNumber)
  133.             End If
  134.         End While
  135.         Return possibleNumbers
  136.     End Function

Viewing all articles
Browse latest Browse all 27241

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>