views:

231

answers:

2

I am looking for a tree implementation... you can see the me previous question here. but I won't like to implement it myself,

example functionalities needed:

  • I need FindElement(node)
  • I need GetParent(node) - will do the find again
  • GetSubTreeFrom(node) - will find the element and return a subtree..

I know C5 - but all the trees there are red-black (and I don't want it to be ordered) I tried Powercollection didn't find Tree...

I am not sure but maybe Set or Hash can do the job.

any help would be appreciated.

A: 

Well an unordered tree isn't much good for anything, generally. Searching, then becomes an O(n) operation, defeating the whole purpose of using a tree to begin with.

Maybe what you need is a tree of trees (of trees of trees....). Each level in the directory can be its own tree. Sub-categories can be a sub-tree member of the parent category node.

I haven't thought this through very well, but with it you can use off the shelf data structures instead of writing your own.

Steve K
A: 

You could implement what you want like this:

class Category
{
    ArrayList Next;
    string name;

    public Category()
    {
        name = "";
        Next = new ArrayList();
    }

    public Category(string name)
    {
        this.name = name;
        Next = new ArrayList();
    }

    public void Add(string name)
    {
        Next.Add(new Category(name));
    }

    public Category Find(string name)
    {
        Category a;
        foreach (Category c in Next)
        {
            if (c.name == name)
                return c;
            a = c.Find(name);
            if (a != null) return a;
        }
        return null;
    }
  //  other functions you need
}

And the use it like this:

Category c = new Category();
c.Add("books");
Category a;
a = c.Find("books");
a.Add("SF");
a.Add("drama");
if (c.Find("SF") != null)
    Console.WriteLine("found SF");
if (c.Find("other") == null)
    Console.WriteLine("did not find other");
Razvi