Skip to content Skip to sidebar Skip to footer

I Need To Create A Custom Tree Data-structure Using Javascript

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 = da

Solution 1:

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:

functionGetChildren(node)
{
    var result=newArray();
    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.

Solution 2:

varTree = function () {
    Tree.obj = {};
    returnTree;
};

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

    if (parent === null) {
        if (Tree.obj.hasOwnProperty(child)) {
            returnTree.obj[child];
        } else {
            Tree.obj[child] = {};
            returnTree.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);

Solution 3:

Have a look at this approach on how to create tree structures from SQL queries:

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

Solution 4:

If you want to do some calculation at every node in the tree then you could add a traverse function to your tree nodes, something like:

Tree.prototype.traverse = function( operation ){
    // call the operation function and pass in the current nodeoperation( this )

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

// an example operation that finds all the leaf nodesvar leaves = []
myTree.traverse( function( node ){
    if( !node.children.length )
        leaves.push(node)
}

Alternatively if you're doing something more specific such as manipulating the tree or finding a particular node then you may want to look at the Visitor Design Pattern.

Post a Comment for "I Need To Create A Custom Tree Data-structure Using Javascript"