Merging Treenodes

前端 未结 4 1805
借酒劲吻你
借酒劲吻你 2021-01-19 03:41

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

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


        
4条回答
  •  孤独总比滥情好
    2021-01-19 04:38

    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?

提交回复
热议问题