The title pretty much says it all. I'm just having a little computer science exercise and am building a Tree. I want a node to be able to add a sibling which requires one to first get a reference to the parent, obviously. I was wondering what is the "correct" way, by definition of what a Tree is, to do such a thing?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Well, you add a child to the parent.
But consider that the root can't add a sibling. This means that sibling-adding isn't something that a node in general knows how to do. You can check whether the parent is null, but (assuming you're doing OO) purists would say to put "add sibling" somewhere outside of your node class. Often you have a wrapping "Tree" class which holds a root node; the Tree class might be a good place for a static "add sibling" method.
A lot of trees I've worked with in the wild (window trees in display servers and so on) have just two pointers: