简体   繁体   English

将数据插入C中的二叉树

[英]Inserting data into binary tree in C

So basicaly I was working on a simple program to insert data to a binary tree. 所以从根本上讲,我正在研究一个简单的程序,将数据插入到二叉树中。 The program calls the function for a integer variable of 15 which will be the head node, and then the same function is called for a variable of 12, which should implement the new data on the left branch of the root node. 该程序为整数变量15调用函数,该整数变量将成为头节点,然后为变量12调用相同的函数,该变量应在根节点的左分支上实现新数据。 Unfortunately although the first part works correctly, and the root node is printed out, than when the new value should be implemented for the left branch of the root node, nothing happens. 不幸的是,尽管第一部分可以正常工作并且根节点已打印出来,但是当为根节点的左分支实施新值时,什么也没有发生。 Any tips and suggestions are welcome. 欢迎任何提示和建议。 Thanks. 谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
    int data;
    struct node *right;
    struct node *left;
} NODE;
void *insert(int ins, NODE *start)
{
    NODE *newNode = malloc(sizeof(NODE));
    if (start == NULL )
    {
        newNode->data = ins;
        newNode->left = NULL;
        newNode->right = NULL;

    }
    if(ins<start->data)
    {
        insert(ins, start->left);
    }
    else if(ins>start->data)
    {
        insert(ins, start->right);
    }
}
int main()
{  
    int number;
    NODE *head;
    head=NULL;

    number = 15;
    head = insert(number, head);
    printf("prints the first element (head): %d", head->data);

    number = 12;
    insert(number, head);

    printf("should print the left branch : %d", head->left->data);  // <- THIS DOES NOT SHOW UP 

}

The start parameter is passed by value, so it is never modified. start参数是通过值传递的,因此它永远不会被修改。 One of the options you have is passing a pointer to a pointer of NODE, like this: 您拥有的一种选择是将指针传递给NODE的指针,如下所示:

void insert(int ins, NODE **start)
{
    if (*start == NULL )
    {
      NODE *newNode = (NODE *)malloc(sizeof(NODE));
      newNode->data = ins;
      newNode->left = NULL;
      newNode->right = NULL;
      *start = newNode;
    }
    if(ins< (*start)->data)
    {
      insert(ins, &(*start)->left);
    }
    else if(ins> (*start)->data)
    {
      insert(ins, &(*start)->right);
    }
}

int main()
{

    int number;
    NODE *head;
    head=NULL;

    number = 15;
    insert(number, &head); ///Doesn't need head=insert(...) anymore
    printf("prints the first element (head): %d", head->data);

    number = 12;
    insert(number, &head);

    printf("should print the left branch : %d", head->left->data);
}

It looks like the following is close to what you want. 看起来以下内容接近您想要的。

This is a recursive insert() function which takes the address of a node and looks to see if it should either add a new node into the tree or to take a branch to continue looking for the place to do a node insertion. 这是一个递归insert()函数,该函数获取节点的地址,并查看是否应将新节点添加到树中还是采用分支以继续寻找进行节点插入的位置。

One thing to also consider is what if the value already exists in the tree. 还需要考虑的一件事是,如果树中已经存在该值,该怎么办。 In this case we just skip it and assume that the tree should contain unique values only. 在这种情况下,我们只是跳过它,并假定树应仅包含唯一值。

I'm not really up on tree lore so I'm not sure what kind of binary tree this is or traversal, etc. I'll leave that up to you. 我并没有真正了解树的知识,所以我不确定这是哪种二叉树或遍历树,等等。我将由您自己决定。

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

typedef struct node
{
    int data;
    struct node *right;
    struct node *left;
} NODE;

void *insert(int ins, NODE **start)
{
    if (*start == NULL)
    {
        NODE *newNode = malloc(sizeof(NODE));

        newNode->data = ins;
        newNode->left = NULL;
        newNode->right = NULL;

        (*start) = newNode;

    }
    else if (ins < (*start)->data)
    {
        insert(ins, &((*start)->left));
    }
    else if (ins > (*start)->data)
    {
        insert(ins, &((*start)->right));
    }

    // if we already have this value in the tree then we will do nothing
    // and ignore it.
    return *start;
}

void printTree(NODE *start)
{
    // print tree
    if (start->left) printTree(start->left);
    printf("Element value %d\n", start->data);
    if (start->right) printTree(start->right);
}

int main ()
{
    int number;
    NODE *phead = NULL;

    number = 15;
    insert(number, &phead);
//  printf("prints the first element (head): %d", head.data);

    number = 12;
    insert(number, &phead);

//  printf("should print the left branch : %d", head.left->data);  // <- THIS DOES NOT SHOW UP 

    number = 22;
    insert(number, &phead);

    number = 48;
    insert(number, &phead);

    number = 33;
    insert(number, &phead);

    // try a duplicate node data.
    number = 48;
    insert(number, &phead);

    printTree(phead);
    return 0;
}

The output generated is: 生成的输出为:

Element value 12
Element value 15
Element value 22
Element value 33
Element value 48

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

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