二叉树的创建【字符】(java)

package test.Tree;

import java.util.LinkedList;
import java.util.Queue;

 class Node<T> {
    T val;
    Node left;
    Node right;

    public Node() {
    }

    public Node(T val) {
        this.val = val;
    }

    public Node(T val, Node left, Node right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Tree_String {
    private static char[] arr = {'A', 'B', 'C', ',', ',', 'D', 'E', ',', 'G', ',', ',', 'F', ',', ',', ','};
    private static int count = 0;

    /**
     * 构建二叉树(,代表空结点)
     *
     * @return
     */
    public static Node Create() {
        if (count >= arr.length || arr[count] == ',') {
            count++;// 跳过空结点
            return null;
        }
        Node node = new Node<>(arr[count++]);
        node.left = Create();
        node.right = Create();
        return node;
    }

    /**
     * 先序遍历
     *
     * @param root
     */
    public static void preOrder(Node root) {
        if (root != null) {
            System.out.print(root.val + " ");
            preOrder(root.left);
            preOrder(root.right);
        }
    }

    /**
     * 中序遍历
     *
     * @param root
     */
    public static void inOrder(Node root) {
        if (root != null) {
            inOrder(root.left);
            System.out.print(root.val + " ");
            inOrder(root.right);
        }
    }

    /**
     * 后序遍历
     *
     * @param root
     */
    public static void postOrder(Node root) {
        if (root != null) {
            postOrder(root.left);
            postOrder(root.right);
            System.out.print(root.val + " ");
        }
    }

    /**
     * 层次遍历
     *
     * @param root
     */
    public static void levOrder(Node root) {
        if (root != null) {
            Node p = root;
            Queue<Node> queue = new LinkedList<>();
            queue.add(p);
            while (!queue.isEmpty()) {
                p = queue.poll();
                System.out.print(p.val + " ");
                if (p.left != null) {
                    queue.add(p.left);
                }
                if (p.right != null) {
                    queue.add(p.right);
                }
            }
        }
    }

    /**
     * 计算叶子节点的个数
     *
     * @param root
     * @return
     */
    public static int getSize(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        } else {
            return getSize(root.left) + getSize(root.right);
        }
    }

    /**
     * 计算二叉树的高度
     *
     * @param root
     * @return
     */
    public static int getHeight(Node root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return (leftHeight > rightHeight) ? (leftHeight + 1) : (rightHeight + 1);
    }

    public static void main(String[] args) {
        Node root = Create();
        System.out.println("先序遍历:");
        preOrder(root);

        System.out.println("\n中序遍历:");
        inOrder(root);

        System.out.println("\n后序遍历:");
        postOrder(root);

        System.out.println("\n层次遍历:");
        levOrder(root);

        System.out.println("\n二叉树叶子结点数:" + getSize(root));
        System.out.println("二叉树高度:" + getHeight(root));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值