【问题标题】:Number of leaves in binary search tree in CC中二叉搜索树中的叶子数
【发布时间】:2016-12-14 00:13:20
【问题描述】:

我是一名初学者,正在研究 C 二叉搜索树。我正在尝试做一个方法来返回我的树中的叶子数。叶子是指没有子节点(左/对)这是我的树结构:

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

typedef struct Node TNode;
typedef struct Node *binary_tree;

它是这样创建的:

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;
}

我向它添加元素,例如:

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);
        } else {
            Insert(&(*tree)->right, val);
        }
    }
}

我实际计算叶子数的方法:

 int nbleaves(binary_tree tree)
 {
     int nb;
     if(tree->right==NULL && tree->left ==NULL){
        nb=nb+1;
     }
     printf("%d",nb);
 }

当然这首先不起作用,没有实际的循环,但是我试过它没有返回任何错误,而是返回 0(例如,将元素 2222 和 3 添加到树后,此函数返回 0)。我不知道如何执行此功能.

谢谢!

【问题讨论】:

  • 函数的缺失位,将printf("%d",nb);替换为else { if (tree-&gt;right != NULL) nb += nbleaves(tree-&gt;right); if (tree-&gt;left != NULL) nb += nbleaves(tree-&gt;left); } return nb;
  • @ikegami 我试过这个,它确实可以编译,但是当我用那个代码调用那个函数时,它会使程序崩溃

标签: c tree binary-tree binary-search-tree


【解决方案1】:

因为您必须初始化 nb

int nb = 0;

由于nb 未初始化,它包含“random”或“garbage”值,因此您看到的行为是因为该值可能非常大。但无法预测该值是多少。

注意:不要对空格“吝啬”,不要使用太多,但要让你的代码呼吸一点。

比较

if(tree->right==NULL && tree->left ==NULL){
    nb=nb+1;
}

if ((tree->right == NULL) && (tree->left == NULL)) {
    nb = nb + 1;
}

【讨论】:

  • 谢谢,编辑了我的帖子,没错。但现在我的函数只返回 0,我不太确定如何循环。
  • @ChristopherM。发布答案后请勿编辑帖子!现在我的答案是无效的。!!!!!!这不是 SO 的工作方式。
【解决方案2】:

除了像@iharob 指出的那样进行初始化之外,您只需要在树的左右两半上递归并将其添加到您的总数中(如 cmets 中所述)。这种方法在我的测试中对我有用,所以我不确定你在尝试时遇到了什么错误。这是我的nbleaves() 函数:

int nbleaves(binary_tree tree)
{
  int nb=0;
  if(tree->right==NULL && tree->left ==NULL){
    nb=nb+1;
  }
  else {
    if(tree->left!=NULL)
      nb += nbleaves(tree->left);
    if(tree->right!=NULL)
      nb += nbleaves(tree->right);
  }
  return nb;
}

例如,在这个测试用例上:

int main() {    
  binary_tree root=NULL;

  root=NewBinaryTree(5);
  Insert(&root,3);
  Insert(&root,7);
  Insert(&root,2);
  Insert(&root,8);
  Insert(&root,6);
  Insert(&root,1);
  Insert(&root,4);
  Insert(&root,9);
  traverse(root); /*Just a function I created for testing*/

  printf("%d\n",nbleaves(root));

  free_tree(root); /*Also a function I wrote*/
  return 0;
}

它产生这个输出:

5: 3 7 
3: 2 4 
2: 1 NULL 
1: NULL NULL 
4: NULL NULL 
7: 6 8 
6: NULL NULL 
8: NULL 9 
9: NULL NULL 
4

最后一行是叶子数,其余是traverse()的输出。

对于我的完整程序:https://repl.it/Epud/0

【讨论】:

  • 哦,我现在明白了!非常感谢@abacles
  • 没问题!祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
相关资源
最近更新 更多