查找二叉树之JAVA实现

Java实现查找二叉树的相关操作

//树的节点类
class BinaryNode<T> {
    T element;
    BinaryNode<T> left;
    BinaryNode<T> right;

    public BinaryNode(T theElement) {
        element = theElement;
    }

    public BinaryNode(T theElement, BinaryNode<T> left, BinaryNode<T> right) {
        this.element = theElement;
        this.left = left;
        this.right = right;
    }
}

//查找二叉树
public class BinarySearchTree<T extends Comparable> {
    //私有属性
    private BinaryNode<T> root;//根节点

    //构造方法
    BinarySearchTree() {
        this.root = null;
    }

    //置空操作
    public void makeEmpty() {
        this.root = null;
    }

    //判断是否为空
    public boolean isEmpty() {
        return root == null;
    }

    //判断是否包含某节点
    public boolean contains(T x) {
        return contains(x, root);
    }

    //查找最小的元素
    public T findMin() {
        if (isEmpty())
            throw new RuntimeException("searchTree is empty");
        return findMin(root).element;
    }

    //查找最大的元素
    public T findMax() {
        if (isEmpty())
            throw new RuntimeException("searchTree is empty");
        return findMax(root).element;
    }

    //插入操作
    public void insert(T x) {
        this.root = insert(x, root);
    }

    //删除操作
    public void remove(T x) {
        this.root = remove(x, root);
    }
    //获取树的最大深度
    public int maxDeep() {
        return maxDeep(root);
    }
    //获取树的最小深度
    public int minDeep() {
        return minDeep(root);
    }
    //先序打印操作
    public void prePrintTree() {
        prePrintTree(root);
        System.out.println();
    }
    //中序打印操作
    public void infPrintTree() {
        infPrintTree(root);
        System.out.println();
    }
    //后序打印操作
    public void epiPrintTree() {
        epiPrintTree(root);
        System.out.println();
    }
    //判断是否是AVL树
    public boolean isAVL() {
        return Math.abs(maxDeep(root.right) - maxDeep(root.left)) < 2;
    }

    public boolean contains(T x, BinaryNode<T> t) {
        if (t == null)
            return false;
        int compareResult = x.compareTo(t.element);
        if (compareResult < 0)
            return contains(x, t.left);
        else if (compareResult > 0)
            return contains(x, t.right);
        else
            return true;
    }
    public BinaryNode<T> findMin(BinaryNode<T> t) {
        if (t == null)
            throw new RuntimeException("this binaryNode is Empty");
        if (t.left == null)
            return t;
        return findMin(t.left);
    }
    public BinaryNode<T> findMax(BinaryNode<T> t) {
        if (t == null)
            throw new RuntimeException("this binaryNode is Empty");
        if (t.right == null)
            return t;
        return findMax(t.right);
    }
    public BinaryNode<T> insert(T x, BinaryNode<T> t) {
        if (t == null)//传入节点为空,则将该节点作为根节点返回
            return new BinaryNode<>(x, null, null);
        int compareResult = x.compareTo(t.element);
        if (compareResult < 0)//传入元素x小于t
            t.left = insert(x, t.left);
        else if (compareResult > 0)//传入元素x大于t
            t.right = insert(x, t.right);
        return t;
    }
    public BinaryNode<T> remove(T x, BinaryNode<T> t) {
        if (!contains(x, root))
            throw new RuntimeException("this searchTree is not contains element:" + x);
        if (t == null)//传入节点为空
            throw new RuntimeException("this binaryNode is empty");
        int compareResult = x.compareTo(t.element);
        if (compareResult < 0)
            t.left = remove(x, t.left);
        else if (compareResult > 0)
            t.right = remove(x, t.right);
        else if (t.left != null && t.right != null) { //有两个儿子
            t.element = findMax(t.left).element;
            remove(t.element, t.left);
        } else //单儿子情形
            t = t.left == null ? t.right : t.left;
        return t;
    }
    public int maxDeep(BinaryNode<T> t) {
        int dl, dr;//记录左右树的深度
        if (t == null)
            return 0;
        else {
            dl = maxDeep(t.left);
            dr = maxDeep(t.right);
        }
        return dl > dr ? dl + 1 : dr + 1;
    }
    public int minDeep(BinaryNode<T> t) {
        int dl, dr;//记录左右树的深度
        if (t == null)
            return 0;
        else {
            dl = maxDeep(t.left);
            dr = maxDeep(t.right);
        }
        return dl < dr ? dl + 1 : dr + 1;
    }

    public void prePrintTree(BinaryNode<T> t) {
        if (t == null)
            return;
        System.out.print(t.element + " ");
        prePrintTree(t.left);
        prePrintTree(t.right);
    }
    public void infPrintTree(BinaryNode<T> t) {
        if (t == null)
            return;
        infPrintTree(t.left);
        System.out.print(t.element + " ");
        infPrintTree(t.right);
    }
    public void epiPrintTree(BinaryNode<T> t) {
        if (t == null)
            return;
        epiPrintTree(t.left);
        epiPrintTree(t.right);
        System.out.print(t.element + " ");
    }
    //测试方法
    public static void main(String[] args) {
        BinarySearchTree bst = createBinarySearchTree();
        System.out.println("先序打印出该树===>>>");
        bst.prePrintTree();
        System.out.println("中序打印出该树===>>>");
        bst.infPrintTree();
        System.out.println("后序打印出该树===>>>");
        bst.epiPrintTree();
        System.out.println("查找最大值===>>>" + bst.findMax());
        System.out.println("查找最小值===>>>" + bst.findMin());
        System.out.println("树中是否包含元素:" + bst.contains(5));
        System.out.println("树的最大深度" + bst.maxDeep());
        System.out.println("树的最大深度" + bst.minDeep());
        System.out.println("删除某一元素===>>>");
        bst.remove(6);
        bst.prePrintTree();
        bst.makeEmpty();
        System.out.println(bst.maxDeep());
    }

    /** * 创造一个如下的查找二叉树: * 6 * / \ * 2 8 * / \ * 1 4 * / * 3 * * */
    private static BinarySearchTree createBinarySearchTree() {
        BinarySearchTree<Integer> bst = new BinarySearchTree();
        bst.insert(6);
        bst.insert(2);
        bst.insert(1);
        bst.insert(4);
        bst.insert(3);
        bst.insert(8);
        return bst;
    }
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/pengqiaowolf/article/details/70143812
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞