dimanche 19 juin 2016

Unable to allocate NULL to an object (Binary tree, C++)

basically i'm used to use C# but current I try to code a binary tree in C++. The functions work fine, except Delete(). Deleting nodes with 1 or 2 child nodes works fine. But if I want to delete a node without children, NULL allocation won't work.

Here's my code snippet:

  void Delete(Node<T>* delNode) {
        Node<T>* value = (Search(delNode));
        if (value == NULL) return;

        if (value != NULL) {

            // no child nodes (does not work)
            if (value->right == NULL && value->left == NULL) {

                (*value).info = NULL;

                // value = NULL doesn't work as well

                return;
            }

            // one child node (works fine)
            if (value->left == NULL) {
                (*value).info = (&(*value->right))->info;
                (*value).left = (&(*value->right))->left;
                (*value).right = (&(*value->right))->right;
                return;
            }

            if (value->right == NULL) {
                (*value).info = (&(*value->left))->info;
                (*value).left = (&(*value->left))->left;
                (*value).right = (&(*value->left))->right;
                return;
            }

            // 2 child nodes (works fine)
            if (value->right != NULL && value->left != NULL) {
                Node<T> **tp1, *tp2;
                tp1 = &(value->left);
                while ((*tp1)->right != NULL) tp1 = &((*tp1)->right);
                tp2 = *tp1;
                (*value).info = (*tp1)->info;
                *tp1 = (*tp1)->left;
                return;
            }
        }
    }

Visual Studio says "There's no operator = that accepts a right-handed operand of the type int". But this message doesn't make sense to me.

Hope you could help me. Thanks beforehand.

ek187

Aucun commentaire:

Enregistrer un commentaire