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

标签 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 != 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);
    }
}

For this test case 100 11 11 17 30 40 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 593 597 599 603 628 642 650 651 655 658 679 704 711 715 737 745 746 783 783 797 802 808 823 825 826 827 832 834 845 857 861 871 872 877 883 894 907 921 922 940 943 949 951 952 956 958 959 976 979 987 997 第二个函数输出 100 而第一个函数输出 96

对于此测试案例: 100 7 10 29 32 40 52 55 76 83 103 116 122 122 123 135 162 162 163 163 170 184 192 192 193 205 221 226 226 226 226 235 257 259 298 259 298 305 310 310 310 338 338 349 349 349 396 397 397 399 399 399 408 412 419 429 443 443 461 481 485 490 504 508 509 515 517 522 545 547 564 580 596 601 611 616 622 635 664 665 676 684 687 688 689 695 703 724 734 764 771 775 815 816 819 827 849 852 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;
    }

最佳答案

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

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

关于c - 为什么我在查找二叉搜索树的高度时得到不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733715/

相关文章:

c# - 比较 C# 中 DateTime 的二进制表示形式

data-structures - 效率: What data structure to use. ..?

c++ - itoa() 的安全对应物?

c - 跳回向量化余数循环的一些迭代

algorithm - 数据结构排序

c - 使用 C 中的 fscanf 读取 .txt 文件

python - 哪种数据结构和/或算法适合这个问题?

c - 当生产者不能再提供时停止消费者

c - 带无符号分子的有符号除法

c++ - fstream 文件 I/O 问题 - 何时关闭文件流