二叉排序树删除节点

/**
 * @Description 二叉排序树的节点
 * @auther Eleven
 * @create 2020-04-09 20:43
 **/
public 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 (this.value>node.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 midPrint(Node root) {
        if (root == null){
            return;
        }
        midPrint(root.left);
        System.out.println(root.value);
        midPrint(root.right);
    }

    //查找节点
    public Node searchNode(Node node) {
        if (this.value == node.value){
            return this;
        }else if (this.value>node.value){
            //当前值大于node的值
            if (this.left == null) return null;
            return   left.searchNode(node);
        }else {
            if (this.right == null) return null;
            return  right.searchNode(node);
        }
    }

    public void deleteNode(Node node) {

    }

    /**
     * 查找父节点
     * @param node
     * @return
     */
    public Node searchParent(Node node) {
        //如果当前节点的左节点不为空,且左节点的值等于查找的node的值 || 如果当前节点的右节点不为空,且右节点的值等于查找的node的值 当前节点即为父节点
        if ((this.left.value==node.value && this.left !=null)||(this.right.value==node.value&& this.right !=null)){
            return this;
        }else{

            if (this.left!=null && this.value>node.value){  //当当前node的value小于当前的往左找
               return this.left.searchParent(node);

            }else if (this.right!=null  && this.value<node.value){ //当当前node的value大于当前的往右找
                return this.right.searchParent(node);
            }
            return null;
        }
    }
}
/**
 * @Description 二叉排序树
 * @auther Eleven
 * @create 2020-04-09 20:43
 **/
public class BinarySortTree {
    Node root;

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

    }

    public void midPrint() {
        if (root !=null){
            root.midPrint(root);
        }
    }

    public Node searchNode(Node node){
        if (root==null){
            return null ;
        }else {
            return root.searchNode(node);
        }
    }

    /**
     * 删除节点
     * @param node
     */
    public void deleteNode(Node node){
        if (root == null){
            return;
        }else {
            //找到这个节点
            Node target = searchNode(node);
            if (target==null){
                return;
            }
            //找到这个节点的父节点
            Node parent = searchParent(node);
            //删除叶子节点
            if (target.left==null && target.right==null){
                //删除左节点
               if (target==parent.left){
                   parent.left=null;
               }else {
                   //删除右节点
                   parent.right=null;
               }
            }else if (target.left!=null && target.right!=null){
                 //找到右子树中最小的节点
                Node minNode = findMinNode(target.right);
                //替换目标节点的值
                target.value=minNode.value;

            }else {//删除只有一个子节点的节点
                if (target.left!=null){//左子节点不为空
                    //删除左节点
                    if (target==parent.left){
                        parent.left=target.left;
                    }else {
                        //删除右节点
                        parent.right=target.left;
                    }
                }else {//右子节点不为空
                    //删除左节点
                    if (target==parent.left){
                        parent.left=target.right;
                    }else {
                        //删除右节点
                        parent.right=target.right;
                    }
                }

            }
        }
    }

    /**
     * 找一颗数中的最小节点
     * @param node
     * @return
     */
    private Node findMinNode(Node node) {
        Node target = node;
        while (target.left!=null){
            target = target.left;
        }
        //删除最小节点
        deleteNode(target);
        return target;
    }


    /**
     * 搜索父节点
     * @param node
     * @return
     */
    public Node searchParent(Node node){
        if (root == null){
            return null;
        }else{
            return root.searchParent(node);
        }
    }
}
    public static void main(String[] args) {
        BinarySortTree binarySortTree = new BinarySortTree();
        int[] arr = new int[]{7,3,10,12,5,1,9};
        for (int i:arr){
            binarySortTree.add(new Node(i));
        }
        binarySortTree.midPrint();
       // System.out.println(binarySortTree.searchNode(new Node(1)).value);
        System.out.println("--------------------");
        binarySortTree.deleteNode(new Node(3));
        binarySortTree.midPrint();
    }
### 平衡二叉排序树中的节点删除 在平衡二叉排序树(AVL树或红黑树等)中执行节点删除操作时,不仅需要移除指定的节点,还需要保持树的高度平衡属性。对于普通的二叉排序树而言,删除过程涉及三种情况: 1. **目标节点无子节点** 2. **目标节点仅有一个子节点** 3. **目标节点有两个子节点** 当涉及到平衡二叉排序树时,在完成上述任一情形的操作之后可能破坏了原有的平衡条件,则需通过旋转等方式恢复其性质。 #### 删除操作的具体实现 下面给出一段基于C++语言描述的简单示例代码来展示如何在一个假设为自定义类型的`BSTNode*`指针所指向的数据结构上实施删除逻辑,并尝试维持该树作为一棵高度平衡的二叉搜索树[^1]。 ```cpp // 定义二叉树结点 struct BSTNode { int data; BSTNode *left, *right; BSTNode(int val) : data(val), left(nullptr), right(nullptr) {} }; // 查找最小值辅助函数 BSTNode* minValueNode(BSTNode* node){ BSTNode* current = node; /* 循环向下直到最左边 */ while (current && current->left != nullptr) current = current->left; return current; } // 执行删除操作并返回新的根节点 BSTNode* deleteNode(BSTNode* root, int key){ // 基本情况 if (!root) return root; // 如果键小于当前节点数据则进入左子树继续寻找 if (key < root->data) root->left = deleteNode(root->left, key); // 同理处理右子树的情况 else if (key > root->data) root->right = deleteNode(root->right, key); // 当找到要删除的目标节点时... else { // 节点拥有两个孩子或者只有一个孩子的情形 if ((root->left == nullptr)||(root->right == nullptr)) { BSTNode* temp = root->left ? root->left : root->right; // 若没有子节点则是叶子节点直接释放内存即可 if(temp == nullptr){ temp = root; root = nullptr; } else { // 只有单侧存在子节点的情况下复制过来再清理原位置资源 *root = *temp; } free(temp); }else{ // 两侧都有子节点的时候用后继替代被删者的位置 BSTNode* temp = minValueNode(root->right); // 将后续者的数值赋给待删除节点 root->data = temp->data; // 接着去右边把那个用来替换的小家伙干掉 root->right = deleteNode(root->right , temp->data); } } // 更新height以及判断是否失衡进而调整(此处省略具体细节) return root; } ``` 这段代码实现了基本的删除功能,但对于真正的平衡二叉排序树来说,每次修改后的重新平衡步骤是非常重要的部分,这通常会涉及到额外的状态维护和复杂的算法设计,比如AVL树里的LL/RR/LR/RL四种旋转方式或是红黑树特有的颜色翻转机制等等[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值