Removing an item in a binary search tree (BST)

I'm having difficulty removing an item from BST. This code is based on an algorithm from Cormen's book. Here are two functions needed to remove an item:

el *TreeSuccessor(el *x) { el *y; if (x->right != NULL){ return TreeMinimum(x->right); } y= x->p; while (y != NULL && x == y->right){ x = y; y = y->p; } return y; } void TreeDelete (el* &root, el *z, el* &y) { el *x; if (z->left == NULL || z->right == NULL){ y=z; } else{ y=TreeSuccessor(z); } if(y->left != NULL){ x = y->left; } else{ x = y->right; } if (x!=NULL){ x->p = y->p; } if (y->p == NULL){ root=x; } else if (y == y->p->left){ y->p->left = x; } else{ y->p->right = x; } if (y!=z){ z->indeks = y->indeks; strcpy(z->name,y->name); strcpy(z->surname,y->surname); } } 

And here is the code responsible for calling the function in int main() :

  for (int i=0; i<n; i++) { File>>keyToSearch; searchedLeave=TreeSearch(root,keyToSearch); TreeDelete(root,searchedLeave,leaveToDelete); delete leaveToDelete; } 

Here is the complete program http://pastebin.com/FgvWPzm9

Program crashes while uninstalling.

+4
source share

All Articles