Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二叉搜索树中的指针插入和C中的搜索_C_Pointers_Recursion_Binary Search Tree - Fatal编程技术网

二叉搜索树中的指针插入和C中的搜索

二叉搜索树中的指针插入和C中的搜索,c,pointers,recursion,binary-search-tree,C,Pointers,Recursion,Binary Search Tree,我是C编程新手,这是我第一次编写复杂的程序。该程序将把一个电话簿(姓名和号码)放在一个二进制搜索树中。出现的问题是,为什么递归和程序中使用的所有指针都会出错。 我的struct def也可能是错误的。。 如果有人能告诉我这个问题以及如何解决它,我会很高兴的 typedef struct _bstree { char * name[60]; unsigned long phone; struct bstree *left; struct bstree *right;

我是C编程新手,这是我第一次编写复杂的程序。该程序将把一个电话簿(姓名和号码)放在一个二进制搜索树中。出现的问题是,为什么递归和程序中使用的所有指针都会出错。 我的struct def也可能是错误的。。 如果有人能告诉我这个问题以及如何解决它,我会很高兴的

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;
下面是函数(我不允许更改函数的类型或它们的参数):

为什么树结构有
。树的节点应该有左和右,而不是树本身。树结构应该只有一个
节点

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
作为输入,并在树中插入一个新的
bst\u节点
。请记住,您的根是
bstree::root
,而不是
bstree
本身

 void bst_insert_node(bstree* bst, unsigned long phone, char *name)
 {
      //insert new node
 }
试试这个代码,伙计

#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);
    }
}
#包括
#包括
#包括
类型定义结构(bst)节点
{
字符名[60];
无符号长电话;
}bst_节点;
类型定义结构树
{
bst_节点密钥;
结构树*左;
结构树*右;
}B树;
静态内联bstree*创建_节点(无符号长电话,字符*名称)
{
bstree*newNode=(bstree*)malloc(sizeof(bstree));
newNode->key.phone=phone;
strcpy(newNode->key.name,name);
newNode->left=NULL;
newNode->right=NULL;
返回newNode;
}
void bst\u insert\u节点(bstree*bst,未签名长电话,char*name)
{
如果(bst==NULL)
{
返回;
}
如果(bst->key.phone>phone)
{
如果(bst->left==NULL)
{
bst->left=创建_节点(电话、姓名);
返回;
}
bst_插入_节点(bst->左侧,电话,姓名);
}
其他的
{
如果(bst->right==NULL)
{
bst->right=创建_节点(电话、姓名);
返回;
}
bst_插入_节点(bst->右侧,电话,姓名);
}
}
bst_节点*查找节点(bstree*bst,无符号长电话)
{
如果(bst==NULL)
{
返回NULL;
}
如果(bst->key.phone>phone)
{
返回查找节点(bst->左侧,电话);
}
否则如果(bst->key.phone右侧,电话);
}
返回&(bst->键);
}
无效打印树(bstree*bst,整数级)
{
内部温度=0;
同时(温度<水平)
{
printf(“-”);
++温度;
}
printf((%ld-%s)\n),bst->key.phone,bst->key.name);
如果(bst->left!=NULL)
{
打印树(bst->左侧,级别+1);
}
如果(bst->right!=NULL)
{
打印树(bst->右侧,级别+1);
}
}

char*name[60]-->
字符名[60]修复了这几个错误,多亏了。
bstree
bst_节点
是不同的类型。
typedef struct bstree{…}bstree在我看来,您并没有在节点和树之间进行太多区分!我没有收到任何错误:)只是无法编译和查看,除非我完成另外两个函数free和remove。我会更新你的!再次非常感谢:)欢迎@AhmedBaldr发表更多评论如果你需要更多帮助没问题,伙计,我会在几分钟内为你发布代码。为此,我创建了一个新帖子:)
 void bst_insert_node(bstree* bst, unsigned long phone, char *name)
 {
      //insert new node
 }
#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);
    }
}