一次删除整个二进制搜索树

Deleting the entire binary search tree at once

本文关键字:二进制 搜索树 删除 一次      更新时间:2023-10-16

我一直在尝试实现delete BST函数,但我不知道为什么它不起作用,我认为它在逻辑上是正确的。任何人都能告诉我,为什么我会出现运行时错误,以及我应该如何纠正它吗?

  #include <iostream>
  using namespace std;
class node{
public:
int data;
node *right;
node *left;
node(){
    data=0;
    right=NULL;
    left=NULL;
      }
};
class tree{
node *head;
int maxheight;
     public:
tree(){head=0;maxheight=-1;}
bool deletenode(int key,node* root);
int get_height(){return maxheight;}
void insert(int key);
void pre_display(node* root);
     void delete_tree(node *root);
     node* get_head(){return head;}
         };
void tree::insert(int key){
     node *current=head;
    node *newnode=new node;
    if(newnode==NULL)
    throw(key);
    newnode->data=key;
    int height=0;
if(head==0){
head=newnode;
     }
else
{
    while(1){
    if(current->right==NULL && current->data < newnode->data)
    {
        current->right=newnode;
        height++;
        break;
    }
    else if(current->left==NULL && current->data > newnode->data)
    {
        current->left=newnode;
        height++;
        break;
    }
    else if(current->right!=NULL && current->data < newnode->data)
    {
         current=current->right;
         height++;
   }
    else if(current->left!=NULL && current->data > newnode->data)
    {
               current=current->left;
          height++;
    }
         }
 }
 if(height>maxheight)
 maxheight=height;
 }
 void tree::pre_display(node *root){
 if(root!=NULL)
 {
 cout<<root->data<<" ";
 pre_display(root->left);
 pre_display(root->right);
 }
 }
 void tree::delete_tree(node *root){
  if(root!=NULL)
 {
 delete_tree(root->left);
 delete_tree(root->right);
 delete(root);
 if(root->left!=NULL)
 root->left=NULL;
 if(root->right!=NULL)
 root->right=NULL;
 root=NULL;
 }
 }
int main(){
tree BST;
int arr[9]={17,9,23,5,11,21,27,20,22},i=0;
for(i=0;i<9;i++)
BST.insert(arr[i]);
BST.pre_display(BST.get_head());
cout<<endl;
BST.delete_tree(BST.get_head());
BST.pre_display(BST.get_head());
cout<<endl;
system("pause");
return 0;
}

所有其他函数都正常工作,你只需要检查delete_tree函数,提供了其他代码来给出我的BST的结构。

在您的delete_tree 中

void tree::delete_tree(node *root){
    if(root!=NULL)
    {
        delete_tree(root->left);
        delete_tree(root->right);
        delete(root);
        if(root->left!=NULL)
            root->left=NULL;
        if(root->right!=NULL)
            root->right=NULL;
        root=NULL;
    }
}

删除root变量后,您正在访问该变量

你也打电话给

    BST.delete_tree(BST.get_head());
BST.pre_display(BST.get_head());

删除树后的预显示delete_tree删除树后,还应将BST.head设置为NULL

也是一种批判。BST属于树型。它已经有一个指示根节点的head成员变量。因此delete_tree/pre_display根本不需要任何参数。

您可以通过以下方式删除://这是清洁功能:

void cleantree(tree *root){
 if(root->left!=NULL)cleantree(root->left);
 if(root->right!=NULL)cleantree(root->right);
 delete root;}

//这就是我们称之为清洁功能的地方:

cleantree(a);
a=NULL;

//其中"a"是指向树根的指针。

问题就在这里:

 delete_tree(root->left);
 delete_tree(root->right);
 delete(root);
 if(root->left!=NULL)
 root->left=NULL;
 if(root->right!=NULL)
 root->right=NULL;
 root=NULL;

您正试图将NULL分配给根的成员:

root->left=NULL;

已被删除。没有必要这样做,因为您已经在释放delete_tree(root->left); 中的内存

递归删除左右子树,您的树将被删除,简单如下:

void delete(node *root){
  // If node is empty, don't bother
  if (root == NULL) { return; }
  // Delete subtrees
  delete(root->left);
  delete(root->right);
  // Delete current node
  free(root);
  root = NULL;
}

删除后不应该从根目录中读取。向下移动delete(root)行。

简单回答:节点描述符的实现缺少正确的显式析构函数实现(编译器生成的默认值将通过调用delete运算符来使用:调用析构函数并释放堆上分配的空间(-清除对兄弟的引用的实现

这里是我使用递归性的建议。。

 template <class T> void Tree<T>::destroy(Noeud<T> ** r){
            if(*r){
                if(!(*r)->left && !(*r)->right){  //a node having no child
                  delete *r; *r=NULL;
                }else if((*r)->left && (*r)->right){ //a node having two childs
                    destroy(&(*r)->left); //destroy the left tree
                    destroy(&(*r)->right); //destroy the right tree
                    destroy(r); //destroy the node
                }else if((*r)->left){ //a node has only left child
                    destroy(&(*r)->left); //destroy the left tree
                    destroy(r); //destroy the node
                }else if((*r)->right){ //a node has only right child
                    destroy(&(*r)->right); //destroy the right tree
                    destroy(r); //destroy the node
                }
            }
}
//in function main()
int main(){
Tree<int> a(5); // 'a' is a tree of int type with a root value equal 5
a.add(2);a.add(7);a.add(6);a.add(-1);a.add(10); // insert values into the tree 
a.destroy(&a.root);  //destroy the tree
}