• 二叉树排序和堆排序


    View Code
    /**
     * TODO 折半查找,二叉树与后序遍历,堆排序
     * 
     */
    
    import java.util.Arrays;
    import java.util.Random;
    
    public class search {
    
        public static int discount(Integer[] data, int t) {
            int low = 0;
            int high = data.length - 1;
    
            while (low <= high) {
                int mid = (low + high) / 2;
                if (t < data[mid]) {
                    high = mid - 1;
                } else if (t > data[mid]) {
                    low = mid + 1;
                } else {
                    return mid;
                }
            }
            return -1;
        }
    
        public static class BiTree<T extends Comparable<T>> {
            T data;
            BiTree<T> rChild, lChild;
    
            public BiTree(T data) {
                super();
                this.data = data;
            }
    
            public T getData() {
                return data;
            }
    
            public void setData(T data) {
                this.data = data;
            }
    
            public BiTree<T> getrChild() {
                return rChild;
            }
    
            public void setrChild(BiTree<T> rChild) {
                this.rChild = rChild;
            }
    
            public BiTree<T> getlChild() {
                return lChild;
            }
    
            public void setlChild(BiTree<T> lChild) {
                this.lChild = lChild;
            }
    
            public void insertChild(BiTree<T> c) {
                if (c.getData().compareTo(data) < 0) {
                    if (rChild == null) {
                        rChild = c;
                        return;
                    }
                    rChild.insertChild(c);
                } else {
                    if (lChild == null) {
                        lChild = c;
                        return;
                    }
                    lChild.insertChild(c);
                }
            }
    
            /**
             * 后序遍历输出
             */
            @Override
            public String toString() {
                // TODO Auto-generated method stub
                StringBuilder sb = new StringBuilder();
                if (rChild != null) {
                    sb.append(rChild.toString());
                }
                sb.append(data).append(",");
                if (lChild != null) {
                    sb.append(lChild.toString());
                }
                return sb.toString();
            }
        }
    
        public static <T extends Comparable<T>> void createBiTree(T[] data) {
            BiTree<T> root = new BiTree<T>(data[0]);
            for (int i = 1; i < data.length; i++) {
                root.insertChild(new BiTree<T>(data[i]));
            }
            System.out.println(Arrays.toString(root.toString().split(",")));
        }
    
        /**
         * 小顶堆
         * 
         * @param data
         */
        public static <T extends Comparable<T>> void heapAdjust(T data[], int start) {
            if (start * 2 > data.length - 1) {
                return;
            }
            for (int i = start; i > 0; i--) {
                if (i * 2 + 1 <= data.length - 1) {
                    int r = 0;
                    if (data[i * 2].compareTo(data[i * 2 + 1]) <= 0) {
                        if (data[i].compareTo(data[i * 2]) > 0) {
                            T tmpT = data[i * 2];
                            data[i * 2] = data[i];
                            data[i] = tmpT;
                            r = 1;
                        }
                    } else if (data[i * 2].compareTo(data[i * 2 + 1]) > 0) {
                        if (data[i].compareTo(data[i * 2 + 1]) > 0) {
                            T tmpT = data[i * 2 + 1];
                            data[i * 2 + 1] = data[i];
                            data[i] = tmpT;
                            r = -1;
                        }
                    }
    
                    if (r == 1) {
                        heapAdjust(data, i * 2);
                    } else if (r == -1) {
                        heapAdjust(data, i * 2 + 1);
                    }
                } else {
                    if (data[i].compareTo(data[i * 2]) > 0) {
                        T tmpT = data[i * 2];
                        data[i * 2] = data[i];
                        data[i] = tmpT;
                    }
                }
    
            }
        }
    
        public static <T extends Comparable<T>> void heapSort(T data[]) {
            // Integer[] data = new Integer[data2.length + 1];
            // System.arraycopy(data2, 0, data, 1, data2.length);
            // int res[] = new int[data.length - 1];
            T res[] = Arrays.copyOf(data, data.length);
            for (int i = 1; i < res.length; i++) {
                heapAdjust(data, (data.length - 1) / 2);
                res[i] = data[1];
                data[1] = data[data.length - 1];
                data = Arrays.copyOf(data, data.length - 1);
            }
            System.out.println(Arrays.toString(res));
        }
    
        public static void main(String args[]) {
            Double[] data = new Double[12];
            for (int j = 0; j < 10; j++) {
                Random random = new Random(System.currentTimeMillis());
                data = new Double[10];
                for (int i = 0; i < data.length; i++) {
                    data[i] = Math.abs(random.nextDouble() % 10);
                }
                System.out.println(Arrays.toString(data));
                createBiTree(data);
                Double[] data2 = new Double[data.length + 1];
                System.arraycopy(data, 0, data2, 1, data.length);
                heapSort(data2);
            }
        }
    }
    xxx
  • 相关阅读:
    键值表
    CRC校验方法
    extern 使用
    编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
    学习积累
    二分查找写法
    生活技术常识
    JAVA实例
    JAVA_Sprint学习(一)
    手把手使用Git?
  • 原文地址:https://www.cnblogs.com/valder/p/2548182.html
Copyright © 2020-2023  润新知