手敲数据结构——二分搜索树

import javax.management.Query;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BST> {

    private class Node {
        public E e;
        public Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node root;
    private int size;

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

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //向二分搜索树中添加新的元素e
    public void add(E e) {
        root = add(root, e);
    }

    // 向以node为根的二分搜索树中插入元素E,递归算法
    // 返回插入新节点后二分搜索树的根
    public Node add(Node node, E e) {
        if (node == null) {
            size++;
            return 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;
    }

    //看二分搜索树中是否包含元素e
    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node node, E e) {
        if (node == null)
            return false;
        if (e.compareTo(node.e) < 0) {
            return contains(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            return contains(node.right, e);
        }
        return true;
    }

    //查询二分搜索树的最小元素
    public E minimun() {
        return minimun(root).e;
    }

    private Node minimun(Node node) {
        if (isEmpty()) throw new IllegalArgumentException("BST is empty");
        if (node.left != null)
            return minimun(node.left);
        return node;
    }

    //查询二分搜索树的最大元素
    public E maximun() {
        return maximun(root).e;
    }

    private Node maximun(Node node) {
        if (isEmpty()) throw new IllegalArgumentException("BST is empty");
        if (node.right != null) {
            return maximun(node.right);
        }
        return node;
    }

    //前序遍历
    public void preOrder() {
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) return;
        System.out.println(node);
        preOrder(node.left);
        preOrder(node.right);
    }

    //从二分搜索树中删除最小值所在的节点,返回最小值
    public E removeMin() {
        E ret = minimun();
        removeMin(root);
        return ret;
    }

    //删除掉以node为根的二分搜索树中的最小节点
    //返回删除节点后新的二分搜索树的根
    private Node removeMin(Node node) {
        if (node.left == null) {
            Node right = node.right;
            node.right = null;
            size--;
            return right;
        }
        node.left = removeMin(node.left);
        return node;
    }

    public E removeMax() {
        E ret = maximun();
        removeMax(root);
        return ret;
    }

    private Node removeMax(Node node) {
        if (node.right == null) {
            //找到最大节点
            Node left = node.left;
            node.left = null;
            size--;
            return left;
        }
        node.right = removeMax(node.right);
        return node;
    }

    public void remove(E e) {
        root = remove(root,e);
    }

    //删除以node为根的二分搜索树中值为e的节点,递归算法
    //返回删除节点后新的二分搜索树的根
    private Node remove(Node node, E e) {
        if (node == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left,e);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = remove(node.right, e);
            return node;
        } else {
            //待删除节点左子树为空的情况
            if (node.left == null) {
                Node right = node.right;
                node.right = null;
                size--;
                return right;
            }
            //待删除节点有字数为空的情况
            if (node.right == null) {
                Node left = node.left;
                node.left = null;
                size--;
                return left;
            }
            //待删除节点左右字数不为空的情况
            //找到比待删除节点大的最小节点,即待删除节点右子树的最小节点
            //用这个节点顶替待删除节点的位置
            Node successor = minimun(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            //注意这里面的size--在removeMin中已经执行了
            node.left = node.right = null;
            return successor;
        }

    }

    //二分搜索树的非递归前序遍历
    public void preOrderNR() {
        Stack 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 levelOrder() {
        Queue q = new LinkedList<>();
        ((LinkedList) q).addFirst(root);
        while (!q.isEmpty()) {
            Node cur = q.remove();
            System.out.println(cur.e);
            if (cur.left != null) {
                ((LinkedList) q).addLast(cur.left);
            }
            if (cur.right != null) {
                ((LinkedList) q).addLast(cur.right);
            }
        }
    }

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

    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node);
        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);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        generateBSTString(root, 0, sb);
        return sb.toString();
    }

    //生成以node为根节点,深度为depth的描述二叉树的字符串
    private void generateBSTString(Node node, int depth, StringBuilder sb) {
        if (node == null) {
            sb.append(generateDepthString(depth)).append("null\n");
            return;
        }
        sb.append(generateDepthString(depth)).append(node.e).append("\n");
        generateBSTString(node.left, depth + 1, sb);
        generateBSTString(node.right, depth + 1, sb);
    }

    private String generateDepthString(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("--");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        BST bst = new BST<>();
        int[] nums = {5, 3, 6, 8, 4, 2};
        for (int i = 0; i < nums.length; i++) {
            bst.add(nums[i]);
        }
        ////////////////////////
        //           5
        //          /\
        //         3  6
        //        /\   \
        //        2  4   8
        ////////////////////////
        //前序5 3 2 4 6 8
        bst.preOrder();
        System.out.println();
        System.out.println(bst);
        bst.preOrderNR();
        bst.levelOrder();
//        System.out.println();
//        bst.inOrder();
//        System.out.println();
//
//        bst.postOrder();
    }
}

使用非递归的方式进行前序遍历,借助栈的数据结构:

 //二分搜索树的非递归前序遍历
    public void preOrderNR() {
        Stack 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 levelOrder() {
        Queue q = new LinkedList<>();
        ((LinkedList) q).addFirst(root);
        while (!q.isEmpty()) {
            Node cur = q.remove();
            System.out.println(cur.e);
            if (cur.left != null) {
                ((LinkedList) q).addLast(cur.left);
            }
            if (cur.right != null) {
                ((LinkedList) q).addLast(cur.right);
            }
        }
    }

问题:
中序和后续遍历的非递归实现

你可能感兴趣的:(手敲数据结构——二分搜索树)