NodeT* deleteNode(struct Node *root, int data){ if(root == NULL) return root; // Search for the key if(data < root->data) root->left = deleteNode(root->left, data); else if(data > root->data) root->right = deleteNode(root->right, data); // Once found, delete it else{ // One child or no child if ((root->left == NULL) || (root->right == NULL)){ NodeT* tempNode; if(root->left == NULL && root->right != NULL) tempNode = root->right; else tempNode = root->left; if(tempNode == NULL){ tempNode = root; root = NULL; } else *root = *tempNode; free(tempNode); } // Two children else{ NodeT* tempNode = minValueNode(root->right); root->data = tempNode->data; root->right = deleteNode(root->right, tempNode->data); } } return balanceTree(root); }
struct Node * getSuccessorNode(struct Node* root, int key ) { struct Node* current = getNodeWithKey(root, key); struct Node* maxnode = maxValueNode(root); if (key == maxnode->key) return NULL; // CASE 1: it has a right subtree (easy): if (current->right != NULL) return minValueNode(current->right); // CASE 2: it does not (harder): struct Node* parent; parent = getParentNode(root, key); struct Node* x = current; struct Node* y = parent; while (y != NULL && x == y->right) { x = y; y = getParentNode(root, y->key); } return y; }
AVL_node* delete_AVL(int key, AVL_node* root) { if(root==NULL) return NULL; if(root->key > key) root->left = delete_AVL(key,root->left); else if(root->key < key) root->right = delete_AVL(key,root->right); else { if(root->left == NULL) { AVL_node* temp = root; root = root->right; free(temp); return(root); } else if(root->right == NULL) { AVL_node* temp = root; root = root->left; free(temp); return(root); } AVL_node* temp = minValueNode(root->right); root->key = temp->key; root->right = delete_AVL(temp->key, root->right); return root; } if (root == NULL) return root; root->height = max(height(root->left), height(root->right)) + 1; int balance = height(root->left) - height(root->right); if (balance > 1 && height(root->left->left) - height(root->left->right) >= 0) return rightrotate(root); if (balance > 1 && height(root->left->left) - height(root->left->right) < 0) { root->left = leftrotate(root->left); return rightrotate(root); } if (balance < -1 && height(root->right->left) - height(root->right->right) <= 0) return leftrotate(root); if (balance < -1 && height(root->right->left) - height(root->right->right) > 0) { root->right = rightrotate(root->right); return leftrotate(root); } return root; }
// OKay, now work on meat of this program struct node * deleteNode(struct node *root, int data) { if (!root) { return root; } if ( data < root->key) { root->left = deleteNode(root->left, data); return root; } else if ( data > root->key) { root->right = deleteNode(root->right, data); return root; } // Now that, root is not null, given key not < or > the root->key, // implies, we are at found the key position. else if ( data == root->key) { //printf("%s: root = %p, root->key = %d root->left = %p root->right = %p\n", __FUNCTION__, root, root->key, root->left, root->right); //case 1. the node to be deleted either has one child or no children // eg., 20 or 40 //so the following 2 conditions work, for one or no children if (root->left == NULL) { struct node * temp = root->right; free(root); return temp; } if (root->right == NULL) { struct node * temp = root->left; free(root); return temp; } // Now, handle the case when node to be deleted has both the children eg., 50 or 70 // replace key to be deleted ie., root->key with min element on the right side like inorder successor. { struct node *temp = minValueNode(root->right); root->key = temp->key; //printf(" data = %d temp->key = %d\n", data, temp->key); root->right = deleteNode(root->right,temp->key); return root; } } return root; }
/* Given a binary search tree and a key, this function deletes the key and returns the new root */ struct node* deleteNode(struct node* curr, int key) { // base case if (curr == NULL) return curr; // If the key to be deleted is smaller than the root's key, // then it lies in left subtree if (key < curr->key) curr->left = deleteNode(curr->left, key); // If the key to be deleted is greater than the root's key, // then it lies in right subtree else if (key > curr->key) curr->right = deleteNode(curr->right, key); // if key is same as root's key, then This is the node // to be deleted else { // node with only one child or no child if (curr->left == NULL) { struct node *temp = curr->right; free(curr); return temp; } else if (curr->right == NULL) { struct node *temp = curr->left; free(curr); return temp; } // node with two children: Get the inorder successor (smallest // in the right subtree) struct node* temp = minValueNode(curr->right); // Copy the inorder successor's content to this node curr->key = temp->key; // Delete the inorder successor curr->right = deleteNode(curr->right, temp->key); } return curr; }
// Recursive function to delete a node with given key // from subtree with given root. It returns root of // the modified subtree. struct Node* deleteNode(struct Node* root, int key) { // STEP 1: PERFORM STANDARD BST DELETE if (root == NULL) return root; // printf("key: %d, rkey: %d; ",key,root->key); // If the key to be deleted is smaller than the // root's key, then it lies in left subtree if ( key < root->key ) root->left = deleteNode(root->left, key); // If the key to be deleted is greater than the // root's key, then it lies in right subtree else if( key > root->key ) root->right = deleteNode(root->right, key); // if key is same as root's key, then This is // the node to be deleted else { // node with only one child or no child if( (root->left == NULL) || (root->right == NULL) ) { struct Node *temp = root->left ? root->left : root->right; // No child case if (temp == NULL) { // printf("no child case\n"); // MN comment temp = root; root = NULL; } else { // One child case // printf("one child case\n"); // MN comment *root = *temp; // Copy the contents of // the non-empty child } free(temp); } else { // node with two children: Get the inorder // successor (smallest in the right subtree) struct Node* temp = minValueNode(root->right); // Copy the inorder successor's data to this node root->key = temp->key; // Delete the inorder successor root->right = deleteNode(root->right, temp->key); } } // If the tree had only one node then return if (root == NULL) { // printf("tree had only one node case\n"); return root; } // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE root->height = 1 + mymax(height(root->left), height(root->right)); // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to // check whether this node became unbalanced) int balance = getBalance(root); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if (balance > 1 && getBalance(root->left) >= 0) return rightRotate(root); // Left Right Case if (balance > 1 && getBalance(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); } // Right Right Case if (balance < -1 && getBalance(root->right) <= 0) return leftRotate(root); // Right Left Case if (balance < -1 && getBalance(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); } return root; }
// Function to delete a node from the Binary Search Tree and return the new root struct node* delete_node(struct node *tree, int num) { int balance_factor; struct node *temp = NULL; // base case if (tree == NULL) return tree; // Search element to be deleted in left sub-tree if (num < tree->data) tree->left = delete_node(tree->left, num); else if (num > tree->data) // Search element to be deleted in right sub-tree tree->right = delete_node(tree->right, num); else //The current node is to be deleted { // node with only one child or no child if( (tree->left == NULL) || (tree->right == NULL) ) { if (tree->left != NULL) temp = tree->left; else temp = tree->right; // No child case if(temp == NULL) { temp = tree; tree = NULL; } else // One child case *tree = *temp; // Copy the contents of the non-empty child free(temp); } else { // node with two children: Get the inorder successor (smallest in the right subtree) temp = minValueNode(tree->right); // Copy the inorder successor's content to this node tree->data = temp->data; // Delete the inorder successor tree->right = delete_node(tree->right, temp->data); } } // If the tree had only one node which is now deleted then return if (tree == NULL) return tree; // Compute new height after deletion tree->height = max(compute_height(tree->left), compute_height(tree->right)) + 1; // Compute new balance factor after deletion balance_factor = get_balance_factor(tree); // If unbalanced, then 4 cases of rotation // 1. Left Left Case - Only 1 Right Rotate will solve unbalancy if (balance_factor > 1 && get_balance_factor(tree->left) >= 0) return right_rotate(tree); // 2. Right Right Case - Only 1 Left Rotate will solve unbalancy if (balance_factor < -1 && get_balance_factor(tree->right) <= 0) return left_rotate(tree); // 3. Left Right Case - Left rotation on left child will make it left left case if (balance_factor > 1 && get_balance_factor(tree->left) < 0) { tree->left = left_rotate(tree->left); return right_rotate(tree); } // 4. Right Left Case - Right rotation on right child will make it right right case if (balance_factor < -1 && get_balance_factor(tree->right) > 0) { tree->right = right_rotate(tree->right); return left_rotate(tree); } // If balanced then return unchanged node return tree; }
/* Delete an Element from the tree */ avl_node *avlTree::deleteNode(avl_node* root, string dogId) { // STEP 1: PERFORM STANDARD BST DELETE if (root == NULL) return root; // If the key to be deleted is smaller than the root's key, // then it lies in left subtree if (dogId < root->data.getID()) root->left = deleteNode(root->left, dogId); // If the key to be deleted is greater than the root's key, // then it lies in right subtree else if (dogId > root->data.getID()) root->right = deleteNode(root->right, dogId); // if key is same as root's key, then This is the node // to be deleted else { // node with only one child or no child if ((root->left == NULL) || (root->right == NULL)) { avl_node *temp; if (root->left) temp = root->left; else temp = root->right; //avl_node *temp = root->left ? root->left : root->right; // No child case if (temp == NULL) { temp = root; root = NULL; } else // One child case *root = *temp; // Copy the contents of the non-empty child delete temp; } else { // node with two children: Get the inorder successor (smallest // in the right subtree) avl_node* temp = minValueNode(root->right); // Copy the inorder successor's data to this node root->data = temp->data; // Delete the inorder successor root->right = deleteNode(root->right, temp->data.getID()); } } // If the tree had only one node then return if (root == NULL) return root; root = balance(root); return root; }
/* Delete Node */ ptr_t DeleteTreeNode(data_t *myHeap, data_t *Master2SysAlloc, int *fixedStack, int *secondStack, int stackPtr_avl, ptr_t rootPtr, int key){ int flag_stop = 0; int flag_stackIsUsed = 0; ptr_t nowPtr = rootPtr; struct stack_t stackOutput; while(flag_stop == 0){ struct sub_t subResult = DeleteNodeSub(myHeap, nowPtr, key); if(subResult.feedback == FB_DONE){ flag_stop = 1; // --------- Deletion --------- ptr_t leftPtr = node_get_left_pointer(myHeap, nowPtr); ptr_t rightPtr = node_get_right_pointer(myHeap, nowPtr); if(leftPtr == NULL_PTR || rightPtr == NULL_PTR){ // at least one null children if(leftPtr != NULL_PTR){ struct node_t_std nowNode = node_read_std(myHeap, leftPtr); node_write_std(myHeap, nowPtr, nowNode); node_delete(Master2SysAlloc, leftPtr); }else if(rightPtr != NULL_PTR){ struct node_t_std nowNode = node_read_std(myHeap, rightPtr); node_write_std(myHeap, nowPtr, nowNode); node_delete(Master2SysAlloc, rightPtr); }else{ node_delete(Master2SysAlloc, nowPtr); nowPtr = NULL_PTR; } }else{ // both leaves exists //1.get the inorder successor ptr_t rightPtr = node_get_right_pointer(myHeap, nowPtr); ptr_t minPtr = minValueNode(myHeap, rightPtr); //2.copy the inorder successor's data to this node int copyData = node_read_data(myHeap, minPtr); node_write_data(myHeap, nowPtr, copyData); //3.delete the inorder successor ptr_t tempPtr = DeleteSuccessor(myHeap, Master2SysAlloc, secondStack, 0, rightPtr, copyData); node_set_right(myHeap, nowPtr, tempPtr); } // --------- Balancing --------- if(flag_stackIsUsed == 0){ if(nowPtr == NULL_PTR){ rootPtr = nowPtr; }else{ rootPtr = ProcessNodeDeletion(myHeap,nowPtr); } }else{ stackOutput = AVL_STACK_READ(fixedStack, stackPtr_avl); stackPtr_avl = stackOutput.hdPtr_avl; if(stackOutput.operation == GOING_LEFT){ node_set_left(myHeap, stackOutput.pointer, nowPtr); }else{ node_set_right(myHeap, stackOutput.pointer, nowPtr); } while(stackPtr_avl > 0){ nowPtr = stackOutput.pointer; ptr_t nowPtr_new = ProcessNodeDeletion(myHeap, nowPtr); stackOutput = AVL_STACK_READ(fixedStack, stackPtr_avl); stackPtr_avl = stackOutput.hdPtr_avl; if(nowPtr_new != nowPtr){ if(stackOutput.operation == GOING_LEFT){ node_set_left(myHeap, stackOutput.pointer, nowPtr_new); }else{ node_set_right(myHeap, stackOutput.pointer, nowPtr_new); } } } rootPtr = ProcessNodeDeletion(myHeap, stackOutput.pointer); } }else{ flag_stackIsUsed = 1; if(subResult.feedback == FB_LEFT){ stackOutput = AVL_STACK_WRITE(fixedStack, stackPtr_avl, nowPtr, GOING_LEFT); stackPtr_avl = stackOutput.hdPtr_avl; nowPtr = node_get_left_pointer(myHeap, nowPtr); }else{ stackOutput = AVL_STACK_WRITE(fixedStack, stackPtr_avl, nowPtr, GOING_RIGHT); stackPtr_avl = stackOutput.hdPtr_avl; nowPtr = node_get_right_pointer(myHeap, nowPtr); } if(nowPtr == NULL_PTR){ flag_stop = 1; } } } return rootPtr; }
AVLTreeNode* deleteNode(AVLTreeNode* root, int val) { // STEP 1: PERFORM STANDARD BST DELETE if(root == NULL) return root; if(val < root->val) { // If the val to be deleted is smaller than the root's val, // then it lies in left subtree root->left = deleteNode(root->left, val); root->leftSize--; } else if(val > root->val) { // If the val to be deleted is greater than the root's val, // then it lies in right subtree root->right = deleteNode(root->right, val); } else { // if val is same as root's val, then This is the node // to be deleted if(root->count > 1) { // If val is present more than once, simply decrement // count and return (root->count)--; return root; } // ElSE, delete the node // node with only one child or no child if((root->left == NULL) || (root->right == NULL)) { AVLTreeNode* temp = root->left ? root->left : root->right; // No child case if(temp == NULL) { temp = root; root = NULL; } else { temp->leftSize = root->leftSize; // One child case *root = *temp; // Copy the contents of the non-empty child } delete(temp); } else { // node with two children: Get the inorder successor (smallest // in the right subtree) AVLTreeNode* temp = minValueNode(root->right); // Copy the inorder successor's data to this node root->val = temp->val; // Delete the inorder successor root->right = deleteNode(root->right, temp->val); } } // If the tree had only one node then return if(root == NULL) return root; // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE root->height = max(height(root->left), height(root->right)) + 1; // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether // this node became unbalanced) int balance = getBalance(root); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if(balance > 1 && getBalance(root->left) >= 0) { return rightRotate(root); } // Left Right Case if(balance > 1 && getBalance(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); } // Right Right Case if(balance < -1 && getBalance(root->right) <= 0) { return leftRotate(root); } // Right Left Case if(balance < -1 && getBalance(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); } return root; }
struct node* deleteNode(struct node* root, int key) { if (root == NULL) return root; if ( key < root->key ) root->left = deleteNode(root->left, key); else if( key > root->key ) root->right = deleteNode(root->right, key); else { if( (root->left == NULL) || (root->right == NULL) ) { struct node *temp = root->left ? root->left : root->right; // No child case if(temp == NULL) { temp = root; root = NULL; } else // One child case *root = *temp; free(temp); } else { // node with two children: Get the inorder successor (smallest // in the right subtree) struct node* temp = minValueNode(root->right); // Copy the inorder successor's data to this node root->key = temp->key; root->right = deleteNode(root->right, temp->key); } } if (root == NULL) return root; root->height = max(height(root->left), height(root->right)) + 1; int balance = getBalance(root); if (balance > 1 && getBalance(root->left) >= 0) return rightRotate(root); if (balance > 1 && getBalance(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); } if (balance < -1 && getBalance(root->right) <= 0) return leftRotate(root); if (balance < -1 && getBalance(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); } return root; }
/******************** this function removes items from the queue based on priority. *********************/ void dequeue(queueNode *myQueue) { treeNode *node; node = minValueNode(myQueue->myTree->root); deleteNode(myQueue->myTree->root, node->data, myQueue->myTree->compareptr); }