Example: binary tree deletion
void deleteNode(Node *root, int data) { if(root == NULL) { cout << "Tree is empty\n"; return; } queue<Node*> q; q.push(root); while(!q.empty()) { Node *temp = q.front(); q.pop(); if(temp->data == data) { Node *current = root; Node *prev; while(current->right != NULL) { prev = current; current = current->right; } temp->data = current->data; prev->right = NULL; free(current); cout << "Deleted\n"; return; } if(temp->left != NULL) q.push(temp->left); if(temp->right != NULL) q.push(temp->right); } cout << "Node not found for deletion\n"; }
Comments
Post a Comment