Click here to Skip to main content
11,802,442 members (71,570 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: .NET Trees
I know there is not tree structure in .Net Framework, it's interesting, I think the tree is one of most useful data strucutres. Syntax tree, object inheritance tree, and xml tree, even .net use tree that frequently, why don't supply a common tree structure?

I have found a open source solution? Generic Tree in C#[^]

Is there any more?
Posted 28-Jun-11 19:47pm
Edited 28-Jun-11 22:21pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There is this one: Generic Tree Container in C# 2.0[^]
And this: A Generic Tree Collection[^]

But if you try googling, you will find loads more: Google[^]
SAKryukov at 29-Jun-11 2:49am
Good to use the well-developed code (my 5) but...
It's too simple.

Please see my solution.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I think a tree structure is not available in standard .NET libraries because writing it using available collections suck as generic List would take… minutes, certainly less then an hour.

Hard to believe?

Here is the idea:

using NodeList = System.Collection.Generic.List<TreeNode>
class TreeNodeData {
   //later on you can make it a generic type
class TreeNode {
   TreeNodeData TreeNodeData;
   NodeList Children = new NodeList(); //here is where the recursive nature comes up

Are you getting it?

Add constructor, accessors, operations and you're done!

OriginalGriff at 29-Jun-11 3:08am
An hour? :WTF: That's like, foreveh, man! :laugh:
SAKryukov at 29-Jun-11 3:19am
If you mean you personally do it faster, I will readily agree. I mean an average man. :-)

Thank you.
OriginalGriff at 29-Jun-11 3:21am
SA, no, sorry - we need a sarcasm icon - I meant that a whole hour is far too much time and effort for most Q&A posters to spend on a project! :laugh:
SAKryukov at 29-Jun-11 3:56am
Oh yes. You know, somehow I felt the sarcastic tone of your voice through your writing note. I guess you know how. :-)
Ray.L14 at 29-Jun-11 4:31am
May be it's not so easy, you need to consider sort, traversing, insertion, retrieveal, also performace, serialization, etc.
SAKryukov at 29-Jun-11 5:05am
It ***is*** so easy.
All this is implemented via the available properties of the list. This is exactly as difficult as using them. Who needs serialization? (I mean legacy variant). Use Data Contract -- everything is already done for you.
Ray.L14 at 30-Jun-11 7:42am
Yes, we can use many properties of the list, but I still think it depends on the scenario. Like if you just want to sort children of a specific node, traverse in a specific order, retrieve nodes in specific deep, etc. But I still like you idea. My 5.
SAKryukov at 30-Jun-11 13:42pm
Thank you, Ray. I agree with you, but for a difference scenario you can also use a different available container instead of list, such as SortedList, SortedDictionary, etc.

So, how about formally accepting this solution (green button)?
Than you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 KrunalRohit 366
1 OriginalGriff 365
2 F-ES Sitecore 295
3 Afzaal Ahmad Zeeshan 230
4 CPallini 230
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,645
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 29 Jun 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100