【问题标题】:Pointers in Binary Search Tree insertions and search in C二叉搜索树中的指针插入和 C 中的搜索
【发布时间】:2017-05-10 20:54:22
【问题描述】:

我是 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;

这里是函数(我不允许更改函数的类型或其参数):

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

【问题讨论】:

  • char * name[60]; --> char name[60];
  • 这修复了一些错误,谢谢。
  • bstreebst_node 是不同的类型。
  • typedef struct bstree { ... }bstree;
  • 在我看来你没有区分节点和树

标签: c pointers recursion binary-search-tree


【解决方案1】:

试试这个代码伙伴

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

【讨论】:

  • 非常感谢!我没有得到任何错误:) 除非我完成另外两个免费的功能并删除,否则无法编译和查看。会更新你!再次非常感谢:)
  • 如果您需要更多帮助,欢迎 @AhmedBaldr 发布更多 cmets
  • 没问题的小伙伴,我一会儿给你贴代码
  • 我为此创建了一个新帖子 :) link
【解决方案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
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多