oop - Best practice: A child node's knowledge about its parent -
i wanted idea of best practice how information child node in tree should know parent.
my current problem simple , straight-forward. have tree of information , wanted "full name" of leaf node (in case name of each node in tree leaf node separated dot). can either adding "getfullname" method leaf nodes traverses tree root , prepends each parent's name , returns final result require leaf nodes knowing class type of parent (leaf , non-leaf not same class). or can add utility function somewhere else same thing knows different class types.
i attempted search around question little broad useful hits on google.
thanks in advance.
you do have lot of options here, , yes question addresses common situation of trading off processing efficiency on 1 hand increased storage requirements , possible redundancy on other.
there no single best practice. space/time tradeoff depends on situation. if go redundant storage, keeping both child , parent links within nodes, need make sure data structure encapsulated , methods keep consistent.
since have use case traverse nodes top, parent links work fine , can compose full name via recursion or building string front. because use case situation, not bad idea.
another option store full name in node, adds redundancy in case move nodes.
in short, don't have worry violating best practices, should weigh choices in making choice right you.
now if making general-purpose tree data structure, such java's treenode, create interface , allow people implement things see fit, providing suitable general implementation (defaultmutabletreenode) has links -- parent, child, , sibling.
Comments
Post a Comment