简体   繁体   English

将C二叉树根值保存在变量中

[英]Save C binary tree root value in variable

Im working on a C library for binary tree, and Im wondering how I could save the value of the root of the tree to be able to display it later in a function. 我正在研究用于二叉树的C库,我想知道如何保存树根的值以便以后在函数中显示它。

My tree struct: 我的树结构:

struct Node {
    int value;
    struct Node *left;
    struct Node *right;
};

typedef struct Node TNode;
typedef struct Node *binary_tree;

The binary tree root is initialised with the 3 value like this: 二进制树的根使用以下3个值初始化:

caller: 呼叫者:

tree = NewBinaryTree(3);

NewBinaryTree method: NewBinaryTree方法:

binary_tree NewBinaryTree(int value_root) {
    binary_tree newRoot = malloc(sizeof(TNode));
    if (newRoot) {
        newRoot->value = value_root;
        newRoot->left = NULL;
        newRoot->right = NULL;
    }
    return newRoot;
}

Basically I would like to be able to do a function that just display the value_root function even after adding elements to the binary tree and be able to still display the value_root value.This might be very basic but im learning C and im not sure. 基本上,我希望能够做一个即使在将元素添加到二叉树后仍显示value_root函数的功能,并且仍然能够显示value_root的值。这可能是非常基本的,但是我正在学习C并且不确定。

thank you 谢谢

在调用方中: printf( "%d\\n", tree->value ) – user3386109

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

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