今天看了csdn中lifetragedy的一篇精华博文《通向架构师的道路(第六天)之漫谈基于数据库的权限系统的设计》,里面提到的菜单表的设计方式。其中使用lft,rgt两个字段的标示和预计算,从而jquery等tree控件能够从检索结果,通过一次遍历达到构建菜单目录树的效果。详情参见:http://blog.csdn.net/lifetragedy/article/details/7734864。

笔者想到创建表的时候可以直接从树形结构的菜单,通过计算获得lft,rgt,level字段,并与其他所需信息一同入库,这个方式在初始建库的时候特别有用。不需要每一个菜单项都是用lifetragedy给出的四步,当然,如果后续添加一些菜单,还是使用lifetragedy的方法直接一些。

下面给出笔者的模拟代码(其中的tree使用的是lifetragedy在原文中给出的示例,这里的Tree可以是从xml文件中读取,那么这段代码的实用性就大了。):

package com.ss.util;public class CreateMenuTable {private static Node tree;static{//create a menu treetree = new Node("caidan");//first levelNode baobiao = new Node("baobiao");Node xitong = new Node("xitongguanli");tree.addChild(baobiao);baobiao.setParent(tree);tree.addChild(xitong);xitong.setParent(tree);//second levelNode nianbao = new Node("nianbao");Node jibao = new Node("jibao");Node yuebao = new Node("yuebao");Node zhoubao = new Node("zhoubao");baobiao.addChild(zhoubao);zhoubao.setParent(baobiao);baobiao.addChild(yuebao);yuebao.setParent(baobiao);baobiao.addChild(jibao);jibao.setParent(baobiao);baobiao.addChild(nianbao);nianbao.setParent(baobiao);Node yonghuguanli = new Node("yonghuguanli");Node jueseguanli = new Node("jeuseguanli");xitong.addChild(yonghuguanli);yonghuguanli.setParent(xitong);xitong.addChild(jueseguanli);jueseguanli.setParent(xitong);//third levelNode zengjia = new Node("zengjia");Node shanchu = new Node("shanchu");yonghuguanli.addChild(zengjia);zengjia.setParent(yonghuguanli);yonghuguanli.addChild(shanchu);shanchu.setParent(yonghuguanli);Node zengjia1 = new Node("zengjia");Node shanchu1 = new Node("shanchu");jueseguanli.addChild(zengjia1);zengjia1.setParent(jueseguanli);jueseguanli.addChild(shanchu1);shanchu1.setParent(jueseguanli);}//use depth first traversal method to calc width of each node.//every node's span is 2, so set it's width = 2private void calculate_width(Node tree){//leaf node set width = 2if(tree.getChilds().size() == 0){tree.setWidth(2);return;}//calc each child's widthfor(Node child : tree.getChilds()){calculate_width(child);}//calc this root node's widthfor(Node child : tree.getChilds()){tree.setWidth(tree.getWidth() + child.getWidth());}tree.setWidth(tree.getWidth() + 2);//his own width}//calc by breadth first traversal method and use the following equals://for the first child node--//lft = parent.lft + 1//rgt = lft + width -1////for orther child node--//lft = rgt_before + 1//rgt = lft + width -1private void calculate_landr(Node tree){//for leaf nodeif(tree.getChilds().size() == 0){return;}//for trees has child node//calc first node's landrNode firstNode = tree.getChilds().get(0);firstNode.setLft(firstNode.getParent().getLft() + 1);firstNode.setRft(firstNode.getLft() + firstNode.getWidth() -1);printNodeInfo(firstNode);Node preNode = firstNode;for(int index=1, size=tree.getChilds().size(); index<size; index++){Node otherNode = tree.getChilds().get(index);otherNode.setLft(preNode.getRft() + 1);otherNode.setRft(otherNode.getLft() + otherNode.getWidth() -1);printNodeInfo(otherNode);preNode = otherNode;}//calc tree's child's child's landrfor(Node child : tree.getChilds()){calculate_landr(child);}}private void printNodeInfo(Node node){StringBuilder sbuilder = new StringBuilder();sbuilder.append(node.getName());sbuilder.append(" ");sbuilder.append(node.getLft());sbuilder.append(" ");sbuilder.append(node.getRft());sbuilder.append(" ");sbuilder.append(node.getWidth());sbuilder.append(" ");System.out.println(sbuilder.toString());}public static void main(String[] arg){CreateMenuTable cmt = new CreateMenuTable();cmt.calculate_width(tree);tree.setLft(1);tree.setRft(1 + tree.getWidth() - 1);cmt.printNodeInfo(tree);cmt.calculate_landr(tree);}
}

下面给出Node类的代码:

package com.ss.util;import java.util.ArrayList;
import java.util.List;public class Node {private Node parent;private List<Node> childs = new ArrayList<Node>();private String name;private int lft;private int rft;private int depth;private int width;public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public Node(String name){this.name = name;}public Node getParent() {return parent;}public void setParent(Node parent) {this.parent = parent;}public String getName() {return name;}public void addChild(Node child){this.childs.add(child);}public List<Node> getChilds(){return this.childs;}public int getLft() {return lft;}public void setLft(int lft) {this.lft = lft;}public int getRft() {return rft;}public void setRft(int rft) {this.rft = rft;}public int getDepth() {return depth;}public void setDepth(int depth) {this.depth = depth;}
}

运行输出的控制台结果如下:

caidan 1 26 26
baobiao 2 11 10
xitongguanli 12 25 14
zhoubao 3 4 2
yuebao 5 6 2
jibao 7 8 2
nianbao 9 10 2
yonghuguanli 13 18 6
jeuseguanli 19 24 6
zengjia 14 15 2
shanchu 16 17 2
zengjia 20 21 2
shanchu 22 23 2 

符合lft, rgt的无限分类算法的Java生成代码相关推荐

  1. 基于系统数据过滤权限设计之----lft, rgt的无限分类算法

    系统权限设计中的一般常见会有: 用户表 角色表 用户与角色关联表 资源表 角色与资源关联表. 当然这里介绍是笼统的,有些系统中会有用户组表或者角色组表,甚至为了把菜单资源和按钮资源分开,也创建单独的表 ...

  2. 约瑟夫环 java代码_约瑟夫环算法的Java实现代码

    相信大家都知道这是一个的算法问题,约瑟夫环的c语言实现是利用了指针链表的形式,java实现呢,我的这个是用了内部类. 算法描述:n个人围成一圈,每人有一个各不相同的编号,选择一个人作为起点,然后顺时针 ...

  3. 第四篇:决策树分类算法原理分析与代码实现

    前言 本文详细介绍机器学习分类算法中的决策树算法,并全面详解如何构造,表示,保存决策树,以及如何使用决策树进行分类等等问题. 为了全面的理解学习决策树,本文篇幅较长,请耐心阅读. 算法原理 每次依据不 ...

  4. Logistic回归分类算法原理分析与代码实现

    前言 本文将介绍机器学习分类算法中的Logistic回归分类算法并给出伪代码,Python代码实现. (说明:从本文开始,将接触到最优化算法相关的学习.旨在将这些最优化的算法用于训练出一个非线性的函数 ...

  5. java 鸢尾花分类算法_鸢尾花分类算法实现 java

    使用的贝叶斯分类算法实现的,编程语言为java.是我本学期修的数据库与数据挖掘的课程的期末课程作业,算法本身不难,思路理清楚了很简单. 先看看鸢尾花(Iris)数据集(下图为数据集的部分截图),鸢尾花 ...

  6. 机器学习监督学习之分类算法---朴素贝叶斯代码实践

    目录 1. 言论过滤器 1.1 项目描述 1.2 朴素贝叶斯 工作原理: 1.2.1 词条向量 1.3 开发流程: 1.4 代码实现 1.4.1 创建样本 1.4.2 构建词汇表,用于建立词集向量 1 ...

  7. 决策树分类算法的案例(代码实现及运行测试)

    1 案例需求 我们的任务就是训练一个决策树分类器,输入身高和体重,分类器能给出这个人是胖子还是瘦子. 所用的训练数据如下,这个数据一共有10个样本,每个样本有2个属性,分别为身高和体重,第三列为类别标 ...

  8. php移除所有子节点,PHP 循环删除无限分类子节点的实现代码

    private  function _deleteSubNode($ids){ $subNodes = array(); $mod = D('Node'); foreach (explode ( ', ...

  9. php mysql 分类_php+mysql实现无限分类实例详解

    本文实例讲述了php+mysql实现无限分类的方法.分享给大家供大家参考.具体分析如下: 1.数据库通过设置父类ID来进行唯一索引,然后使用函数的递归调用实现无限分类: 2.数据库设计通过特定格式进行 ...

最新文章

  1. 如何在 Linux 上永久挂载一个 Windows 共享
  2. Centos 升级MySQL版本或者Yum安装Mysql5.6
  3. 计算机主机安装系统安装系统安装软件,电脑安装软件时提示安装过程出错系统设置未被修改怎么办...
  4. ubuntu下rar文件解压后文件名乱码
  5. oracle常用函数介绍
  6. IOS ViewController 生命周期
  7. 【微服务架构】SpringCloud组件和概念介绍
  8. 《I'm a Mac:雄狮训练手册》——2.3 账户类型
  9. switchHosts下载
  10. 饿汉式单例模式,懒汉式单例模式
  11. logit回归模型的参数估计过程_计量模型——面板向量自回归模型(PVAR模型)操作全过程...
  12. 工程师职业发展的四个阶段
  13. 【ANSYS SCDM】,软件介绍及基本操作
  14. 核心单词Word List 5
  15. 免费的二维码图片生成API接口和使用
  16. 定制属于你的潮流轻链竞品分析
  17. PORIS门禁控制器
  18. 浅析PC机串口通讯流控制
  19. Mysql常用语句帅哥特供版
  20. java class常用反编译操作

热门文章

  1. ANT无线通信技术(1) 简介
  2. 发布有礼!2015 Autodesk程序商店有奖发布活动拉开序幕
  3. [Practical.Vim(2012.9)].Drew.Neil.Tip01 学习摘要
  4. php模拟登陆正方教务系统(2018年)
  5. 编码通信与魔术初步(四)——通信编码魔术的基本原理
  6. php+供应商管理系统,供应商管理系统
  7. more than and less than
  8. 全国计算机二级考试设置地点,黑龙江2020年3月全国计算机等级考试考点设置
  9. 什么是IP封锁, 如何解封?
  10. 足球与oracle系列(2):巴西揭幕战预演,oracle体系结构杂谈