There are two future upcoming features that will help here, the first is a function that counts the number of children of a node (a node is an entity or a location or the ether). The second is per-node attributes / stats. I may have already added the first experimentally, I need to check it. The second I kind of half added experimentally again, but the api is awful at the moment, and it's not been tested at all. I add a lot of things experimentally until I understand if they are hard to use or easy to use, before I document them and they become "supported".
There may be a brute force way of implementing this logic using dynamic booleans or dynamic ints. Maybe Gareth can think it through using the standard functions. He's very good at novel solutions using the basic functions.