Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, not sure if it's better but I had fun writing this tree. </p> <p>It uses dictionnary for nodes (should use something better as I use the tag "data" which is unclean)</p> <p>Root is root of tree, it gets filled by parsing initial data</p> <p>About complexity, it's unclear what is n : </p> <ul> <li>if you have lots of project (x) with few tags per project (y) then you want a good complexity on x and you don't care about y</li> <li>if you have a few projects with lots of tags, then you want the contrary</li> </ul> <p>I suppose first solution is mote common.</p> <p>I I'm not mistaken, my solution runs in x.y.ln(y) (I need to sort tags per project) to create the tree.</p> <p>Displaying the tree is y.y (but uses recursion and a temporary list so it must bloat memory).</p> <pre><code>d= { "apple": ["fruit", "green", "nature"], "sometree": ["tree", "green", "wood", "nature"], "banana": ["fruit", "nature"], "someplant": ["green", "wood", "nature"], "otherplant": ["green", "wood", "nature"] } root=dict() root['data']=[] for k,v in d.iteritems(): v.sort() # r=root for c in v: try: r=r[c] except KeyError: r[c]=dict() r=r[c] r['data']=[] r['data']+=[k] def dump(r,hist): result=r['data'][:] for x in r.keys(): if x=='data': continue result+=dump(r[x],hist[:]+[x]) if len(result)&gt;0 and len(r['data'])&gt;0: print (hist,result) return result dump(root,[]) </code></pre> <p>Code is far from perfect (quick and dirty mode) but it looks like it works:</p> <pre><code>$ c:\Python27\python.exe temp.py (['green', 'nature', 'tree', 'wood'], ['sometree']) (['green', 'nature', 'wood'], ['otherplant', 'someplant']) (['fruit', 'green', 'nature'], ['apple']) (['fruit', 'nature'], ['banana']) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload