数据结构之二叉树前序、中序、后序、深度、高度(Java实现)

学习目标:数据结构二叉树的实现

掌握二叉树的构造和求结点深度


学习内容:

1、 掌握Java构造二叉树
2、 掌握Java求结点深度


学习产出:

Java没有指针,所以全部用数字作为索引

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

设置初始变量

public static int NIL = -1; //表示空指针
public static int MAX = 10000; //最大节点数
static Node[] T; //保存节点的数组
static int[] H;	 //保存节点的高度
static int[] D;	 //保存节点的深度
public static int n; //节点个数

深度:是指从根节点到该节点的最长简单路径边的条数
高度:是指从该节点到叶子节点的最长简单路径边的条数

求结点高度

高度是该节点到叶子节点的距离,所以判断左右节点是否为空即可

public static int setHeight(int u)
{
    int h1 = 0;
    int h2 = 0;
    if(T[u].left != NIL)
        h1 = setHeight(T[u].left)+1;
    if(T[u].right != NIL)
        h2 = setHeight(T[u].right)+1;
    return H[u] = Math.max(h1,h2);
}

求结点深度

深度是根节点到该节点的距离,所以从根节点出发,记录当前深度d,遍历到哪个节点就赋值给它。

public static void setDepth(int u,int d)
{
    if(u == NIL)
        return;
    D[u] = d;
    setDepth(T[u].left,d+1);
    setDepth(T[u].right,d+1);
}

求兄弟节点

就看有没有节点的parent跟当前节点的parent相同

static int getSibling(int u){
    if(T[u].parent != -1){
        int p = T[u].parent;
        if(T[p].left != -1 && T[p].left != u){
            return T[p].left;
        }
        else if(T[p].right != -1 && T[p].right != u){
            return T[p].right;
        }
    }
    return -1;
}

求节点的类型和度

比较简单就不说了

static int getDegree(int u)
{
   int d= 0;
   if(T[u].left != NIL)
       d++;
   if(T[u].right != NIL)
       d++;
   return d;
}
static String type(int u)
{
   String type;
   if(T[u].parent == -1)
       type = "root";
   else if(T[u].left == NIL && T[u].right == NIL)
       type = "leaf";
   else
       type = "internal node";
   return type;
}

前序、中序、后序遍历

前序是先输出该节点,再输出左子树和右子树
中序是先输出左子树,再输出该节点和右子树
后序是先输出左子树和右子树,最后输出该节点

static void PreOrder(int u) {
    if(u == NIL)
        return;
    System.out.print(" "+u);
    PreOrder(T[u].left);
    PreOrder(T[u].right);
}
static void InOrder(int u) {
    if(u == NIL)
        return;
    InOrder(T[u].left);
    System.out.print(" "+u);
    InOrder(T[u].right);
}
static void PostOrder(int u) {
    if(u == NIL)
        return;
    PostOrder(T[u].left);
    PostOrder(T[u].right);
    System.out.print(" "+u);
}

完整代码

package AizuOJ.BinarySearchTree;

import java.util.Scanner;

public class BinaryTree {
    public static class Node {
        int parent, left, right;

        public Node() {
            this.parent = -1;
            this.left = -1;
            this.right = -1;
        }
    }

    public static int NIL = -1;
    public static int MAX = 10000;
    static Node[] T;
    static int[] H;
    static int[] D;
    public static int n;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n;
        n = input.nextInt();
        T = new Node[n];
        H = new int[n];
        D = new int[n];
        for (int i = 0; i < n; i++) {
            T[i] = new Node();
            T[i].parent = NIL;
        }
        int v, l, r;
        for (int i = 0; i < n; i++) {
            v = input.nextInt();
            l = input.nextInt();
            r = input.nextInt();
            T[v].left = l;
            T[v].right = r;
            if (l != NIL)
                T[l].parent = v;
            if (r != NIL)
                T[r].parent = v;
        }
        int root = 0;
        for (int i = 0; i < n; i++) {
            if (T[i].parent == NIL)
                root = i;
        }
//        setDepth(root, 0);
//        setHeight(root);
//        Print(n);
        System.out.println("Preorder");
        PreOrder(root);
        System.out.println();
        System.out.println("Inorder");
        InOrder(root);
        System.out.println();
        System.out.println("Postorder");
        PostOrder(root);
        System.out.println();
    }

    public static void setDepth(int u, int d) {
        if (u == NIL)
            return;
        D[u] = d;
        setDepth(T[u].left, d + 1);
        setDepth(T[u].right, d + 1);
    }

    public static int setHeight(int u) {
        int h1 = 0;
        int h2 = 0;
        if (T[u].left != NIL)
            h1 = setHeight(T[u].left) + 1;
        if (T[u].right != NIL)
            h2 = setHeight(T[u].right) + 1;
        return H[u] = Math.max(h1, h2);
    }

    public static void Print(int num) {
        for (int i = 0; i < num; i++) {
            System.out.print("node " + i + ": parent = " + T[i].parent + ", sibling = " + getSibling(i));
            System.out.print(", degree = " + getDegree(i) + ", ");
            System.out.print("depth = " + D[i] + ", ");
            System.out.print("height = " + H[i] + ", ");
            System.out.print(type(i));
            System.out.println();
        }
    }

    static int getDegree(int u) {
        int d = 0;
        if (T[u].left != NIL)
            d++;
        if (T[u].right != NIL)
            d++;
        return d;
    }

    static String type(int u) {
        String type;
        if (T[u].parent == -1)
            type = "root";
        else if (T[u].left == NIL && T[u].right == NIL)
            type = "leaf";
        else
            type = "internal node";
        return type;
    }

    static int getSibling(int u) {
        if (T[u].parent != -1) {
            int p = T[u].parent;
            if (T[p].left != -1 && T[p].left != u) {
                return T[p].left;
            } else if (T[p].right != -1 && T[p].right != u) {
                return T[p].right;
            }
        }
        return -1;
    }

    static void PreOrder(int u) {
        if(u == NIL)
            return;
        System.out.print(" "+u);
        PreOrder(T[u].left);
        PreOrder(T[u].right);
    }
    static void InOrder(int u) {
        if(u == NIL)
            return;
        InOrder(T[u].left);
        System.out.print(" "+u);
        InOrder(T[u].right);
    }
    static void PostOrder(int u) {
        if(u == NIL)
            return;
        PostOrder(T[u].left);
        PostOrder(T[u].right);
        System.out.print(" "+u);
    }
}

题目链接ALDS1_7_B
ALDS1_7_C

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值