红黑树

【深入理解Java集合框架】史上最清晰的红黑树讲解(上)
一步一图一代码,一定要让你真正彻底明白红黑树
红黑树
红黑树的C++完整实现源码
经典算法研究系列:五、红黑树算法的实现与剖析
#算法学习录#红黑树结构
彻底搞懂红黑树(二)
红黑树探索笔记

删除

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Node {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == NULL) {
            return NULL;
        }
        if (key < root->val) {
            root->left = deleteNode(root->left, key);
        }
        else if (key > root->val) {
            root->right = deleteNode(root->right, key);
        }
        /* Cur node has to be deleted, handle 3 cases below */
        else {
            TreeNode * oldroot = root;
            // single or no child case.等价于 if (!root->left || !root->right) root = (!root->left) ? root->right : root->left;
            if (root->left == NULL) {
                return root->right;
            }
            else if (root->right == NULL) {
                return root->left;
            }
            /* Find smallest in right sub-tree, copy its val and delete it */
            else {
                TreeNode *minNode = findMin(root->right);
                root->val = minNode->val; /* curval = smallest in right subtree */
                root->right = deleteNode(root->right, root->val);
                /* next will already be deleted, nothing to del in this case */
                oldroot = NULL;
            }
            delete oldroot;
        }
        return root;
    }
private:
    /* Find the left-most node in the tree     */
    inline TreeNode *findMin(TreeNode *node) {
        while (node->left != NULL) {
            node = node->left;
        }
        return node;
    }
};
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Node {
public:
    TreeNode *deleteNode(TreeNode* root, int key) {
        if (root == NULL) {
            return NULL;
        }
        if (key < root->val) {          //key在左半树上
            root->left = deleteNode(root->left, key);
        }
        else if (key > root->val) {     //key在右半树上
            root->right = deleteNode(root->right, key);
        }
        else {                          //当root->val等于NULL时,删除root
            TreeNode * oldroot = root;
            if (root->left == NULL) {
                return root->right;
            }
            else if (root->right == NULL) {
                return root->left;
            }
            else {      //we can chose root->left or root->right to be the new root in case it is not NULL.
                TreeNode *minNode = findMin(root->right);
                root->val = minNode->val;
                root->right = deleteNode(root->right, root->val);
                oldroot = NULL;
            }
            delete oldroot;
        }
        return root;
    }
private:
    TreeNode *findMin(TreeNode *node) {
        while (node->left != NULL) {
            node = node->left;
        }
        return node;
    }
};

你可能感兴趣的:(红黑树)