I have an algorithm problem. I'll simplify the issue, because most of what I'm dealing with has nothing to do with the algorithm I need.
Basically, I have a list of objects that each have properties. Let's say for the sake of simplicity that this was a simple struct or another simple data type containing a string ID and an array of strings that are its properties. The properties can be things like "tool", "weapon", "food", etc.
What I need to do is turn this list of objects into a tree, where the most common properties go on top, and the least common go on the bottom. It's a bit more complex than that, actually. For example, let's say that I have:
- Four objects with only the "weapon" property.
- Six objects with a "tool" and "weapon" property.
- Two items with a "food" and "fruit" property.
- One item with a "tool" property.
If I were to turn this into the tree that I want, it'd look like this:
weapon(as there are ten items that have the weapon property)tool(as there are six items with the tool and weapon properties)
foodfruit
tool
It's simple to do by hand, but I can't seem to wrap my head around putting this into program form. Any help?
toolentries (one at top level and another underweapon), but only oneweaponentry?toolandweaponproperty, but there are more items with theweaponproperty - so theweaponproperty is at the top. Then thetoolentry comes under that, because there are six items with theweaponandtoolproperty - so if you were to flatten it, you would get an object with bothweaponandtool.