【问题标题】:How to find the closest or exact value in a binary search tree in C如何在 C 中的二叉搜索树中找到最接近或精确的值
【发布时间】:2020-12-19 20:15:53
【问题描述】:

我创建了一个 C 程序,其中一个充满 doubles 的排序数组被转换为二叉搜索树。创建 BST 后,我想找到最接近我的键的值,即 1.0。一旦达到最接近的值,我的程序就会崩溃。但是,如果我的密钥是可以在 BST 中找到的精确值,它可以正常工作并打印“找到”消息。有谁知道我该如何解决这个问题?

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

struct node { 
    double data; 
    struct node* left; 
    struct node* right; 
};

struct node* createnewnode(double data);
struct node* bstcreate(double* arr, int start, int end); 
struct node* search(struct node* root, double key);

int main(int argc, char *argv[]) {
    
    double array[] = {-4.526682861, -3.682840076, -2.953453251,-1.709721126, -0.936102616, 
    0.18335189, 1.038874246, 2.618022636, 3.259094511, 4.198243959};
    
    int index = sizeof(array) / sizeof(array[0])-1;
    //printf("%d\n", index);
    
    struct node *root = bstcreate(array, 0, index);
    search(root, 1.0);
    //search(root, 1.538874246);
    return 0;
}

struct node* bstcreate(double* arr, int start, int end) 
{ 
    if (start > end) 
      return NULL; 
  
    int mid = (start + end)/2; 
    struct node *root = createnewnode(arr[mid]); 

    root->left =  bstcreate(arr, start, mid-1); 
    root->right = bstcreate(arr, mid+1, end); 
  
    return root; 
} 
  
struct node* createnewnode(double data)  
{ 
    struct node* node = (struct node*)malloc(sizeof(struct node)); 
    node->data = data; 
    node->left = NULL; 
    node->right = NULL; 
  
    return node; 
}

struct node* search(struct node* root, double key) {
    printf("%lf\n",root->data); 
    
    if (root == NULL || root->data == key) {
        printf("Found: %lf\n", root->data);
        return root; 
    }
    
    else if (root->data < key) {
        return search(root->right, key);
    }
    else {
        return search(root->left, key); 
    }
}

这是程序崩溃前输出的内容:

【问题讨论】:

  • 为什么会崩溃?
  • 我不确定。这就是我问这个问题的原因。
  • 好吧,想想这个。您的搜索仅基于root == NULL 或完全匹配(后者不会发生)。首先,考虑一下第一个条件如何为真的疯狂,以及您在检查之前立即做什么。 IE。 printf("%lf\n", root-&gt;flux);。嗯....就此而言,即使将其取出,请考虑一下里面检查的内容。同样的问题,printf("Found: %lf\n", root-&gt;flux);rootNULL 时,您认为其中其中一个 会做什么,这很有可能(实际上用您的样本数据保证)?
  • 你应该学会使用调试器,看看它为什么会崩溃。
  • 如果 rootNULL,您可以取消引用它(例如,您完全可以执行 root-&gt;data)。要解决此问题,请在 search 中将其添加为 first 行:if (root == NULL) return root; 这应该可以解决问题。之后,您可以删除任何具有root == NULLroot != NULLif 子句,因为在第一个[new] 行之后,root 不能是NULL [因为我们已经检查过了]

标签: arrays c data-structures struct binary-search-tree


【解决方案1】:

这是正确的搜索功能:

struct node* search(struct node* root, double key) {
     
    if (root == NULL) {
        return root; 
    }
    printf("%lf\n",root->data);
    
    if (root->data == key) {
        printf("Found: %lf\n", root->data);
        return root; 
    }
    else if (root->data < key) {
        return search(root->right, key);
    }
    else {
        return search(root->left, key); 
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2021-01-28
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多