数据结构——二叉树 创建 和 遍历

import java.util.ArrayList;

/**
 * 链表需要知道头结点, 树需要知道根节点
 *           A
 *      B        C
 *   D    E   F    G
 * H
 * <p>
 * Created by exile on 2017/8/28.
 */
public class BinaryTree {
    private ArrayList<TreeNode> treeNodes = new ArrayList<>();
    private String[] strings = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
    private TreeNode root = null;

    public BinaryTree() {
        for (int i = 0; i < strings.length; i++) {
            treeNodes.add(new TreeNode(i, strings[i]));
        }
        createBinaryTree();
    }

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        System.out.println("高度:" + binaryTree.getHeight());
        System.out.println("节点数:" + binaryTree.getNodeSize() + "\n 数组长度:" + binaryTree.strings.length);

        binaryTree.preOrder(binaryTree.root);
    }

    /**
     * 创建一个树
     * 根据二叉树性质 5 :
     * 5.若对含 n 个结点的完全二叉树从上到下且从左至右进行 1 至 n 的编号,则对完全二叉树中任意一个编号为 i 的结点:
     * (1) 若 i=1,则该结点是二叉树的根,无双亲, 否则,编号为 [i/2] 的结点为其双亲结点;
     * (2) 若 2i>n,则该结点无左孩子,  否则,编号为 2i 的结点为其左孩子结点;
     * (3) 若 2i+1>n,则该结点无右孩子结点,  否则,编号为2i+1 的结点为其右孩子结点。
     */
    public void createBinaryTree() {
        for (int index = 0; index < strings.length / 2; index++) {
            int leftIndex = index * 2 + 1;
            int rightIndex = index * 2 + 2;
            TreeNode temp = treeNodes.get(index);

            if (leftIndex >= strings.length) {
                System.out.println("没有左结点");
                return;
            }
            temp.leftChild = treeNodes.get(leftIndex);

            if (rightIndex >= strings.length) {
                System.out.println("没有右结点");
                return;
            }
            temp.rightChild = treeNodes.get(rightIndex);
            if (index == 0) {
                root = temp;
            }

        }
    }

    /**
     * 获取高度
     */
    public int getHeight() {

        return getHeight(root);

    }

    private int getHeight(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int leftHeight = getHeight(node.leftChild);
        int rightHeight = getHeight(node.rightChild);
        return leftHeight < rightHeight ? rightHeight + 1 : leftHeight + 1;
    }

    /**
     * 获取节点数的方法
     *
     * @return
     */
    public int getNodeSize() {
        return getNodeSize(root);
    }

    private int getNodeSize(TreeNode root) {
        if (root == null) {
            return 0;
        }
        System.out.println("getNodeSize遍历节点:" + root.data);
        return 1 + getNodeSize(root.leftChild) + getNodeSize(root.rightChild);
    }

    /**
     * 前序遍历
     */
    public void preOrder(TreeNode node){
        if (node == null) {
            return;
        }
        System.out.println("pre:"+node.data);
        preOrder(node.leftChild);
        preOrder(node.rightChild);
    }

    public class TreeNode {
        private int index;
        private String data;
        private TreeNode leftChild;
        private TreeNode rightChild;

        public TreeNode(int index, String data) {
            this.index = index;
            this.data = data;
        }

    }


}

    原文作者:埃赛尔
    原文地址: https://www.jianshu.com/p/5002fb0ef304
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞