已知各个结点的父结点,和子结点
如何建立二叉树?如下JSON数据,c1是左结点,c2是右结点
var users={data:[{account:'mc172',name:'乔峰',c1:'NaN',c2:'NaN',father:'mc170'},{account:'mc171',name:'马国忠',c1:'NaN',c2:'NaN',father:'mc170'},{account:'mc170',name:'1111',c1:'mc171',c2:'mc172',father:'NaN'},{account:'mc169',name:'1111',c1:'NaN',c2:'NaN',father:'NaN'},{account:'mc168',name:'1111',c1:'NaN',c2:'NaN',father:'NaN'}]};

解决方案 »

  1.   

    以account 为key进行一遍解析就可以了,
    还有就是,这结构是没问题的,可以认为是三叉链表。
    退一步说,如果你数据有问题,构建出来的最多就是一个图。
      

  2.   

    我将你的users数据小改了一下,去掉了我认为没用的c1,c2,把名字改成有描述意思的,为了看起来盟明显,特意设置father组成了一颗树。HTML结构用div表示,为了看出层次关系,设置了DIV margin
    div{border:1px solid gray;margin:10px;margin-left:20px;}var users = { data: [{ account: 'mc172', name: 'Level 1 A', father: 'mc170' }, { account: 'mc171', name: 'Level 1 B', father: 'mc170' }, { account: 'mc170', name: 'Root', father: 'NaN' }, { account: 'mc169', name: 'Level 2 B', father: 'mc171' }, { account: 'mc168', name: 'Level 2 A', father: 'mc172' }, { account: 'mc167', name: 'Level 2 B', father: 'mc172'}] };    $.each(users.data, function() {
        $('body').append("<div id='" + $(this)[0].account + "'>" + $(this)[0].name + "</div>");
    });$.each(users.data, function() {
    if ($(this)[0].father != "NaN") {
        $("#" + $(this)[0].account).appendTo($("#" + $(this)[0].father));
        }
        
    });
      

  3.   

    to wzhiyuan
    有完整点的么?
      

  4.   

    还不完整吗,你弄一个网页,把css和js都弄上去,引入jquery库,一运行网页,结果不就出来了
      

  5.   


    <!doctype html >
    <html>
    <head>
    <title>二叉树</title>
    <meta content="text/html; charset=gb2312" http-equiv="Content-Type"/>
    <script type="text/javascript">
    var users={data:[{account:'mc172',name:'乔峰',c1:'NaN',c2:'NaN',father:'mc170'},{account:'mc171',name:'马国忠',c1:'NaN',c2:'NaN',father:'mc170'},{account:'mc170',name:'1111',c1:'mc171',c2:'mc172',father:'NaN'},{account:'mc169',name:'1111',c1:'NaN',c2:'NaN',father:'NaN'},{account:'mc168',name:'1111',c1:'NaN',c2:'NaN',father:'NaN'}]};

    var bnode = function(account, name, left, right){
    this.account = account || "";
    this.name = name || "";
    this.left = left != 'NaN' ? left : null;
    this.right = right != 'NaN' ? right : null;
    };
    bnode.prototype = {
    toString : function(){
    var html = ["<ul><li>"];
    html.push(this.name);
    if(this.left) html.push(this.left.toString());
    if(this.right) html.push(this.right.toString());
    html.push("</li></ul>"); return html.join("");
    }
    }; function findFather(root, account){
    for(var i = 0; i < root.length; i++){
    if(root[i].left === account){
    return {parent:root[i], sub: 0};
    }
    if(root[i].right === account){
    return {parent:root[i], sub: 1};
    }
    }
    return null;
    }
    function createTree(users){
    var root = [], u, t = null, i = 0, p, data = users.data;
    while(data.length > 0){
    u = data[i];
    if(u.father != 'NaN'){
    p = findFather(root, u.account);
    if(p){
    t = new bnode(u.account, u.name, u.c1, u.c2);
    p.parent[p.sub?"right":"left"] = t;
    data.splice(i, 1);
    }else{
    i++;
    }
    }else if(u.father == 'NaN'){
    t = new bnode(u.account, u.name, u.c1, u.c2);
    data.splice(i, 1);
    root.push(t);
    }else{
    i++;
    }
    if(i >= data.length) i = 0;
    }
    return root;
    } window.onload = function(){
    var tree = document.getElementById("tree");
    var root = createTree(users), html = [];
    for(var i = 0; i < root.length; i++){
    html.push( root[i].toString() );
    }
    tree.innerHTML = html.join("");
    };
    </script>
    </head>
    <body>
    <div id="tree"></div>
    </body>
    </html>