4

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

4

4 回答 4

7

这种结构最易于管理的方法是恕我直言,使用链表。

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 等。

于 2013-07-19T16:36:54.603 回答
2

var Tree = function () {
    Tree.obj = {};
    return Tree;
};

// Parent Will be object
Tree.AddChild = function (parent, child) {

    if (parent === null) {
        if (Tree.obj.hasOwnProperty(child)) {
            return Tree.obj[child];
        } else {
            Tree.obj[child] = {};
            return Tree.obj[child];
        }
    } else {
        parent[child] = {};
        return parent[child];
    }
};


// Uses

// Inserting - 
var t = Tree();
var twoDoor = t.AddChild(null, "2 Door");
var fdoor = t.AddChild(null, "4 Door");
t.AddChild(fdoor, "manual");
t.AddChild(twoDoor, "automatic");
var man = t.AddChild(twoDoor, "manual");
t.AddChild(man, "Extended Cab");
console.log(t.obj);

于 2015-07-13T07:38:27.667 回答
1

看看这个关于如何从 SQL 查询创建树结构的方法:

http://blog.tcs.de/creating-trees-from-sql-queries-in-javascript/

于 2013-10-31T18:10:00.960 回答
0

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

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

或者,如果您正在做一些更具体的事情,例如操作树或查找特定节点,那么您可能需要查看Visitor Design Pattern

于 2013-07-19T16:34:43.097 回答