「java实现树」java实现树形菜单一个表内多级菜单
今天给各位分享java实现树的知识,其中也会对java实现树形菜单一个表内多级菜单进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
用java怎么构造一个二叉树?
定义一个结点类:\x0d\x0apublic class Node {\x0d\x0a private int value;\x0d\x0a private Node leftNode;\x0d\x0a private Node rightNode;\x0d\x0a \x0d\x0a public Node getRightNode() {\x0d\x0a return rightNode;\x0d\x0a }\x0d\x0a public void setRightNode(Node rightNode) {\x0d\x0a this.rightNode = rightNode;\x0d\x0a }\x0d\x0a public int getValue() {\x0d\x0a return value;\x0d\x0a }\x0d\x0a public void setValue(int value) {\x0d\x0a this.value = value;\x0d\x0a }\x0d\x0a public Node getLeftNode() {\x0d\x0a return leftNode;\x0d\x0a }\x0d\x0a public void setLeftNode(Node leftNode) {\x0d\x0a this.leftNode = leftNode;\x0d\x0a }\x0d\x0a \x0d\x0a}\x0d\x0a \x0d\x0a初始化结点树:\x0d\x0apublic void initNodeTree()\x0d\x0a {\x0d\x0a int nodeNumber;\x0d\x0a HashMap map = new HashMap();\x0d\x0a Node nodeTree = new Node();\x0d\x0a \x0d\x0a Scanner reader = new Scanner(System.in);\x0d\x0a \x0d\x0a nodeNumber = reader.nextInt();\x0d\x0a for(int i = 0; i map, int nodeValue, Node parentNode) {\x0d\x0a int value = 0;\x0d\x0a if (map.containsKey("L" + nodeValue)) {\x0d\x0a value = map.get("L" + nodeValue);\x0d\x0a Node leftNode = new Node();\x0d\x0a leftNode.setValue(value);\x0d\x0a parentNode.setLeftNode(leftNode);\x0d\x0a \x0d\x0a setChildNode(map, value, leftNode);\x0d\x0a } \x0d\x0a \x0d\x0a if (map.containsKey("R" + nodeValue)) {\x0d\x0a value = map.get("R" + nodeValue);\x0d\x0a Node rightNode = new Node();\x0d\x0a rightNode.setValue(value);\x0d\x0a parentNode.setRightNode(rightNode);\x0d\x0a \x0d\x0a setChildNode(map, value, rightNode);\x0d\x0a }\x0d\x0a }\x0d\x0a \x0d\x0a前序遍历该结点树:\x0d\x0apublic void preTraversal(Node nodeTree) {\x0d\x0a if (nodeTree != null) {\x0d\x0a System.out.print(nodeTree.getValue() + "\t");\x0d\x0a preTraversal(nodeTree.getLeftNode());\x0d\x0a preTraversal(nodeTree.getRightNode());\x0d\x0a }\x0d\x0a }
如何用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);
}
}
java 如何实现树、图结构?
你如果要树形展示,在JSP上只能用树控件,类似dtree.js这种第三方JS包
如果是树体系,JAVA还是面向对象,只能用代码描述出一棵树,包括各个属性,能通过数据体现一个树的体系(子父编号关联),但无法直观的看出图形来
关于java实现树和java实现树形菜单一个表内多级菜单的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-19,除非注明,否则均为
原创文章,转载请注明出处。