【问题标题】:Delete node from BST in C从 C 中的 BST 中删除节点
【发布时间】:2016-10-01 11:27:39
【问题描述】:

我试图了解这个在线创建的功能,用于从 BST 中删除节点。有些事情我看不懂

这是代码:

struct Node* Delete(struct Node *root, int data) {
  if (root == NULL) {
     return NULL;
  }
  if (data > root->data) {  // data is in the left sub tree.
      root->left = Delete(root->left, data);
  } else if (data > root->data) { // data is in the right sub tree.
      root->right = Delete(root->right, data);
  } else {
     // case 1: no children
     if (root->left == NULL && root->right == NULL) {
        delete(root); // wipe out the memory, in C, use free function
        root = NULL;
     }
     // case 2: one child (right)
     else if (root->left == NULL) {
        struct Node *temp = root; // save current node as a backup
        root = root->right;
        delete temp;
     }
     // case 3: one child (left)
     else if (root->right == NULL) {
        struct Node *temp = root; // save current node as a backup
        root = root->left;
        delete temp;
     }
     // case 4: two children
     else {
        struct Node *temp = FindMin(root->right); // find minimal value of right sub tree
        root->data = temp->data; // duplicate the node
        root->right = Delete(root->right, temp->data); // delete the duplicate node
     }
  }
  return root; // parent node can update reference
}

问题:

1) 为什么会这样

if (data > root->data) {  // data is in the left sub tree.
          root->left = Delete(root->left, data);

不应该是if(data < root->data) 吗? (后面的两行代码也一样)

2)函数返回一个指向节点的指针,这是否意味着在主函数中我必须做这样的事情?

int main(){
struct Node *tree=malloc(sizeof(Node));
...
struct Node *new_tree=malloc(sizeof(Node));
new_tree= Delete(tree,24);

所以函数用新树替换旧树,没有节点的 val 24?如果我希望函数是 void 类型,我应该使用双指针吗?

【问题讨论】:

    标签: c binary-search-tree nodes


    【解决方案1】:

    对于您的第一个问题,您应该是:if(data < root->data)。 第二个问题不完全是。您显然应该定义一个指针头,它是树的头,并创建一个将数据插入 bst 的函数,所以这个函数执行 malloc。您在 main 中需要的只是头指针在开始时初始化为 NULL,因此它应该如下所示:

    int main(){
    struct Node *tree=NULL;
    int number=...; 
    ...
    input_to_bst(&tree,number);
    ...
    new_tree= Delete(tree,24);
    

    另请注意,新树不需要具有 malloc,因为您的函数返回一个已经显示到结构的指针,而您所做的是 new_tree 也将指向该结构。

    对于你的最后一个问题,是的,当然你可以传递双指针(实际上我在 input_to_bst(&tree); 的定义中遵循了这种方式)。

    函数 input_to_bst 定义的示例可以是:

    void input_to_bst(treeptr* head,int number){
      if((*head)==NULL){
           (*head)=(treeptr)malloc(sizeof(struct tree));
           (*head)->data=number;
           (*head)->left=NULL;
           (*head)->right=NULL;
      }
      else{
          if(((*head)->data)>number) input_to_bst(&((*head)->left),number);
          else (((*head)->data)<number) input_to_bst(&((*head)->right),number);
      }
    }
    

    我们假设我们已经定义了结构:

    struct tree{
        int data;
        struct tree* right;
        struct tree* left;
    };
    
    typedef struct tree* treeptr;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2021-10-23
      • 2014-05-26
      • 2016-11-11
      • 2021-04-06
      相关资源
      最近更新 更多