开发者

What is the cleanest way to extend this design for tree-rewriting?

I have a tree of objects, where each object has a std::vector of pointers to its children. The class of the object has a rewrite() method that is recursively applied to alter the object and its children, typically with the following transformations, where [N] is the object being rewritten and (M) is the element that rewrite() returns:

                 (A)
 [A]             / \               [A]
 / \     -->    B   X               |    -->    (B)
B   C                \              B
                      C

What's the cleanest way of extending this setup to allow transformations like this?

    A             X
    |            / \
   [B]   -->    C   A
    |               |
    C              (Y)

That is, those that re-root the tree, move an element, insert a new element, and return the inserted element. I'm having a hard time coming up with something nice that also involves minim开发者_如何学Cal refactoring. Thoughts?


Looks like, for speed, you'll need a "back-pointer" from each child node to its parent node. This way you can follow the chain of parent pointers given a pointer to any node all the way to the root, if that's what you need (I'm not sure how else you planned to find the root given just a pointer to an inner node, without backpointers?). Of course, you'll need to adjust the back pointer, as well as the "children" std::vector, whenever you rearrange the tree -- an unfortunate consequence of the slight duplication of information (no more than, say, a doubly linked list presents;-), but a small price to pay for the ease and speed of such bidirectional navigation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜