java手写常用数据结构和算法

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

class Scratch {
    public static void main(String[] args) {
        Tree tree = new Tree();
        for (int j = 0; j < 10; j++) {
            tree.putVal(j);
        }
        Tree.Node root = tree.root;
        recurTree(root);
        iterTree(root);
        System.err.println("..........");
        recurTree2(root);
        iterTree2(root);
        System.err.println("----------");
        recurTree3(root);
        iterTree3(root);

        HashMap map = new HashMap();
        for (int j = 0; j < 20; j++) {
            map.put("key" + j, j);
        }
        System.err.println(map.get("key16"));
        map.put("key16", "overWrite value");

        ArrayList arrayList = new ArrayList();
        for (int j = 0; j < 20; j++) {
            System.err.println(map.get("key" + j));
            arrayList.add(j);
        }

        for (int j = 0; j < 20; j++) {
            System.err.println(arrayList.get(j));
        }
        System.err.println(fibbonacc(5));
    }

    static long fibbonacc(long n) {
        if (n <= 1)
            return n;
        return fibbonacc(n - 2) + fibbonacc(n - 1);
    }

    static int parseInt(String str) {
        if (!str.matches("\\d+")) {
            throw new NumberFormatException();
        }
        int len = str.length();
        int l = len - 1;
        int val = 0;
        for (int j = 0; j < len; j++) {
            int bit = 1;
            for (int k = l--; k > 0; k--) {
                bit *= 10;
            }
            val += (str.charAt(j) - '0') * bit;
        }
        return val;
    }

    static void recurTree(Tree.Node node) {
        if (node != null) {
            System.err.println(node.val);
            recurTree(node.left);
            recurTree(node.right);
        }
    }

    static void recurTree2(Tree.Node node) {
        if (node != null) {
            recurTree2(node.left);
            System.err.println(node.val);
            recurTree2(node.right);
        }
    }

    static void recurTree3(Tree.Node node) {
        if (node != null) {
            recurTree3(node.left);
            recurTree3(node.right);
            System.err.println(node.val);
        }
    }

    /**
     * 先序遍历,从根,左子树,右子树
     *
     * @param node
     */
    static void iterTree(Tree.Node node) {
        Stack<Tree.Node> stack = new Stack();
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                System.err.println(node.val);
                //有左孩子则压栈
                stack.push(node);
                //往左找
                node = node.left;
            } else {
                //无左孩子则出栈(最近访问的节点)
                node = stack.pop();
                //往右找
                node = node.right;
            }
        }
    }

    /**
     * 中序遍历,从左子树开始,根,右子树
     *
     * @param node
     */
    static void iterTree2(Tree.Node node) {
        Stack<Tree.Node> stack = new Stack();
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                stack.push(node);
                node = node.left;
            } else {
                node = stack.pop();
                System.err.println(node.val);
                node = node.right;
            }
        }
    }

    /**
     * 后序遍历,从左子树开始,右子树,根
     *
     * @param node
     */
    static void iterTree3(Tree.Node node) {
        Stack<Tree.Node> stack = new Stack();
        //辅助栈存放根,右,左
        Stack<Tree.Node> stack_ = new Stack<>();
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                stack.push(node);
                stack_.push(node);
                node = node.right;
            } else {
                node = stack.pop();
                node = node.left;
            }
        }
        while (stack_.iterator().hasNext()) {
            System.err.println(stack_.pop().val);
        }
    }

    static class Tree {
        Node root;
        Node last;
        int fork = 2;

        public Tree() {
        }

        void putVal(Object val) {
            if (root == null) {
                root = new Node(val);
                last = root;
            } else {
                Node newNode = new Node(val, last, null, null);
                if (fork == 2)
                    last.left = newNode;
                if (fork == 1)
                    last.right = newNode;
                fork--;
                if (last.right != null && last.left != null) {
                    last = newNode;
                    fork = 2;
                }
            }
        }

        static class Node {
            Object val;
            Node parent, left, right;

            public Node(Object val) {
                this.val = val;
            }

            public Node(Object val, Node parent, Node left, Node right) {
                this.val = val;
                this.parent = parent;
                this.left = left;
                this.right = right;
            }
        }

    }

    static class HashMap<K, V> {
        Node[] nodes = new Node[8];

        void put(K k, V v) {
            int hc = k.hashCode();
            int idx = hc % 8;
            Node node = nodes[idx];
            if (node == null)
                nodes[idx] = node = new Node();
            node.addEntry(new Entry(k, v));
        }

        V get(K k) {
            if (k == null)
                throw new RuntimeException("key must not be null");
            int hc = k.hashCode();
            int idx = hc % 8;
            Node node = nodes[idx];
            if (node == null)
                throw new RuntimeException("map has no data");
            return ((V) node.getVal(k));
        }

        static class Node<K, V> {
            LinkedList entrys = new LinkedList<>();

            void addEntry(Entry entry) {
                Entry e = entrys.get(entry.k);
                if (e != null)
                    e.v = entry.v;
                else
                    entrys.add(entry);
            }

            V getVal(K k) {
                return ((V) entrys.get(k).v);
            }
        }

        static class Entry<K, V> {
            K k;
            V v;
            Entry next;

            public Entry(K k, V v) {
                this.k = k;
                this.v = v;
            }
        }

        static class LinkedList<K, V> {
            Entry<K, V> head, last;

            public void add(Entry entry) {
                Entry tail_ = entry;
                if (head == null) {
                    last = head = entry;
                } else {
                    last.next = entry;
                    last = tail_;
                }
            }

            public Entry get(K k) {
                Entry tmp = head;
                while (tmp != null) {
                    if (tmp.k.equals(k)) {
                        return tmp;
                    } else {
                        tmp = tmp.next;
                    }
                }
                return null;
            }
        }

    }

    static class ArrayList<E> {
        int initLength = 8;
        Object[] list = new Object[initLength];
        int count = 0;

        void add(E e) {
            if (count == initLength - 1) {
                int newLength = list.length;
                list = Arrays.copyOf(list, newLength + 1);
                list[newLength - 1] = e;
            } else
                list[count++] = e;
        }

        Object get(int i) {
            return list[i];
        }
    }
}
发布了22 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lmx1989219/article/details/98083077