struct node * BSTnodeRemove (struct node * current, EleType d) { if(strcmp(current->value, d)==0){ // if current->value == d if(current->right == 0){ return current->left; } else{ current->value = leftMostChild(current->right); current->right = removeLeftmostChild(current->right); } } else if(strcmp(d,current->value)<0){ // if d<currentval current->left = removeLeftmostChild(current->left); } else{ current->right = removeLeftmostChild(current->right); } return current; }
bool deleteElement(BinaryTree *tree, int value) { BinaryTreeElement *temp = tree->root; bool successfulySearch = false; int direction = 0; if (temp == NULL) return false; else { while (temp != NULL) { if (value == temp->value) { successfulySearch = true; break; } else { if (value >= temp->value) { temp = temp->rigth; direction = 1; } else { temp = temp->left; direction = -1; } } } } if (successfulySearch) { if (temp->left == NULL && temp->rigth == NULL) { BinaryTreeElement *prov = temp; if (direction == 0) tree->root = NULL; else { if (direction == 1) temp->parent->rigth = NULL; else temp->parent->left = NULL; } delete prov; } else { if (temp->left == NULL) { BinaryTreeElement *prov = temp->rigth; temp->value = prov->value; temp->left = prov->left; temp->rigth = prov->rigth; if (prov->left != NULL) prov->left->parent = temp; if (prov->rigth != NULL) prov->rigth->parent = temp; delete prov; } else { if (temp->rigth == NULL) { BinaryTreeElement *prov = temp->left; temp->value = prov->value; temp->left = prov->left; temp->rigth = prov->rigth; if (prov->left != NULL) prov->left->parent = temp; if (prov->rigth != NULL) prov->rigth->parent = temp; delete prov; } else { BinaryTreeElement *prov = temp->rigth; prov = leftMostChild(tree, prov); temp->value = prov->value; if (prov->parent->value <= prov->value) prov->parent->rigth = NULL; else prov->parent->left = NULL; delete prov; } } } return true; } return false; }