数据结构-二叉排序树(四)

92 篇文章 0 订阅
16 篇文章 0 订阅

Binary Sort Tree

概述

  • 二叉排序树(BST, Binary Sort Tree), 任何一个非叶子节点相比左子节点大, 相比右子节点小.
    * 如果值相同, 即可以放左或右子节点

实例代码


public class BinarySortTreeApp {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 11, 13};
        BinarySortTree binarySortTree = new BinarySortTree();
        /** 循环添加节点*/
        for(int i = 0; i< arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历:");
        binarySortTree.infixOrder();
        System.out.println("当前根节点 " + binarySortTree.getRoot());

        binarySortTree.delNode(10);
        System.out.println("删除结点后:");
        binarySortTree.infixOrder();
    }
}

/** 二叉排序树*/
class BinarySortTree {
    private Node root;
    public Node getRoot() {
        return root;
    }

    /** 添加节点*/
    public void add(Node node) {
        if(root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /** 搜索节点*/
    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;
        }else {
            /** 首先搜索是否存在*/
            Node targetNode = search(value);
            if(targetNode == null) {
                return;
            }

            /** 要删的节点如果是根节点*/
            if(root.left == null && root.right == null) {
                root = null;
                return;
            }

            /** 查找要删的节点的父节点*/
            Node parent = searchParent(value);
            /** 当要删的节点是叶子结点*/
            if(targetNode.left == null && targetNode.right == null) {
                /** 判断要删的节点是属于它的父节点的左节点*/
                if(parent.left != null && parent.left.value == value) { /** 左子节点*/
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) { /** 右子节点*/
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { /** 删除有两颗子树的节点*/
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal; /** 将右子节点下的最小的值, 覆盖到当前要删除的节点*/
            } else {
                /** 删除只有一颗子树的节点*/
                /** 如果要删除的节点有左子节点*/
                if(targetNode.left != null) {
                    if(parent != null) {
                        /** 如果 targetNode是 parent的左子节点*/
                        if(parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            /** targetNode是 parent的右子节点*/
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {
                    /** 如果要删除的节点有右子节点*/
                    if(parent != null) {
                        /**如果 targetNode是 parent的左子节点*/
                        if(parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            /** 如果 targetNode是 parent的右子节点*/
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    /**
     * @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 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;
    }

    /** 添加节点方法*/
    public void add(Node node) {
        if(node == null) {
            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 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);
        }
    }

    /** 查找父节点*/
    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 infixOrder() {
        if(this.left != null) {
            this.left.infixOrder();
        }

        System.out.println(this);

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

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

}

输出:
> 中序遍历:
> Node [value=1]
> Node [value=3]
> Node [value=5]
> Node [value=7]
> Node [value=9]
> Node [value=10]
> Node [value=11]
> Node [value=12]
> Node [value=13]
> 当前根节点 Node [value=7]
> 删除结点后:
> Node [value=1]
> Node [value=3]
> Node [value=5]
> Node [value=7]
> Node [value=9]
> Node [value=11]
> Node [value=12]
> Node [value=13]

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值