首页 文章

带有自定义数据的C二叉搜索树

提问于
浏览
0

假设我有一个外部库bst,用于处理bst中的自定义数据类型插入

以下是new_node,insert和search函数:

//new node

struct bst_node* new_node(void* data) 
{
    struct bst_node* result = malloc(sizeof(struct bst_node));
    assert(result);

    result->data = data;
    result->left = result->right = NULL;
    return result;
}

//insert node

void insert(struct bst_node** root, void* data) {
    struct bst_node** node = search(root, data);
    if (*node == NULL) {
        *node = new_node(data);
    }
}

//search node

    struct bst_node** search(struct bst_node** root, void* data) {
        struct bst_node** node = root;
        while (*node != NULL) {

        if (data, (*node)->data < 0)
            node = &(*node)->left;
        else if (compare_result > 0)
            node = &(*node)->right;
        else
            break;
    }
    return node;
}

和main.c,假设我从txt文件中读取模型:

#include <stdio.h>
#include <stdlib.h>
#include "bst.h"
#include <string.h>

#define MAX 50

typedef struct data_t{
    int gg,mm,aaaa;    
}data;

typedef struct accesories_t{
    char name[MAX];
    int price;
    struct accesories_t *next;    
}accesories;

typedef struct model_t{
    //int index;
    char name[MAX];
    char file_a[MAX];
    data date;
    int price;
    accesories *acs;
}model;


int main(int argc, char *argv[])
{
    int menu=0;

    char nf[MAX];

    char name[MAX];
    char fa[MAX];
    int price,gg,mm,a;

    strcpy(nf,argv[1]);

    FILE *fp=fopen(nf,"+r");
    model m;
    struct bst_node* root = NULL;

    while(fscanf(fp,"%s %d//%d//%d %d %s",name,gg,mm,a,price,fa)!=EOF){
        strcpy(m.name,name);
        strcpy(m.file_a,fa);
        m.date.gg=gg;
        m.date.mm=mm;
        m.date.aaaa=a;
        m.price=price;
        m.index=index++;  

        insert(&root ,m);  
    }

  system("PAUSE");  
  return 0;
}

所以我的问题出现在搜索功能中,我如何管理自定义数据上的比较器(假设插入按名称排序的模型(strcmp)?我对如何将名称传递给bst.c非常困惑,因为bst.c不知道我的模型结构是如何制作的 .

我应该修改bst库,也许在bst struct add之前添加一些索引并将其用作比较器吗?

好吧我已经设法通过在struct bst中添加一个字符串键来解决这个问题

我现在想要实现的是将 void data* 类型返回到结构模型中,假设_I得到了包含数据的节点的树,一旦我进行搜索,我想返回例如节点中包含的数据和工作,任何线索????

如果节点是来自搜索功能的返回节点,那么尝试一样就没有任何成功

model *m;
m=(model*)node->data;

我怎么能实现这个目标?

1 回答

  • 0

    使用比较函数作为回调的示例 . 为简洁起见省略了定义 .

    int llist_cmp(struct llist *l, struct llist *r)
    {
    if (!l) return 1;
    if (!r) return -1;
    return strcmp(l->payload,r->payload);
    }
    
    struct llist * llist_split(struct llist **hnd, int (*cmp)(struct llist *l, struct llist *r) )
    {
    struct llist *this, *save, **tail;
    
    for (save=NULL, tail = &save; this = *hnd; ) {
            if (! this->next) break;
            if ( cmp( this, this->next) <= 0) { hnd = &this->next; continue; }
            *tail = this->next;
            this->next = this->next->next;
            tail = &(*tail)->next;
            *tail = NULL;
            }
    return save;
    }
    
    struct llist * llist_merge(struct llist *one, struct llist *two, int (*cmp)(struct llist *l, struct llist *r) )
    {
    struct llist *result, **tail;
    
    for (result=NULL, tail = &result; one && two; tail = &(*tail)->next ) {
            if (cmp(one,two) <=0) { *tail = one; one=one->next; }
            else { *tail = two; two=two->next; }
            }
    *tail = one ? one: two;
    return result;
    }
    

    BTW:上面的代码片段处理链表,但是传递函数指针的机制当然与树相同 . 毕竟这是家庭作业;-)

相关问题