I'm trying to implement a tree structure and I would like that every node has a pointer to both its children and parent. I.e. the reference between two nodes goes both ways.
Do languages exist (e.g. python) where such a relation can be modeled nicely? What I am doing right now is:
class Node {
setParent(Node p) {
this.parent.chilren.remove(this)
p.chilren.add(this)
this.parent = p
}
// ...
}
But I would prefer an approach where I can factor out this aspect of a bidirectional reference such that I can reuse the same construct in other places. (e.g. a more declarative approach would be nice).