-2

I have created the following function to delete a Node from a tree:

void delNode(int key, Node *root) {
  if(root->data != key && root->left != NULL) {
    delNode(key,root->left);
  }
  if(root->data != key && root->right != NULL) {
    delNode(key,root->right);
  }
  if(root->data != key && root->left == NULL) {
    return;
  }
  if(root->data != key && root->right == NULL) {
    return;
  }
  if(root->data == key) {
    root = NULL;
    return;
  }
}

I have the following function for traversal

void printInorder(Node *root) {
  if(root!=NULL) {
    printInorder(root->left);
    printf("%d ",root->data);
    printInorder(root->right);
  }
}

When I delete an element & display the tree even the deleted node is shown.

PS: I want to delete the entire tree keeping the Node as the root

Styx
  • 8,485
  • 8
  • 36
  • 46
Prajval M
  • 1,978
  • 8
  • 26
  • Show us what elements doest root have (The node, show us the node, where you create that structure and all the elements). – M.K Oct 25 '17 at 17:37
  • Compile with all warnings and debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). **Use the debugger** `gdb` (and `valgrind`). Your fix-my-code question is off-topic here. – Basile Starynkevitch Oct 25 '17 at 17:45
  • 1
    `root` is a pointer pointing to a node in the tree. If you assign NULL to `root` that doesn't mean the node is deleted but it just breaks the link of `root` pointer and node it's pointing to but the parents either `left` or `right` pointer is still pointing to that node. You need to `free (root)` in order to delete it. But just freeing it is not sufficient, you need to break the link between the parent node and the node which has been freed (by assigning NULL to respective `left` or `right` pointer). Your `delNode()` function is incomplete. – H.S. Oct 25 '17 at 17:47
  • free(root) removes the existence of the the location root is pointing. I want it to be present just that it must be NULL – Prajval M Oct 25 '17 at 17:55

3 Answers3

2

This will not delete the node, root is just a reference to a memory location.
Here is a simple explanation:
So root its like an arrow to a box, you are pointing an arrow to some box(memory location) say box1 with some contents. Here root = NULL you are pointing the arrow to nothing that is say an empty space, but box1 still has the contents in it.

What you need to do is, clear the contents of the box1

free(root);   //This is make memory space available, i.e deallocates the memory 

Also your logic to tree node deletion is flawed/incomplete(probably incomplete), but I believe you know that already.

Rathan Naik
  • 758
  • 7
  • 19
1

your delNode method is not correct. Deleting a node from a tree is not so simple. Just consider how will you handle left sub-tree and right sub-tree of the node to be deleted. By assigning root= NULL; you are not going to achieve anything at all. By using free(root) also you are not going to achieve anything but permanently delete the child nodes (child trees) of root currently pointing to. I have corrected you method see below and try with this:-

    Node* delNode(int key, Node *root) {
      Node *nodeToBeDeleted=NULL
      Node *predecessor = root;
      if(root->data != key && root->left != NULL) {
        nodeToBeDeleted = delNode(key,root->left);
        if(nodeToBeDeleted){  //if we found the node to be deleted in left sub tree
          if(nodeToBeDeleted->right != NULL){
             root->left = nodeToBeDeleted->right;
             predecessor = findPredecessor(root->right);
             predecessor->left = nodeToBeDeleted->left;
          }          
          else{
             root->left = nodeToBeDeleted->left;
          }
          free(nodeToBeDeleted);
        }
      }
      if(root->data != key && root->right != NULL) {
        nodeToBeDeleted = delNode(key,root->right);
         if(nodeToBeDeleted){  //if we found the node to be deleted in the right sub tree
            if(nodeToBeDeleted->right != NULL){
             root->right = nodeToBeDeleted->right;
             predecessor = findPredecessor(root->right);
             predecessor->left = nodeToBeDeleted->left;
          }          
          else{
             root->right = nodeToBeDeleted->left;
          }
          free(nodeToBeDeleted);
         }
      }
      if(root->data != key && root->left == NULL) {
        return NULL;
      }
      if(root->data != key && root->right == NULL) {
        return NULL;
      }
      if(root->data == key) {
        return root;
      }

    }

add the below method to find predecessor node of the tree

    Node* findPredecessor(Node *root) {
     Node *predecessor = root;
      if(root!=NULL) {
        predecessor = findPredecessor(root->left);   
      }
      if(predecessor == NULL)
            return root;
      else
            return predecessor;
    }
Abhijit Pritam Dutta
  • 5,010
  • 2
  • 6
  • 16
0

I needed to free the node first and then use delete. My bad.

Prajval M
  • 1,978
  • 8
  • 26