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

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

挑战程序设计竞赛(算法和数据结构)——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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5573724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存