dimanche 21 février 2021

How to test a destructor for a binary tree?

A naive recursive destructor for a binary tree looks like this:

void destroy_tree(Node *tree) {
    if (tree->left != NULL)
        destroy_tree(tree->left);
    if (tree->right != NULL)
        destroy_tree(tree->right);
    free(tree);
}

The question is: how to test such code? How to ensure that the traversal is correct? How to check that all nodes are indeed deleted?

Aucun commentaire:

Enregistrer un commentaire