Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void findPredecessor(Node** predecessor, Node** parent)
- {
- //profiler.countOperation(nrOfOperations[0], sizeCount, 1);
- //if (*predecessor == NULL)
- //return;
- (*parent)->size--;
- while ((*predecessor)->right != NULL)
- {
- profiler.countOperation(nrOfOperations[0], sizeCount, 3);
- *parent = *predecessor;
- (*parent)->size--;
- *predecessor = (*predecessor)->right;
- }
- profiler.countOperation(nrOfOperations[0], sizeCount, 1);
- }
- Node* osDelete(Node* root, int rank)
- {
- if (root == NULL) //rank too big
- return root;
- int rankOfRoot;
- //profiler.countOperation(nrOfOperations[1], sizeCount, 1);
- if (root->left == NULL)
- rankOfRoot = 1;
- else
- rankOfRoot = root->left->size + 1;
- if (rank < rankOfRoot)
- {
- root->left = osDelete(root->left, rank);
- root->size--;
- return root;
- }
- else
- if (rank > rankOfRoot)
- {
- root->right = osDelete(root->right, rank - rankOfRoot);
- root->size--;
- return root;
- }
- else // node with needed rank found
- {
- if(root->left == NULL)
- if (root->right == NULL) //leaf
- {
- free(root);
- return NULL;
- }
- else //single child to the right
- {
- root->size--;
- Node* aux = root->right;
- free(root);
- return aux;
- }
- else // we have child on left
- if (root->right == NULL) // single child to the left
- {
- root->size--;
- Node* aux = root->left;
- free(root);
- return aux;
- }
- else // 2 children.
- {
- Node* predecessor = root->left;
- Node* parent = root;
- //in findPredecessor we update the size field
- findPredecessor(&predecessor, &parent);
- //printf("predecessor is %d, parent is %d\n", predecessor->key, parent->key);
- root->key = predecessor->key;
- if (parent == root)
- {
- root->key = predecessor->key;
- root->left = predecessor->left;
- free(predecessor);
- }
- else
- {
- root->key = predecessor->key;
- parent->right = predecessor->left;
- free(predecessor);
- }
- return root;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement