C 为什么在查找二叉搜索树的高度时会得到不同的输出?

C 为什么在查找二叉搜索树的高度时会得到不同的输出?,c,data-structures,C,Data Structures,我已经编写了两个不同的程序来查找二叉搜索树的高度,但它们都给出了不同的输出,因为我在两个函数中计算高度的逻辑是相同的: int findheight(Node* node) { if (node == NULL) return 0; int lh = 0, rh = 0; if (node->left != NULL) lh = findheight(node->left); if (node->right !=

我已经编写了两个不同的程序来查找二叉搜索树的高度,但它们都给出了不同的输出,因为我在两个函数中计算高度的逻辑是相同的:

int findheight(Node* node) {
    if (node == NULL)
        return 0;
    int lh = 0, rh = 0;
    if (node->left != NULL)
        lh = findheight(node->left);
    if (node->right != NULL)
        rh = findheight(node->right);
    return max(lh, rh) + 1;
}
计算二叉搜索树高度的第二个函数:

int findheight(struct node* node) {
    if (node == NULL)
        return 0;
    else {
        int ldepth = findheight(node->left);
        int rdepth = findheight(node->right);

        if (ldepth > rdepth)
            return (ldepth + 1);
        else
            return (rdepth + 1);
    }
}
对于此测试用例
100 11 17 30 70 71 90 92 117 148 151 157 160 174 193 203 227 263 276 280 291 296 307 311 322 340 345 346 373 374 398 402 411 419 437 441 446 450 476 476 493 503 513 523 530 533 545 573 573 597 599 603 628 642 650 655 658 679 704 711 737 745 783 797 808 823 825 826 826 827 832 867 871872 877 883 894 907 921 922 940 943 949 951 952 956 958 959 976 979 987 997
第二个函数的输出为100,而第一个函数的输出为96

对于此测试用例:
1007102932402557683103112123162163170184192205221226232532592592983903389408419419419429942994434344343434148148490504050505454545475645809611 616666666666467686886846849852 855 864 882 887 893 902 911 937 940 941 943 965 966 968 984 985 993 998
第二个函数给出输出:100,第一个函数给出99

查找二叉搜索树高度的完整代码:

    #include <bits/stdc++.h>
    using namespace std;

    struct node{
    int key;
    struct node *left;
    struct node *right;
    };

    struct node *newnode(int item){
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
    }

    struct node *insert(struct node *node,int key){
        if(node==NULL) return newnode(key);

        if(key< node->key)
            node->left = insert(node->left,key);
        else
            node->right = insert(node->right,key);

        return node;
    }

   /*Insert any one of the functions mentioned above*/

    int main(){
        int n,m;
        cin>>n;
        struct node *root = NULL;
        for(int i=0;i<n;i++){
            cin>>m;
            root=insert(root,m);
        }
        cout<< maxdepth(root);

    return 0;
    }
#包括
使用名称空间std;
结构节点{
int键;
结构节点*左;
结构节点*右;
};
结构节点*新节点(int项){
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
temp->key=项目;
临时->左=临时->右=空;
返回温度;
}
结构节点*插入(结构节点*节点,int键){
如果(node==NULL)返回newnode(key);
如果(键<节点->键)
节点->左=插入(节点->左,键);
其他的
节点->右侧=插入(节点->右侧,键);
返回节点;
}
/*插入上述任何一个函数*/
int main(){
int n,m;
cin>>n;
结构节点*root=NULL;
对于(int i=0;i>m;
根=插入(根,m);
}

cout这两个函数都返回相同的值。您的代码中还有其他东西会在输出中产生差异

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node{
    int key;
    struct node *left;
    struct node *right;
};
int findheight(struct node* node) {
    if (node == NULL)
        return 0;
    else {
        int ldepth = findheight(node->left);
        int rdepth = findheight(node->right);

        if (ldepth > rdepth)
            return (ldepth + 1);
        else
            return (rdepth + 1);
    }
}
int findheight2(struct node* node) {
    if (node == NULL)
        return 0;
    int lh = 0, rh = 0;
    if (node->left != NULL)
        lh = findheight(node->left);
    if (node->right != NULL)
        rh = findheight(node->right);
    return (lh>rh ?lh : rh) + 1;
}
struct node *newnode(int item){
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

struct node *insert(struct node *node,int key){
    if(node==NULL) return newnode(key);

    if(key< node->key)
        node->left = insert(node->left,key);
    else
        node->right = insert(node->right,key);

    return node;
}

/*Insert any one of the functions mentioned above*/

int main(){
    int n,m;
    n=100;
    struct node *root = NULL;
    for(int i=0;i<n;i++){
        m = n;
        root=insert(root,m);
    }
    printf("%d", findheight(root));
    printf("%d", findheight2(root));
    return 0;
}
#包括
#包括
#包括
结构节点{
int键;
结构节点*左;
结构节点*右;
};
int findheight(结构节点*节点){
if(node==NULL)
返回0;
否则{
int ldepth=findheight(节点->左侧);
int rdepth=findheight(节点->右侧);
如果(ldepth>rdepth)
返回(ldepth+1);
其他的
返回(rdepth+1);
}
}
int findheight2(结构节点*节点){
if(node==NULL)
返回0;
内部左侧=0,右侧=0;
如果(节点->左!=NULL)
左侧=FindHight(节点->左侧);
如果(节点->右侧!=NULL)
rh=findheight(节点->右侧);
返回(左侧>右侧?左侧:右侧)+1;
}
结构节点*新节点(int项){
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
temp->key=项目;
临时->左=临时->右=空;
返回温度;
}
结构节点*插入(结构节点*节点,int键){
如果(node==NULL)返回newnode(key);
如果(键<节点->键)
节点->左=插入(节点->左,键);
其他的
节点->右侧=插入(节点->右侧,键);
返回节点;
}
/*插入上述任何一个函数*/
int main(){
int n,m;
n=100;
结构节点*root=NULL;

对于(int i=0;I.请显示示例树和两个不同的输出。在编辑的同时,请给出一个代码。也就是说,给我们一个代码基础,我们可以将您的函数应用到保证同一棵树的树上。优选地是一个更小的树。使用两个不同的函数名来在一个代码文件中同时使用这两个函数。m在同一棵树上,并显示两个输出。对齐原型以避免怀疑差异导致的错误。不要忘记显示节点类型的定义。两个函数在两个测试用例上都返回100-请参阅