「java获取树的每层数据」java获取树结构
今天给各位分享java获取树的每层数据的知识,其中也会对java获取树结构进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java树形结构如何从数据库读取数据
定义一个TreeNode类.里面有
id 自身id
parentId 父节点id
name 树显示文本
url 链接地址
open 是否展开树
等属性.
将从数据库查出来的数据一个个set到TreeNode里面.封装成一个ListTreeNode.
再将这个List转换成你的树所需要的格式内容.比如json格式
如何统计一个二叉树每一层的节点个数
import java.io.*;
import java.util.*;
class TreeNode{//节点类
public String data;//节点数据
public TreeNode lchild,rchild;//左孩子及右孩子
public TreeNode(String d){// 节点构造函数
System.out.println("node created");//构造标志
data = d;
}
public TreeNode(){// 节点构造函数
System.out.println("node created");
}
public void visitNode(){//输出节点数据
System.out.print(data);
}
}
public class BiTree{
public BiTree(){//主类构造函数
TreeNode tn = new TreeNode();//建立新树根为空
TreeNode tn2 = tn;
tn.visitNode();
System.out.println("start creating tree");//开始建树标志
try{//建立二叉树
createBiTree(tn);
}catch(IOException e){
e.printStackTrace();
}
outputTree(tn);//按层输出二叉树
}
String instr;//接受输入的字符串变量
String over = new String(" ");//结束符标志
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public TreeNode createBiTree(TreeNode tn1)throws IOException{//从键盘输入字符串,按先序次序输入并创建二叉树
System.out.println("input node");
try{//从键盘输入节点数据
instr = br.readLine();
}catch(IOException e){//捕获异常并输出异常信息
e.printStackTrace();
}
if(instr.equals(over)){//如果为空则表示没有节点
System.out.println("null node");
tn1 = null;
}
else{//否则建立节点,并为该节点的左孩及右孩子建立节点如此循环
tn1.data = instr;
tn1.lchild = new TreeNode();
tn1.lchild = createBiTree(tn1.lchild);
tn1.rchild = new TreeNode();
tn1.rchild = createBiTree(tn1.rchild);
}
return tn1;
}
public void outputTree(TreeNode tn){//二叉树按层输出,传入参数树根
LinkedList ll = new LinkedList();//建立一个队列
TreeNode tm = new TreeNode("newh");//新行标志
int i = 0;
if(tn!=null){
ll.addLast(tm);//标志位入队尾
ll.addLast(tn);//根入队列尾
}
int numfornode=1;
while(ll.size() 1){//如果栈里有元素,取第一个队列第一个元素并输出
TreeNode tn1 = (TreeNode)ll.getFirst();
if(tn1.data.equals("newh")){
ll.removeFirst();
System.out.println();
System.out.println("第"+(++i)+"层: ");
ll.addLast(tn1);
numfornode=1;
}
else{
System.out.print("第"+numfornode+"个元素:");
tn1.visitNode();//输出节点数据
ll.removeFirst();//移出第一个元素
numfornode++;
}
if(!(tn1.lchild==null)){//如果有左孩子,将其加入队列尾
ll.addLast(tn1.lchild);
}
if(!(tn1.rchild==null)){
ll.addLast(tn1.rchild);
}
}
}
public void preOrder(TreeNode tn){
if(!(tn==null)){
tn.visitNode();
preOrder(tn.lchild);
preOrder(tn.rchild);
}
}
public static void main(String args[]){//程序入口
BiTree bt =new BiTree();
}
}
如何用java实现二叉树特定的层数的值的个数?
public class Tree {
public Tree left;
public Tree right;
public int calc(int level) {
int result = 0;
if (level == 0)
result = 1;
else {
if (left != null)
result += left.calc(level - 1);
if (right != null)
result += right.calc(level - 1);
}
return result;
}
/**
* 计算二叉树特定层的节点数
*
* @param tree
* 二叉树
* @param level
* 层
* @return
*/
public static int calc(Tree tree, int level) {
return tree == null || level 0 ? 0 : tree.calc(level);
}
}
java 树形表的读取,望大牛解答怎么实现
大概有以下几种做法:
parentId法,每条数据都有一个parentId,记录父记录ID,构建树的话,如果数据量小,全部加载,然后内存里构建树
parentId+parentIds法,每条数据有一个parentId和parentIds两个字段,构建构,查询parentIds,加载数据,然后内存构建树
parentId + startId + endId法,创建一条记录,指定他的子,可以开始和结束的ID范围。
java获取树的每层数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取树结构、java获取树的每层数据的信息别忘了在本站进行查找喔。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。