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

时间:2016-12-26 11:18:11

标签: c pointers recursion binary-search-tree

我是C编程的新手,这是我第一次开始编写一个复杂的程序。该程序将把电话簿(名称和号码)放在二叉搜索树中。出现的问题是为什么我得到了一个错误的Recursion和我在程序中使用的所有指针。 我的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;

以下是函数(我不允许更改函数的类型或它们的参数):

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;
}

2 个答案:

答案 0 :(得分:2)

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

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

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_node。记住您的根是bsttree::root而不是bsttree本身。

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

答案 1 :(得分:0)

试试这个代码伙伴

#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);
    }
}