2012-02-26 46 views
0

は、私がここでBSTCバイナリ検索ツリー

でカスタムデータ型の挿入を扱う外部ライブラリのBSTを得たと仮定しnew_node、挿入して、検索機能です:

//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; 
} 

とメイン.C、私はtxtファイルからモデルを読み込むと仮定します。だから私の質問は、(のは、NAMが注文したモデルを挿入しましょう私は、カスタムデータにコンパレータを管理することができますどのように、検索機能で発生

#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; 
} 

e(strcmp)? 私は非常にどのようにbst.cに名前を渡すことができます混乱しているbst.c私のモデル構造体がどのように作られているか分かりません。

bstライブラリを変更する必要がありますか?おそらくbst構造体の前にデータを追加してインデックスを作成し、コンパレータとして使用しますか?

私は、私が今実現しようとしているどのような構造体BST

内の文字列のキーを追加することにより、構造体モデルにキャストvoid *型データタイプを返すことであることを修正するために管理し仮定する ましOK _私はデータを含むノードをツリーに持っています。検索を実行すると、ノードに含まれているデータの例である が返されます。

は 仮定するノードがどのように私はこれを達成できる検索機能から返されたノード

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

ある任意の成功なしのようにいろいろ書いてみましたか?

+0

これは、比較が必要なすべての関数の余分な引数で関数ポインタとして渡すことができます。 – wildplasser

+0

またはbst作成時に関数ポインタを渡すことができます – Kevin

+0

正確に。 (しかし、それは特別な「頭部」型の必要性を生むだろう)そこに行く。あなたのサービスで.... – wildplasser

答えて

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; 
} 

はところで:上記のスニペットはリンクリストを処理しますが、関数ポインタを渡すためのメカニズムはもちろんの木、と同じです。結局それは宿題でした;-)

+0

私はbst.c関数の中でmodel.nameを管理する方法を見つけることができませんが、voidポインタを使うべきですが、例えばモデル項目を渡すときにnew_node関数を呼び出すときなど私はそのitem.nameを比較する方法のアイデアがありません –

+0

あなたのmain()ではmが(構造体)モデルであると宣言しています。それをポインタに変更し、必要に応じてmのスペースをmalloc(fscanf()ループ内で)します。insert関数も変更する必要があります(すでにvoid *ポインタを引数として取ります)。そしてcompare関数を作成してください。これはおそらく、私のスニペットに似た、構造体内の名前を比較するでしょう。 – wildplasser