0

I use an object structure like this:

class Node
{
    string name;
    List<Node> children = new List<Node>{};
    Node parent;
}

I can add children or nodes at the same level as the current node.

Is there a term for objects like that?

How would I fill a wpf tree view with this so it would look like this:

parent0
   child0
   child1
       child0ofchild1
    .
    .
    .

I want this to be able to be infinitly long so a fixed for-loop is not an option. foreach worked quite well but i run into problems when going down more than 2 levels in this tree. Is there something simpler already in c# or am I on the right track?

Sorry if this is a repetetive question but I dont know the right term to google...

Fildor
  • 14,510
  • 4
  • 35
  • 67
Szeperator
  • 21
  • 6
  • 2
    A tree with an upper bound of `n` on the number of sibling nodes is called an `N-ary tree`. What you're describing has no upper limit on the number of sibling nodes, so it would just be called a "Tree". – Matthew Watson Feb 27 '20 at 15:41
  • 1
    By _"infinitly"_ you mean very, very, deep or long? Really "infinitely" is of course quite hard to do. The other thing is: Given, you manage to get TreeView to display this incredibly large tree, it will be incredibly slow and cumbersome to operate. Is that really what you want? – Fildor Feb 27 '20 at 15:41
  • Does this answer your question? [Tree data structure in C#](https://stackoverflow.com/questions/66893/tree-data-structure-in-c-sharp) – LLSv2.0 Feb 27 '20 at 15:42
  • 1
    You should use binding. Make those public properties rather than private variables. Bind the itemssource of a treeview to a public property which is a List of node. Give the class a better name than node. Define a hierarchicaldatatemplate in the treeview. In that bind the itemssource to children. https://learn.microsoft.com/en-us/dotnet/api/system.windows.hierarchicaldatatemplate?view=netframework-4.8 – Andy Feb 27 '20 at 15:42
  • @Fildor Is there a better way to display something like this? Treeview seems to be the only viable option? – Szeperator Feb 27 '20 at 15:48
  • 1
    @Szeperator Cannot really recommend something out of lack of knowledge about your usecase. TreeView is of course great for displaying trees :) _but_ it also has limitations. You mentioned "infinitly" large trees. That raises some flags, that's all. – Fildor Feb 27 '20 at 15:52
  • "infinitly" might be the wrong word, but I dont want to be limited on how many levels I can go up and down. The goal is sort/link files in a tree like I discribed. @Fildor – Szeperator Feb 27 '20 at 15:55
  • 1
    "Files" sounds reasonable for TreeView. Have a look into Andy's link (see comment above). It's not as easy as some other GUI Elements, but definitely doable. Just keep in mind you may want to also have like a "search" field or something to avoid minute-long mousewheel torture. And maybe a "collapse all" ... – Fildor Feb 27 '20 at 16:07
  • @Fildor thanks for all the tip. Appreciate it! I consider my questions as answered, how can I mark your comments as the solution? – Szeperator Feb 27 '20 at 16:16
  • You cannot mark comments as a solution. That's why you'll once in a while see someone writing "if you have an answer, write one" ... :D However, I didn't quite _answer_ the question in the sense that question and answer would be helpful to anyone with the same problem. (Which SO is targeted to) – Fildor Feb 27 '20 at 16:23

1 Answers1

0

To traverse through a tree, a recursive function is the universal solution. If binding to a WPF tree, I would use ObservableCollection instead of List for the child items. I have made a simple example with a recursive function building up a tree, and then displaying it in a WPF TreeView. This is the data class:

    public class TreeNode {
        public ObservableCollection<TreeNode> Children {
            get;
        } = new ObservableCollection<TreeNode>();

        public string Name {
            get;set;
        }
    }

This is the recursive function to fill the tree (it generates 5 generations of children, each parent node having 6 children):

        /// <summary>
        /// Fill a recursive tree
        /// </summary>
        /// <param name="node">The treenode to generate children for</param>
        /// <param name="depth">Current depth, always incremented by 1 when going deeper in recursion</param>
        /// <param name="namePostfix">Name postfix, just for generating display name for the nodes</param>
        void fillTree(TreeNode node, int depth, string namePostfix) {
            // exit to prevent endless recursion
            if (depth >= 5) {
                return;
            }
            for (int i = 0; i < 6; ++i) {
                var child = new TreeNode() {
                    Name = $"Node{namePostfix}{i}"
                };
                fillTree(child, depth + 1, namePostfix + i);
                node.Children.Add(child);
            }
        }

If you want to traverse the tree, you should write something similar according to the traverse method you choose (from the previous answer).

XAML markup (see this link for a basic tutorial):

    <TreeView x:Name="tv">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

And in the constructor of the Window/Page:

            fillTree(tree, 0, "");
            tv.Items.Add(tree);  // tv is the x:Name of the TreeView

Of course this is just a simple example, I would use view model and data binding etc, but that is out of the scope of this question.