Jump to content
  • Advertisement
Sign in to follow this  
JDUK

[C#] Best way to store Data in a "tree" type set up??

This topic is 4567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im bulding somthing that will alow people to make objects out of building blocks (akin to lego) when blocks are interconnecting they form an object, if two objects interconnect they merge. I have planned this out on paper in a tree like structure So if blocks where linked like so:
            [----5----]
            [-4-]
[-------3-------]
[-1-]       [-2-]
            [-6-]
The data tree would look like this:
Obj 1:
1
|3
 |2
 ||6
 |
 |4
  |5
On paper i found this a good way to calculate how objects would merge if a block spanned 2 objects and how an object would break into multiple objects if spanning blocks where removed. But whats the best way to store this kind of information in memory, in a manner that is intuative to read and write to? Is it just a simple matter of arrays? or is there a better tool (or method) for the job? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Just a simple example... try to get the idea:

using System;
using System.Collections.Generic;
public class Test
{
class Tree
{
public Tree(int leaf)
{
_Leaf = leaf;
_Branch = new Dictionary<int,Tree>();
}
public void Add(int leaf)
{
_Branch[leaf] = new Tree(leaf);
}
private int _Leaf;
public int Leaf
{
get { return _Leaf; }
}
private Dictionary<int,Tree> _Branch;
public Tree this[int i]
{
get { return _Branch; }
}
private string ToString(int off)
{
string s,o="";
s = string.Format("{0}\n", _Leaf);
for (int i = 0; i < off ; i++, o += " ") ; o += "|";
foreach (KeyValuePair<int,Tree> kv in _Branch)
{

s += string.Format(o+"{0}\n", kv.Value.ToString(off+1));
}
return s;
}
public override string ToString()
{
return ToString(0);
}
}
public static void Main()
{
Tree Obj1 = new Tree(1);
Obj1.Add(3);
Obj1[3].Add(2);
Obj1[3][2].Add(6);
Obj1[3].Add(4);
Obj1[3][4].Add(5);
Console.WriteLine(Obj1);
}

}




Output:

1
|3
|2
|6


|4
|5


[Edited by - Kambiz on April 20, 2006 7:53:30 PM]

Share this post


Link to post
Share on other sites
Many thanks, I've only just migrated from 2003 to 2005 so those dictionaries are new to me.... but i get the gist, thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!