简体   繁体   English

最大功能c树高

[英]max function c tree height

is there a max function in c so i can do something like this to calculate tree height :or perhaps there is a better way to calculate tree height. 在c中有一个最大函数,所以我可以做这样的事情来计算树高:或者可能有更好的方法来计算树高。

int height(struct node *tree)
{ 
    if (tree == NULL) return 0;
    return 1 + max(height (tree->left), height (tree->right)); 
}

if so what includes do i need? 如果是这样,我需要什么?

currently i get this error : 目前我收到此错误:

dict-tree.o: In function 'height': dict-tree.o:在函数'height'中:
/home/ex10/dict-tree.c:36: undefined reference to `max' /home/ex10/dict-tree.c:36:对'max'的未定义引用

No, there isn't one built in. Typically you'd write your own inline function, eg 不,没有内置的。通常你会编写自己的内联函数,例如

static inline int max(int a, int b)
{
    return (a > b) ? a : b;
}

(using whichever 'inline' hint syntax your compiler prefers). (使用编译器喜欢的'内联'提示语法)。 In your case, though, you might as well just spell this out manually - it's simple enough: 但是,在你的情况下,你也可以手动拼出这个 - 这很简单:

int height(struct node *tree)
{ 
    int height_left, height_right;
    if (tree == NULL) return 0;

    height_left = height (tree->left);
    heigth_right = height (tree->right);

    return 1 + ((height_left > height_right) ? height_left : height_right);
}

NB beware of the max macro trap. 注意最大宏陷阱。 It's tempting to do something like 做某事很诱人

#define MAX(a,b) (((a) > (b)) ? (a) : (b))

which you can then use for any inputs regardless of their types, but the problem here is if either of the input expressions have side effects, eg MAX(++i, ++j) . 然后你可以将它用于任何输入而不管它们的类型,但问题是如果任何一个输入表达式都有副作用,例如MAX(++i, ++j) The issue then is that the side effects will get evaluated twice for whichever of the inputs is the max. 那么问题是,无论哪个输入是最大值,副作用都会被评估两次。 If you're going to code up max you must use an (inline) function rather than a macro. 如果要编写最大代码,则必须使用(内联)函数而不是宏。 Unfortuantely since you're in C not C++ without overloading / templates this will limit you to one set of input / output types per named max function. 不幸的是,由于你在C而不是C ++而没有重载/模板,这将限制你为每个命名的max函数设置一组输入/输出类型。

Probably because max is an undefined function, 可能因为max是一个未定义的函数,

try implementing max first before proceeding. 在继续之前尝试执行max。

int max(int a, int b) {
    if(a > b) return a;
    else return b;
}

No there isn't. 不,没有。 There is a family of functions to compute the maximum of floating-point values (see fmax () and friends), which you could certainly use yourself, but I think it's easier to just do it locally. 有一系列函数来计算浮点值的最大值(参见fmax ()和朋友),你当然可以自己使用它,但我认为在本地执行它更容易。

Something like: 就像是:

const size_t left = height (tree->left);
const size_T right = height (tree->right);
return left > right ? left : right;
int height(struct node *tree)
{ 
if (tree == NULL) 
{
    return 0;
}    
int left = height(tree->left);
int right = height(tree->right);
return (1 + ((left >right)?left:right)); 
}

//if else is better than function max in this case //在这种情况下,如果else比函数max更好

If you are willing to use C++ rather than just plain C, there is. 如果你愿意使用C ++而不仅仅是普通的C,那就是。 It's in the Standard Template Library, so you will have to include the requisite file. 它位于标准模板库中,因此您必须包含必需的文件。 See here for an example: 请看这里的例子:

http://www.cplusplus.com/reference/algorithm/max/ http://www.cplusplus.com/reference/algorithm/max/

Reproduced for your convenience: 为方便起见,转载:

// max example
#include <iostream>
#include <algorithm>
using namespace std;

int main () {
  cout << "max(1,2)==" << max(1,2) << endl;
  cout << "max(2,1)==" << max(2,1) << endl;
  cout << "max('a','z')==" << max('a','z') << endl;
  cout << "max(3.14,2.72)==" << max(3.14,2.72) << endl;
  return 0;
}

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

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