The maximum number of data in a tree structure to find neighboring nodes have the same attribute method

This article describes a data structure in a tree to find the longest link type attribute are neighbor nodes of a node number, if there is any other intermediate nodes inserted therein, that is over this length must It is adjacent. One of the most simple example is in a tree structure, there are two nodes to be connected, but if the nodes are connected to a type, then for no longer than five, more than five then the node can not be connected.

As shown below:

 

 

  When making connections between nodes, such as through adjacent nodes are a type attribute of the connection can not be more than five. FIG node has assumed the type attribute, and all of a, then the connection node 2.1 and 3.1 can be connected to the node, but can not be 2.3 and 3.3 for connection, because the connection node longest to 6.1, longer than five. 5.1 behind the other can not be connected to a node of the type. To determine how that connection during this length is more than 5, which is the content of this article to introduce an algorithm, but there is a prerequisite.

  First, there must be information on these nodes: ID of this node, ID of its parent node, its child nodes ID; each node has a type attribute, is what marks this node node; there are ways to obtain information about all the nodes, facilitate inquiries by ID.

Some would say, then how to obtain information about all the nodes it? In fact, as long as when you create, the information is stored in a node object to to ID as a key, its information as a key value;

Object represented as follows:

Nodes = const { 
  ... // other node information 
  ID21: { 
    the nodeId: ID21, 
    type: 'A' , 
    parentNodeIds: { 
      idll of: to true , 
      ID12: to true , 
    }, 
    childNodeIds: { 
      ID31: to true , 
      ID32: to true , 
    } 
  } 
  ... // other node information 
}

nodes where each node holds some of the information at the time of creation, when there are changes (connection, disconnect the connection, delete) operations, and modify data, you can easily get all the nodes connected by nodes information, type information, and other information stored in them. After re-connecting node when you can get directly to the type of the node type.

  Only to find the type properties are adjacent at the right time to the total number of nodes with a certain value of this approach, of course, to find ideas can also be used in other places, you see the hole in the brain.

  Under the first thought, when connected, we can first determine if two connected nodes is not all type is a, is a case only of all, have the necessary follow-up calculations; up after the inquiry into the the maximum number of layers adjacent links connected to the node type is composed of a few; there is downwardly adjacent the query type is connected to a node link composed of the longest a few layers; after by these two values ​​to determine whether there is a length of more than 5. For that is an idea to write the code.

  Up and down the query type type used idea is the same, are the first to find the node of all child nodes of the type as a node ID record it, and then all of these child nodes child nodes are taken out to for the next operation. Here to use recursion, recursive end condition is the type of node is not a type of sub is a, then the recursion is over.

Directly on the pseudo-code:

function statisticalNodeNumber(node1, node2) {
  const type1 = node1.type;
  const type2 = node2.type;
  if (type1 === 'a' && type2 === 'a') {
    const beforeNum = statisticalBeforeNodeNumber([node1.nodeId], 0);
    const nextNum = statisticalNextNodeNumber([node2.nodeId], 0);
    return beforeNum + nextNum > 5;
  }
}

function statisticalNextNodeNumber(nodeIds, num) {
  let total = num;
  let nodeTypeIsA = [];
  let childNodeIds =[]; 
  Const Nodes = Nodes; // get all node information or object methods 
  nodeIds.forEach ((Val) => { 
    const type = Nodes [Val] .Type;
     IF (=== type 'A' ) {
       / / If is a type, then stored in the array nodeTypeIsA, after the determination of whether the length of the array nodeTypeIsA 0 
      // if not zero this indicates that there is a hierarchy of a node type, the total number may be increased by one, and then remove all type as a child node 
      // performed at a computing node, until the hierarchy is not a type of node has been 
      nodeTypeIsA.push (Val); 
      const n- ? = nodes [Val] .childNodeIds Object.keys (nodes [Val ] .childNodeIds): []; 
      childNodeIds = [... childNodeIds, ... n-]; 
    } 
  }); 
  IF(nodeTypeIsA.length === 0 ) {
     return Total; 
  }
   ++ Total;
   return statisticalNextNodeNumber (childNodeIds, Total); 
} 

function statisticalBeforeNodeNumber (nodeIDs, NUM) { 
  the let Total = NUM; 
  the let nodeTypeIsA = []; 
  the let beforeNodeIds = [ ]; 
  const nodes = nodes; // get all node information or object methods 
  nodeIds.forEach ((Val) => { 
    const type = nodes [Val] .Type;
     IF (=== type 'a' ) { 
      nodeTypeIsA. push (val); 
      const n-= nodes[val].parentNodeIds ? Object.keys(nodes[val].parentNodeIds) : [];
      beforeNodeIds = [...beforeNodeIds, ...n];
    }
  });
  if (nodeTypeIsA.length === 0) {
    return total;
  }
  ++total;
  return statisticalNextNodeNumber(beforeNodeIds, total);
}

The above two functions most important idea is to follow the hierarchy to find, only the hierarchy there is a type, then the number of plus 1, and subsequent calculations only on the type of a node as a child node of. So you can find out when the two connected nodes adjacent node type is a maximum number of how many.

 

These are the sharing of content, we want to help.

Guess you like

Origin www.cnblogs.com/wertantan/p/11443041.html