Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. void deleteNodeRejoin(Node*root) {
  2.             //determine connectors and tails amounts and arrange mapping
  3.             int n_connectors = root->connectors.size();
  4.             int n_tails = root->tails.size();
  5.  
  6.             vector<Node*> tails = root->tails;
  7.             vector<Node*> connectors = root->connectors;
  8.             Node* root_copy = root;
  9.  
  10.             //establish connectors and tails
  11.             //loop through tails and connect to connectors
  12.             Node* tail_node;
  13.             Node* connector_node;
  14.  
  15.             for (int t = 0; t < n_tails; t++) {     //connect tails to connector
  16.                 for (int c = 0; c < n_connectors; c++)
  17.                     insertConnector(root->tails[t],root->connectors[c]);
  18.             }
  19.  
  20.             delete root;
  21.  
  22.             //find pointers that point to node and remove and null them, to avoid dangling pointers
  23.             for (int t = 0; t < n_tails; t++) { //loop through tails to find pointers to root
  24.                 tail_node = tails[t];
  25.                 for (int n = 0; n < tail_node->connectors.size(); n++) { //loop through tail_nodes connectors
  26.                     if (tail_node->connectors[n] == root_copy) { //if the tail node connects to the root
  27.                         tail_node->connectors.erase(tail_node->connectors.begin() + n);
  28.                         n--;
  29.                     }
  30.                 }
  31.             }
  32.  
  33.             for (int c = 0; c < n_connectors; n++) { //loop through connectors to find pointers to root
  34.                 connector_node = connectors[c];
  35.                 for (int n = 0; n < connector_node->tails.size(); n++) { //loop through connector nodes
  36.                     if (connector_node->tails[n] == root_copy) {        //if the connector node connects to the root
  37.                         connector_node->tails.erase(connector_node->tails.begin() + n);
  38.                         n--;
  39.                     }
  40.                 }
  41.             }
  42.             delete root_copy;
  43.  
  44.             for (int i = 0;i<tails.size();i++)
  45.                 delete tails[i];
  46.             for (int i = 0;i<connectors.size();i++)
  47.                 delete connectors[i];
  48.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement