473,389 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

Data structure (tree)

Hello friends,
I would like to know what is the best way to store a tree structure in
c#? Are there any good implementation examples for seing how to do it?

Thank u!

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
8 5060
On Mon, 05 May 2008 09:26:49 -0700, csharpula csharp <cs*******@yahoo.com>
wrote:
I would like to know what is the best way to store a tree structure in
c#?
That depends on the nature of the tree. However, it's simple enough to
write your own class that maintains a tree in whatever manner is
appropriate for your data. Common tree implementations would include
references to children either via a fixed number of instance fields (e.g.
binary tree) or an instance field that holds a reference to a collection
of child nodes (e.g. a List<Tinstance containing all the children for
that node).
Are there any good implementation examples for seing how to do it?
A number of Framework classes use tree-like collections. You certainly
could examine them to learn more about different .NET implementations that
exist. For example, the Control class (Control.Controls), the TreeView
class (TreeView.Nodes, and TreeNode.Nodes), and the generic SortedList<T>
class, to name a few.

Pete
Jun 27 '08 #2

The question is what is the best way to implement a tree structure which
will store the deserialization result of xml structure which represents
a tree. what is the best way? is there any class that already implements
that?

Thank u very much!
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #3
Since you are working with xml, would XmlDocument or XDocument suffice?

Marc
Jun 27 '08 #4

But I want to represent a tree in a c# class not via xml ,the result of
xml deserialization will be the data of this class. how to do it? thank
u!
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #5
It depends; if you know what the data looks like in advance, then fine -
use XmlSerializer to read the xml into your class structure. But this
won't work for ad-hoc xml.

Example below - is this what you had in mind? If not - please explain
what you do want...

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
[Serializable]
public sealed class TreeNode
{
[XmlAttribute]
public string Key { get; set; }
[XmlAttribute]
public string Text { get; set; }

private List<TreeNodenodes;

public List<TreeNodeNodes
{
get
{
if(nodes == null) nodes = new List<TreeNode>();
return nodes;
}
}

public override string ToString()
{
return Text;
}
}

static class Program {
static void Main() {
TreeNode tree = new TreeNode {
Key = "A", Text = "Foo",
Nodes = {
new TreeNode
{
Key = "B", Text = "Bar",
Nodes = {
new TreeNode {
Key = "C", Text = "Blip"
}
}
},
new TreeNode {
Key = "D", Text = "Blop"
}
}
};
StringBuilder sb = new StringBuilder();
XmlSerializer xser = new XmlSerializer(typeof(TreeNode));
using (XmlWriter writer = XmlWriter.Create(sb))
{
xser.Serialize(writer, tree);
writer.Close();
}

string xml = sb.ToString();
Console.WriteLine(xml);

using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
TreeNode backAgain = (TreeNode) xser.Deserialize(reader);
}
}
}
Jun 27 '08 #6
re-hacked in case you are using C# 2 - actually it is more readable this
way, too...

added 2 .ctor()s for TreeNode:

public TreeNode() : this("","") { }
public TreeNode(string key, string text,
params TreeNode[] nodes)
{
Key = key;
Text = text;
if (nodes != null && nodes.Length 0)
{
Nodes.AddRange(nodes);
}
}

and changed demo setup code:

TreeNode tree = new TreeNode("A", "Foo",
new TreeNode("B", "Bar",
new TreeNode("C", "Blip")
),
new TreeNode("D", "Blop")
);

Same result, just done differently...

Marc
Jun 27 '08 #7

But my question is if I need to create the TreeNode class by myself and
implement all the tree functionality or I can use some pre defined class
or implment some tree template interface?
Thanks!
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #8
csharpula csharp wrote:
But my question is if I need to create the TreeNode class by myself and
implement all the tree functionality or I can use some pre defined class
or implment some tree template interface?
Thanks!
To come back full-circle, XmlDocument is a fully-functional pre-defined
class for loading an arbitrary tree of data... if you want something in
between, you're going to have to be a lot more specific about what you
want (and probably write it yourself).

In System.Web.UI, there are some interfaces - IHierarchyData,
IHierarchicalEnumerable, IHierarchicalDataSource - but to be honest I
wouldn't worry unless you are using the web tree-view.

Marc
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marek Mänd | last post by:
var criterions = { subconds:{ tree: } } // MUSIC , {condID:17, strrep:'Music', subconds:{ tree:
4
by: Sasha | last post by:
Hi everyone, I would like to hear your opinions about a design solution I am contemplating. The problem I am following: Write an editor for a data structure that is recursive in nature. In...
4
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
14
by: SD | last post by:
I am thinking about writing a text editor in C for unix sometime soon. I am just doing this to learn more about C. I want to write something like ed.c, a simple line editor. What types of data...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
10
by: Will Honea | last post by:
I have a data set which I need to analyze but I am having a problem figuring out a structure for the database - or whether there are better ways of attacking the problem. The base data set is a...
4
by: Alexander Adam | last post by:
Hello folks, I got a few question on some basic data structure stuff. Sorry if those questions might sound too easy or such but after googling a lot I didn't find a real answer to all those...
2
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non - cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
1
by: rhaas | last post by:
Howdy. I've been trying to parse the return of XML::TreePP from an xBRL file using perl and Data::Dumper. The resulting Data::Dumper output looks like this: $VAR1 = { 'xbrl' =>...
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.