Question

I implemented a tree structure in c# where a node looks like the following

public class Node
{
   public int ID{get;set;}
   public string Name{get;set;}
   public Node Parent {get;set;}
   public IList<Node> Children{get;set;}
   public IList<Object> Items{get;set;}
   public IEnumerable<Ancestors> {get{return this.GetAncestors();}}
}

I want to improve my structure but i am not sure what is this kind of tree is called, its not a binary tree since the children count varies and can be more than 2, i use recursion for almost every operation from getting a node by Name,Id or reference to removing nodes, in my case when a node is removed i add both the Items and Children Properties to the Parent node.

I did it from scratch and i am sure someone did it better, so could you please help me figure the name of this tree structure so i can google it for improvements?

Was it helpful?

Solution

k-ary tree is probably the closest to what you're looking for. This typically refers to a tree where each node has at most k children (for some k, e.g. a binary tree is a 2-ary tree).

If you're looking for the case where the number of children per node is unbounded, I don't believe that has a specific name, it's just called a tree (although I imagine some resources might call that a k-ary tree as well).


An obvious place for improvement I see here is to use generics for your structure (you should replace IList<Object> with a generic data type, and rename Items to Data ... probably).

Without knowing what you want to do, I can't say whether IList<Object> is a good idea - an alternative might be to have a class with members with specific types instead, or IList<SomeOtherType>.

Having each node store a reference to its parent is not that typical, but if there's a need for it, it can be done.

OTHER TIPS

There are a few places where these structures are also called n-ary trees . If you want examples , you can google for Tries and B-tree.

I think a trie comes closest to what you are trying to structure

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top