HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Trees and their uses

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andusestheirtrees

Problem

I was getting sicker and sicker of using an ordinary collection to do a tree's work, so I built a tree. A requirement of the tree are that it needs to contain a key to sort the tree by, so I used a Dictionary to store the items. Here is the Tree:

public class Tree
{
    public Tree AddItem(int key, T value)
    {
        if (TreeItems.ContainsKey(key))
        {
            throw new ArgumentException("Tree already contains value at that location.");
        }

        TreeItems.Add(key, value);
        return this;
    }

    public Tree AddDirectory(int key, T item, Tree value)
    {
        this.AddItem(key, item);

        if (TreeMenus.ContainsKey(key))
        {
            throw new ArgumentException("Tree already contains directory at that location.");
        }
        TreeMenus.Add(key, value);
        return this;
    }

    public IEnumerable GetChildren()
    {
        return TreeItems.OrderBy(k => k.Key).Select(v => v.Value);
    }

    public IEnumerable> GetChildrenDirectories()
    {
        return TreeMenus.OrderBy(k => k.Key).Select(v => v.Value);
    }

    private Dictionary> TreeMenus = new Dictionary>();
    private Dictionary TreeItems = new Dictionary();
}


Now, I like that part good enough, the trouble is getting the values from higher up the tree. I am defining a tree like this in my MenuItemManager class:

```
public MenuItemManager()
{
Fill();
}

private Tree dataTree = new Tree();
private void Fill()
{
dataTree.AddDirectory(0, new MenuItem(string.Empty, typeof(Nullable), Menus.OneNote2013, AppVersion.OneNoteDesktop2013), new Tree()

.AddDirectory(0, new MenuItem(resourceFile.GetString("OneNote"), typeof(Tutorials.Desktop2013Data.Menus.OneNote), Menus.OneNote2013, AppVersion.OneNoteDesktop2013), new Tree()
)

.AddDirectory(1, new MenuItem(resourceFile.GetString("File2013"), typeof(Tutorials.Desktop2013Data.Menus.FileTab), Menus.File2013, AppVersion.OneNoteDesktop2013), new Tree()

Solution

I'm not sure if this is more of a tree or a file structure as I need it to be able to have multiple nodes at any level, with the true "root" being the variable holding it all together.

What's the difference? Is there one? According to Wikipedia a tree is


a way of representing the hierarchical nature of a structure in a graphical form. It is named a "tree structure" because the classic representation resembles a tree, even though the chart is generally upside down compared to an actual tree, with the "root" at the top and the "leaves" at the bottom.

So, a file structure (at the level of abstraction that we tend to think of them on) is a tree structure.

Now, I think your tree implementation is really pretty good, except you've gotten the reason you built it and what it is all mixed up. You've botched the abstraction and that is why you're confused about it.

public Tree AddDirectory(int key, T item, Tree value)


You're not adding a directory here. You're adding a branch. A more appropriate name would be AddBranch or AddChild. Then, you've separated the concept of a file directory from that of a tree branch. The reason you built the data structure no longer taints the data structure itself.

You've a similar problem here.

public IEnumerable> GetChildrenDirectories()


and here

private Dictionary> TreeMenus = new Dictionary>();


Trees don't have Directories and Menus.

Side Note: The methods you wrote to traverse and search the tree look like they could be cleaned up with some Linq.

Code Snippets

public Tree<T> AddDirectory(int key, T item, Tree<T> value)
public IEnumerable<Tree<T>> GetChildrenDirectories()
private Dictionary<int, Tree<T>> TreeMenus = new Dictionary<int, Tree<T>>();

Context

StackExchange Code Review Q#84623, answer score: 7

Revisions (0)

No revisions yet.