简体   繁体   English

将值插入C中的二叉树根

[英]Insert value to binary tree root in C

Im a beginner in C programming and im trying to do a binary tree c library. 我是C编程的初学者,我试图做一个二叉树c库。

heres my binary tree struct: 这是我的二叉树结构:

#include <stdio.h>

struct Noeud
{
    int valeur ;
    struct Noeud* gauche ;
    struct Noeud* droit ;
};

typedef struct Noeud TNoeud;
typedef struct Noeud* TArbre;

Heres the way I create it 这是我创建它的方式

TArbre NouvelArbreVide( void )
{
    return NULL;
}

However i would wonder on how to put a value to the root of the tree like 但是我想知道如何在树的根上赋一个值

TArbre NouvelArbreVide(int value_root)
{
    return NULL;
}

that would put the value_root value to the binary tree root.Im not sure on how to do that even though its probably very basic. 那将把value_root的值放到二叉树的根上。我不确定该怎么做,即使它可能很基础。

thank you 谢谢

To start your tree with a single node, you want to allocate a new root like this: 要从单个节点开始树,您需要分配一个新的根,如下所示:

TArbre NouvelArbreVide(int value_root)
{
    TArbre newRoot = malloc(sizeof(TNoeud));
    if (newRoot)
    {
        newRoot->valeur = value_root;
        newRoot->gauche = NULL;
        newRoot->droit = NULL;
    }

    return newRoot;
}

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

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