package com.wj;

import java.util.LinkedList;
import java.util.Queue;

public class BST<Key extends Comparable<Key>, Value> {

    private class Node {
        private Key key;
        private Value value;
        private Node left, right;

        public Node(Key key, Value value) {
            this.key = key;
            this.value = value;
            this.left = this.right = null;
        }
    }


    private Node root;//根节点
    private int count;

    public BST() {
        root = null;
        count = 0;
    }

    public void insert(Key key, Value value) {
        root = insert(root, key, value);
    }

    public boolean containsKey(Key key) {
        return containsKey(root, key);
    }

    public Value get(Key key){
        Node node = get(root, key);
        if (node != null) {
            return node.value;
        }
        return null;
    }

    private Node get(BST<Key, Value>.Node node, Key key) {
        if (null == node) return null;

        if (key.compareTo(node.key) == 0) {
            return node;
        } else if (key.compareTo(node.key) < 0) {
            return get(node.left, key);
        } else {
            return get(node.right, key);
        }
    }


    // 二分搜索树的前序遍历
    public void preOrder(){
        preOrder(root);
    }

    // 二分搜索树的中序遍历
    public void inOrder(){
        inOrder(root);
    }

    // 二分搜索树的后序遍历
    public void postOrder(){
        postOrder(root);
    }

    //层序遍历
    public void leaveOrder() {
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            System.out.print(node.key + " ");
            if (null != node.left) {
                queue.add(node.left);
            }

            if (null != node.right) {
                queue.add(node.right);
            }
        }
    }

    //查找最小节点key
    public Key findMin(){
        if (null == root) return null;

        return findMin(root).key;
    }

    //查找最大节点key
    public Key findMax(){
        if (null == root) return null;

        return findMax(root).key;
    }

    //删除最小值
    public void deleteMin() {
        root = deleteMin(root);
    }

    //删除最大值
    public void deleteMax() {
        root = deleteMax(root);
    }

    //删除任意值
    public void remove(Key key){
        root = remove(root, key);
    }
    
    public void remove2(Key key){
        root = remove2(root, key);
    }

    private BST<Key, Value>.Node remove(BST<Key, Value>.Node node, Key key) {
        if (null == node) return null;

        if (key.compareTo(node.key) < 0) {
            node.left = remove(node.left, key);
            return node;
        } else if (key.compareTo(node.key) > 0) {
            node.right = remove(node.right, key);
            return node;
        } else {

            if (node.left == null) {
                Node nodeRight = node.right;
                node.right = null;
                --count;
                return nodeRight;
            } else if (node.right == null) {
                Node nodeLeft = node.left;
                node.left = null;
                --count;
                return nodeLeft;
            } else {
                Node nodeRightMin = findMin(node.right);
                nodeRightMin.right = deleteMin(node.right);
                nodeRightMin.left = node.left;
                return nodeRightMin;
            }

        }
    }

    private BST<Key, Value>.Node remove2(BST<Key, Value>.Node node, Key key) {
        if (null == node || !containsKey(key)) return node;

        //获取key位置
        Node currentNode = get(node, key);
        
        if (currentNode.left == null) {
            currentNode = currentNode.right;
            currentNode.right = null;
            --count;
        } else if (currentNode.right == null) {
            currentNode = currentNode.left;
            currentNode.left = null;
            --count;
        } else {
            Node nodeRightMin = findMin(currentNode.right);
            nodeRightMin.right = deleteMin(currentNode.right);
            nodeRightMin.left = currentNode.left;
            currentNode = nodeRightMin;
        }
        
        return node;

    }

    private Node deleteMax(BST<Key, Value>.Node node) {
        if (node.right == null) {
            Node leftNode = node.left;
            node.left = null;
            --count;
            return leftNode;
        }

        node.right = deleteMax(node.right);

        return node;
    }

    private Node deleteMin(BST<Key, Value>.Node node) {
        if (node.left == null) {
            Node nodeRight = node.right;
            node.right = null;
            --count;
            return nodeRight;
        }

        node.left = deleteMin(node.left);

        return node;

    }

    private Node findMax(BST<Key, Value>.Node node) {
        if (node.right == null) return node;

        return findMax(node.right);
    }

    private Node findMin(BST<Key, Value>.Node node) {
        if (node.left == null) return node;

        return findMin(node.left);
    }

    //清空二分搜索树
    //    public void clear() {
    //        System.out.println(root);
    //        clear(root);
    ////        root = null;
    ////        count = 0;
    //    }
    //    
    //    private void clear(Node node) {
    //        System.out.println(node);
    //        node = null;
    //        count = 0;
    //            
    //    }

    private void postOrder(BST<Key, Value>.Node node) {
        if (null != node) {
            postOrder(node.left);
            postOrder(node.right);
            System.out.print(node.value + " ");
        }

    }

    private void inOrder(BST<Key, Value>.Node node) {
        if (null != node) {
            inOrder(node.left);
            System.out.print(node.value + " ");
            inOrder(node.right);
        }

    }

    private void preOrder(BST<Key, Value>.Node node) {
        if (null != node) {
            System.out.print(node.value + " ");
            preOrder(node.left);
            preOrder(node.right);
        }

    }

    private boolean containsKey(BST<Key, Value>.Node node, Key key) {
        if (null == node) return false;

        if (key.compareTo(node.key) == 0) {
            return true;
        } else if (key.compareTo(node.key) < 0) {
            return containsKey(node.left, key);
        } else {
            return containsKey(node.right, key);
        }
    }

    private Node insert(Node node, Key key, Value value) {
        if (node == null) {
            ++count;
            return new Node(key, value);
        } else if (key.compareTo(node.key) == 0) {
            node.value = value;
        } else if (key.compareTo(node.key) < 0) {
            node.left = insert(node.left, key, value);
        } else {
            node.right = insert(node.right, key, value);
        }
        return node;
    }


    public static void main(String[] args) throws InterruptedException {
        //        Map<String, String> map = new HashMap<>();

        BST<Integer, Integer> bst = new BST<>();
        bst.insert(1, 1);
        bst.insert(2, 2);
        bst.insert(16, 16);
        bst.insert(13, 13);
        bst.insert(49, 49);
        bst.insert(38, 38);
        bst.insert(3, 3);
        bst.insert(3, 3);
        bst.insert(61, 61);
        bst.insert(17, 17);
        //        bst.deleteMin();
        //        bst.deleteMin();
        //        bst.deleteMin();
        //        bst.deleteMax();
        //        bst.deleteMax();
        bst.remove2(16);
        System.out.println(bst.root == null);
        System.out.println(bst.count);
        System.out.println(bst.containsKey(3));
        System.out.println(bst.get(3));
        System.out.println("前驱遍历");
        bst.preOrder();
        System.out.println();
        System.out.println("中驱遍历");
        bst.inOrder();
        System.out.println();
        System.out.println("后驱遍历");
        bst.postOrder();
        System.out.println();
        System.out.println("层序遍历");
        bst.leaveOrder();
        System.out.println();
        System.out.println("最小值" + bst.findMin());
        System.out.println("最大值" + bst.findMax());
    }
}