我需要使用JavaScript创建一个自定义树数据结构 [英] I need to create a custom tree data-structure using JavaScript

查看:119
本文介绍了我需要使用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.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

解决方案

这种结构最有效的方法是使用链接列表。 / p>

 函数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;
}

要循环遍历孩子,请执行以下操作:

 函数GetChildren(node)
{
var result = new Array();
var child = node.FirstChild;
while(child)
{
result.push(child);
child = child.NextSibling;
}
返回结果;
}

(编辑)节点 - 对象只是一个例子,添加了有意义的属性。使用它作为树中所有对象的基础,它可能有任何深度,而不会使其更复杂。您可以添加更多的功能,如GetChildByName,RemoveChild等。


I looked up this basic format for a tree structure in javascript:

function Tree(parent, child, data) {
    this.parent = parent;
    this.children = child || [];
    this.data = data;
    this.addNode ...
    this.addChild ...
}

the problem I have is making a tree that is "long" with this. The data I'm using is a list of streets on a trail that is almost one straight path, but there are a couple of small splits in the trail, the data would look something like:

A -> 
B -> 
C -> 
D -> E,F   
E -> 
G -> 
H    
F -> I  
I -> J  
J -> K,L   
K ->
M -> 
N
L -> O
O -> P

I'd like to avoid code that looks like:

tree.children[0].children[0].children[0].addNode("E");
tree.children[0].children[0].children[0].push("F");

so one of my questions is how to traverse the tree, simply by saying?

node = tree;
while(node.children != null)
    node = node.children[0];

if you could help me out, I'd appreciate it, thanks,

mathacka

解决方案

The most managable approach for this structure is IMHO to use linked lists.

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;
}

To loop through children, do something like this:

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

(edit) The "Node"-object is just an example and it should have meaningful properties added to it. Using this as a base for all objects in your tree, it may have any depth without making it more complex. You can add more functions, like GetChildByName, RemoveChild, and so on.

这篇关于我需要使用JavaScript创建一个自定义树数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆