C ++删除二叉树中的最小元素

时间:2013-02-08 02:20:33

标签: c++ pointers binary-tree

任何人都可以向我解释为什么delete_min()功能不起作用?我一直试图解决这个问题几个小时了。

我尝试了不同的功能变体,但似乎要么删除错误的东西,只修改数据而不删除它,或删除错误的东西。此外,在调用delete_min()之后插入函数并不总能正常工作,所以我确定这与指针有关。

这是功能:

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;
}

这是一个可编辑的例子:

#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;
}

2 个答案:

答案 0 :(得分:4)

马上会想到两件事:

  1. 父节点仍指向您删除的节点

  2. 您要删除的节点可能有一个node->right指针悬挂在它上面,而您正在砍掉树,而不是将其重新挂回树中。

    < / LI>

    可能存在更多问题,但您需要至少解决这些问题。

答案 1 :(得分:-1)

他父母的引用仍然指向内存中的某个地址。

试试这个:

int BinaryTree::delete_min_helper(TreeNode *node){

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

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

  prev->left = NULL;

  delete node;
  node = NULL;
  return 0;
}