2016-12-26 26 views
1

Ich bin neu in C-Programmierung und das ist mein erstes Mal an einem komplizierten Programm arbeiten. Das Programm speichert ein Telefonbuch (Name und Nummer) in einem binären Suchbaum. Die Frage, die auftaucht, ist, warum ich einen Fehler mit der Rekursion und allen Zeigern erhalte, die ich in dem Programm verwendete. Mein struct def ist auch wahrscheinlich falsch .. Würde mich freuen, wenn mir jemand auf das Problem hinweisen und wie man es lösen könnte.Zeiger in binäre Suche Baumeinfügungen und Suche in 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; 

und hier sind die Funktionen (Ich bin die Art der Funktionen oder ihre Argumente ändern nicht erlaubt):

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 * name [60];' -> 'char name [60];' – BLUEPIXY

+0

Diese feste wenige Fehler durch. – Baldr

+0

'bstree' und' bst_node' sind unterschiedliche Typen. – BLUEPIXY

Antwort

0

versuchen diesen Code Buddy

#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

Vielen Dank! Ich habe keine Fehler dafür :) Nur kann nicht kompilieren und sehen, wenn ich zwei weitere Funktionen frei und entfernen. Wird dich aktualisieren! Nochmals vielen Dank :) – Baldr

+0

Sie sind willkommen @AhmedBaldr weitere Kommentare, wenn Sie weitere Hilfe benötigen – DrPrItay

+0

kein Problem Kumpel, ich werde den Code für Sie in ein paar – DrPrItay

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

Warum Ihre Baumstruktur haben left und right. Die Knoten des Baums sollten links und rechts und nicht der Baum selbst sein. Die Baumstruktur sollte nur einen root Knoten haben.

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

und dann die Baumstruktur

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; 

Ihre insert() Funktion sollte bstree als Eingabe und eine neue bst_node im tree.Remeber geben Sie Ihre Wurzel ist bsttree::root und nicht bsttree selbst.

void bst_insert_node(bstree* bst, unsigned long phone, char *name) 
{ 
     //insert new node 
}