Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void delete_id(node_id* tree, int id) {
- if (tree == NULL) {
- return;
- }
- if (id < tree->id)
- //delete node from left subtree
- delete_id(tree->left, id);
- else if (id > tree->id)
- //delete node from right subtree
- delete_id(tree->right, id);
- else {
- if (tree->left == NULL) {
- node_id* temp = tree->right;
- delete_name(tree, temp->name);
- printf("%d, %s has been deleted\n\n",temp->id,temp->name);
- free(tree);
- return;
- }
- else if (tree->right == NULL) {
- node_id* temp = tree->left;
- delete_name(tree, temp->name);
- printf("%d, %s has been deleted\n\n",temp->id,temp->name);
- free(tree);
- return;
- }
- node_id* temp = min_value_node_id(tree->right);
- tree->id = temp->id;
- strcpy(tree->name, temp->name);
- tree->right = delete_id(tree->right, temp->id);
- }
- return;
- }
- void delete_name(node_id* tree, char* name) {
- if (tree == NULL) {
- return;
- }
- if (strcmp(name, tree->name) > 0)
- delete_name(tree->left, name);
- else if (strcmp(name, tree->name) < 0) {
- delete_name(tree->right, name);
- }
- else {
- if (tree->left == NULL) {
- node_id* temp = tree->right;
- delete_id(tree, temp->id);
- printf("%d, %s has been deleted\n\n",temp->id,temp->name);
- free(tree);
- return;
- }
- else if (tree->right == NULL) {
- node_id* temp = tree->left;
- delete_id(tree, temp->id);
- printf("%d, %s has been deleted\n\n",temp->id,temp->name);
- free(tree);
- return;
- }
- node_id* temp = min_value_node_id(tree->right);
- strcpy(tree->name, temp->name);
- tree->id = temp->id;
- delete_name(tree->right, temp->name);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement