Binary tree

I have a tree structure with a node with a parent identifier (unlimited child nodes). For display purposes, I need this tree structure as a binary tree. As I do this, at each level, nodes are grouped into one node based on the condition. When you select a node, its children are displayed. Example:

N-ary tree

Green is when the condition trueand red isfalse

BTree

B, C are grouped to the left node, and D, E are to the right, based on their conditions.

: KnockoutJS , , node , node (s) node (s). , . / ?

var tree = [
    { groupNodeId: "A", childNodes: [
        { nodeId: "A", childGroupNodes: [
            { groupNodeId: "B", condition: true, childNodes: [
                { nodeId: "B", childGroupNodes: []},
                { nodeId: "C", childGroupNodes: []}
            ]},
            { groupNodeId: "D", condition: false, childNodes: [
                { nodeId: "D", childGroupNodes: []},
                { nodeId: "E", childGroupNodes: []}
            ]}
        ]}
    ]}
];
+5
1

, , , :

  • "groupNodes", , groupNodes .
  • , groupNodes.
  • nodeId, , true false, childNodes.
  • groupNodes, .

.

function binize(tree) {
  var left,right,t,u,
    stack=[tree];
  while(t=stack.pop()) {
    left=[];
    right=[];
    while(u=t.childNodes.pop()) {
      (u.condition?left:right).push(u);
      stack.push(u);
    }
    left.length&&t.childNodes.push({
      groupNodeId:left[0].nodeId,
      condition:true,
      childNodes:left
    });
    right.length&&t.childNodes.push({
      groupNodeId:right[0].nodeId,
      condition:false,
      childNodes:right
    });
  }
}

( , - , , ).

var tree={nodeId:'A',childNodes:[
  {nodeId:'B',condition:true,childNodes:[]},
  {nodeId:'C',condition:true,childNodes:[]},
  {nodeId:'D',condition:false,childNodes:[
    {nodeId:'F',condition:false,childNodes:[]},
    {nodeIf:'G',condition:false,childNodes:[]}
  ]},
  {nodeId:'E',condition:false,childNodes:[]}
]};

, . , , (, , ), .

, KnockoutJS , . , ; , .

+3

All Articles