读书笔记——二叉查找树

以下代码主要来自于《数据结构与算法分析 Java语言描述》第四章,增加了测试代码,增加了文中提到的removeMin方法,为我自己理解后编写的,可能有所不周,后续发现问题再修改吧。


public class BinarySearchTree<T extends Comparable<? super T>> {
    private static class BinaryNode<T> {
        BinaryNode(T theElement) {
            this(theElement, null, null);
        }

        BinaryNode(T theElement, BinaryNode<T> lt, BinaryNode<T> rt) {
            element = theElement;
            left = lt;
            right = rt;
        }

        T element;
        BinaryNode<T> left;
        BinaryNode<T> right;
    }

    private BinaryNode<T> root;

    public BinarySearchTree() {
        root = null;
    }

    public void makeEmpty() {
        root = null;
    }

    public boolean isEmpty() {
        return null == root;
    }

    public boolean contains(T x) {
        return contains(x, root);
    }

    public T findMin() {
        if (isEmpty()) {
            throw new BufferUnderflowException();
        }

        return findMin(root).element;
    }

    public T findMax() {
        if (isEmpty()) {
            throw new BufferUnderflowException();
        }

        return findMax(root).element;
    }

    public void insert(T x) {
        root = insert(x, root);
    }

    public void remove(T x) {
        remove(x, root);
    }

    public void printTree() {
        if (isEmpty()) {
            System.out.println("Empty tree");
        } else {
            printTree(root);
        }
    }

    private boolean contains(T x, BinaryNode<T> t) {
        if (null == x) {
            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;
        }
    }

    private BinaryNode<T> findMin(BinaryNode<T> t) {
        if (null == t) {
            return null;
        } else if (null == t.left) {
            return t;
        }

        return findMin(t.left);
    }

    private BinaryNode<T> findMax(BinaryNode<T> t) {
        while (null != t.right) {
            t = t.right;
        }

        return t;
    }

    private BinaryNode<T> insert(T x, BinaryNode<T> t) {
        if (null == t) {
            return new BinaryNode<T>(x, null, null);
        }

        int compareResult = x.compareTo(t.element);

        if (compareResult < 0) {
            t.left = insert(x, t.left);
        } else if (compareResult > 0) {
            t.right = insert(x, t.right);
        } else {
            ;
        }

        return t;
    }

    private BinaryNode<T> remove(T x, BinaryNode<T> t) {
        if (null == t) {
            return t;
        }

        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 (null != t.left && null != t.right) {
                                    t.element = findMin(t.right).element;
                                    t.right = remove(t.element, t.right);
           // t = removeMin(t);
        } else {
            t = (null != t.left) ? t.left : t.right;
        }

        return t;
    }

    private void printTree(BinaryNode<T> t) {
        if (null != t) {
            if (null != t.left) {
                System.out.print(t.element + " left:");
            }
            printTree(t.left);
            System.out.println(t.element);
            if (null != t.right) {
                System.out.print(t.element + " right:");
            }
            printTree(t.right);
        }
    }

    private BinaryNode<T> removeMin(BinaryNode<T> t) {
        BinaryNode<T> min = t.right;
        BinaryNode<T> minParent = t;

        while (null != min.left) {
            minParent = min;
            min = min.left;
        }

        t.element = min.element;

        if (minParent != t) {
            minParent.left = null;
        } else {
            t.right = null;
        }

        return t;
    }

    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();
        tree.insert(5);
        tree.insert(7);
        //tree.insert(14);
        tree.insert(6);
        tree.insert(1);
        tree.insert(62);
        tree.insert(68);
        tree.insert(16);
        tree.insert(10);
        tree.insert(23);

        tree.printTree();

        tree.remove(7);
        tree.printTree();
    }
}

运行结果:

5 left:1
5
5 right:7 left:6
7
7 right:62 left:16 left:10
16
16 right:23
62
62 right:68
5 left:1
5
5 right:10 left:6
10
10 right:62 left:16
16 right:23
62
62 right:68

主要是删除节点时,如果有2个子节点,一般是用右子树的最小数据代替该节点的数据,并递归删除那个节点。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值