c - 在C中将值插入二叉树根

标签 c tree binary-tree

我是 C 编程的初学者,正在尝试创建一个二叉树 C 库。

这是我的二叉树结构:

#include <stdio.h>

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

typedef struct Noeud TNoeud;
typedef struct Noeud* TArbre;

这是我创建它的方式

TArbre NouvelArbreVide( void )
{
    return NULL;
}

但是我想知道如何为树的根赋值

TArbre NouvelArbreVide(int value_root)
{
    return NULL;
}

这会将 value_root 值放入二叉树根。我不确定如何做到这一点,尽管它可能非常基本。

谢谢

最佳答案

要使用单个节点启动树,您需要像这样分配一个新的根:

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

    return newRoot;
}

关于c - 在C中将值插入二叉树根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41008202/

相关文章:

C:将两个 ScanF 相乘

r - randomForest、randomForestSRC 或 cforest 中单棵树的重要性可变吗?

java - 如何使用 <E extends Comparable<E>> 扩展通用抽象类

c++ - 从二叉树错误中删除

c - 如何为输入 la shell 的 `bg` 命令创建后台进程 block ?

c - C语言中 "<>"是什么意思?

Java返回语句优先级

binary-tree - 这是一棵完整的二叉树吗?

c - 如何同时打印两个输入的输出?

algorithm - 词组树成数组