小编Chr*_* M.的帖子

C中二叉搜索树中的叶数

我是一个初学者,正在研究C二叉搜索树.我正在尝试一种方法,它将返回树中叶子的数量.叶子我的意思是一个没有孩子的节点(父节点)(左/右)Heres我的树结构:

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

typedef struct Node TNode;
typedef struct Node *binary_tree;
Run Code Online (Sandbox Code Playgroud)

它是这样创建的:

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;
}
Run Code Online (Sandbox Code Playgroud)

我添加元素如:

void Insert(binary_tree *tree, int val) {
    if (*tree == NULL) {
        *tree = (binary_tree)malloc(sizeof(TNode));
        (*tree)->value = val;
        (*tree)->left = NULL;
        (*tree)->right = NULL;
    } else {
        if (val < (*tree)->value) {
            Insert(&(*tree)->left, val); …
Run Code Online (Sandbox Code Playgroud)

c tree binary-tree binary-search-tree

2
推荐指数
1
解决办法
363
查看次数

标签 统计

binary-search-tree ×1

binary-tree ×1

c ×1

tree ×1