二进制搜索代码中出现什么错误?

时间:2015-08-10 17:33:20

标签: c algorithm

我正在使用一些操作(新节点,搜索,插入和显示)实现二叉搜索树。 它会导致以下警告。

binarytree.c:70: warning: assignment makes pointer from integer without a     cast
binarytree.c:72: warning: assignment makes pointer from integer without a cast
binarytree.c:73: warning: return makes integer from pointer without a cast
binarytree.c: At top level:
binarytree.c:78: warning: conflicting types for ‘printInoder’
binarytree.c:47: warning: previous implicit declaration of ‘printInoder’ was here
binarytree.c: In function ‘NewNode’:
binarytree.c:122: warning: return makes pointer from integer without a cast
Undefined symbols for architecture x86_64:
  "_newNode", referenced from:
   _main in cckg2mll.o
  _insert in cckg2mll.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

您将在下方看到我的源代码,但我希望听到您的建议。我完全理解二进制树函数,但我认为它与任何算法无关,但它是一个编程问题。

源代码

这是非常长的代码,但我不能为你们减少它。

#include <stdlib.h>
#include <stdio.h>

#define false 0
#define true 1

struct Node
{
    int data;
    struct Node *left, *right;
}*node,*root;


int main(void)
{   
    root = (struct Node *)malloc(sizeof(struct Node));

    int option = 1;
    int choice,value,target,newvalue;

    while(option)
    {
        printf("Enter your choice \n");
        scanf("%d\n",&choice);

    switch(choice)
    {
        case 1:
        printf("Enter the a new value\n");
        insert(root,value);
        scanf("%d\n",&value);
        break;

        case 2:
        printf("Enter the traget \n");
        scanf("%d\n",&target);
        lookup(root,target);
        break;

            case 3:
            printf("Enter the new node \n");
            scanf("%d\n",&newvalue);
            newNode();
            break;

            case 4:
            printInoder();
            break;
        }
        printf("Enter 0 or 1\n");
        scanf("%d\n",&option);
    }
}


int insert(struct Node *node ,int data) 
{
    // 1. If the tree is empty, return a new, single node
    node = (struct Node *)malloc(sizeof(struct Node));

    if (node == NULL) 
    {
    return(newNode(data));
}

else 
{
    // 2. Otherwise, recur down the tree
    if (data <= node->data) 
        node->left = insert(node->left, data);
    else 
        node->right = insert(node->right, data);
    return(node); // return the (unchanged) node pointer
   }
  }

void printInoder()
  {

root = (struct Node *)malloc(sizeof(struct Node));

if (root != NULL)
{
    printInoder(root->left);
    printf("%d ",root->data);
    printInoder  (root->right);
   }
  }


   int lookup(struct Node *node, int target) 
   {
    node = (struct Node *)malloc(sizeof(struct Node));
   // 1. Base case == empty tree
   // in that case, the target is not found so return false
    if (node == NULL) 
   {
        return(false);
   }
    else 
    {
    // 2. see if found here
        if (target == node->data) return(true);

        else 
        {
   // 3. otherwise recur down the correct subtree
            if (target < node->data) 
                return(lookup(node->left, target));
           else return(lookup(node->right, target));
            }
   }
 }


struct Node *NewNode(int x) 
{
    node =(struct Node *)malloc(sizeof(struct Node)); // "new" is like "malloc"
    node->data = x;
    node->left = NULL;
    node->right = NULL;
    return(x);
  }

3 个答案:

答案 0 :(得分:3)

因此,最大的问题是您忽略了数据类型。 insert函数返回int,但无论何时指定其返回值或甚至返回此函数内的某些内容,您使用的变量都是指针 - 因此将insert的返回类型更改为{{1} }。

此外,在struct Node *node部分中,您使用函数case 4,该函数在调用之后声明并定义(在printInoder函数下方),这就是为什么它会警告您至少应该这样做在main之前声明此函数。

最后,在main函数内,您调用insert,但此函数名为newNode,并且还在NewNode下面声明并定义。

答案 1 :(得分:0)

您使用newNode而非NewNode调用了以下函数。另外,将*移出NewNode部分。

struct Node *NewNode(int x) 
{
    node =(struct Node *)malloc(sizeof(struct Node)); // "new" is like "malloc"
    node->data = x;
    node->left = NULL;
    node->right = NULL;
    return(x);
  }

您的insert函数返回节点指针,但声明为int返回类型。

此外,您需要在上面第一次调用它们时声明函数。在页面顶部添加一个声明语句,以使您的函数正常工作。

return_type function_name(params);

最后,将您的功能重命名为printInOrder()而不是printInoder()

答案 2 :(得分:0)

您的代码有很多错误。 我编辑了一下:

#include <stdlib.h>
#include <stdio.h>

#define false 0
#define true 1

struct Node
{
    int data;
    struct Node *left, *right;
};

void insert(struct Node **node, int data);
int lookup(struct Node *node, int target);
void printInoder(struct Node *root);
struct Node *NewNode(int x);

int main(void)
{
    struct Node *root = NULL;
    int option = 1;
    int choice, value, target;

    while (option)
    {
        choice = value = target = 0;
        printf("Enter your choice \n");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            printf("Enter the a new value\n");
            scanf("%d", &value);
            insert(&root, value);
            break;

        case 2:
            printf("Enter the traget \n");
            scanf("%d", &target);
            printf("%d\n", lookup(root, target));
            break;

        case 3:
            printf("Printing\n");
            printInoder(root);
            printf("\n");
            break;
        }
        printf("Enter 0 or 1\n");
        scanf("%d", &option);
    }
    return 0;
}


void insert(struct Node **root, int data)
{
    // 1. If the tree is empty, return a new, single node
    struct Node *node = NewNode(data);

    if (*root == NULL)
    {
        *root = node;
        return;
    }

    // 2. Otherwise, recur down the tree
    if (data <= (*root)->data)
        insert(&(*root)->left, data);
    else
        insert(&(*root)->right, data);
}

void printInoder(struct Node *root)
{
    if (root == NULL) return;
    printInoder(root->left);
    printf("%d ", root->data);
    printInoder(root->right);
}


int lookup(struct Node *root, int target)
{

    // 1. Base case == empty tree
    // in that case, the target is not found so return false
    if (root == NULL)
    {
        return(false);
    }

    // 2. see if found here
    if (target == root->data) return(true);

    // 3. otherwise recur down the correct subtree
    if (target < root->data)
        return(lookup(root->left, target));
    else return(lookup(root->right, target));
}


struct Node *NewNode(int x)
{
    struct Node *node= malloc(sizeof *node); 
    node->data = x;
    node->left = NULL;
    node->right = NULL;
    return node;
}

尝试一下,看看代码中的变化。