在二叉树中插入字符串

时间:2013-05-17 23:46:36

标签: c

我需要使用c创建一个程序(使用二叉树)这个程序需要保存学生的名字和id,并允许你按顺序插入,删除,搜索和显示名称,按顺序和后期顺序,我写了代码但它没有工作有一个问题,在插入只需要id,它不采取名称,我想知道这个代码的问题是什么

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


struct bin_tree
 {
int data;
char  name[100];
struct bin_tree * right, * left;
};
 typedef struct bin_tree node;

  void insert(node ** tree, int val, char  word[100])
{
node *temp = NULL;
if(!(*tree))
{
    temp = (node *)malloc(sizeof(node));
    temp->left = temp->right = NULL;
    temp->data = val;
    temp->data =  word;
    *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("\n student not found ");
    return(ptr);
}
else
{
    if(ptr->data < x)
    {
        ptr->right = delet(ptr->right,x);

    }
    else if (ptr->data >x)
    {
        ptr->left=delet(ptr->left,x);
        return ptr;
    }
    else
    {
        if(ptr->data == x)
        {
            if(ptr->left == ptr->right)
            {
                free(ptr);
                return(NULL);
            }
            else if(ptr->left==NULL)
            {
                p1=ptr->right;
                free(ptr);
                return p1;
            }
            else if(ptr->right==NULL)
            {
                p1=ptr->left;
                free(ptr);
                return p1;
            }
            else
            {
                p1=ptr->right;
                p2=ptr->right;
                while(p1->left != NULL)
                    p1=p1->left;
                p1->left=ptr->left;
                free(ptr);
                return p2;
            }
        }
    }
}
return(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\n%s\n",tree->data,tree->name);
    print_preorder(tree->left);
    print_preorder(tree->right);
}

 }

  void print_inorder(node * tree)
 {
if (tree)
{
    print_inorder(tree->left);
    printf("%d\n%s\n",tree->data,tree->name);
    print_inorder(tree->right);
}
}

 void print_postorder(node * tree)
{
if (tree)
{
    print_postorder(tree->left);
    print_postorder(tree->right);
    printf("%d\n%s\n",tree->data,tree->name);
}
 }



 void main()
 {
node *root;
node *tmp;
int a,item_no,z,i=0;
char  x,b[100],c;

root=NULL;
while(x!='5')
{
    printf("\nmenu\n----\n1. insert\n2. delete\n3. search\n4. display\n5. end program     \n\n");

    printf("\nEnter the choice:");
    scanf("%s",&x);
    switch(x)
    {
    case '1':
    {
        printf("\nEnter the id: ");
        scanf("%d",&a);
        printf("\nenter the name: ");
        while ( ( c = getchar() ) != '\n' && i < 100 )
        {
            b[ i++ ] = c;
        }
        b[ i ] = '\0';
        insert(&root,a,b[100]);
        break;
    }
    case '2':
    {
        printf("\n Enter the student id to be deleted : ");
        scanf(" %d",&item_no);
        root=delet(root,item_no);
        break;
    }
    case '3':
    {
        printf("\nEnter student id: ");
        scanf("%d",&z);
        tmp = search(&root, z);
        if (tmp)
        {
            printf("\nstudent id=%d\nstudent name: %s\n", tmp->data,tmp->name);
        }
        else
        {
            printf("\nData Not found.\n");
        }
    }
    case '4':
    {
        printf("Pre Order Display\n");
        print_preorder(root);

        printf("In Order Display\n");
        print_inorder(root);

        printf("Post Order Display\n");
        print_postorder(root);
    }
    case '\t':
    case '\n':
    case ' ':
    default :{
    printf("\nwrong entery!\n");
    break;
    }

    }


}

return 0;
 }
 }

1 个答案:

答案 0 :(得分:0)

好吧,看看你的插入功能。

void insert(node ** tree, int val, char  word[100])
{
    node *temp = NULL;
    if(!(*tree))
    {
        temp = (node *)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        temp->data = val;
        temp->data =  word;
        *tree = temp;
        return;
    }
 ...

我认为问题是temp->data = word。我的编译器(gcc)生成警告assignment makes integer from pointer without a cast。 word是char数组,data是int。我假设你想要temp->name = word

因为没有大量警告就无法编译。我认为你应该首先尝试编译它,因为我认为还有其他地方会导致编译错误或警告。

如果您使用gcc,请添加标记-Wall -Wextra以显示可能有助于修复错误的大量警告。


我们如何看待我们称之为插入的位置。

case '1':
{
    printf("\nEnter the id: ");
    scanf("%d",&a);
    printf("\nenter the name: ");
    while ( ( c = getchar() ) != '\n' && i < 100 )
    {
        b[ i++ ] = c;
    }
    b[ i ] = '\0';
    insert(&root,a,b[100]);
    break;
}

在这里你传递b [100]作为单词。

  1. b定义为char b[100],因此当您在数组外部访问时,调用b [100]是未定义的行为。
  2. b [100]返回一个char,然后将其传递给正在寻找数组的函数。您应该将其称为insert(&root,a,b)
  3. 启用编译器警告。