2016-12-26 23 views
1

私はCプログラミングを初めて使いました。これは初めての複雑なプログラムの作業です。プログラムは、電話帳(名前と番号)をバイナリ検索ツリーに配置します。発生する問題は、プログラムで使用した再帰とすべてのポインタでエラーが発生する理由です。 私のstruct defもおそらく間違っています。 誰かが私に問題を指摘し、それを解決する方法を教えてくれたら幸せでしょうか。バイナリ検索でのポインタツリーの挿入とCでの検索

typedef struct _bstree { 
    char * name[60]; 
    unsigned long phone; 
    struct bstree *left; 
    struct bstree *right; 

}bstree; 

typedef struct _bst_node { 
    int value; 
    struct node* next; 
}bst_node; 

と、ここでの機能(私は関数やその引数の種類を変更することはできませんよ)です:あなたのツリー構造はleftrightを持っていないのはなぜ

void bst_insert_node(bstree* bst, unsigned long phone, char *name) { 
if (bst == NULL) { 
    bstree *newNode = (bstree *)malloc(sizeof(bstree)); 
    newNode->phone = phone; 
    strcpy(newNode->name,name); 
    newNode->left = NULL; 
    newNode->right = NULL; 
    } 
else if (bst->phone>phone) { 
     bst_insert_node(bst->left,phone, name); //ERROR 
    } 
else { 
bst_insert_node(bst->right, phone, name); //ERROR 
    } 
} 



bst_node* find_node(bstree* bst, unsigned long phone) { 
if (bst==NULL) { 
    printf("Cannot find Number"); 
    return NULL; 
} 
//if phone ist root 
if (phone== bst->phone) 
    return bst;   //ERROR 

bstree* searching = NULL; 
//left search 
searching = find_node(bst->left,phone); //ERROR 
if (searching) 
    return searching;  //ERROR 

// right search 
searching = find_node(bst->right,phone);  //ERROR 
if (searching) 
    return searching; //ERROR 

if (searching) 
    return searching;  //ERROR 

return NULL; 
} 
+1

を試みる 'のchar *名[60];' - > 'char型の名前[60];' – BLUEPIXY

+0

これは、いくつかのエラー感謝を修正しました。 – Baldr

+0

'bstree'と' bst_node'は異なるタイプです。 – BLUEPIXY

答えて

0

このコードバディ

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

typedef struct _bst_node 
{ 
    char   name[60]; 
    unsigned long phone; 
} bst_node; 

typedef struct _bstree 
{ 
    bst_node key; 
    struct _bstree * left; 
    struct _bstree * right; 
} bstree; 


static inline bstree * create_node(unsigned long phone, char * name) 
{ 

     bstree * newNode = (bstree *) malloc(sizeof(bstree)); 
     newNode->key.phone = phone; 
     strcpy(newNode->key.name, name); 
     newNode->left = NULL; 
     newNode->right = NULL; 

     return newNode; 
} 

void bst_insert_node(bstree * bst, unsigned long phone, char * name) 
{ 
    if (bst == NULL) 
    { 
     return; 
    } 

    if (bst->key.phone > phone) 
    { 
     if (bst->left == NULL) 
     { 
      bst->left = create_node(phone, name); 
      return; 
     } 

     bst_insert_node(bst->left, phone, name); 
    } 
    else 
    { 
     if (bst->right == NULL) 
     { 
      bst->right = create_node(phone, name); 
      return; 
     } 

     bst_insert_node(bst->right, phone, name); 
    } 
} 



bst_node * find_node(bstree* bst, unsigned long phone) 
{ 
    if (bst == NULL) 
    { 
     return NULL; 
    } 

    if(bst->key.phone > phone) 
    { 
     return find_node(bst->left, phone); 
    } 
    else if (bst->key.phone < phone) 
    { 
     return find_node(bst->right, phone); 
    } 

    return &(bst->key); 
} 


void print_tree(bstree * bst, int level) 
{ 
    int temp = 0; 
    while(temp < level) 
    { 
     printf("-"); 
     ++temp; 
    } 

    printf(" (%ld-%s)\n", bst->key.phone, bst->key.name); 

    if (bst->left != NULL) 
    { 
     print_tree(bst->left, level + 1); 
    } 

    if (bst->right != NULL) 
    { 
     print_tree(bst->right, level + 1); 
    } 
} 
+0

ありがとう!私はこのためにエラーがない:)私は2つのより多くの関数を終了し、削除しない限り、コンパイルして見ることができません。あなたを更新します!もう一度ありがとう:) – Baldr

+0

あなたは大歓迎です@AhmedBaldrあなたがより多くの助けを必要とする場合、より多くのコメントを投稿してください – DrPrItay

+0

問題の仲間、私はあなたのためのコードをいくつか投稿します – DrPrItay

2
typedef struct _bstree { 
char * name[60]; 
unsigned long phone; 
struct bstree *left; 
struct bstree *right; 
}bstree; 

。ツリーのノードには、ツリー自体ではなく左右が必要です。ツリー構造にはrootノードが必要です。

typedef struct _bst_node { 
char name[60]; 
unsigned long phone; 
struct _bst_node *left; 
struct _bst_node *right; 
}bst_node; 

、その後、ツリー構造

typedef struct _bstree { 
bst_node *root; //this will point to the root node of the tree and will be NULL if the tree is emty. 
}bstree; 

あなたinsert()関数は、入力としてbstreeを取り、あなたのルートがbsttree::rootなくbsttreeそのものであるtree.Remeberに新しいbst_nodeを挿入する必要があります。

void bst_insert_node(bstree* bst, unsigned long phone, char *name) 
{ 
     //insert new node 
} 
関連する問題