| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5992 人关注过本帖
标题:java实现数据结构中二叉树的图形化表示
只看楼主 加入收藏
檐語
Rank: 1
来 自:江西
等 级:新手上路
帖 子:9
专家分:2
注 册:2010-6-25
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:10 
java实现数据结构中二叉树的图形化表示
用户输入一组数据,程序将数据转化成图形化树结构显示,最好提供增、删结点操作。
麻烦各位高手帮帮忙,谢谢了。。。
搜索更多相关主题的帖子: 二叉树 数据结构 java 图形 
2010-06-30 13:32
午夜屠猪男
Rank: 4
等 级:业余侠客
威 望:2
帖 子:194
专家分:259
注 册:2007-1-3
收藏
得分:25 
我只用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() {
    }
   
}

2010-06-30 15:16
午夜屠猪男
Rank: 4
等 级:业余侠客
威 望:2
帖 子:194
专家分:259
注 册:2007-1-3
收藏
得分:0 
其次是数的属性类 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;
    }
   
}

2010-06-30 15:17
午夜屠猪男
Rank: 4
等 级:业余侠客
威 望:2
帖 子:194
专家分:259
注 册:2007-1-3
收藏
得分:0 
        具体操作和方法:
        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;

2010-06-30 15:18
午夜屠猪男
Rank: 4
等 级:业余侠客
威 望:2
帖 子:194
专家分:259
注 册:2007-1-3
收藏
得分:0 
/**
     * @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;
    }

2010-06-30 15:18
午夜屠猪男
Rank: 4
等 级:业余侠客
威 望:2
帖 子:194
专家分:259
注 册:2007-1-3
收藏
得分:0 
然后引用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();
        }
    });

2010-06-30 15:19
午夜屠猪男
Rank: 4
等 级:业余侠客
威 望:2
帖 子:194
专家分:259
注 册:2007-1-3
收藏
得分:0 
你参考下。可能不是很合你要求,但是方法都差不多的应该,
主要是JSTREE的用法。网上资料不是很多。数据加载是AJAX的。


2010-06-30 15:22
aina
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:91
专家分:117
注 册:2009-9-22
收藏
得分:25 
写的不错,我也学习了。
2010-06-30 16:51
檐語
Rank: 1
来 自:江西
等 级:新手上路
帖 子:9
专家分:2
注 册:2010-6-25
收藏
得分:0 
回复 6楼 午夜屠猪男
不好意思,结贴分弄错了。

小小的天,大大的夢想。
2010-07-03 12:09
wtuaimmmm
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-07-05 20:19
快速回复:java实现数据结构中二叉树的图形化表示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044847 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved