在java中遍历非二叉树 [英] traversing a non binary tree in java
本文介绍了在java中遍历非二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一棵不是二叉树的树,每个节点有两个以上的孩子,我正在寻找一种遍历树的算法,我在学习数据结构方面真的很新,我知道如何遍历二叉树但是我在遍历非二叉树时迷路了.任何人都可以给我一个提示吗?
I have a tree which is not a binary tree and each node has more that 2 children, I am looking for an algorithm traverse the tree, I am really new in learning data structure, I know how to traverse a binary tree but I get lost when it comes to traverse a non-binary tree . Could any one gives me a hint ?
推荐答案
在非二叉树中,将有一个 Vector
或其他一些引用所有子树的结构.做一个递归方法:
In a non-binary tree, there will be a Vector
or some other structure that has references to all the children. Make a recursive method as so:
public void traverse(Node child){ // post order traversal
for(Node each : child.getChildren()){
traverse(each);
}
this.printData();
}
类似的东西.
这篇关于在java中遍历非二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文