简体   繁体   English

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

[英]Pointers in Binary Search Tree insertions and search in C

I'm new to C programming and this is my first time working on a complicated program. 我是C编程的新手,这是我第一次处理复杂的程序。 The program will put a phonebook (name and number) in a binary search tree. 该程序会将电话簿(名称和号码)放入二进制搜索树中。 The question that arises is why I get an error with the Recursion and all pointers that I used in the program. 出现的问题是为什么我在递归和程序中使用的所有指针都出错。 My struct def is also probably wrong.. Would be happy if anyone could point me to the problem and how to solve it. 我的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;

and here are the functions (I'm not allowed to change the type of the functions or their arguments): 这里是函数(不允许更改函数的类型或它们的参数):

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;
}
typedef struct _bstree {
char * name[60];
unsigned long phone;
struct bstree *left;
struct bstree *right;
}bstree;

Why does your tree structure have left and right .The nodes of the tree should have left and right and not the tree itself.The tree structure should just have a root node. 为什么你的树结构已经leftright树的.The节点应该有左,右,而不是树itself.The树结构应该只是有一个root节点。

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

and then the tree structure 然后是树状结构

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;

Your insert() function should take bstree as input and insert a new bst_node in the tree.Remeber your root is bsttree::root and not bsttree itself. 您的insert()函数应将bstree作为输入并在bst_node中插入一个新的bst_node 。记住您的根是bsttree::root而不是bsttree本身。

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

try this 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);
    }
}

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

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