
正文
java树形菜单递归代码 java树形结构递归实现
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用Java实现树形结构啊?
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历
*
* 参考资料0:数据结构(C语言版)严蔚敏
*
* 参考资料1:
*
* 参考资料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 内部类:节点
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0个索引处的值即为根节点
Node root = nodeList.get(0);
System.out.println("先序遍历:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
postOrderTraverse(root);
}
}
相关问答
Q1: java递归遍历某个菜单下的菜单树
不太清楚你这个Menu是哪来的类,不过如果上文你的程序能执行的话,说明menu.getChilds()是个集合,应该带有size()的函数。你可以取出menu.getChilds()的大小,再从头到尾遍历它。
int count=menu.getChilds().size();
for(int i=0;icount;i++)
{
showMenu( ((Menu)menu.getChilds().get(i)) , 0 );
//我估计这些children是个list,可以顺序遍历;但也有
//部分可能是set,那样就得用iterator了。
}
Q2: JSP动态树形菜单,菜单项从数据库中获得
jsp动态树形菜单须用到递归算法,比如在数据库有张表,parent表,parent的字段有id,name,depth,leve,ID自增,depth设置为级数,如这条数据最大,为0,如为字菜单就为1,而leve就指定它父节点的id,给段代码自己可以摸索下 public Vector getModuleTree()
{
Vector pclass = new Vector();
try
{
stmt =con.createStatement();
String sql = "select * from Module where parentid = 0";
rs = stmt.executeQuery(sql);
Module cvo = null;
while(rs.next())
{
cvo = new Module();
cvo.setModule_id(rs.getInt("Module_id"));
cvo.setModule_name(rs.getString("Module_name"));
cvo.setModule_url(rs.getString("Module_url"));
cvo.setParentid(rs.getInt("parentid")); cvo.setRootid(rs.getInt("rootid")); cvo.setDepth(rs.getInt("depth")); pclass.add(cvo);
}
for (int i = 0; i pclass.size(); i++)
{
Module pcvo = (Module) pclass.get(i);
ShowTreeMenu(pcvo);
}
con.commit(); } catch (SQLException e)
{
e.printStackTrace();
} finally
{
try
{
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(con!=null)
{
con.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
return classList;
}
public void ShowTreeMenu(Module c)
{
Module ccvo = null;
String sql = "select * from Module where parentid = " + c.getModule_id();
Vector cclass = new Vector();
try
{
Module cvotemp;
stmt =con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
cvotemp = new Module();
cvotemp.setModule_id(rs.getInt("Module_id"));
cvotemp.setModule_name(rs.getString("Module_name"));
cvotemp.setModule_url(rs.getString("Module_url"));
cvotemp.setParentid(rs.getInt("parentid")); cvotemp.setRootid(rs.getInt("rootid")); cvotemp.setDepth(rs.getInt("depth")); cclass.add(cvotemp);
}
System.out.println(cclass.size()+"(((((((((((((((((((((((((9");
if (cclass.size() 0)
{
c.setHasChild("have");
classList.add(c);
for (int j = 0; j cclass.size(); j++)
{
ccvo = (Module) cclass.get(j);
ShowTreeMenu(ccvo);
} } else
{
classList.add(c);
}
} catch (SQLException e)
{
e.printStackTrace();
}
}
Q3: Java递归如何正确输出树形菜单
首先我们要建立树节点的类:
[java] view plain copy
package com.tree;
public class Node {
private Integer id;
private Integer parentId;
private String name;
private String link;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
输出树形菜单类:
[java] view plain copy
package com.tree;
import java.util.ArrayList;
import java.util.List;
public class Tree {
private StringBuffer html = new StringBuffer();
private ListNode nodes;
public Tree(ListNode nodes){
this.nodes = nodes;
}
public String buildTree(){
html.append("ul");
for (Node node : nodes) {
Integer id = node.getId();
if (node.getParentId() == ) {
html.append("\r\nli id='" + id + "'" + node.getName()+ "/li");
build(node);
}
}
html.append("\r\n/ul");
return html.toString();
}
private void build(Node node){
ListNode children = getChildren(node);
if (!children.isEmpty()) {
html.append("\r\nul");
for (Node child : children) {
Integer id = child.getId();
html.append("\r\nli id='" + id + "'" + child.getName()+ "/li");
build(child);
}
html.append("\r\n/ul");
}
}
private ListNode getChildren(Node node){
ListNode children = new ArrayListNode();
Integer id = node.getId();
for (Node child : nodes) {
if (id.equals(child.getParentId())) {
children.add(child);
}
}
return children;
}
}
java树形菜单递归代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java树形结构递归实现、java树形菜单递归代码的信息别忘了在本站进行查找喔。







