二叉查找(排序)树/二叉树----建树,遍历

二叉查找(排序)树/二叉树----建树,遍历

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class buildBinaryTree {

    // 静态内部类建立树结构
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {}
        TreeNode(int val) {this.val = val;}
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    //建立二叉查找树
    public static TreeNode buildBST(int[] arr) {
        if (arr.length == 0) return new TreeNode();

        TreeNode root = new TreeNode(arr[0]);
        for (int i = 1; i < arr.length; i++) {
            createBST(root, arr[i]);
        }
        return root;
    }
    private static void createBST(TreeNode root, int a) {
        if (root.val > a) {
            if (root.left == null)
                root.left = new TreeNode(a);
            else
                createBST(root.left, a);
        }
        else {
            if (root.right == null)
                root.right = new TreeNode(a);
            else
                createBST(root.right, a);
        }
    }

    //建立普通的二叉树
    public static TreeNode buildBinaryTree(TreeNode root, int[] arr, int index) {
        if (index > arr.length / 2) return root;
        if (index == 1) root.val = arr[0];

        root.left = new TreeNode(arr[2 * index - 1]);
        root.right = new TreeNode(arr[2 * index]);
        buildBinaryTree(root.left, arr, index + 1);
        buildBinaryTree(root.right, arr, index + 2);

        return root;
    }

    //层次遍历二叉树
    public static List<Integer> leverOrder(TreeNode root) {
        if (root == null) return new ArrayList<>();

        List<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();

            while (size-- > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add((node.right));
            }
        }
        return list;
    }

    //中旬遍历二叉树
    public static List<Integer> inOrder(TreeNode root) {
        if (root == null) return new ArrayList<>();

        List<Integer> list = new ArrayList<>();
        inOrder(root, list);
        return list;
    }
    private static void inOrder(TreeNode root, List<Integer> list) {
        if (root == null) return;

        inOrder(root.left, list);
        list.add(root.val);
        inOrder(root.right, list);
    }

    //打印 List内容
    private static void printList(List<Integer> l) {
        for (int num : l) {
            System.out.print(num + " ");
        }
        System.out.println();
    }


    // 测试函数
    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 3, 5};
        TreeNode r1 = buildBinaryTree.buildBST(arr);
        TreeNode r2 = buildBinaryTree.buildBinaryTree(new TreeNode(), arr, 1);

        List<Integer> l1 = buildBinaryTree.inOrder(r1);
        List<Integer> l2 = buildBinaryTree.inOrder(r2);

        printList(l1);
        printList(l2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值