二叉查找树(java版本)

package BinarySearch;

public class BinarySearchTree {

    private Node root = null;

    public BinarySearchTree(int[] elements) {
        if (elements != null && elements.length >= 1) {
            for (int element : elements) {
                insert(element);
            }
        }
    }

    public boolean delete(int value) {
        return operation(value, false, true);
    }


    private void deleteNode(Node deleteNode) {
        boolean isLeftNull = deleteNode.left == null;
        boolean isRightNull = deleteNode.right == null;
        if (isLeftNull && isRightNull) {
            deleteNode = null;
        } else if (isLeftNull) {//如果只有左子树为空,把右子树直接提上来就好了
            Node rightNode = deleteNode.right;
            deleteNode.value = deleteNode.right.value;
            deleteNode.left = rightNode.left;
            deleteNode.right = rightNode.right;
        } else if (isRightNull) {//如果只有右子树为空,把左子树直接提上来就好了
            Node leftNode = deleteNode.left;
            deleteNode.value = deleteNode.left.value;
            deleteNode.left = leftNode.left;
            deleteNode.right = leftNode.right;
        } else {
            //如果左右都不为空的话,那么就找到 被删除节点的左节点的最右节点,
            //将它顶替当前的被删除节点,然后将此最右节点的左子树放到 最右节点的父节点的右子树
            Node tempLeftNode = deleteNode.left;

            Node lastLeftTreeRightNode = tempLeftNode;
            Node lastLeftTreeRightNodeParent = null;
            while (lastLeftTreeRightNode.right != null) {
                lastLeftTreeRightNodeParent = lastLeftTreeRightNode;
                lastLeftTreeRightNode = lastLeftTreeRightNode.right;
            }
            deleteNode.value = lastLeftTreeRightNode.value;
            if (lastLeftTreeRightNodeParent != null) {
                lastLeftTreeRightNodeParent.right = lastLeftTreeRightNode.left;
            }
            lastLeftTreeRightNode = null;
        }
    }

    public boolean search(int value) {
        return !operation(value, false, false);
    }

    public boolean insert(int value) {
        return operation(value, true, false);
    }

    private boolean operation(int value, boolean insert, boolean delete) {
        if (root == null) {//根节点为空,直接new一个Node
            if (insert) root = new Node(value);
            else if (delete) return false;
            return true;
        }
        Node tempRoot = root;
        while (true) {
            if (value == tempRoot.value) {//如果存在该元素的话,那么不用插入了
                if (delete) {
                    deleteNode(tempRoot);
                    return true;
                }
                return false;
            } else if (value > tempRoot.value) {
                if (tempRoot.right == null) {
                    if (insert) {
                        tempRoot.right = new Node(value);
                    } else if (delete) {
                        return false;
                    }
                    return true;
                }
                tempRoot = tempRoot.right;
            } else {
                if (tempRoot.left == null) {
                    if (insert) {
                        tempRoot.left = new Node(value);
                    } else if (delete) {
                        return false;
                    }
                    return true;
                }
                tempRoot = tempRoot.left;
            }
        }
    }

    private static class Node {
        private int value;
        private Node left;
        private Node right;

        public Node(int value) {
            this.value = value;
        }
    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mymilkbottles/article/details/78038414
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭