[英]C binary search tree with custom data
Suppose I got an external library bst that handles custom data types insertion in a bst 假设我有一个外部库bst来处理bst中的自定义数据类型插入
Here are the new_node,insert and search functions : 这是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;
}
and the main.c ,suppose i read the models from a txt file : 和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;
}
So my question arises in the search function, how can i manage a comparator on custom data (let's say insert the models ordered by name (strcmp) ? I'm very confused on how can i pass the names to the bst.c given that bst.c has no idea how my model struct is made. 所以我的问题出现在搜索功能中,如何管理自定义数据上的比较器(假设插入按名称排序的模型(strcmp)?)我很困惑如何将名称传递给bst.c bst.c不知道如何制作我的模型结构。
Should I modify the bst library and maybe on bst struct add before data some sort of index and use that as comparator ? 我应该修改bst库,或者在bst struct上添加数据之前的某种索引并将其用作比较器吗?
OK I've managed to fix that by adding a string key inside the struct bst 好的,我设法通过在struct bst中添加一个字符串键来解决此问题
What I'm trying to achieve now is to return the void* data type casted into struct model, suppose _I got the tree with nodes containing the data, once I do a search I'd like to return for example the data contained in a node and work on it, any clues ???? 我现在想要实现的是返回转换为struct模型的void *数据类型,假设_我得到了带有包含数据的节点的树,一旦执行搜索,我想返回例如包含在a中的数据。节点并进行处理,有任何线索吗???
tried someting like without any success suppose node is a returned node from a search function 尝试过类似的尝试,但没有成功,假设节点是搜索功能返回的节点
model *m;
m=(model*)node->data;
how could I achieve this? 我怎么能做到这一点?
Example for using compare functions as callbacks. 使用比较函数作为回调的示例。 Definitions omitted for brevity.
为了简洁起见,省略了定义。
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: the above snippet handles linked lists, but the mechanism for passing function pointers is the same as with trees, of course. 顺便说一句:上面的代码片段处理链表,但是,传递函数指针的机制与树相同。 And after all it was homework ;-)
毕竟这是家庭作业;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.