3

I want to display a tree structure. Do i really need to give the user/tree a predefined hardcoded root node like "RootUnit" where he can add his children or descendants?

Does this make sense or cause only trouble when adding nodes?

4
  • You sould add a tag corresponding to the language / framework you are using...
    – assylias
    Jul 6, 2012 at 16:49
  • 1
    There is only one language I would use :P
    – Pascal
    Jul 6, 2012 at 16:52
  • If the question is one of display, then it depends on how your nodes are named. If the root has a name, that should do. If you are displaying paths, e.g. "/Gramps/Dad/me" then "/" might be the displayed root of your family tree. Aside: I have had occassions to have one set of nodes in two trees simultaneously, e.g. contacts that may be traversed by either name or address.
    – HABO
    Jul 6, 2012 at 17:08
  • @Piskvor Borland Pascal I did when I was 12 ;-) My love is C#.
    – Pascal
    Jul 6, 2012 at 17:33

5 Answers 5

5

If you have two roots then you have two trees.

5
  • This is not an answer to my question rather a general statement about trees.
    – Pascal
    Jul 9, 2012 at 18:54
  • @Pascal - your question is "should a tree have exactly one root node" and the answer should be "yes" but Stackoverflow doesn't allow so short answers. I guess you have a deeper question but reading through the rest of your question it still eludes me.
    – LosManos
    Jul 9, 2012 at 21:19
  • Look at the last part of my question:"... or cause only trouble when adding nodes..." I would like to discuss this scenario considering the user has a add_descendant and add_children button.
    – Pascal
    Jul 10, 2012 at 13:01
  • Through reading stackoverflow.com/a/11366436/521554 I believe I understand your question. Am I correct that it could be something in the way of "Do I have to create a root node to start with or is it ok to check if the root node is created the first time the user adds a node?" Hmm. Not very easy to understand my rephrasing either... I believe you have to make your question clearer.
    – LosManos
    Jul 10, 2012 at 21:19
  • 1
    I will make a new question with sample scenario.
    – Pascal
    Jul 12, 2012 at 15:06
2

A tree should have only one root. But you need not hardcode a root. Just treat the first created tree node as root.

4
  • 1
    Then I would to check every time a node is added if nodes.count == 0 or nodes == Null then newNode.parent = null. I do not like that very cumbersome.
    – Pascal
    Jul 6, 2012 at 16:51
  • Not really, you know it when you are creating a root. Just create the nodes normally, and when the parent node is NULL, you're creating a root.
    – Rango
    Jul 6, 2012 at 17:10
  • The user has no AddRoot button. Just a Add Node button so I must always do a count or parent == null check on the existing nodes etc.
    – Pascal
    Jul 7, 2012 at 11:23
  • Checking for a root node isn't that much of a hassle IMHO. You can do it with code like myNode = otherNodePossiblyParentPossiblyRoot ?? new Node();
    – LosManos
    Jul 15, 2012 at 18:57
2

A tree by definition has only one root and every child node has exactly one parent (except the root which has no parent). If these restrictions are not met then your tree is no longer a tree but a graph (oriented or not)

2

It depends on the context. From a strict mathematical definition, you cannot have multiple root nodes to a tree. However, there some implementations of trees that ignore that and have multiple top level nodes anyway (Such as the TreeView control you tagged this question with). You simply need to ask yourself if your particular program would be better or worse with multiple top level nodes. Given that we know nothing else about your program it's not a decision we can really make for you.

1

Rather than using the same constructor for every node, supply a default constructor used for the root node and one for everything else. It isn't ugly, and it works.

public Node()
{
  // Set properties if you'd like.
  // such as having no children yet or whatnot.
}

public Node(Node parent)
{
  // Similar to Node()
}

See! Nice and clean.

1
  • From where do I know when to call the overloaded Note constructor? There exist no Add Root node button. That would also make no sense.
    – Pascal
    Jul 9, 2012 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.