Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tree *del(struct Tree *tree, int key)
- {
- Tree* parrent=NULL;
- // jesli pusty
- if (tree == NULL)
- {
- cout<<"nie znaleziono"<<endl;
- return tree;
- }
- //Jeśli klucz do usuniecia jest mniejszy od obecnego->lewa gałąż
- if (key < tree->key){
- parrent=tree;
- tree->left = del(tree->left, key);}
- //Jeśli klucz do usuniecia jest wiekszy od obecnego->prawa gałąż
- else if (key > tree->key){
- parrent=tree;
- tree->right = del(tree->right, key);}
- //inna opcja jestesmy w wezle do usniecia
- else
- {
- // z jednym dzickiem lub bez
- if (tree->left == NULL)
- {
- Tree *temp = tree->right;
- cout<<"Usunieto "<<endl;
- free(tree);
- return temp;
- }
- else if (tree->right == NULL)
- {
- Tree *temp = tree->left;
- cout<<"Usunieto "<<endl;
- free(tree);
- return temp;
- }
- //Z dwoma dziećmi: bierzemy najmniejszy wezel z prawego poddrezwa
- Tree* help=NULL;
- parrent=tree;
- tree=tree->right;
- while (tree->left != NULL){
- help=tree;
- tree = tree->left;
- }
- if(tree->right)help->left=tree->right;
- tree->right=parrent->right;
- tree->left=parrent->left;
- help=parrent;
- parrent=tree;
- delete help;
- }
- return parrent;
- }
Advertisement
Add Comment
Please, Sign In to add comment