当前位置: 代码迷 >> Java Web开发 >> java实现数据结构中二叉树的图形化表示
  详细解决方案

java实现数据结构中二叉树的图形化表示

热度:342   发布时间:2010-06-30 13:32:55.0
java实现数据结构中二叉树的图形化表示
用户输入一组数据,程序将数据转化成图形化树结构显示,最好提供增、删结点操作。
麻烦各位高手帮帮忙,谢谢了。。。
搜索更多相关主题的帖子: 二叉树  数据结构  java  图形  

----------------解决方案--------------------------------------------------------
我只用JS做过。

我给你提供思路和部分方法吧

首先你需要一个数的节点类TreeNodes

public class TreeNodes {

    private String state;
    private String data;
    private List<TreeNodes> children;
    private TreeAttributes attributes;
    public TreeAttributes getAttributes() {
        return attributes;
    }
    public void setAttributes(TreeAttributes attributes) {
        this.attributes = attributes;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }
    public List<TreeNodes> getChildren() {
        return children;
    }
    public void setChildren(List<TreeNodes> children) {
        this.children = children;
    }
    public TreeNodes() {
    }
   
}

----------------解决方案--------------------------------------------------------
其次是数的属性类 TreeAttributes

public class TreeAttributes {
    private Integer id;
    private Integer parentId;
    private int level;
   
    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 int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }
   
}

----------------解决方案--------------------------------------------------------
        具体操作和方法:
        treeList = new ArrayList<TreeNodes>();
        hierarchyList = ServiceManager.pub.getPubHierarchyService().cacheHierarchyList();
        if (hierarchyList == null || hierarchyList.size() == 0) {
            return SUCCESS;
        }
        TreeNodes parent = new TreeNodes();
        parent.setState("close");
        parent.setData(hierarchyList.get(0).getHierarchyName());
        TreeAttributes ta = new TreeAttributes();
        ta.setId(hierarchyList.get(0).getHierarchyId());
        ta.setParentId(0);
        parent.setAttributes(ta);
        List<TreeNodes> child = new ArrayList<TreeNodes>();
        if (hierarchyList != null && hierarchyList.size() >= 2) {
            child = this.getJsonByList(hierarchyList.subList(1, hierarchyList.size()), hierarchyList.get(0).getHierarchyId());
        }
        parent.setChildren(child);
        treeList.add(parent);
        return Action.SUCCESS;

----------------解决方案--------------------------------------------------------
/**
     * @see 递归得到所有目录,并存放事先封装好的树类
     * @param parentId
     *            父节点ID
     * @param medType
     * @return
     * @throws Exception
     */
    private List<TreeNodes> getJsonByList(List<HierarchyDto> list, Integer parentId) throws Exception {
        List<TreeNodes> nodeList = new ArrayList<TreeNodes>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            HierarchyDto dto = (HierarchyDto) iterator.next();
            if (dto.getParentId().equals(parentId)) {
                TreeNodes tp = new TreeNodes();
                tp.setState("close");
                tp.setData(dto.getHierarchyName());
                TreeAttributes tb = new TreeAttributes();
                tb.setId(dto.getHierarchyId());
                tp.setAttributes(tb);
                tb.setParentId(dto.getParentId());
                tb.setLevel(dto.getNum());
                List<TreeNodes> child = new ArrayList<TreeNodes>();
                child = getJsonByList(list, dto.getHierarchyId());
                tp.setChildren(child);
                nodeList.add(tp);
            }
        }
        return nodeList;
    }

----------------解决方案--------------------------------------------------------
然后引用JSTREE

html

html = "<div style='overflow:auto;height:250px'><div id='myTree'></div></div>"

js

var addId = "";
    var path= $('#path').val();
    $.ajaxSetup({cache:false});     //这个是为了树的准确性做的一个缓存区清理的工作  最好加上,否则IE会出现问题
    $("#myTree").tree({
        data:{
            async : false,      // 设为true后会自动请求原来的树。添加到此节点下
            type : "json",
            opts : {
                method:"GET",
                url: path+"/pub/cacheHierarchyList.do"
            }
        },
        ui : {
            theme_name : "classic",
            context : []
        },
        lang : {
                new_node    : "新栏目",
                loading        : "加载中 ..."
        },
        rules:  
        {  
             draggable   : "all"   //这个设置可以使得节点进行拖动操作  
        },  
        
        plugins: {
                           contextmenu:{}
        },
        callback : {
            onselect : function(NODE){
                var id = $(NODE).attr("id");
                var name = $(NODE).find("a:first").text().replace(/(^\s*)|(\s*$)/g, "");
                $("#hierarchyId").val(id);
                $("#hierarchyName").val(name);
                $("#mask").css("display","none");
                $("#tree").css("display","none");
            },
            beforerename: function(NODE,LANG,TREE_OBJ) {
                var tmpId = $(NODE).attr("id");   // 当前ID()
                var parentId = $(NODE).parent().parent().attr("id");  // 父节点ID
                if(parentId=="myTree"){
                    alert("此栏目不允许重命名!");
                    return false;
                }
                return true ;
            },
            onrename : function(NODE,TREE_OBJ,RB){
                var newName=$.trim($(NODE).find("a:first").text());   // 新名称
                var tmpId = $(NODE).attr("id");   // 当前ID()
                var myUrl = path+"/pub/ajaxTreeMethod.do";
                if (tmpId == "") {
                    var params={
                        method:3,
                        hierarchyId:addId,
                        hierarchyName:$.trim(newName)
                    };
                    $.post(myUrl,params,function(data){
                        if(data==1){
                            alert("添加节点"+newName+"成功");
                        }else {
                            alert("添加失败");
                        }
                    });
                    return;
                }
                if(confirm("你确定要重命名吗?")){
                    var params={
                        method:3,
                        hierarchyId:tmpId,
                        hierarchyName:$.trim(newName)
                    };
                    $.post(myUrl,params,function(data){
                        if(data==1){
                            alert("修改名称"+newName+"成功");
                        }else {
                            alert("修改栏目名称失败");
                        }
                        return true;
                    });
                }
            },
            beforedelete : function(NODE,TREE_OBJ){
                if(confirm("你确定删除吗?")){
                    return true;
                }
            },
            ondelete : function(NODE,TREE_OBJ){
                    var tmpId = $(NODE).attr("id");   // 当前ID()
                    var myUrl = path+"/pub/ajaxTreeMethod.do";
                    var params={
                        method:2,
                        hierarchyId:tmpId
                    };
                    $.post(myUrl,params,function(data){
                        if(data==1){
                            alert("删除成功");
                        }else {
                            alert("删除失败");
                        }
                        return true;
                    });
            },
            oncreate:function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
                var newName=$(NODE).find("a:first").text();   // 新名称
                var tmpId = $(NODE).attr("id");   // 当前ID()
                var parentId = $(NODE).parent().parent().attr("id");  // 父节点ID
                var level=$(NODE).parent().parent().attr("level");
                if(parseInt(level)==2){
                    alert("不能再新建子节点");
                    $.tree.focused().refresh(TREE_OBJ.parent(NODE));
                    return false;
                }
                if(parentId=="" || parentId==null){
                    parentId=0;
                    level=1;
                }else{
                    level=parseInt(level)+1;
                }
                var myUrl = path+"/pub/ajaxTreeMethod.do";
                var params={
                    method: 1,
                    hierarchyName:$.trim(newName),
                    parentId:parentId,
                    num:level
                };
              $.ajax({
                  type:"post",
                  dataType:"json",
                  data:params,
                  url:myUrl,
                  success:function(data){
                      addId = data;
                  }
              });
            }
        }
        
    });
    $('#newMenu').live('click',function(){
        if($(current_node).find("a:first").text()==""){
            alert("你还未选中要处理的栏目");
            return false;
        }else{
            $.tree.focused().create();
        }
    });
    $('#renameMenu').live('click',function(){
        if($(current_node).find("a:first").text()==""){
            alert("你还未选中要处理的栏目");
            return false;
        }else{
            $.tree.focused().rename();
        }
    });
    $('#deleteMenu').live('click',function(){
        if($(current_node).find("a:first").text()==""){
            alert("你还未选中要处理的栏目");
            return false;
        }else{
            $.tree.focused().remove();
        }
    });

----------------解决方案--------------------------------------------------------
你参考下。可能不是很合你要求,但是方法都差不多的应该,
主要是JSTREE的用法。网上资料不是很多。数据加载是AJAX的。


----------------解决方案--------------------------------------------------------
写的不错,我也学习了。
----------------解决方案--------------------------------------------------------
回复 6楼 午夜屠猪男
不好意思,结贴分弄错了。
----------------解决方案--------------------------------------------------------
  相关解决方案