Concept question and best approach for linking list of nodes

I'm trying to link a random list of nodes such that each node may have 4 directions (up/down/left/right) that point to another node. I was thinking of just starting with the first node and randomly selecting how many of the 4 directions I want and linking those nodes up. After that I go to those nodes and call that function until I'm out of nodes. Issue I see is how do I know when to populate other directions so I'm not just favoring a single path from initial setup? I do want them to distribute somewhat evenly (or at least randomly)

Another issue is that even though I'd know that every node on the list is linked I'd also like to link any of the nearby nodes that can be linked with each other. Like if a node has a node above and below it but there also exists a node on its right than they link to each other. I was thinking of trying to check out the node below see if a link exists to node to the right and than above and if that node exists than link them, if not than try the node above+right+below and if that path exists than link them. I feel like there would be a lot of exceptions where this might not work or probably a much faster way than checking each node's missing links?

Posted Jul 23, 2021, 2:14 AM UTC

13

This is something of a requirements question. Is your goal that every path from the root is roughly the same length? Should most directions be populated close to the root? Do you want long stretches of nodes with no branches with occasional "nexus" nodes that go off in every direction? You can make any of that, but you need to decide what you want. Also, do you want your list to have cycles? Because if so, we've gotta go back and change everything.

Without firmer requirements, I'd default to what's basically a breadth-first search. Any time your search would walk into a blank space, instead toss a new node into that space.

1
GosexualOP•

I don't need a same length path from root and it can have as few or many directions and would prefer to avoid long stretches. I am looking at breadth-first search I'm mostly trying to figure out the cycle. If a node is directly across from another node than to link them. Like this blue node that is being created should connect back to the node above root and the node below it

1

Is there something geometric here? If I go up right down left, should I be back at the first node?

1