简体   繁体   English

在C中插入二叉搜索树

[英]Inserting a binary search tree in C

I tried to create a binary search tree by using the insert function. 我试图通过使用insert函数创建一个二进制搜索树。
The the result was not what I expected, it only yielded the first 结果不是我所期望的,它只产生了第一个
value of the node of the tree. 树节点的值。 Can anyone find out what is the problem? 谁能找出问题所在?
Thank you! 谢谢!

And can someone check if my other functions are right too? 有人可以检查我的其他功能是否也正确吗?

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    struct node* left;
    struct node* right;
    int val;
}treeNode;
int searchTree(treeNode *T, int key, treeNode *T1, treeNode** p)
{
    if(!T)
    {
        *p=T1;
        return 0;
    }
    else if(T->val==key)
    {
        *p=T;
        return 1;
    }
    else if(T->val<key)
    {
        searchTree(T->right,key,T,p);
    }
    else
    {
        searchTree(T->left,key,T,p);
    }
    return 1;
}
int insert(treeNode **T, int key)
{
    treeNode *p;
    treeNode *s;
    if(!searchTree(*T,key,NULL,&p))
    {
        s= malloc(sizeof(treeNode));
        s->val=key;
        s->left=s->right=NULL;
        if(!p)      
       {
            *T=s;
        }
        else if(p->val<key)
        {
            p->right=s;
        }
        else
        {
            p->left=s;
        }
    }
    else
    {
        return -1;
    }
    return 1;
}
int delete(treeNode **T)
{
    treeNode *q;

    if(!(*T)->left)
    {
        q=*T;
        *T=(*T)->right;
        free(q);
    }
    else if(!(*T)->right)
    {
        q=*T;
        *T=(*T)->left;
        free(q);
    }
    else
    {
        treeNode *s;
        q=*T;
        s=(*T)->right;
        while(s->left)
        {
            q=s;
            s=s->left;
        }
        (*T)->val=s->val;
        if(q!=*T) q->left=s->right;
        else q->right=s->right;
        free(s);
    }
    return 1;
}
void preOrder(treeNode *T)
{
    if(!T)  return;
    preOrder(T->left);
    printf("%d\n",T->val);
    preOrder(T->right);
}
int main() {
    int a[10]={62,88,58,47,35,73,51,99,37,93};
    treeNode *T=NULL;
    for(int i=0;i<10;i++)
    {
        insert(&T,a[i]);
    }
    preOrder(T);
    return 0;
}

The result is 62 rather than the whole array! 结果是62,而不是整个数组!

The problem is the return value from searchTree . 问题是searchTree的返回值。 When you do recursive calls you need to pick up the return value from those recursive calls. 进行递归调用时,您需要从那些递归调用中获取返回值。 Like: 喜欢:

int searchTree(treeNode *T, int key, treeNode *T1, treeNode** p)
{
    if(!T)
    {
        *p=T1;
        return 0;
    }
    else if(T->val==key)
    {
        *p=T;
        return 1;
    }
    else if(T->val<key)
    {
        return searchTree(T->right,key,T,p);  //notice the return
    }
    else
    {
        return searchTree(T->left,key,T,p);  // notice the return
    }
    return 1;  // Not really needed...
}

Your search function does not work as you expect 您的搜索功能无法按预期工作

You can just remove it and do : 您可以删除它并执行以下操作:

int insert(treeNode ** t, int key)
{
  if (*t == NULL)
  {
    treeNode * s = malloc(sizeof(treeNode));

    s->val=key;
    s->left=s->right=NULL;
    *t = s;
  }
  else if ((*t)->val == key) /* I am not sure but it seems you do not want to insert several times the same key, else that case */
    return -1;
  else if((*t)->val < key)
    insert(&(*t)->right, key);
   else
     insert(&(*t)->left, key);

   return 1;
}

as you see the code is much more simple ... and it works 如您所见,代码要简单得多……而且可以正常工作

Compilation and execution 编译与执行

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall t.c
pi@raspberrypi:/tmp $ ./a.out
35
37
47
51
58
62
73
88
93
99

Your delete function doesn't work, if I add delete(&t); 如果我添加delete(&t);则您的删除功能不起作用delete(&t); at the end of the main and I execute under valgrind there are memory leaks : 在main的末尾,我在valgrind下执行,存在内存泄漏:

==14950==    definitely lost: 12 bytes in 1 blocks
==14950==    indirectly lost: 96 bytes in 8 blocks

A simple way to do is : 一个简单的方法是:

void delete(treeNode **t)
{
  if (*t != NULL) {
    delete(&(*t)->left);
    delete(&(*t)->right);
    free(*t);
    *t = NULL;
  }
}

After that change there is no memory leaks 更改之后,没有内存泄漏

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

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