TAGS :Viewed: 15 - Published at: a few seconds ago

[ C++ deleting minimum element in binary tree ]

Can anyone explain to me why the delete_min() function does not work? I have been trying to figure this out for hours now.

I have tried different variations of the function, but it seems to either delete the wrong thing, only alter data and not delete it, or delete the wrong thing. Also, the insertion function doesn't always work properly after delete_min() is called, so I'm sure this has something to do with pointers.

Here is the function:

int BinaryTree::delete_min_helper(TreeNode *node){

   while(node->left != NULL){
      node = node->left;
   }

  if(node == root){
     puts("attempted to delete root");
     return 1;
  }

  delete node;
  node = NULL;
  return 0;
}

And here is a compilable example:

#ifndef _TREE_H_
#define _TREE_H_

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct TreeNode {
  int val;
  char *str;
  TreeNode *left;
  TreeNode *right;
};

class BinaryTree {
  public:

  BinaryTree();
  ~BinaryTree();

  int insert_node(unsigned int val, char *str);
  TreeNode *find_min();
  int delete_min();
  void print();

 private:

 int insert_node_helper(TreeNode *&node, unsigned int val, char *str);
 int delete_min_helper(TreeNode *node);
 void print_helper(TreeNode *node);

 TreeNode *root;  
};

#endif

BinaryTree::BinaryTree(){
  this->root = NULL;
}

BinaryTree::~BinaryTree(){
}

int BinaryTree::insert_node(unsigned int val, char *str){
  return insert_node_helper(this->root, val, str);
}

int BinaryTree::insert_node_helper(TreeNode *&node, unsigned int val, char *str){

  if(node == NULL){
    node = new TreeNode;
    node->val = val;
    node->str = strdup(str);
    node->left = NULL;
    node->right = NULL;
    if(node != NULL){
      return 0;
    }else{
      puts("inserted null node");
      return 1;
   }
 }else if(val <= node->val){
   return insert_node_helper(node->left, val, str);
 }else if(val > node->val){
   return insert_node_helper(node->right, val, str);
 }

 return 1;
}

void BinaryTree::print(){
  print_helper(this->root);
}


void BinaryTree::print_helper(TreeNode *node){

    if(node != NULL){
      print_helper(node->right);
      printf("%d occurrences of \"%s\"\n", node->val, node->str);
      print_helper(node->left);
    }

}

TreeNode *BinaryTree::find_min(){
  TreeNode *temp = this->root;
  while(temp->left != NULL){
    temp = temp->left;
  }
  return temp;
}

int BinaryTree::delete_min(){
   return delete_min_helper(root);
}

int BinaryTree::delete_min_helper(TreeNode *node){

   while(node->left != NULL){
     node = node->left;
   }

  if(node == root){
    puts("attempted to delete root");
    return 1;
  }

  delete node;
  node = NULL;
  return 0;
}

#include <time.h>

int main(){

  BinaryTree bt;

  srand(time(NULL));
  for(int i = 0; i < 10; i++){
     bt.insert_node(rand() % 20, "test");
  }

  bt.print();
  printf("min val = %d\n", bt.find_min()->val);
  puts("#################################");
  bt.delete_min();
  bt.print();
  printf("min val = %d\n", bt.find_min()->val);

  return 0;
}

Answer 1


Two things come to mind right away:

  1. a parent node is still pointing at the node which you deleted

  2. the node you're deleting might have a node->right pointer hanging off of it, which you are lopping off the tree, instead of re-hooking it back into the tree.

There might be more problems, but you need to address these issues at a bare minimum.