数据结构与算法-二叉排序树

二叉排序树

一、思路在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、源码

1.Node类

代码如下(示例):

package BinarySortTree;

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

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

    @Override
    public String toString() {
        return "Node{" +
                "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 (this.value == value) {
            return this;
        } else if (this.value < value) {
            //向右边递归查找
            if (this.right == null) {
                return null;
            }
            return this.right.Search(value);
        } else if (this.value > value) {
            //向左递归查找
            if (this.left == null) {
                return null;
            }
            return this.left.Search(value);
        } else {
            return null;
        }
    }

    //查找要删除节点的父节点
    public Node SearchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else if (this.value > value && this.left != null) {
            return this.left.SearchParent(value);
        } else if (this.value < 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.value);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

}

2.BinarySortTree类

代码如下(示例):

package BinarySortTree;

public class BinarySortTree {
    private Node root;

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

    //查找到删除的这个节点
    public Node Search(int value) {
        if (root != null) {
            return root.Search(value);
        } else {
            return null;
        }
    }

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

    //删除这个节点
    public void deleteNode(int value) {
        if (root == null) {
            return;
        } else {
            Node targetNode = Search(value);
            if (targetNode == null) {
                return;
            }
            if (root.right == null & root.left == null) {
                root = null;
                return;
            }
            Node ParentNode = SearchParent(value);
            if (targetNode.left == null && targetNode.right == null) {
                if (ParentNode.left.value == targetNode.value && ParentNode.left != null) {
                    ParentNode.left = null;
                } else if (ParentNode.right.value == targetNode.value && ParentNode.right != null) {
                    ParentNode.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
                int intmin = Intmin(targetNode.right);
                targetNode.value = intmin;
            } else {
                if (targetNode.left!=null){
                    if (ParentNode!=null){
                        if (ParentNode.left.value==value){
                            ParentNode.left = targetNode.left;
                        }else {
                            ParentNode.right = targetNode.left;
                        }
                    }else {
                        root = targetNode.left;
                    }
                }else {
                    if (targetNode.right!=null){
                        if (ParentNode!=null){
                            if (ParentNode.left.value ==value){
                                ParentNode.left = targetNode.right;
                            }else {
                                ParentNode.right = targetNode.right;
                            }
                        }else {
                            root = targetNode.right;
                        }
                    }
                }
            }
        }

    }

    //查找右边节点的最小值min
    public int Intmin(Node node) {
        Node temp = node;
        while (temp.left != null) {
            temp = temp.left;
        }
        deleteNode(temp.value);
        return temp.value;
    }

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



演示Demo



package BinarySortTree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9};
        BinarySortTree binarySortTree = new BinarySortTree();
        //将数组中的每个数依次以节点的形式加入到树中
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历");
        binarySortTree.infixOrder();
        binarySortTree.deleteNode(7);
        binarySortTree.deleteNode(3);
        binarySortTree.deleteNode(10);
        binarySortTree.deleteNode(1);
        System.out.println("中序遍历");
        binarySortTree.infixOrder();
    }
}

总结

思路有点绕,但是都是比较好理解的,理清楚就行!
特别是利用其中两种方法除外就是第三种方法的思想很不错

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值