jeudi 26 juillet 2018

Trie Datastructure recursive to iteratie convert

Below is the recursive trie datastructure delete function. This need to convert into iterative delete. I know somehow need to track of the prev and curr node and traverse reverse, please help me to convert this into iterative delete.

bool is_delete(trie *root){
    for(int i = 0; i < SIZE; i++){
        if(root->children[i] != NULL)
            return false;
        return true;
    }
}

bool delete_util(trie *root, string key, int st, int end){
    if(root!=NULL){
        if(st == end){
            root->is_end = false;
            if(is_delete(root))
                return true;
            return false;
        }
        if(delete_util(root->children[key[st]], key, st +1, end)){
            delete(root->children[key[st]]);
            root->children[key[st]] = NULL;

            if(is_delete(root) && root->is_end == false)
                return true;
            return false;
        }else
            return false;
    }
    return false;
}

void delete_key(trie *root, string key){
    delete_util(root, key, 0, key.length());
}

Aucun commentaire:

Enregistrer un commentaire