void Binary_search_tree::remove_helper(int x, BST_node* t, BST_node* p) { if (t != nullptr){ if (x > t->get_data()) { remove_helper(x, t->get_right_child(), t); } else if (x < t->get_data()) { remove_helper(x, t->get_left_child(), t); } else { remover(t, p); } } }
void Binary_search_tree::remover(BST_node* t, BST_node* p) { if (t->get_left_child() == nullptr) { if (t->get_right_child() == nullptr) { if (t->get_data() < p->get_data()) { delete t; p->set_left_child(nullptr); } else { delete t; p->set_right_child(nullptr); } } else { t->set_data(get_min(t->get_right_child())->get_data()); remove_helper(get_min(t->get_right_child())->get_data(), t->get_left_child(), t); remove_helper(get_min(t->get_right_child())->get_data(), t->get_right_child(), t); } } else { t->set_data(get_max(t->get_left_child())->get_data()); remove_helper(get_max(t->get_left_child())->get_data(), t->get_left_child(), t); remove_helper(get_max(t->get_left_child())->get_data(), t->get_right_child(), t); } }
void Q3ButtonGroup::remove(QAbstractButton *button) { fixChildren(); remove_helper(button); }
int Q3ButtonGroup::insert(QAbstractButton *button, int id) { remove_helper(button); return insert_helper(button, id); }
bool ChangeSet::remove(int start, int end) { return remove_helper(start, end - start); }
void AVLTree::remove_helper(Node * & t, const std::string& name) { if( t == NULL ) return; // wasn't able to find the node else if(name < t->city.name) { remove_helper( t->left, name ); } else if(name > t->city.name) { remove_helper( t->right, name ); } else { // else found the node // found the node to delete // Case 1: node has no children if((!t->left) && (!t->right)) { delete t; t = NULL; } // Case 2a: node has 1 child on left else if(!t->right) { Node * save_left = t->left; delete t; t = save_left; } //Case 2b: node has 1 child on right else if(!t->left) { Node * save_right = t->right; delete t; t = save_right; } // Case 3: node has 2 children else { // find the inorder successor, left most child of right subtree Node * succ = t->right; // case 1: succ has no left child if(!succ->left) { delete t; t = succ; } // case 2: succ does have left child else { while(succ->left) succ = succ->left; Node * save_right = succ->right; t->city = succ->city; delete succ; succ = save_right; } } } if(!t) return; t->height = MAX( height(t->left), height(t->right) ) + 1; const int balance = getBalance( t ); const int balance_left = getBalance( t->left ); const int balance_right = getBalance( t->right ); // Case 1: Left Left Case if((balance > 1) && (balance_left >= 0)) { rotateWithLeftChild(t); } // Case 2: Left Right Case else if((balance > 1) && (balance_left < 0)) { rotateWithRightChild(t->left); rotateWithLeftChild(t); } // Case 3: Right Right else if((balance < -1) && (balance_right <= 0)) { rotateWithRightChild(t); } // Case 4: Right Left else if((balance < -1) && (balance_right > 0)) { rotateWithLeftChild(t->right); rotateWithRightChild(t); } }
void AVLTree::remove(const std::string& name) { remove_helper(root, name); }
void Binary_search_tree::remove(int x) { remove_helper(x, root, nullptr); }