我需要使用JavaScript创建一个自定义树数据结构

我需要使用JavaScript创建一个自定义树数据结构,javascript,data-structures,tree,Javascript,Data Structures,Tree,我在javascript中查找了树状结构的基本格式: function Tree(parent, child, data) { this.parent = parent; this.children = child || []; this.data = data; this.addNode ... this.addChild ... } 我的问题是用这个做一棵“长”的树。我使用的数据是一条几乎是一条直道的小道上的街道列表,但是小道上有几个小裂口,数据看起

我在javascript中查找了树状结构的基本格式:

function Tree(parent, child, data) {
    this.parent = parent;
    this.children = child || [];
    this.data = data;
    this.addNode ...
    this.addChild ...
}
我的问题是用这个做一棵“长”的树。我使用的数据是一条几乎是一条直道的小道上的街道列表,但是小道上有几个小裂口,数据看起来像:

A -> 
B -> 
C -> 
D -> E,F   
E -> 
G -> 
H    
F -> I  
I -> J  
J -> K,L   
K ->
M -> 
N
L -> O
O -> P
Tree.prototype.traverse = function( operation ){
    // call the operation function and pass in the current node
    operation( this )

    // call traverse on every child of this node
    for( var i=0; i<this.children.length; i++ )
        this.children[i].traverse( operation )
}

// an example operation that finds all the leaf nodes
var leaves = []
myTree.traverse( function( node ){
    if( !node.children.length )
        leaves.push(node)
}
我希望避免出现如下代码:

tree.children[0].children[0].children[0].addNode("E");
tree.children[0].children[0].children[0].push("F");
所以我的一个问题是如何穿越这棵树,简单地说

node = tree;
while(node.children != null)
    node = node.children[0];
如果你能帮我我会很感激的谢谢


mathacka

如果您想在树中的每个节点上进行一些计算,那么您可以向树节点添加一个
遍历
函数,类似于:

A -> 
B -> 
C -> 
D -> E,F   
E -> 
G -> 
H    
F -> I  
I -> J  
J -> K,L   
K ->
M -> 
N
L -> O
O -> P
Tree.prototype.traverse = function( operation ){
    // call the operation function and pass in the current node
    operation( this )

    // call traverse on every child of this node
    for( var i=0; i<this.children.length; i++ )
        this.children[i].traverse( operation )
}

// an example operation that finds all the leaf nodes
var leaves = []
myTree.traverse( function( node ){
    if( !node.children.length )
        leaves.push(node)
}
Tree.prototype.traverse=函数(操作){
//调用操作函数并传入当前节点
行动(本)
//对该节点的每个子节点调用遍历

对于(var i=0;i而言,此结构最易于管理的方法是使用链表

function Node(parentNode)
{
    this.Parent=parentNode;
    this.FirstChild=null;
    this.LastChild=null;
    this.PreviousSibling=null;
    this.NextSibling=null;
}
Node.prototype.AddChild=function(child)
{
    child.Parent = this;
    child.PreviousSibling = this.LastChild;
    if (this.LastChild != null)
        this.LastChild.NextSibling = child;
    this.LastChild = child;
    if (this.FirstChild == null)
        this.FirstChild = child;
}
要循环遍历子对象,请执行以下操作:

function GetChildren(node)
{
    var result=new Array();
    var child=node.FirstChild;
    while(child)
    {
        result.push(child);
        child=child.NextSibling;
    }
    return result;
}

(编辑)“节点”-对象只是一个示例,它应该添加有意义的属性。使用它作为树中所有对象的基础,它可能具有任何深度,而不会使其变得更复杂。您可以添加更多函数,如GetChildByName、RemoveChild等。

了解如何从SQL查询创建树结构的这种方法:

var树=函数(){
Tree.obj={};
回归树;
};
//父对象将是对象
Tree.AddChild=函数(父,子){
如果(父项===null){
if(Tree.obj.hasOwnProperty(child)){
返回树obj[child];
}否则{
Tree.obj[child]={};
返回树obj[child];
}
}否则{
父[子]={};
返回父[子];
}
};
//使用
//插入-
var t=Tree();
var twoord=t.AddChild(null,“2门”);
var fdoor=t.AddChild(空,“4门”);
t、 AddChild(fdoor,“手册”);
t、 AddChild(双门,“自动”);
var man=t.AddChild(双门,手动);
t、 AddChild(男子,“加长驾驶室”);

console.log(t.obj)
你试过你的代码吗?我知道我可以在一个链表中放置两个孩子,你对如何遍历多个孩子列表有什么建议吗?上面的代码可以让你遍历无限多的孩子。每个孩子可以有无限多的孙子,等等。试试吧。代码模式称为链表。谢谢,我也发现了这一点,你能补充一下链接方法是如何解决问题的吗?