【数据结构】树-实现树的深度、叶子数、节点数(c++、java)

GitHub同步更新(已分类)Data_Structure_And_Algorithm-Review

公众号:URLeisure 的复习仓库
公众号二维码见文末

以下是本篇文章正文内容,下面案例可供参考。


二叉树的深度

1). 算法步骤

算法步骤:

  1. 如果二叉树为空,则深度为 0;
  2. 非空则深度为根的左、右子树的深度最大值加 1。

2). 代码

c++代码如下(示例):

int Depth(Btree T) {
    int m = 0, n = 0;
    if (!T) {
        return 0;
    }
    m = Depth(T->lchild);
    n = Depth(T->rchild);
    if (m > n) {
        return m + 1;
    }
    return n + 1;
}

java代码如下(示例):

public static int depth(BNode t) {
    int m = 0, n = 0;
    if (t == null) {
        return 0;
    }
    m = depth(t.lchild);
    n = depth(t.rchild);
    if (m > n) {
        return m + 1;
    }
    return n + 1;
}

二叉树的叶子数

  • 特殊情况:如果二叉树为空,则叶子数为 0;如果根的左、右子树都为空,则叶子数为 1;一般情况下二叉树的叶子数等于左子树的叶子数与右子树的叶子数之和

1). 算法步骤

算法步骤:

  1. 如果二叉树为空,则叶子数为 0;
  2. 如果根的左、右子树都为空,则叶子数为 1;
  3. 否则求左子树的叶子数和右子树的叶子数之和,即为二叉树的叶子数。

2). 代码

c++代码如下(示例):

int LeafCount(Btree T) {
    if (!T) {
        return 0;
    }
    if (!T->lchild && !T->rchild) {
        return 1;
    }
    return LeafCount(T->lchild) + LeafCount(T->rchild);
}

java代码如下(示例):

public static int leafCount(BNode t) {
    if (t == null) {
        return 0;
    }
    if (t.lchild == null && t.rchild == null) {
        return 1;
    }
    return leafCount(t.lchild) + leafCount(t.rchild);
}

二叉树的节点数

  • 节点数很简单,全部节点过一遍就 ok 了。

1). 算法步骤

算法步骤:

  1. 如果当前节点为空,结束递归;
  2. 如果不为空,递归其左、右子树,并加 1。

2). 代码

c++代码如下(示例):

int NodeCount(Btree T) {
    if (!T) {
        return 0;
    }
    return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}

java代码如下(示例):

public static int nodeCount(BNode t){
    if(t == null){
        return 0;
    }
    return nodeCount(t.lchild) + nodeCount(t.rchild) + 1;
}

整合代码

c++代码如下(示例):

#include<iostream>

using namespace std;

typedef struct BNode {
    char data;
    BNode *lchild, *rchild;
} *Btree;

void CreateTree(Btree &T) {
    char ch;
    cin >> ch;
    if (ch != '#') {
        T = new BNode;
        T->data = ch;
        CreateTree(T->lchild);
        CreateTree(T->rchild);
    } else {
        T = nullptr;
    }
}

int Depth(Btree T) {
    int m = 0, n = 0;
    if (!T) {
        return 0;
    }
    m = Depth(T->lchild);
    n = Depth(T->rchild);
    if (m > n) {
        return m + 1;
    }
    return n + 1;
}

int LeafCount(Btree T) {
    if (!T) {
        return 0;
    }
    if (!T->lchild && !T->rchild) {
        return 1;
    }
    return LeafCount(T->lchild) + LeafCount(T->rchild);
}

int NodeCount(Btree T) {
    if (!T) {
        return 0;
    }
    return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}

int main() {
    Btree T;
    CreateTree(T);
    cout << Depth(T) << endl;
    cout << LeafCount(T) << endl;
    cout << NodeCount(T) << endl;
    return 0;
}
// ABD##E##CF#G###

java代码如下(示例):

import java.util.Scanner;

public class A {
    public static Scanner sc = new Scanner(System.in);
    
    public static class BNode {
        String data;
        BNode lchild, rchild;
    }

    public static BNode createTree(BNode t) {
        String ch = sc.nextLine();
        if (!ch.equals("#")) {
            t = new BNode();
            t.data = ch;
            t.lchild = createTree(t.lchild);
            t.rchild = createTree(t.rchild);
        } else {
            t = null;
        }
        return t;
    }

    public static int depth(BNode t) {
        int m = 0, n = 0;
        if (t == null) {
            return 0;
        }
        m = depth(t.lchild);
        n = depth(t.rchild);
        if (m > n) {
            return m + 1;
        }
        return n + 1;
    }

    public static int leafCount(BNode t) {
        if (t == null) {
            return 0;
        }
        if (t.lchild == null && t.rchild == null) {
            return 1;
        }
        return leafCount(t.lchild) + leafCount(t.rchild);
    }

    public static int nodeCount(BNode t){
        if(t == null){
            return 0;
        }
        return nodeCount(t.lchild) + nodeCount(t.rchild) + 1;
    }
    
    public static void main(String[] args) {
        BNode t = new BNode();
        t = createTree(t);
        System.out.println(depth(t));
        System.out.println(leafCount(t));
        System.out.println(nodeCount(t));
    }
}
/*
A
B
D
#
#
E
#
#
C
F
#
g
#
#
#
 */

关注公众号,感受不同的阅读体验

请添加图片描述

下期预告:三元组创建二叉树

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扑腾的江鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值