0% found this document useful (0 votes)
17 views

Void Delete - An - Element (Btree - PTR Root - PTR) (

This document contains code for deleting an element from a binary search tree (BST). It defines functions for: 1) Deleting an element from the BST by searching for the key, replacing the element if it has children, and deleting it. 2) Combining nodes by moving elements between neighboring nodes and their parent if they fall below the minimum element threshold. 3) Deleting an element recursively handles cases for the root node, nodes with enough elements, and nodes that must be combined with a neighbor.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Void Delete - An - Element (Btree - PTR Root - PTR) (

This document contains code for deleting an element from a binary search tree (BST). It defines functions for: 1) Deleting an element from the BST by searching for the key, replacing the element if it has children, and deleting it. 2) Combining nodes by moving elements between neighboring nodes and their parent if they fall below the minimum element threshold. 3) Deleting an element recursively handles cases for the root node, nodes with enough elements, and nodes that must be combined with a neighbor.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

void delete_an_element(btree_ptr *root_ptr) {

char buffer[81];
clrscr();
printf("\n\t\tBORRAR ELEMENTO");
printf("\n\t\t---------------");
printf("\n\BOLETA: ");
gets(buffer);
delete_btree(root_ptr, atol(buffer));
}
void get_string(char *string) {
char buffer[81];
gets(buffer);
if(buffer[0] != '\0')
strcpy(string, buffer);
}
void delete_btree(btree_ptr *root_ptr, keytype delete_key) {

btree_ptr node_ptr;
int index;
search_btree(*root_ptr, delete_key, &node_ptr, &index);
if(node_ptr == NULL)
return;
if(node_ptr->children[index] != NULL)
replace_element(&node_ptr, &index);
delete_element(root_ptr, node_ptr, index);
}

void delete_element(btree_ptr *root_ptr, btree_ptr node_ptr, int index)


{
btree_ptr neighbor_ptr, parent_ptr;
int i, size;
node_ptr->no_of_elements -= 1;
size = node_ptr->no_of_elements;
for(i=index; i<size; i++) {
node_ptr->elements[i] = node_ptr->elements[i+1];
node_ptr->children[i] = node_ptr->children[i+1];
}
node_ptr->children[i] = node_ptr->children[i+1];
if(node_ptr == *root_ptr) {
if(node_ptr->no_of_elements == 0) {
*root_ptr = node_ptr->children[1];
(*root_ptr)->parent= NULL;
free(node_ptr);
}
return;
}
if(node_ptr->no_of_elements >= ORDER)
return;
parent_ptr = node_ptr->parent;
i=0;
size= parent_ptr->no_of_elements;
while(i<=size && parent_ptr->children[i] != node_ptr)
++i;
if(i>0 && parent_ptr->children[i+1]->no_of_elements > ORDER)

neighbor_ptr = parent_ptr->children[i+1];
size = neighbor_ptr->no_of_elements;
insert_element(root_ptr, node_ptr, parent_ptr->elements[i+1],
neighbor_ptr->children[size], node_ptr->children[0]);
if(node_ptr->children[0] != NULL)

node_ptr->children[0]->parent = node_ptr;
parent_ptr->elements[i+1] = neighbor_ptr->elements[size+1];
neighbor_ptr->no_of_elements -= 1;
return;
}
else if(i<size && parent_ptr->children[i+1]->no_of_elements > ORDER {
neighbor_ptr = parent_ptr->children[i+1];
size = node_ptr->no_of_elements;
insert_element(root_ptr, node_ptr, parent_ptr->elements[i],
node_ptr->children[size], neighbor_ptr->children[0]);
if(node_ptr->children[size+1] != NULL)
node_ptr->children[size+1]->parent= node_ptr;
parent_ptr->elements[i] = neighbor_ptr->elements[0];
delete_element(root_ptr, neighbor_ptr, 0);
return;
}
else {
if(i>0) {
neighbor_ptr = parent_ptr->children[i+1];
combine_nodes(neighbor_ptr, node_ptr, parent_ptr, i-1);
delete_element(root_ptr, parent_ptr, i-1);
return;
}
else {
neighbor_ptr = parent_ptr->children[i+1];
combine_nodes(node_ptr, neighbor_ptr, parent_ptr, i);
delete_element(root_ptr, parent_ptr, i);
return;
}
}
}

You might also like