views:

522

answers:

4

I have an application that models a tree, with classes Tree, Node and Edge (I need the Edge class!), and I'm not sure how to represent the relationships in the class diagram for this. I've read other posts about this, but am still in doubt.

The Tree object have a pointer to a Node (its root), which I believe defines an one-way association (Tree -> Node) with multiplicity 1..1 in both ends. Is it right?

Each Node object have pointers to the edges that comes out of it (Edge objects). Since these edges only exist if the node exist, I believe it's a composition association.

But then I have, in each Edge object, a pointer to the target Node of the edge. How can I represent this relationship, considering I already have the Node -> Edge composition described above?

Also, if you're still reading :), each Node have a pointer to its parent Node. Here, I would use an one-way unary association, but I don't know which name to use for this relationship.

Thanks for any help.

+1  A: 

I'd say that:

  • A tree has a root node
  • Nodes have child nodes
  • Nodes may have a parent node

Note:

  • You might want to distinguish between a UML class diagram versus a UML object diagram
  • I don't know whether root node and child node are the same class (a child node might be a subclass of root node, because both types of node have children but only child nodes have a parent node)

Edge might not be a class at all: instead an Edge might just be an instance of a Node.

ChrisW
I like the idea of specializing the Node class. Regarding the Edge, I'll not get into details, but I need it for this application. Thanks!
Vinicius Pinto
+1  A: 

From what you describe it sounds like the UML class diagram should have:

Tree -> Node, aggregation, 1..0-1
Node -> Edge, aggregation, 1..*
Edge -> Node, composition, 1..2 (an edge exists only if it connects 2 nodes)
Node -> Node, aggregation, 1..1 (would be composition, except the root node doesn't point to a node.

The difference between aggregation and composition can be thought of in terms of lifetimes. If two objects lifetimes are the same because they are co-dependent on each other then the relationship is one of composition, otherwise it is aggregation.

Phillip Ngan
+1  A: 

--The Tree object have a pointer to a Node (its root), which I believe defines an one-way --association (Tree -> Node) with multiplicity 1..1 in both ends. Is it right?

No, the multiplicity should be 0..1 - 1 (not all nodes will be the root of a tree)

You should also think whether Edge is really a class or not. If you don't need to store any kind of information about the edges (i.e. they are not labelled), I'd just model edges as a binary associations between nodes

Jordi Cabot
A: 

The handling of a Tree structure in OO is solved by the Composite Design Pattern.

You seem to be slightly confused by the difference between aggregation and composition. Use composite when the lifetime of the whole and the part are equal, use aggregation where the lifetime of the parts is different, i.e. you add and/or remove parts from the collection.

Martin Spamer