2016-12-24 4 views
0

CプログラミングでID値と文字列名を持つB +ツリーの構造を記述しようとしています。しかし、私には1つの問題があります。プログラムでは、私は15のIDと名前、すべてのIDの値をソートと表示が、名前がIDで表示されていない、私は理由を知らないが、それを表示する必要があります書く。おそらく私は21行目で何かが見つからない、それはtemp->name = wordであるかもしれないが、ビジュアルプログラムはそれを受け入れない。ここで文字列名のB +ツリーの構造

は私のコードです:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
struct bin_tree 
{ 
    int data; 
    char name[30]; 
    struct bin_tree * right, * left; 
}; 
typedef struct bin_tree node; 

void insert(node **tree, int val, char word[30]) 
{ 
    node *temp = NULL; 
    if(!(*tree)) 
    { 
     temp = (node *)malloc(sizeof(node)); //Dynamic Allocation 
     temp->left = NULL; 
     temp->right = NULL; 
     temp->data = val; 
     temp->name[30]= word; //temp->name = word; - HATA 
     *tree = temp; 
     return; 
    } 
    if(val < (*tree)->data) 
    { 
     insert(&(*tree)->left, val,word); 
    } 
    else if(val > (*tree)->data) 
    { 
     insert(&(*tree)->right, val,word); 
    } 
} 

struct tree *delet(struct bin_tree *ptr, int x) 
{ 
    struct bin_tree *p1,*p2; 
    if(!ptr) 
    { 
     printf("ID not found"); 
     return(0); 
    } 
    else 
    { 
     if(ptr->data < x) 
     { 
      delet(ptr->right,x); //ptr->right=delet(ptr->right,x); 

     } 
     else if (ptr->data >x) 
     { 
      delet(ptr->left,x); //ptr->left=delet(ptr->left,x); 
     } 
     else 
     { 
      if(ptr->data == x) 
      { 
       if(ptr->left == ptr->right) 
       { 
        free(ptr); 
        return(NULL); 
       } 
       else if(ptr->left==NULL) 
       { 
        p1=ptr->right; 
        free(ptr); 
       } 
       else if(ptr->right==NULL) 
       { 
        p1=ptr->left; 
        free(ptr); 
       } 
       else 
       { 
        p1=ptr->right; 
        p2=ptr->right; 
        while(p1->left != NULL) 
         p1=p1->left; 
        p1->left=ptr->left; 
        free(ptr); 
       } 
      } 
     } 
    } 
} 

node* search(node ** tree, int val) 
{ 
    if(!(*tree)) 
    { 
     return NULL; 
    } 

    if(val < (*tree)->data) 
    { 
     search(&((*tree)->left), val); 
    } 
    else if(val > (*tree)->data) 
    { 
     search(&((*tree)->right), val); 
    } 
    else if(val == (*tree)->data) 
    { 
     return *tree; 
    } 
} 

void print_preorder(node * tree) 
{ 

    if (tree) 
    { 
     printf("%d - %s ->",tree->data, tree->name); // Preorder ID - Name çıkmıyor 
     print_preorder(tree->left); 
     print_preorder(tree->right); 
    } 

} 

void print_inorder(node * tree) 
{ 
    if(tree) 
    { 
     print_inorder(tree->left); 
     printf("%d - %s ->",tree->data,tree->name); //Inorder ID - Name çıkmıyor 
     print_inorder(tree->right); 
    } 
} 

void print_postorder(node * tree) 
{ 
    if (tree) 
    { 
     print_postorder(tree->left); 
     print_postorder(tree->right); 
     printf("%d - %s ->",tree->data,tree->name); //Postorder ID - Name çıkmıyor 
    }  
} 

int main() 
{ 
    node *root; 
    node *tmp; 
    int a; //ID for Add func. 
    int item_no; //Id for delete func. 
    int z; //ID for search func. 
    int i=0; 

    char ans,b[100],c; 

    root=NULL; 
    while(ans!='5') 
    { 
     printf("1. Add\n"); 
     printf("2. Delete\n"); 
     printf("3. Search\n"); 
     printf("4. Display\n"); 
     printf("5. Exit\n"); 
     printf("Enter the choice: "); 
     scanf("%s",&ans); 
     switch(ans) 
     { 
     case '1': 

      { 
       printf("Enter the Student ID: "); 
       scanf("%d",&a); 
       printf("Enter the student's name: "); 
       scanf("%d", &c); 
       while((c != '\n' && i<30) 
       { 
        b[i++] = c; 
       } 
       b[i] = '\0'; 
       insert(&root,a,b); 
       i=0; 
       break; 
      } 
     case '2': 
      { 
       printf("Enter the ID: "); 
       scanf(" %d",&item_no); 
       delet(root, item_no); 
       break; 
      } 
     case '3': 
      { 
       printf("Enter the Student ID: "); 
       scanf("%d",&z); 
       tmp = search(&root, z); 
       if (tmp) 
       { 
        printf("Student ID: %d\nStudent name: %s\n", tmp->data, tmp->name); 
       } 
       else 
       { 
        printf("Data not found.\n"); 
       } 
      } 
     case '4': 
      { 
       printf("\nPre Order Display: "); 
       print_preorder(root); 

       printf("\nIn Order Display: "); 
       print_inorder(root); 

       printf("\nPost Order Display: "); 
       print_postorder(root); 
      } 
     case '5': 
      { 
       printf("\nDone."); 
       exit (0); 
      } 
     default : 
      { 
       printf("Wrong choice!\n"); 
       break; 
      } 
     } 
    } 
    return 0; 
} 
+1

[初心者の本](http://stackoverflow.com/questions/)に戻る必要があります。 562303/the-definitive-c-bo ok-guide-and-list)、アレイの章を再読み込みしてください。 –

答えて

1

何をしたい文字をコピーすることです:

strcpy(temp->name, word); 

(あなたがこれも、単一のcharにcharの配列を割り当てるしようとしています配列のサイズを超えてしまいます。nameのサイズは30なので、有効なインデックスの範囲は0から29です)

+0

ありがとうございました、今は動作しています! @ペーター・ヘッセルベルク+1 – psyGhost