栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

挑战程序设计竞赛(算法和数据结构)——8.3二叉树的表达的JAVA实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

挑战程序设计竞赛(算法和数据结构)——8.3二叉树的表达的JAVA实现

题目:




有了有根树这篇博文的代码基础,二叉树很快就能写出来,只要把左右节点指针进行改写即可!

https://blog.csdn.net/weixin_42887138/article/details/121472382

import java.io.BufferedInputStream;
import java.util.Scanner;

public class BinaryTree {
    public static class Node{
        int parent, left, right;
        Node(int parent, int left, int right){
            this.parent = parent;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        System.out.println("请输入树的节点数:");
        int n = cin.nextInt();
        Node[] T = new Node[10000];
        int[] H = new int[10000];
        int[] D = new int[10000];
        System.out.println("接下来,每行输入节点的编号id,度k,以及第1到第k个子节点的编号c1,c2...ck:");

        for(int i=0;i 

输入:

请输入树的节点数:
9
接下来,每行输入节点的编号id,度k,以及第1到第k个子节点的编号c1,c2...ck:
0 1 4
1 2 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

输出:

node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root
node 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node
node 2: parent = 1, sibling = 3, degree = 0, depth = 2, height = 0, leaf
node 3: parent = 1, sibling = 2, degree = 0, depth = 2, height = 0, leaf
node 4: parent = 0, sibling = 1, degree = 2, depth = 1, height = 2, internal node
node 5: parent = 4, sibling = 8, degree = 2, depth = 2, height = 1, internal node
node 6: parent = 5, sibling = 7, degree = 0, depth = 3, height = 0, leaf
node 7: parent = 5, sibling = 6, degree = 0, depth = 3, height = 0, leaf
node 8: parent = 4, sibling = 5, degree = 0, depth = 2, height = 0, leaf
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/582511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号