LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Tree Control Configuration

Solved!
Go to solution

Ok so I'm having trouble configuring a tree control the way I want to...

 

I have a 2D array of strings that looks something like this [1, 1, 1, 2, 2] row 0

                                                                                   [a, a, c, d, e] row 1

                                                                                   [x, y, y, x, x] row 2             

 

For each unique element in row 0, a parent tag should be created and an item added to the tree of the same name as the parent tag.

 

For each unique element in row 1, a child should be added to it's parent

 

For each unique element in row 2, a grandchild should be added to it's parent and grandparent.

 

So the tree from the above 2D array should look like...

 

- 1

      - a

          - x

          - y

      - c

          - y

- 2

      - d

          - x

      - e

           -x

 

 

I've attached my work to date..

 


0 Kudos
Message 1 of 10
(5,157 Views)

You need a more sophisticated algorithm to keep track of whether each level of the tree has already been created and if so, add the item below it rather than creating a new item (with the same name but assigned a unique tag) on the same level.  Here's a quick hack that does what you want.  This is not optimized, so you may want to improve it if you're dealing with a very large 2-D array of strings.  The idea is that for each column in the array, and then for each row, check if an identical column exists up to the same row.  If so, do not create a new tree item.  Snippet in LabVIEW 2011 but VI in LV2010 to match your original.

Created Tree from 2-D Array.PNG

Create Tree from 2-D Array.PNG

Message 2 of 10
(5,138 Views)
Solution
Accepted by Strokes

As I was biking to the train one simple worthwhile optimization occurred to me, which saves a lot of redundant comparisons: use the iterator from the inner-most for loop as the start index for the next loop.  Also, don't loop if the array is empty (you would think that that would be automatic, but it turns out that an autoindexed for loop with an input of a 4x0 array will still iterate 4 times, at least with execution highlighting enabled).  Here's a snippet showing the change, not thoroughly tested but I think it's right:

Tree Control.png

Message 3 of 10
(5,125 Views)

@nathand wrote:

As I was biking to the train one simple worthwhile optimization occurred to me, which saves a lot of redundant comparisons: use the iterator from the inner-most for loop as the start index for the next loop.  Also, don't loop if the array is empty (you would think that that would be automatic, but it turns out that an autoindexed for loop with an input of a 4x0 array will still iterate 4 times, at least with execution highlighting enabled).  Here's a snippet showing the change, not thoroughly tested but I think it's right:


Nathand,

 

Sorry for bothering you again. I've only got back to using the tree control solution you created lately. It has a small bug in that it doesn't place some children under the correct parent. See attached VI for reproduction of the bug..

 

Maybe you could advise on an elegant fix.

 

Strokes

0 Kudos
Message 4 of 10
(5,080 Views)

I see and understand the problem, but I don't have an immediate fix.  I'm sufficiently interested to try to work on it later, if I have time.  The problem is that I assumed that the tag would always be the same as the left cell text, but that isn't happening in your example.  Everything works fine under the "1" subtree, but then under the "2" subtree where it looks for "x" as a tag, it doesn't find it and creates a new tag even though there's already an x_6 (or whatever number) tag there that should be used instead.

 

I did note that your code doesn't exactly match the snippet that I uploaded, and produces different results, although neither version produces the correct result.

Message 5 of 10
(5,072 Views)

Turns out there were two problems.  One was what I described above about the tags, the other was an array indexing error.  I think this version works.  Again, it's a snippet, so drag it to the desktop and then drag to a block diagram (depending on your web browser, you might be able to skip dragging to the desktop).  This relies on the columns in the array being properly grouped and ordered by tree level; if they're not, it won't work and you need more complicated code to keep track of all the tags, not just the most recent column.

BuildTree.png

Message 6 of 10
(5,045 Views)

Excellent stuff nathand.

 

+100 Kudos (if possible...)

0 Kudos
Message 7 of 10
(5,036 Views)

@Strokes wrote:

Excellent stuff nathand.


No problem, but if there's another bug, it's yours to fix 😛

 

I probably should have asked - are you able to follow what this code is doing?

Message 8 of 10
(5,023 Views)

Hi,

after working on the model of Nathand I've found the wrong parent error again.

Did you solve it? Let me know it as soon as possible please.

Thank you

 

0 Kudos
Message 9 of 10
(2,577 Views)

Given that this thread is years old and already marked as solved, you may not get much in the way of replies. I would recommend starting a new thread, with a link to this one.

 

I've been away from LabVIEW for the past several years and can't take time to look into this at the moment, especially as you've provided very little information as to what isn't working. If you want a useful response, please show what you're using as input, what the result is, what result you expected, and explain the difference between them.

0 Kudos
Message 10 of 10
(2,560 Views)