数据结构算法爬坑日记十四

二叉排序树

二叉排序树(BST)

二叉排序树:对于二叉排序树的任意一个非叶子节点,它的左子节点的值比当前节点的值小,右子节点比当前节点的
值大(如果相同,放在左右均可,应尽量避免出现相同的值)

代码实现
  ** 二叉排序树节点类
  //二叉排序树节点
public class Node {
    int value;//节点权值
    Node left;
    Node right;

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

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

    //添加节点
    public void addNode(Node node) {
        if (node == null) {
            return;
        }
        //添加的节点值小于当前节点值,向左递归
        if (node.value < this.value) {
            if (this.left != null) {
                this.left.addNode(node);
            } else {
                this.left = node;
            }
        }else {//不小于当前节点向右递归
            if(this.right!=null){
                this.right.addNode(node);
            }else {
                this.right=node;
            }
        }
    }

    //中序遍历
    public void mid(){
        if(this.left!=null){
            this.left.mid();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.mid();
        }
    }

    //查找要删除的节点
    public Node findNode(int value) {
        if (this.value == value) { // 已经找到该节点
            return this;
        } else if (value < this.value && this.left != null) {
            return this.left.findNode(value); // 向左递归
        } else if (value > this.value && this.right != null) {
            return this.right.findNode(value); // 向右递归
        } else {
            return null;
        }
    }

    //查找要删除的父节点
    public Node findParentNode(int value) {
        if ((this.right != null && this.right.value == value) || (this.left != null && this.left.value == value)) {
            return this; // 当前节点就是要删除的节点的父节点
        } else if (this.left != null && this.value > value) {
            return this.left.findParentNode(value); // 向左递归
        } else if (this.right != null && this.value < value) {
            return this.right.findParentNode(value); // 向右递归
        } else {
            return null;
        }
    }
}
  
  ** 二叉排序树类
//二叉排序树
public class BinarySortTree {
    Node root;//头节点

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

    //中序遍历
    public void midOrder(){
        if(root==null){
            System.out.println("二叉树为空");
        }else {
            root.mid();
        }
    }

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

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

    //查找需要删除的节点的右节点为根节点的最小值节点
    public Node delRightNode(Node node) {
        if (node.left != null) {
            return delRightNode(node.left);
        } else {
            del(node.value);
            return node;
        }
    }

    //删除节点
    @SuppressWarnings("all")
    public void del(int value) {
        Node node = findNode(value);
        Node parentNode = findParentNode(value);
        if (node == null) {
            return;//要删除的节点不存在
        }
        //如果要删除的节点就是根节点且根节点只有一个子节点
        //如果要删除的节点为叶子节点
        if (node.right == null && node.left == null) {
            if (parentNode == null) { // 要删除的节点为根节点且只有一个根节点
                root = null;
            } else {
                if (parentNode.left == node) {
                    parentNode.left = null;
                } else {
                    parentNode.right = null;
                }
            }
        } else if (node.right != null && node.left != null) {
            //如果要删除的节点不为叶子节点且有两个子节点
            Node rightNode = delRightNode(node.right);
            node.value=rightNode.value;
        } else {
            //如果要删除的节点不为叶子节点且有一个子节点
            //如果要删除的节点是根节点
            if (parentNode == null) {
                if (node.left != null) {
                    root = node.left;
                } else {
                    root = node.right;
                }
            } else {
                //判断子节点是哪边节点,要删除的节点是父节点的哪边节点
                if (parentNode.left == node) {
                    if (node.left != null) {
                        parentNode.left = node.left;
                    }
                    if (node.right != null) {
                        parentNode.left = node.right;
                    }
                }
                if (parentNode.right == node) {
                    if (node.left != null) {
                        parentNode.right = node.left;
                    }
                    if (node.right != null) {
                        parentNode.right = node.right;
                    }
                }
            }
        }
    }
}

  ** 测试类
  public class BinarySortTreeTest {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9,2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int value : arr) {
            binarySortTree.addNode(new Node(value));
        }
        binarySortTree.midOrder();
        //删除叶子节点
        System.out.println("--------------");
        binarySortTree.del(2); // 依次测试7 1
        binarySortTree.midOrder();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值