Search code examples
data-structurestreerelationshipnaming

Name of a data relationship where an object cannot have both parent and children?


We recently encountered a case when we need to model this relationship. A node can have one or no parent, or multiple children. A node can not have both a parent and children. It's basically a tree with a max depth of 2.

It's worth mentioning that all the objects are from the same class too. Is there a specific name for this type of relationship? I'm hesitant to use names like "parent" or "children", because they imply a tree-like relationship with arbitrary depth.


Solution

  • If what you have is something like:

    class Node
        int parentOrChildFlag; // tells what kind of node it is
        Node parent;     // reference to parent
        Node children[]; // collection of children
    end class
    

    And logic that somehow ensures that a node can have a parent or children, but not both, then the specific name for this type of node is "bad design."

    This doesn't sound like it should be the same class. The parent class contains child nodes, but no parent. The child nodes have a parent, but no children. To me, that implies this structure:

    class ParentNode
        // some parent-specific stuff
        ChildNode children[]; // collection of child nodes
    end class
    
    class ChildNode
        // child-specific stuff
        ParentNode parent;
    end class
    

    That's just a standard hierarchical relationship.