在二叉搜索树中查找maxdepth

1 c binary-tree

这是二叉搜索树的代码

#include<stdio.h>
#include<conio.h>
#include"malloc.h"

struct node
{
    int data;
    struct node* left;
    struct node* right;
};
int size(struct node* n)
{
    if(n==NULL)
       return 0;
    else
       return (size(n->left)+1+size(n->right));
}

int maxdepth(struct node* n)
{
    int ldepth,rdepth;
    if(n==NULL)
    {
       return 0;
    }
    else
    {
       ldepth=maxdepth(n->left);
       rdepth=maxdepth(n->right);
       if(ldepth>rdepth)
          return (ldepth+1);
       else
          return (rdepth+1);
    }
}

int lookup(struct node* node,int target)
{
    if(node==NULL)
       return 0;
    else if(target==node->data)
       return 1;
    else if(target<node->data)
       return(lookup(node->left,target));
    else
       return(lookup(node->right,target));
}

struct node* newnode(int data)
{
     struct node* newnod=(struct node*)malloc(sizeof(struct node));
     newnod->data=data;
     newnod->left=NULL;
     newnod->right=NULL;
     return newnod;
}

struct node* insert(struct node* root,int target)
{
    if(root==NULL)
        return(newnode(target));
    else if(target<=root->data)
        root->left=insert(root->left,target);
    else 
        root->right=insert(root->right,target);
    return root;
}

void main()
{
    int result,s,max;
    struct node* newnode=NULL;
    clrscr();
    newnode=insert(newnode,2);
    newnode=insert(newnode,3);
    newnode=insert(newnode,4);
    max=maxdepth(newnode);
    printf("maxdepth %d\n",max);
    s=size(newnode);
    result=lookup(newnode,3);
    printf("size %d\n",s);
    printf("%d",result);
    getch();
}
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序.我得到maxdepth了3.

如果我将maxdepth功能更改为

int maxdepth(struct node* n)
{
    int ldepth,rdepth;
    if(n==NULL)
    {
        return 0;
    }
    else
    {
        ldepth=maxdepth(n->left);
        rdepth=maxdepth(n->right);
        if(ldepth>rdepth)
            return (ldepth);
        else
            return (rdepth);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的maxdepth值为0.问题是什么?我想不出来?

ins*_*ity 6

您没有计算当前节点,因此+1需要a.

      {
        ldepth = maxdepth(n->left);
        rdepth = maxdepth(n->right);

        if(ldepth > rdepth)
          return ldepth + 1;
        else
          return rdepth + 1;
      }
Run Code Online (Sandbox Code Playgroud)

没有+1 maxdepth永远的回报0.因为ldepth而且rdepth永远都是0.

具有3个节点的树的示例:

   A
 /   \
B     C
Run Code Online (Sandbox Code Playgroud)

现在你打电话maxdepth(A),这将做:ldepth = maxdepth(B); rdepth = maxdepth(C);,然后maxDepth(B)将做:ldepth = maxdepth(null); rdepth = maxdepth(null); /* ldepth and rdepth are now 0 */,因此maxDepth(B)将返回结果0.类似的maxDepth(C)将返回0.你会做的:

if(ldepth > rdepth)
  return ldepth;
else
  return rdepth;
Run Code Online (Sandbox Code Playgroud)

但两者ldepthrdepth0,所以rdepth将返回这是0.最后maxdepth(A)将返回0作为结果.

这就是为什么+1需要.


pax*_*blo 5

我们来看一个示例树:

    __A__
   /     \
  B       C
 / \     / \
D   E   F   G
Run Code Online (Sandbox Code Playgroud)

在这棵树中,我们完全平衡,所以我们不会担心哪个是每个节点的较高子树(它们的高度相同).所以我们只用左侧分支计算高度.

树的高度是多少?这是高度A.

高度是A多少?这是一个加上高度B.

反过来,高度B是一加上高度D,高度D是一加上D左分支的高度,为零.

所以总高度是1 + 1 + 1 + 0 = 3.

所以这个(简化)案例中的算法是:

def height (node):
    if node is null:
        return 0
    return 1 + height (node.left)
Run Code Online (Sandbox Code Playgroud)

就是为什么你的递归高度函数必须在每个级别添加一个.如果您添加0(这是您的第二个代码段正在执行的操作),则从获取切换1 + 1 + 1 + 0 = 3到获取0 + 0 + 0 + 0 = 0.

如果你修改上面的算法来考虑不同大小的子树,你基本上得到你的第一个代码段,它工作正常:

def height (node):
    if node is null:
        return 0
    leftheight = height (node.left)
    rightheight = height (node.rigth)
    return 1 + max (leftheight, rightheight)
Run Code Online (Sandbox Code Playgroud)