Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void deleteNodeRejoin(Node*root) {
- //determine connectors and tails amounts and arrange mapping
- int n_connectors = root->connectors.size();
- int n_tails = root->tails.size();
- vector<Node*> tails = root->tails;
- vector<Node*> connectors = root->connectors;
- Node* root_copy = root;
- //establish connectors and tails
- //loop through tails and connect to connectors
- Node* tail_node;
- Node* connector_node;
- for (int t = 0; t < n_tails; t++) { //connect tails to connector
- for (int c = 0; c < n_connectors; c++)
- insertConnector(root->tails[t],root->connectors[c]);
- }
- delete root;
- //find pointers that point to node and remove and null them, to avoid dangling pointers
- for (int t = 0; t < n_tails; t++) { //loop through tails to find pointers to root
- tail_node = tails[t];
- for (int n = 0; n < tail_node->connectors.size(); n++) { //loop through tail_nodes connectors
- if (tail_node->connectors[n] == root_copy) { //if the tail node connects to the root
- tail_node->connectors.erase(tail_node->connectors.begin() + n);
- n--;
- }
- }
- }
- for (int c = 0; c < n_connectors; n++) { //loop through connectors to find pointers to root
- connector_node = connectors[c];
- for (int n = 0; n < connector_node->tails.size(); n++) { //loop through connector nodes
- if (connector_node->tails[n] == root_copy) { //if the connector node connects to the root
- connector_node->tails.erase(connector_node->tails.begin() + n);
- n--;
- }
- }
- }
- delete root_copy;
- for (int i = 0;i<tails.size();i++)
- delete tails[i];
- for (int i = 0;i<connectors.size();i++)
- delete connectors[i];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement