0

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).

Konstantin Weitz
  • 6,180
  • 8
  • 26
  • 43

1 Answers1

0

Look at Haskell and functional languages to simplify recursive data structures. See the article here.

isaach1000
  • 1,819
  • 1
  • 13
  • 18