Valerie
Valerie

Reputation: 33

Remove a Node from a binary tree

Please help. What is wrong with my remove function below. Cant seem to figure it out

I seem to be getting an error: The class declaration seems pretty fine. The main issue is to have this remove function to work

void binaryTree::Remove(int) {
  if (node != NULL)
  {
     Node* tmptr = node;
     int rootdata = node->data;
     /int rSubtree;
  }
  {
  // Case 0- no child
  if (node->lChild == NULL && node->rChild == NULL)
  {
      node = NULL;
      //parent-            //set the parent of the node to NULL
      delete node;
  }
  // has one child
  else if (node->lChild == NULL && node->rChild != NULL)
  {
      node = node->rChild;
      node->rChild = NULL;
      delete node;
  }

  else if (node->lChild != NULL && node->rChild == NULL)
  {
      node = node->lChild;`enter code here`
      node->lChild = NULL;
      delete node;
  }
}

Upvotes: 1

Views: 90

Answers (1)

Jakub Fedyczak
Jakub Fedyczak

Reputation: 2354

Try swapping node = NULL with delete node; Otherwise you're trying to delete NULL.

Upvotes: 1

Related Questions