二叉排序树删除节点

本文介绍了一种算法用于删除二叉搜索树中的指定节点,包括处理不同情况下的节点结构变化。
#include<iostream>
using namespace std;

class TreeNode {
public:
    int val;
    TreeNode *left, *right;
    TreeNode(int val) {
        this->val = val;
        this->left = this->right = NULL;
    }
};

class Solution {
public:
    TreeNode* removeNode(TreeNode* root, int val) {
        // if root is null, just return
        if (!root)
           return root; 

        // Find node whose val equal to "val"
        TreeNode* parent = NULL;
        TreeNode* curr = root;
        while (curr) {
            if (curr->val == val) 
                break;
            parent = curr;
            if (curr->val > val)
                curr = curr->left;
            else
                curr = curr->right;
        }

        // If there is no node's value equals to val
        if (!curr)
            return root;
        
        // Situation 1, leafe node
        if (!curr->left && !curr->right) {
            if (!parent) {
                root = NULL;
            } else {
                if (parent->left == curr) 
                    parent->left = NULL;
                else 
                    parent->right = NULL;
            }
            delete curr;
        } else if (curr->left && !curr->right) {
            // Situation 2, no right child
            if (!parent) {
                root = curr->left;
            } else {
                if (parent->left == curr) 
                    parent->left = curr->left;
                else 
                    parent->right = curr->left;
            }
            delete curr;
        } else if (!curr->left && curr->right) {
            // Situation 3, no left child
            if (!parent) {
                root = curr->right;
            } else {
                if (parent->left == curr) 
                    parent->left = curr->right;
                else
                   parent->right = curr->right; 
            } 
            delete curr;
        } else {
            // Situation 4, both childs are existing
            TreeNode* left_max = curr->left;
            parent = curr; 
            while (left_max->right) {
                parent = left_max;
                left_max = left_max->right; 
            }
            
            // Swap with curr node
            curr->val = curr->val ^ left_max->val;
            left_max->val = curr->val ^ left_max->val;
            curr->val = curr->val ^ left_max->val;
            
            // Delete left_max
            if (parent->left == left_max)
                parent->left = NULL;
            else if (parent->right == left_max)
                parent->right = NULL;
            delete left_max;
        }       
        return root;
    }
};

void pLevel(TreeNode* node) {
    if (!node)
        return;
    cout << node->val << endl;
    pLevel(node->left);
    pLevel(node->right);
}

int main() {
    TreeNode* node5 = new TreeNode(5);
    TreeNode* node3 = new TreeNode(3);
    TreeNode* node6 = new TreeNode(6);
    TreeNode* node2 = new TreeNode(2);
    TreeNode* node4 = new TreeNode(4);

    node5->left = node3;
    node5->right = node6;
    node3->left = node2;
    node3->right = node4;

    Solution s;
    TreeNode* ret = s.removeNode(node5, 3);
    pLevel(ret);
    return 0;
}

### 平衡二叉排序树中的节点删除 在平衡二叉排序树(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、付费专栏及课程。

余额充值