javascriptloopsdynamic-datajavascript-objectstree-structure

Dynamic Javascript Tree Structure


I would like to build the hierarchy dynamically with each node created as a layer/level in the hierarchy having its own array of nodes. THIS SHOULD FORM A TREE STRUCTURE.There should be a root node, and an undefined number of nodes and levels to make up the hierarchy size. Nothing should be fixed besides the root node. I do not need to read or search the hierarchy, I need to construct it. The array should start {"name" : "A", "children" : []} and every new node as levels would be created {"name" : "A", "children" : [HERE-{"name" : "A", "children" : []}]}. In the child array, going deeper and deeper. Basically the array should have no values before the call, except maybe the root node. After the function call, the array should comprise of the required nodes of a number that may vary with every call depending on the results of a database query. Every child array will contain one or more node values. There should be a minimum of 2 node levels, including the root. It should initially be a Blank canvas, that is no predefined array values.


Solution

  •     function Tree(name,child){
            this.name = name;
            this.children = child || [];
            this.addNode = function (parent){
                this.children = parent;
            }
            this.addChild = function (parentName){
                this.children.push(new Tree(parentName));
            }
        }
    
        var tree = new Tree("A"); // create a tree (or a portion of a tree) with root "A" and empty children
        tree.addChild("B1");   // A -> B1
        tree.addChild("B2");   // A -> B2
        var subTree1 = new Tree("C1"); // create a sub tree
        subTree1.addChild("D1");   // C1 -> D1
        subTree1.addChild("D2");   // C1 -> D2
        tree.children[0].addNode(subTree1);   // add this sub tree under A->B1
        // Tree now is:  A--> B1
        //                      C1
        //                        D1
        //                        D2
        //                    B2
        tree.children[1].addChild("C2");
        // Tree now is:  A--> B1
        //                      C1
        //                        D1
        //                        D2
        //                    B2
        //                      C2
        //tree.children[0].addChild("C4");
        // Tree now is:  A--> B1
        //                      C1
        //                        D1
        //                        D2
        //                      C4
        //                    B2
        //                      C2    
        console.log(JSON.stringify(tree));
    

    Output

    {
      "name": "A",
      "children": [
        {
          "name": "B1",
          "children": {
            "name": "C1",
            "children": [
              {
                "name": "D1",
                "children": []
              },
              {
                "name": "D2",
                "children": []
              }
            ]
          }
        },
        {
          "name": "B2",
          "children": [
            {
              "name": "C2",
              "children": []
            }
          ]
        }
      ]
    }