二叉树的建立与遍历(Java版本)

版权声明:作者:N3verL4nd 出处: https://blog.csdn.net/lgh1992314/article/details/79622089

前序遍历:

若二叉树为空,则算法结束,否则:
访问根结点;
前序遍历根结点的左子树;
前序遍历根结点的右子树。

中序遍历:

若二叉树为空,则算法结束;否则:
中序遍历根结点的左子树;
访问根结点;
中序遍历根结点的右子树。

后序遍历:

若二叉树为空,则算法结束,否则:
后序遍历根结点的左子树;
后序遍历根结点的右子树;
访问根结点。

层次遍历

广度优先遍历二叉树(层序遍历)是用队列来实现的,从二叉树的第一层(根结点)开始,自上至下逐层遍历;在同一层中,按照从左到右的顺序对结点逐一访问。
按照从根结点至叶结点、从左子树至右子树的次序访问二叉树的结点。
算法:
1. 初始化一个队列,并把根结点入列队;
2. 当队列为非空时,循环执行步骤3到步骤5,否则执行6;
3. 出队列取得一个结点,访问该结点;
4. 若该结点的左子树为非空,则将该结点的左子树入队列;
5. 若该结点的右子树为非空,则将该结点的右子树入队列;
6. 结束。

这里写图片描述

前序遍历:A B C D E G F
中序遍历:C B E G D F A
后序遍历:C G E F D B A
层次遍历:A B C D E F G
叶子结点由3个: C G F
该二叉树的高度为:5

package study;

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 BinaryTree {
    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));
    }
}

参考:
http://www.cnblogs.com/way_testlife/archive/2010/10/07/1845264.html

猜你喜欢

转载自blog.csdn.net/lgh1992314/article/details/79622089