简体   繁体   English

插入节点(二叉搜索树)C

[英]inserting node (Binary search tree) C

I'm trying to insert a node in a binary search tree and I'm getting a little problem. 我正在尝试在二进制搜索树中插入节点,但遇到了一些小问题。

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>


typedef struct Node{
    char name[100];
    struct Node *pGauche;
    struct Node *pDroit;
}Node;

void getName(char[]);
void copy(Node **, Node *,char[]);
void menu(Node **);
void add(Node **);
void search(char[],Node**, Node **,Node **);
void print(Node **);
void inOrder(Node *);

void main(void)
{
    Node *root = NULL;
    menu(&root);
    system("pause");
}

void menu(Node **root)
{
    for (int i=0;i<10;i++)
    {
        add(root);
    }
    print(root);
}

void add(Node **root)
{
    char name[100];
    getName(name);
    Node *p = NULL;
    Node *savep = NULL;
    search(name,root,&p,&savep);
    copy(root,savep,name);
}

void search(char name[],Node **root, Node **p, Node **savep)
{
    *p = *root;

    while ((p == NULL) && (strcmp((*p)->name,name) != 0))
    {
        *savep = *p;

        if (strcmp(name,(*p)->name) < 0)
            *p = (*p)->pGauche;
        else
            *p = (*p)->pDroit;
    }

}

void getName(char name[])
{
    printf("What name do you want to add\n");
    scanf("%s",name);
    fflush(stdin);

}

void copy(Node **root, Node *savep, char name[])
{
    Node *newp = (Node *) malloc(sizeof(Node*));
    newp->pDroit = NULL;
    newp->pGauche = NULL;

    strcpy(newp->name,name);
    printf("%s",newp->name);


    if (*root == NULL)
        *root = newp;
    else
    {
        if (strcmp(name,savep->name) < 0) 
            savep->pGauche = newp;
        else
            savep->pDroit = newp;
    }
    free(newp);
}

void print(Node ** root)
{
    Node *p = *root;
    inOrder(p);
}

void inOrder(Node *p)
{
    if (p != NULL)
    {
        inOrder(p->pGauche);
        printf("%s\n",p->name);
        inOrder(p->pDroit);
    }
}

I know there are some really odd function and useless functions, but this just a "test" for a slightly bigger school project so it will get useful in time, right now I would just like to get the binary tree working ! 我知道确实有一些奇怪的功能和无用的功能,但这只是一个稍大一点的学校项目的“测试”,因此它会及时变得有用,现在我想让二叉树开始工作!

So basically the problem is that I'm getting a "Access violation reading location" after I type in the second name... I'm guessing when doing the strcmp, but I'm really not sure :/ 所以基本上的问题是,我键入第二个名字后得到了一个“访问冲突读取位置”……我猜是在做strcmp时,但是我真的不确定:/

I'd really be glad if someone could help me getting this running :) 如果有人可以帮助我运行它,我将非常高兴:)

This is incorrect: 这是不正确的:

while ((p == NULL) && (strcmp((*p)->name,name) != 0))

and will result in a NULL pointer being dereferenced, which is undefined behaviour. 并导致取消引用NULL指针,这是未定义的行为。 Change to: 改成:

while (*p && strcmp((*p)->name,name) != 0)

This is incorrect: 这是不正确的:

Node *newp = (Node *) malloc(sizeof(Node*));

as it is only allocating enough for a Node* , when it needs to be allocating a Node . 因为它只需要为Node*分配足够的空间,所以它需要分配Node Change to: 改成:

Node *newp = malloc(sizeof(*newp));

and don't free() it in the same function as it is required later. 并且不要在以后需要的相同功能中free()它。 free() ing the Node means the list has dangling pointers and dereferencing one is undefined behaviour, and a probable cause of the access violation. Node free()意味着该列表具有悬挂的指针,并且取消引用一个指针是未定义的行为,并且可能是访问冲突的原因。


Note: 注意:

fflush(stdin);

is undefined behaviour. 是不确定的行为。 From the fflush() reference page: fflush()参考页:

Causes the output file stream to be synchronized with the actual contents of the file. 使输出文件流与文件的实际内容同步。 If the given stream is of the input type, then the behavior of the function is undefined. 如果给定的流属于输入类型,则该函数的行为是不确定的。

A couple of things to get you started. 有几件事可以帮助您入门。 I haven't looked into it too deeply, so you will probably have to continue to drill down into more issues, but fix these things just to get you started: 我没有对它进行太深入的研究,因此您可能必须继续深入研究更多问题,但为了使您入门,请修复以下问题:

In this code in search() : search()这段代码中:

    while ((p == NULL) && (strcmp((*p)->name,name) != 0))

The p parameter will never be NULL. p参数永远不会为NULL。 So, the while loop is never entered. 因此,永远不会进入while循环。 This means that savep would not get set to any value, and is NULL when you call copy() in your add() function. 这意味着savep将不会设置为任何值,并且在add()函数中调用copy()时为NULL。 The copy() function then dereferences the invalid pointer reference, which caused the problem you observed. 然后, copy()函数取消引用无效的指针引用,这导致了您观察到的问题。

You actually want to test to see if *p is NOT NULL. 您实际上想测试看看*p是否不是 NULL。 This allows you to legally dereference it. 这使您可以合法地取消引用它。

    while ((*p != NULL) && (strcmp((*p)->name,name) != 0))

Secondly, as hmjd identified, you do not allocate enough memory for your node inside copy() . 其次,正如hmjd标识的,您没有为copy()节点分配足够的内存。

    Node *newp = (Node *) malloc(sizeof(Node*));

You are only allocating enough memory for one pointer, not for an entire node. 您只为一个指针分配了足够的内存,而不为整个节点分配了足够的内存。 Also, you should not cast the return value of malloc() when coding in C (it will hide a bug that can lead to a crash in the worst case). 另外,在C语言中进行编码时,请勿转换malloc()的返回值(它会隐藏可能导致最坏情况崩溃的错误)。

    Node *newp = malloc(sizeof(Node));

Thirdly, you need to retain the memory you allocate for your nodes rather than freeing them immediately after inserting it at the end of copy() : 第三,您需要保留为节点分配的内存,而不是在将其插入copy()末尾后立即释放它们:

    // I need this memory for my tree!
    //free(newp);

If you call free() like you did, then your tree will be pointing into freed memory, and to access them would cause undefined behavior. 如果像您一样调用free() ,那么您的tree将指向已释放的内存,访问它们将导致未定义的行为。

One minor thing: You shouldn't do fflush(stdin) , as fflush() is only for output streams. 一件小事:您不应该执行fflush(stdin) ,因为fflush()仅用于输出流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM