• java——二分搜索树 BST(递归、非递归)


    ~

    package Date_pacage;
    import java.util.Stack;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Random;
    
    //二分搜索树
    public class BST <E extends Comparable<E>> {
        private class Node{
            public E e;
            public Node left, right;
            
            public Node(E e) {
                this.e = e;
                left = null;
                right = null;
            }
        }
        
        private Node root;
        private int size;
        public BST() {
            root = null;
            size = 0;
        }
        public int size() {
            return size;
        }
        public boolean isEmpty(){
            return size == 0;
        }
        //相等的元素不会重复添加
        public void add(E e) {
            root = add(root, e);
        }
        private Node add(Node node, E e) {
            if(node == null) {
                size ++;
                node = new Node(e);
            }
            if(e.compareTo(node.e) < 0) {
                node.left = add(node.left, e);
            }else if(e.compareTo(node.e) > 0) {
                node.right = add(node.right, e);
            }
            return node;
        }
        public boolean contains(E e) {
            return contains(root, e);
        }
        //搜索二分搜索树是否包含元素e
        private boolean contains(Node node, E e) {
            if(node == null)
                return false;
            if(e.compareTo(node.e) == 0) {
                return true;
            }else if(e.compareTo(node.e) > 0) {
                return contains(node.right, e);
            }else {
                return contains(node.left, e);
            }
        }
        //前序遍历
        public void preOrder() {
            preOrder(root);
        }
        private void preOrder(Node node) {
            if(node == null) {
                return;
            }
            System.out.println(node.e);
            preOrder(node.left);
            preOrder(node.right);
        }
        //非递归前序遍历:栈
        public void preOrderNR() {
            Stack<Node> stack = new Stack<>();
            stack.push(root);
            while(!stack.isEmpty()) {
                Node cur = stack.pop();
                System.out.println(cur.e);
                if(cur.right != null)
                    stack.push(cur.right);
                if(cur.left != null) {
                    stack.push(cur.left);
                }
            }
        }
        //中序遍历
        public void inOrder() {
            preOrder(root);
        }
        private void inOrder(Node node) {
            if(node == null) {
                return;
            }
            inOrder(node.left);
            System.out.println(node.e);
            inOrder(node.right);
        }
        //后序遍历
        public void postOrder() {
            postOrder(root);
        }
        private void postOrder(Node node){
            if(node == null) {
                return;
            }
            postOrder(node.left);
            postOrder(node.right);
            System.out.println(node.e);
        }
        //非递归 层序遍历(广度优先遍历):队列
        public void levelOrder() {
            Queue<Node> q = new LinkedList<>();
            q.add(root);
            while(!q.isEmpty()) {
                Node cur = q.remove();
                System.out.println(cur.e);
                if(cur.left != null) {
                    q.add(cur.left);
                }
                if(cur.right != null) {
                    q.add(cur.right);
                }
            }
        }
        //寻找二分搜索树的最小元素
        public E minimum() {
            if(size == 0) {
                throw new IllegalArgumentException("BST is empty!");
            }
            return minimum(root).e;
        }
        private Node minimum(Node node) {
            if(node.left == null) {
                return node;
            }
            return minimum(node.left);
        }
        public E removeMin() {
            E ret = minimum();
            root = removeMin(root);
            return ret;
        }
        //删除掉以node为跟的二分搜索树的最小a节点
        //返回删除节点后的新的二分搜索树的跟
        private Node removeMin(Node node) {
            if(node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            node.left = removeMin(node.left);
            return node;
        }
        @Override
        public String toString() {
            StringBuilder res = new StringBuilder();
            generateBSTString(root, 0, res);
            return res.toString();
        }
        private void generateBSTString(Node node, int depth, StringBuilder res) {
            if(node == null) {
                res.append(generateDepthString(depth) + "null
    ");
                return;
            }
            res.append(generateDepthString(depth) + node.e + "
    ");
            generateBSTString(node.left, depth+1, res);
            generateBSTString(node.right, depth+1, res);
        }
        
        private String generateDepthString(int depth) {
            StringBuilder res = new StringBuilder();
            for(int i = 0 ; i < depth ; i ++) {
                res.append("--");
            }
            return res.toString();
        }
        
        public static void main(String[] args) {
            BST<Integer> bst = new BST<>();
            Random random = new Random();
            int n = 1000;
            for(int i = 0 ; i < n ; i ++) {
                //从0 到10000的一个数
                bst.add(random.nextInt(10000));
            }
            ArrayList<Integer> nums = new ArrayList<>();
            while( !bst.isEmpty()) {
                nums.add(bst.removeMin());
            }
            System.out.println(nums);
            
        }
    }
  • 相关阅读:
    Vasya and Endless Credits CodeForces
    Dreamoon and Strings CodeForces
    Online Meeting CodeForces
    数塔取数 基础dp
    1001 数组中和等于K的数对 1090 3个数和为0
    1091 线段的重叠
    51nod 最小周长
    走格子 51nod
    1289 大鱼吃小鱼
    POJ 1979 Red and Black
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/9846591.html
Copyright © 2020-2023  润新知