foreach(QString child_to_remove, remove) { int idx = findChildID(child_to_remove); if(idx == -1) continue; BookItem* item = child(idx); MY_ASSERT(item); delete item; children.removeAt(idx); }
/* * Trie deletion has 3 cases * * Case 1: The word is a separate branch in the tree and it doesn't have * any other children. Simply delete the starting node. * Case 2: The word contains suffices. Only remove the "word" marker * Case 3: The reverse of Case 2. The word is a suffix. Delete only the * suffix * * This only replaces the deleted node with NULL and then calling the * sortChild() function and then realloc() * * realloc() only fails if size == 0 or if the block cannot be reallocated */ void deleteWord(Trie trie, char *str) { Node current = trie->dummy, temp = NULL; int i, tmp; if(!searchWord(trie, str)) return; /* Test for case 1*/ if(testCase(trie, str, case1)) { /* Simply remove the node */ i = findChildID(current, str[0]); nodeDelete(current->child[i]); resizeChildren(current); } else if(testCase(trie, str, case2)) { /* Test for case 2 */ /* No real deletion occurs here, the nodes would * still be there, but it makes the word unsearchable */ for(i = 0; i < strlen(str); i++) { current = findChild(current, str[i]); } current->endMarker = false; } else if(testCase(trie, str, case3)) { /* Test for case 3 */ /* Traverse the word and keep track on which node * had children > 1, then find the next matching node * and delete it */ temp = trie->dummy; for(i = 0; i < strlen(str); i++) { temp = findChild(temp, str[i]); if(temp->children > 1) { current = temp; tmp = i+1; } } i = findChildID(current, str[tmp]); nodeDelete(current->child[i]); resizeChildren(current); } }