查找-排序-树遍历 基础算法

本文对数据结构中基础的算法进行了java版实现Demo,具体包括:二分法查找、二叉树查询、插入排序、冒泡排序、快速排序、选择排序、先序遍历、中序遍历、后序遍历、层次遍历

import java.util.*;

public class BaseAlgorithm {
    public static void main(String[] args) {
        testBinarySearch();
        testTreeSearch();
        testInsertSort();
        testBubbleSort();
        testFastSort();
        testSelectSort();
        testPreTree();
        testMidTree();
        testAfterTree();
        testLevelTree();
    }

    private static void testBinarySearch() {
        int [] collection = new int[] {1,3,4,6,8,9,12,35,76,78};
        System.out.println(binarySearch(collection, 76));
    }

    /**
     * 二分法查找
     * @param collection
     * @param val
     * @return
     */
    private static boolean binarySearch(int[] collection, int val) {
        if (null == collection || collection.length == 0) {
            return false;
        }
        int left = 0;
        int right = collection.length-1;
        int mid;
        while (left <= right) {
            mid = (left + right) / 2;
            int tmp = collection[mid];
            if (tmp == val) {
                return true;
            } else if (tmp > val) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return false;
    }

    private static void testTreeSearch() {
        System.out.println(treeSearch(initTree(), 7));
    }

    /**
     * 二叉查找树
     * @param root
     * @param value
     * @return
     */
    private static boolean treeSearch(Node root, int value) {
        if (null == root) {
            return false;
        }
        Node node = root;
        while (null != node) {
            if (node.val == value) {
                return true;
            } else if (node.val > value) {
                node = node.left;
            } else {
                node = node.right;
            }
        }
        return false;
    }

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

    private static void testInsertSort() {
        int[] arr = new int[]{6, 3, 7, 8, 4, 5, 2, 1, 9};
        insertSort(arr);
        for (int i=0; i<arr.length; i++) {
            System.out.print(arr[i]);
        }
    }

    /**
     * 直接插入排序
     * @param arr
     * @return
     */
    private static void insertSort(int[] arr) {
        if (null == arr || arr.length == 1) {
            return;
        }
        for (int i=1; i< arr.length; i++) {
            if (arr[i] < arr[i-1]) {
                int current = arr[i];
                int index = i;
                for (int j=i-1; j>=0 && arr[j] > current; j--) {
                    index = j;
                    arr[j+1] = arr[j];
                }
                arr[index] = current;
            }
        }
    }

    private static void testBubbleSort() {
        int[] arr = new int[]{6, 3, 7, 8, 4, 5, 2, 1, 9};
        bubbleSort(arr);
        for (int i=0; i<arr.length; i++) {
            System.out.print(arr[i]);
        }
    }

    /**
     * 冒泡排序
     * @param arr
     */
    private static void bubbleSort(int[] arr) {
        if (null == arr || arr.length == 1) {
            return;
        }
        for (int i=1; i<arr.length; i++) {
            for (int j=i-1; j>=0; j--) {
                if (arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                } else {
                    break;
                }
            }
        }
    }

    private static void testFastSort() {
        int[] arr = new int[]{6, 3, 7, 8, 4, 5, 2, 1, 9};
        fastSort(arr);
        for (int i=0; i<arr.length; i++) {
            System.out.print(arr[i]);
        }
    }

    /**
     * 快速排序
     * @param arr
     */
    private static void fastSort(int arr[]) {
        if (null == arr || arr.length == 1) {
            return;
        }
        fastSortRecursive(arr, 0, arr.length-1);
    }

    private static void fastSortRecursive(int[] arr, int left, int right) {
        if (left > right) {
            return;
        }
        int low = left, high = right;
        int key = arr[left];
        int temp;
        while (low < high) {
            while (key <= arr[high] && low < high) {
                high--;
            }

            while (key >= arr[low] && low < high) {
                low++;
            }

            if (low < high) {
                temp = arr[low];
                arr[low] = arr[high];
                arr[high] = temp;
            }
        }
        arr[left] = arr[low];
        arr[low] = key;
        fastSortRecursive(arr, left, low - 1);
        fastSortRecursive(arr, low+1, right);
    }

    private static void testSelectSort() {
        int[] arr = new int[]{6, 3, 7, 8, 4, 5, 2, 1, 9};
        selectSort(arr);
        for (int i=0; i<arr.length; i++) {
            System.out.print(arr[i]);
        }
    }

    /**
     * 选择排序
     * @param arr
     */
    private static void selectSort(int[] arr) {
        if (null == arr || arr.length == 1) {
            return;
        }
        for (int i=0; i<arr.length; i++) {
            int minIndex = i;
            int temp;
            for (int j=i; j<arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    private static void testPreTree() {
        System.out.println(preTree(initTree()));
    }

    /**
     * 先序遍历
     * @param root
     * @return
     */
    private static List<Integer> preTree(Node root) {
        List<Integer> resultList = new ArrayList<>();
        if (null == root) {
            return resultList;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node currentNode = stack.pop();
            resultList.add(currentNode.val);
            if (null != currentNode.right) {
                stack.push(currentNode.right);
            }
            if (null != currentNode.left) {
                stack.push(currentNode.left);
            }
        }
        return resultList;
    }

    private static void testMidTree() {
        System.out.println(midTree(initTree()));
    }

    /**
     * 中序遍历
     * @param root
     * @return
     */
    private static List<Integer> midTree(Node root) {
        List<Integer> resultList = new ArrayList<>();
        if (null == root) {
            return resultList;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node currentNode = stack.pop();
            if (null != currentNode.right) {
                stack.push(currentNode.right);
                currentNode.right = null;
            }
            if (null != currentNode.left) {
                stack.push(currentNode);
                stack.push(currentNode.left);
                currentNode.left = null;
            } else {
                resultList.add(currentNode.val);
            }
        }
        return resultList;
    }

    private static void testAfterTree() {
        System.out.println(afterTree(initTree()));
    }

    /**
     * 后序遍历
     * @param root
     * @return
     */
    private static List<Integer> afterTree(Node root) {
        List<Integer> resultList = new ArrayList<>();
        if (null == root) {
            return resultList;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node currentNode = stack.pop();
            if (null == currentNode.left && null == currentNode.right) {
                resultList.add(currentNode.val);
                continue;
            }
            stack.push(currentNode);
            if (null != currentNode.right) {
                stack.push(currentNode.right);
                currentNode.right = null;
            }
            if (null != currentNode.left) {
                stack.push(currentNode.left);
                currentNode.left = null;
            }
        }
        return resultList;
    }

    private static void testLevelTree() {
        System.out.println(levelTree(initTree()));
    }

    /**
     * 层次遍历
     * @param root
     * @return
     */
    private static List<Integer> levelTree(Node root) {
        List<Integer> resultList = new ArrayList<>();
        if (null == root) {
            return resultList;
        }
        Queue<Node> queue = new LinkedList();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node currentNode = queue.poll();
            resultList.add(currentNode.val);
            if (null != currentNode.left) {
                queue.add(currentNode.left);
            }
            if (null != currentNode.right) {
                queue.add(currentNode.right);
            }
        }
        return resultList;
    }

    private static Node initTree() {
        Node node2 = new Node(2, null, null);
        Node node7 = new Node(7, null, null);
        Node node13 = new Node(13, null, null);
        Node node16 = new Node(16, null, null);
        Node node6 = new Node(6, node2, node7);
        Node node15 = new Node(15, node13, node16);
        Node node8 = new Node(8, node6, node15);
        return node8;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_21033663/article/details/79853227