二叉排序树

问题记录

  1. 若二叉排序树中有值相同的节点,将会在search()和searchParent()两个函数中陷入死循环,出现栈溢出异常StackOverflowError。
  2. 目前的解决办法是,在添加节点时的add()函数中判断要添加的节点值是否已经存在,存在则不再添加相同节点。
  3. 后续再思考更好的优化方案!

完整代码

public class BinarySortTreeDemo {

    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2, 10};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }

        binarySortTree.delNode(12);
        binarySortTree.delNode(5);
        binarySortTree.delNode(7);
        binarySortTree.delNode(1);
        
        System.out.println("中序遍历~~~~~~~~~~~~~~~~~~~~~~");
        binarySortTree.infixOrder();
        System.out.println("根节点~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(binarySortTree.getRoot());
    }
}

class BinarySortTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    // 根据要删除的值查找要删除的节点
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找要删除节点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //删除节点
    public void delNode(int value) {
        if (root == null) {
            return;
        }

        Node targetNode = search(value);
        if (targetNode == null) {
            return;
        }

        //二叉排序树只有一个根节点
        if (root.left == null && root.right == null) {
            root = null;
            return;
        }

        Node parentNode = searchParent(value);
        // 要删除的节点是叶子节点
        if (targetNode.left == null && targetNode.right == null) {

            //targetNode是parentNode的左子节点
            if (parentNode.left != null && parentNode.left.value == value) {
                parentNode.left = null;
                return;
            }

            //targetNode是parentNode的左子节点
            if (parentNode.right != null && parentNode.right.value == value) {
                parentNode.right = null;
                return;
            }
        }

        // 要删除的节点有两个叶子节点
        if (targetNode.left != null && targetNode.right != null) {
            int minValue = delRightTreeMin(targetNode.right);
            targetNode.value = minValue;
            return;
        }

        // 要删除的节点只有一个叶子节点
        //要删除的节点有左子节点
        if (targetNode.left != null) {
            if (parentNode != null) {
                //targetNode 是 parent 的左子节点
                if (parentNode.left != null && parentNode.left.value == value) {
                    parentNode.left = targetNode.left;
                } else if (parentNode.right != null && parentNode.right.value == value) {// targetNode 是 parent 的右子节点
                    parentNode.right = targetNode.left;
                }
            } else {
                root = targetNode.left;
            }
        } else { //要删除的节点有右子节点
            if (parentNode != null) {
                //targetNode 是 parent 的左子节点
                if (parentNode.left != null && parentNode.left.value == value) {
                    parentNode.left = targetNode.right;
                } else if (parentNode.right != null && parentNode.right.value == value) {// targetNode 是 parent 的右子节点
                    parentNode.right = targetNode.right;
                }
            } else {
                root = targetNode.right;
            }
        }

    }

    /**
     * 1. 返回以 node 为根节点的二叉排序树的最小节点的值
     * 2. 删除 node 为根节点的二叉排序树的最小节点
     *
     * @param node 传入的节点(当做二叉排序树的根节点)
     * @return 返回的 以 node 为根节点的二叉排序树的最小节点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        // 删除
        delNode(target.value);
        return target.value;
    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空,不能遍历");
        }
    }
}

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

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

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    /**
     * @param value 希望删除的节点的值
     * @return 找到返回该节点,否则返回 null
     */
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }


    /**
     * 查找要删除节点的父节点
     *
     * @param value 要找到的节点的
     * @param value 要找到的节点的
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value)
                || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }
        
 		Node searchNode = search(node.value);
        if (searchNode != null) {
            System.out.println("数据值为" + node.value + "的节点已存在,无					  法添加!");
            return;
        }

        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }

        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值