自底向上转换 N 叉树,无需递归
考虑具有这种节点结构的N叉树:
class NodeTypeA {
String payload;
List<NodeTypeA> children;
}
并且我们希望将树转换为NodeTypeB
树,但是NodeTypeB
的内部结构是不可见的,我们只能构造新树使用两种方法:
//This method can only convert leaf NodeTypeA (no children)
public NodeTypeB convertLeaf(NodeTypeA nodeTypeA);
//This method can convert a non-leaf NodeTypeA as long as its children are converted
public NodeTypeB convertParent(NodeTypeA parentNodeTypeA, List<NodeTypeB> nodeTypeBs);
递归解决方案很简单,我们如何以非递归方式做到这一点?
递归解法
public NodeTypeB convert(NodeTypeA root) {
if(root == null) return null;
if(root.children == null || root.children.size() == 0) {
return convertLeaf(root);
}
List<NodeTypeB> nodeBs = new ArrayList<>();
for(NodeTypeA child:root.children) {
nodeBs.add(convert(child));
}
return convertParent(root, nodeBs);
}
Considering a N-ary tree with this node structure:
class NodeTypeA {
String payload;
List<NodeTypeA> children;
}
And we want to convert the tree to NodeTypeB
tree, but the internal structure of NodeTypeB
is invisible, and we can only construct the new tree using two method:
//This method can only convert leaf NodeTypeA (no children)
public NodeTypeB convertLeaf(NodeTypeA nodeTypeA);
//This method can convert a non-leaf NodeTypeA as long as its children are converted
public NodeTypeB convertParent(NodeTypeA parentNodeTypeA, List<NodeTypeB> nodeTypeBs);
The recursive solution is trivial, how can we do it in non-recursive way?
Recursive solution
public NodeTypeB convert(NodeTypeA root) {
if(root == null) return null;
if(root.children == null || root.children.size() == 0) {
return convertLeaf(root);
}
List<NodeTypeB> nodeBs = new ArrayList<>();
for(NodeTypeA child:root.children) {
nodeBs.add(convert(child));
}
return convertParent(root, nodeBs);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为此使用堆栈。堆叠元素应该具有对
NodeTypeA
实例和NodeTypeB
实例列表的引用。只要该列表的大小小于NodeTypeA
节点的子节点数量,堆栈就应该使用尚未扩展的NodeTypeA
的下一个子节点进行扩展。映射。当一个节点可以被映射时,它将被附加到堆栈顶部的 NodeTypeB 实例列表中,并且该过程继续处理下一个子节点(如果有)。当子级数量相等时,堆叠的 NodeTypeA 实例的所有操作都已完成,并且所有操作都可用于创建内部 NodeTypeB 节点。然后可以从堆栈中弹出该项目,并且可以将创建的节点追加到现在位于堆栈顶部的列表中。
这一直持续到堆栈为空为止。
作为实现,您可以首先定义这个简单的类:
然后
convert
函数可能如下所示:You can use a stack for this. A stacked element should have a reference to a
NodeTypeA
instance, and to a list ofNodeTypeB
instances. As long as the size of that list is smaller than the number of children of theNodeTypeA
node, the stack should be extended with the next child ofNodeTypeA
that has not yet been mapped. When a node can be mapped, it is appended to the list ofNodeTypeB
instances at the top of the stack, and the process continues with the next child (if any).When the number of children is equal, then all has been done for the stacked
NodeTypeA
instance, and all is available to create an internalNodeTypeB
node. This item can then be popped from the stack, and the created node can be appended to the list that is now on the top of the stack.This continues until the stack is empty.
As implementation, you could first define this simple class:
And then the
convert
function could look like this: