合并树节点节点

由网友(你是我拿命去爱的女人)分享简介:有谁知道一个算法,将通过以下方式合并树节点的?Does anyone know of an algorithm that will merge treenodes in the following way?treeA\ child a\node(abc)\ child b\node(xyz)...

有谁知道一个算法,将通过以下方式合并树节点的?

Does anyone know of an algorithm that will merge treenodes in the following way?

treeA
    child a
          node(abc)
    child b
          node(xyz)                   

         + 

treeB
    child a              
          node(qrs)
    child b
          node(xyz)
               node(pdq)
    child c
          node(pdq)

         = // do merge

treeMerged     
    child a
          node(abc) 
          node(qrs)
    child b
          node(xyz)
               node(pdq)
    child c
          node(pdq)

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

推荐答案

那么,一旦我居然拿了去想它的时候,该解决方案原来是比我预期的更简单。 (我已经张贴的code以下的关键部分)

Well, once I actually took the time to think about it, the solution turns out to be far more simple than I anticipated. (I've posted the critical part of the code below)

   private TreeNode DoMerge(TreeNode source, TreeNode target) {
        if (source == null || target == null) return null;

        foreach (TreeNode n in source.Nodes) {
            // see if there is a match in target
            var match = FindNode(n, target.Nodes); // match paths
            if (match == null) { // no match was found so add n to the target
                target.Nodes.Add(n);
            } else { 
                // a match was found so add the children of match 
                DoMerge(n, match);
            }

        }
        return target;

    }

仍然有兴趣知道,如果有人有一个更好的解决方案?

Still interested to know if someone has a better solution?

阅读全文

相关推荐

最新文章