遍历java中的非二叉树 [英] traversing a non binary tree in java

查看:183
本文介绍了遍历java中的非二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一棵不是二叉树的树,每个节点都有2个以上的子节点,我正在寻找一个遍历树的算法,我是学习数据结构的新手,我知道如何遍历二叉树但是在遍历非二叉树时,我迷路了。
可以有人给我一个提示吗?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆