// Replace del_help with the rightmost node in pp bool TreeSetImpl::nodeDel(TreeSetNode *&pp) { if(pp->m_right) { if(nodeDel(pp->m_right)) { return balanceR(pp); } else { return false; } } swapContent(m_deleteHelper,pp); m_deleteHelper = pp; pp = m_deleteHelper->m_left; return true; }
// Remove key from pp. Returns true if pp needs rebalancing. false if not bool TreeSetImpl::nodeDelete(TreeSetNode *&pp, const void *key) { TreeSetNode *p = pp; if(p == NULL) { return false; // key not found } const int c = m_comparator->cmp(p->m_key, key); if(c > 0) { // pp.key > key. Continue search in left subtree if(nodeDelete(p->m_left, key)) { return balanceL(pp); } else { return false; } } if(c < 0) { // pp.key < key. Continue search in right subtree if(nodeDelete(p->m_right, key)) { return balanceR(pp); } else { return false; } } bool result = false; if(p->m_right == NULL) { pp = p->m_left; result = true; } else if(p->m_left == NULL) { pp = p->m_right; result = true; } else { m_deleteHelper = p; if(nodeDel(p->m_left)) { result = balanceL(pp); } p = m_deleteHelper; } deleteNode(p); m_size--; m_updateCount++; return result; }
int main() { Node head = nodeCreate(1); printf("1. head is %d\n\n\n\n", head->num); printf("2. The list and the linked list is now:\n"); int array[] = {10, 3, 10, 7, 2, 11, 5, 9, 11, 6, 0, 6, 11, 6}; for(int i = 0; i < 9; i++) { head = listAppend(head, array[i]); } Node item = head; while(item->next != NULL) { printf("%d ", item->num); item = item->next; } printf("%d\n\n\n\n", item->num); printf("3. Now we are testing the nodDel() function.\n"); Node target = listSearch(head, 1); head = nodeDel(head, target); printf("4.\n"); // printf("3. Now we are testing the delDups() function.\n"); // head = delDups(head); item = head; if(head == NULL) { printf("The list is empty"); } else { while(item->next != NULL) { printf("%d ", item->num); item = item->next; } printf("%d\n\n\n\n", item->num); } return 0; }