Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 二叉搜索树;删除“;功能_C++_C_Binary Search Tree - Fatal编程技术网

C++ 二叉搜索树;删除“;功能

C++ 二叉搜索树;删除“;功能,c++,c,binary-search-tree,C++,C,Binary Search Tree,正在尝试为二进制搜索树编写删除函数。我知道有三种可能的情况需要考虑,但我不太确定从哪里开始。 我的问题主要源于这样一个事实:一旦我找到了需要删除的节点,我需要在需要删除的节点之后将其父节点设置为另一个节点。我应该使用光标来保持父节点还是什么 我有一个节点的结构: struct bt_node { int data; bt_node* left; bt_node* right; }; 移除函数的定义如下所示: void remove(bt_node** top_ref, int da

正在尝试为二进制搜索树编写删除函数。我知道有三种可能的情况需要考虑,但我不太确定从哪里开始。

我的问题主要源于这样一个事实:一旦我找到了需要删除的节点,我需要在需要删除的节点之后将其父节点设置为另一个节点。我应该使用光标来保持父节点还是什么

我有一个节点的结构:

struct bt_node {
  int data;
  bt_node* left;
  bt_node* right;
};
移除函数的定义如下所示:

void remove(bt_node** top_ref, int data);

谢谢你的帮助

简单的方法是扩展搜索代码,以便在沿着树向下移动时,在第二个引用中跟踪“最后一个”父对象。然后,此“扩展”搜索返回节点和节点的父节点


然后通过delete函数使用这个搜索函数,这样就不必在delete逻辑中做任何花哨的事情。

一个简单的方法就是使用递归。您要求delete函数将对当前节点的引用返回给调用函数。这样,您可以轻松地修改父节点。

下面是一些递归代码供您参考:

结构声明:

 typedef struct node {
    int info;
    struct node* lchild;
    struct node* rchild;
}NODE;
NODE* deleteElem(NODE* root, int elem) {
    NODE* save;
    if(root == NULL) {
        printf("Element not in the tree !\n");
        return;
    }
    if(root->info == elem) {
        if(root->rchild == NULL && root->lchild == NULL) {                  // no child
            free(root);
            return NULL;
        }
        else if(root->rchild == NULL || root->lchild == NULL) {             // one child
            if(root->rchild == NULL) {
                save = root->lchild;
                free(root);
                return save;
            }
            else {
                save = root->rchild;
                free(root);
                return save;
            }
        }
        else {                                                             // two children
            save = findPred(root->lchild);
            root->info = save->info;
            root->lchild = deleteElem(root->lchild, root->info);
            return root;
        }
    }
    else if(root->info < elem) {
        root->rchild = deleteElem(root->rchild, elem);
    }
    else if(root->info > elem) {
        root->lchild = deleteElem(root->lchild, elem);
    }
    return root;
}
NODE* findPred(NODE* root) {
    static NODE* pred;
    if(root == NULL) {
        return pred;
    }
    else {
        pred = root;
        return findPred(root->rchild);
    }
}

删除代码:

 typedef struct node {
    int info;
    struct node* lchild;
    struct node* rchild;
}NODE;
NODE* deleteElem(NODE* root, int elem) {
    NODE* save;
    if(root == NULL) {
        printf("Element not in the tree !\n");
        return;
    }
    if(root->info == elem) {
        if(root->rchild == NULL && root->lchild == NULL) {                  // no child
            free(root);
            return NULL;
        }
        else if(root->rchild == NULL || root->lchild == NULL) {             // one child
            if(root->rchild == NULL) {
                save = root->lchild;
                free(root);
                return save;
            }
            else {
                save = root->rchild;
                free(root);
                return save;
            }
        }
        else {                                                             // two children
            save = findPred(root->lchild);
            root->info = save->info;
            root->lchild = deleteElem(root->lchild, root->info);
            return root;
        }
    }
    else if(root->info < elem) {
        root->rchild = deleteElem(root->rchild, elem);
    }
    else if(root->info > elem) {
        root->lchild = deleteElem(root->lchild, elem);
    }
    return root;
}
NODE* findPred(NODE* root) {
    static NODE* pred;
    if(root == NULL) {
        return pred;
    }
    else {
        pred = root;
        return findPred(root->rchild);
    }
}
NODE*deletelem(NODE*root,int-elem){
节点*保存;
if(root==NULL){
printf(“元素不在树中!\n”);
返回;
}
如果(根->信息==元素){
如果(root->rchild==NULL&&root->lchild==NULL){//没有子级
自由根;
返回NULL;
}
如果(root->rchild==NULL | | root->lchild==NULL){//一个子
如果(根->rchild==NULL){
保存=根->lchild;
自由根;
返回保存;
}
否则{
保存=根->rchild;
自由根;
返回保存;
}
}
另外{//两个孩子
save=findPred(root->lchild);
根->信息=保存->信息;
root->lchild=deletelem(root->lchild,root->info);
返回根;
}
}
else if(根->信息rchild=deletelem(root->rchild,elem);
}
否则如果(根->信息>元素){
root->lchild=deletelem(root->lchild,elem);
}
返回根;
}
节点*findPred(节点*root){
静态节点*pred;
if(root==NULL){
返回pred;
}
否则{
pred=根;
返回findPred(root->rchild);
}
}
p.S:对不起,我刚刚注意到您函数的原型声明。我希望更改此代码以匹配原型声明不会太困难。

您可以尝试以下方法:

void delete_tree(struct tree **ptr,int item) 
{ 
      struct tree *move,*back,*temp; 

      if(*ptr==NULL) 
      { 
               printf("nEmpty tree..............n"); 
               return; 
      } 
      else 
      { 
               move=*ptr; 
               back=move;

               while(move->info!=item) 
               { 
                         back=move;

                         if(item<move->info) 
                         {
                                   move=move->left; 
                         } 
                         else 
                         { 
                                  move=move->right; 
                         } 
               }

               if(move->left!=NULL&&move->right!=NULL) 
               {

                         temp=move->right;

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

                         move->info=temp->info; 
                         move=temp; 
               }

               if(move->left==NULL&&move->right==NULL) 
               { 
                         if(back->right==move) 
                         { 
                                  back->right=NULL; 
                         } 
                         else 
                         { 
                                  back->left=NULL; 
                         }

                         free(move); 
                         return; 
               }

               if(move->left==NULL && move->right!=NULL) 
               { 
                         if(back->left==move) 
                         { 
                                  back->left=move->right; 
                         } 
                         else 
                         { 
                                  back->right=move->right; 
                         }

                         free(move); 
                         return; 
               }

               if(move->left!=NULL && move->right==NULL) 
               {
                          if(back->left==move) 
                         { 
                                  back->left=move->left; 
                         } 
                         else 
                         { 
                                  back->right=move->left; 
                         }

                         free(move); 
                         return;
void delete_树(结构树**ptr,int项)
{ 
结构树*移动,*返回,*临时;
如果(*ptr==NULL)
{ 
printf(“nEmpty tree………..n”);
返回;
} 
其他的
{ 
move=*ptr;
后退=移动;
while(移动->信息!=项目)
{ 
后退=移动;
如果(项目信息)
{
移动=移动->左;
} 
其他的
{ 
移动=移动->右;
} 
}
如果(移动->左!=NULL&移动->右!=NULL)
{
温度=移动->向右;
while(临时->左!=NULL)
{ 
背部=温度;
温度=温度->左侧;
}
移动->信息=临时->信息;
移动=温度;
}
如果(移动->左==NULL&&move->右==NULL)
{ 
如果(向后->向右==移动)
{ 
后退->右=空;
} 
其他的
{ 
后退->左=空;
}
自由(移动);
返回;
}
如果(移动->左==NULL&&move->right!=NULL)
{ 
如果(后退->左==移动)
{ 
后退->左=移动->右;
} 
其他的
{ 
后退->向右=移动->向右;
}
自由(移动);
返回;
}
如果(移动->左!=NULL&&move->right==NULL)
{
如果(后退->左==移动)
{ 
后退->向左=移动->向左;
} 
其他的
{ 
后退->右=移动->左;
}
自由(移动);
返回;

您是否尝试过将光标指向父对象?这种方法有问题吗?您似乎知道从哪里开始,只是还没有尝试过。