I have a Datastructure like the following.
array=[
{id:"a",children:[
{id:"b",children:[
{id:"d",children:[]}]},
{id:"c",children:[]}]}
]
If I want to insert the element {id:"e", children:["a","f"] at "c" ("a","e" are no Strings, but copies of the nodes) I want to check, that it exists in the upper Tree and would therefore create a circular reference. So I think I have to reverse walk the array. But as I'm pretty new to Javascript and Node, I have no idea how to do that.
Would it be agood idea, to create an array, I store all the dependencies in? something like this:
[
a:[],
b:[a],
c:[a,b]
d:[a,b]
]
then I could lookup the parent in the array and would see that in c, a and b are allready in dependency
children : [e]actually referred to the parent object, you'd have circular reference, but that seems unlikely and most of the time wouldn't be an issue. Anyway, it's unclear what you want to check for? When inserting that object do you want to check that there aren't more objects with the id"e"or that the object doesn't already have circular references, or what ?childrenproperty. On the other hand i can not imagine under what requirement you ended up with the necessity to check up circular references. Are you trying to nest up a flat data structure..? There might be ways to avoid that check for whatever you are trying to achieve.