Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need to do is to generalize your code: at the moment it assumes exactly two children, and your <code>Tree</code> class has fields</p> <pre><code>Public l As Tree = Nothing Public r As Tree = Nothing </code></pre> <p>but you no longer know how many children each tree has, so you need a collection: <code>List</code> is a reasonable choice</p> <pre><code>Public children As List(Of Tree) = New List(Of Tree) </code></pre> <p>Note that this is initialized to empty: you'll need to add children to it as you discover it.</p> <p>Now you need to look at the code that parses the string and finds the children. At the moment you have a function</p> <pre><code>Private Sub BulidTreeByInTo(ByVal str As String, ByRef l1 As Tree, ByRef r1 As Tree) </code></pre> <p>This should change to </p> <pre><code>Private Sub BulidTreeByInTo(ByVal str As String, ByRef ch As List(Of Tree)) </code></pre> <p>Finally you need to modify the implementation of <code>BulidTreeByInTo</code> so that it uses a loop to add as many children as are needed to <code>ch</code>. So, rather than hardcoding the two almost-identical <code>If ... Else ... End If</code> blocks, you need to have just one of those blocks in a loop. I suggest something along the following lines</p> <pre><code>Do Dim MiddleIndex As Integer = GetTheSameLevel(str) ' find comma at same level Dim p1 As String = str.Substring(0, MiddleIndex) ' substring up to this comma ' str is rest of string past the comma, which can be 0 or more children str = str.Substring(MiddleIndex + 1, str.Length - 1 - MiddleIndex) Try If p1.Contains("(") Then If Not p1.Substring(0, p1.IndexOf("(")).ToString = "\" Then ch.Add(New Tree(p1.Substring(0, p1.IndexOf("(")))) BulidTreeByInTo(p1.Substring(p1.IndexOf("(") + 1, p1.Length - 2 - p1.IndexOf("(")), l1.ch) End If Else If Not p1 = "\" Then ch.Add(New Tree(p1)) End If End If Catch ex As Exception Throw ex End Try While MiddleIndex &gt;= 0 </code></pre> <p>It looks to me like your function <code>GetTheSameLevel</code> finds the next comma at the same level and returns <code>&lt;0</code> if it fails to find the comma, so <code>&lt;0</code> is the loop termination condition. The idea is that we keep most of your existing code and but rather than having <code>p2</code> as the right-hand string, we reassign the right-hand string to <code>str</code>. Each time we go through the loop we chop the next bit out of <code>str</code> and assign to <code>p1</code>.</p> <p>I'm not a VB programmer and haven't compiled or tested this, but hopefully this gives you enough of an idea of how to proceed?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload