二叉搜索树

244 阅读1分钟

二叉搜索树

二叉搜索树,不一定平衡,性能不稳定,但是可以用,操作包括二叉树的先序遍历,中序遍历,后序遍历,层次遍历的递归和非递归算法,以及插入,删除,最大值,最小值等操作。

public class BinarySearchTree {

    private Node root;

    public BinarySearchTree() {
    }

    // 最大值
    public int max() {
        return max(root).data;
    }

    // 最小值
    public int min() {
        return min(root).data;
    }

    // 查询
    public Node search(int data) {
        if (root != null) {
            Node p = root;
            while (true) {
                if (data < p.data) {
                    if (p.left != null) {
                        p = p.left;
                    }else{
                        break;
                    }
                }
                if (data > p.data) {
                    if (p.left != null) {
                        p = p.right;
                    }else{
                        break;
                    }
                }
                if (data == p.data) {
                    return p;
                }
            }
        }
        return null;
    }

    // 查询范围 最大最小值
    public LinkedList<Integer> searchRange(int min, boolean includemin, int max, boolean includemax) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        Stack<Node> stack = new Stack<>();
        Node p = root;
        while (p != null && min != p.data) {
            if (min < p.data) {
                stack.push(p);
                if (p.left != null) {
                    p = p.left;
                }
            }
            if (min > p.data && p.right != null) {
                p = p.right;
            }
        }
        if (p.data == min) {
            if (includemin) {
                linkedList.addLast((p.data));
            }
            while (!stack.empty()){
                Node node = stack.pop();
                if (includemax && node.data == max){
                    linkedList.addLast(node.data);
                    break;
                }
                if (node.data > max) {
                    break;
                }
                linkedList.addLast(node.data);
                if (node.right != null) {
                    node = node.right;
                }
                while (node.left != null) {
                    stack.push(node);
                }
            }
        }
        return linkedList;
    }

    // 查询范围 最小值
    public LinkedList<Integer> searchRangeInMin(int min, boolean includemin) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        Stack<Node> stack = new Stack<>();
        Node p = root;
        while (p != null && min != p.data) {
            if (min < p.data) {
                stack.push(p);
                if (p.left != null) {
                    p = p.left;
                }
            }
            if (min > p.data && p.right != null) {
                p = p.right;
            }
        }
        if (p.data == min) {
            if (includemin) {
                linkedList.addLast((p.data));
            }
            while (!stack.empty()){
                Node node = stack.pop();
                linkedList.addLast(node.data);
                if (node.right != null) {
                    node = node.right;
                }
                while (node.left != null) {
                    stack.push(node);
                }
            }
        }
        return linkedList;
    }

    // 查询范围 最大值
    public LinkedList<Integer> searchRangeInMax(int max, boolean includemax) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        Stack<Node> stack = new Stack<>();
        Node p = root;
        stack.push(p);
        while (p.left != null) {
            stack.push(p.left);
            p = p.left;
        }
        while (!stack.empty()){
            Node node = stack.pop();
            if (includemax && node.data == max){
                linkedList.addLast(node.data);
                break;
            }
            if (node.data > max) {
                break;
            }
            linkedList.addLast(node.data);
            if (node.right != null) {
                node = node.right;
            }
            while (node.left != null) {
                stack.push(node);
            }
        }
        return linkedList;
    }

    // 插入
    public void insert(int data) {
        Node node = new Node(data);
        if (root == null) {
            root = node;
        } else {
            Node p = root;
            while (true) {
                if (data < p.data) {
                    if (p.left != null) {
                        p = p.left;
                    }else{
                        p.left = node;
                        node.parent = p;
                        break;
                    }
                } else {
                    if (p.right != null) {
                        p = p.right;
                    }else{
                        p.right = node;
                        node.parent = p;
                        break;
                    }
                }
            }
        }
    }

    // 删除
    public void remove(int data) {
        if (root != null) {
            Node p = search(data);
            if (p == root) {
                root = null;
            } else {
                Node q = p.parent;
                if (p.left == null && p.right == null) {
                    if (p == q.left) q.left = null;
                    if (p == q.right) q.right = null;
                }
                if (p.left != null && p.right == null) {
                    if (p == q.left) {
                        q.left = p.left;
                        q.left.parent = q;
                    }
                    if (p == q.right) {
                        q.right = p.left;
                        q.right.parent = q;
                    }
                }
                if (p.left == null && p.right != null) {
                    if (p == q.left) {
                        q.left = p.right;
                        q.left.parent = q;
                    }
                    if (p == q.right) {
                        q.right = p.right;
                        q.right.parent = q;
                    }
                }
                if (p.left != null && p.right != null) {
                    Node node = min(p.right);
                    node.parent.left = node.right;
                    node.left = p.left;
                    node.right = p.right;
                    node.parent = p.parent;
                    if (p == q.left) {
                        q.left = node;
                    }
                    if (p == q.right) {
                        q.right = node;
                    }
                }
            }

        }
    }

    // 先序遍历
    public void preOrderTraverse() {
        preOrderTraverse(root);
    }

    // 中序遍历
    public void inOrderTraverse() {
        inOrderTraverse(root);
    }

    // 后序遍历
    public void postOrderTraverse() {
        postOrderTraverse(root);
    }

    // 先序遍历
    public void preOrderTraverse1() {
        preOrderTraverse1(root);
    }

    // 中序遍历
    public void inOrderTraverse1() {
        inOrderTraverse1(root);
    }

    // 后序遍历
    public void postOrderTraverse1() {
        postOrderTraverse1(root);
    }

    // 层次序遍历
    public void levelOrderTraverse1() {
        levelOrderTraverse1(root);
    }

    // 先序遍历(递归实现)
    private void preOrderTraverse(Node root) {
        if (root != null) {
            System.out.println(root.data);
            preOrderTraverse(root.left);
            preOrderTraverse(root.right);
        }
    }

    // 中序遍历(递归实现)
    private void inOrderTraverse(Node root) {
        if (root != null) {
            inOrderTraverse(root.left);
            System.out.println(root.data);
            inOrderTraverse(root.right);
        }
    }

    // 后序遍历(递归实现)
    private void postOrderTraverse(Node root) {
        if (root != null) {
            postOrderTraverse(root.left);
            postOrderTraverse(root.right);
            System.out.println(root.data);
        }
    }

    // 先序遍历(非递归实现)
    private void preOrderTraverse1(Node root) {
        if (root != null) {
            Stack<Node> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                Node node = stack.pop();
                System.out.println(node.data);
                if (node.right != null) {
                    stack.push(node.right);
                }
                if (node.left != null) {
                    stack.push(node.left);
                }
            }
        }
    }

    // 中序遍历(非递归实现)
    private void inOrderTraverse1(Node root) {
        if (root != null) {
            Stack<Node> stack = new Stack<>();
            Node p = root;
            while (p != null || !stack.isEmpty()) {
                while (p != null) {
                    stack.push(p);
                    p = p.left;
                }
                if (!stack.isEmpty()) {
                    p = stack.pop();
                    System.out.println(p.data);
                    p = p.right;
                }
            }
        }
    }

    // 后序遍历(非递归实现)
    private void postOrderTraverse1(Node root) {
        if (root != null) {
            Stack<Node> stack1 = new Stack<>();
            Stack<Node> stack2 = new Stack<>();
            stack1.push(root);
            while (!stack1.isEmpty()){
                Node p = stack1.pop();
                stack2.push(p);
                if (p.left != null){
                    stack1.push(p.left);
                }
                if (p.right != null) {
                    stack1.push(p.right);
                }
            }
            while (!stack2.isEmpty()) {
                System.out.println(stack2.pop().data + " ");
            }
        }
    }

    // 层次序遍历(非递归实现)
    private void levelOrderTraverse1(Node root) {
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node node = queue.remove();
            System.out.println(node.data);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }

    private Node max(Node root) {
        if (root != null) {
            Node p = root;
            while (p.right != null) {
                p = p.right;
            }
            return p;
        }
        return null;
    }

    private Node min(Node root) {
        if (root != null) {
            Node p = root;
            while (p.left != null) {
                p = p.left;
            }
            return p;
        }
        return null;
    }

    public static class Node {
        int data;
        Node parent, left, right;


        public Node() {
        }

        public Node(int data) {
            this.data = data;
            this.left = this.right = null;
        }

    }
}