{"id":1009,"date":"2020-12-02T11:59:55","date_gmt":"2020-12-02T03:59:55","guid":{"rendered":"http:\/\/www.wangkaixuan.tech\/?p=1009"},"modified":"2020-12-02T12:09:27","modified_gmt":"2020-12-02T04:09:27","slug":"linux%e5%86%85%e6%a0%b8%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84-rbtree","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=1009","title":{"rendered":"LINUX\u5185\u6838\u6570\u636e\u7ed3\u6784-RBTREE"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u524d\u8a00<\/h2>\n\n\n\n<p>\u8fd9\u662flinux\u5185\u6838\u6570\u636e\u7ed3\u6784\u7684\u7b2c\u4e09\u7bc7\uff0c\u524d\u4e24\u7bc7\u5206\u522b\u8bb2\u4e86<a href=\"http:\/\/www.wangkaixuan.tech\/?p=990\">list<\/a>\u548c<a href=\"http:\/\/www.wangkaixuan.tech\/?p=1003\">hashtable<\/a>\uff0c\u6709\u4e0d\u719f\u6089\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u79fb\u6b65\u53c2\u89c2\u4e0b\u3002<\/p>\n\n\n\n<p>\u672c\u7bc7\u6211\u4eec\u5c06\u7814\u7a76\u4e0brbtree\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>rbtree\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u5728\u6211\u4eec\u4e0d\u65ad\u63d2\u5165\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4ed6\u4f1a\u6839\u636e\u4e8c\u53c9\u6811\u5de6\u53f3\u5b50\u6811\u7684\u6df1\u5ea6\u60c5\u51b5\u4e0d\u65ad\u5730\u5bf9\u4e24\u8fb9\u8fdb\u884c\u8c03\u6574\uff0c\u4ece\u800c\u4fdd\u8bc1\u5de6\u53f3\u5b50\u6811\u7684\u5e73\u8861\uff0c\u8fdb\u800c\u4fdd\u8bc1<strong>\u826f\u597d\u7684\u67e5\u627e\u6027\u80fd<\/strong>\u3002\u53e6\u5916\uff0crbtree\u8fd8\u662f<strong>\u6709\u5e8f<\/strong>\u7684\uff0c\u8fd9\u4e00\u70b9\u4e5f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n\n\n\n<p>rbtree\u7684\u5b9e\u73b0\u5728linux\u5185\u6838\u4e2d\u5206\u6563\u5728\u4e09\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5206\u522b\u662f<a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/include\/linux\/rbtree_augmented.h\">rbtree_augmented.h<\/a>\u3001<a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/include\/linux\/rbtree.h\">rbtree.h<\/a>\u3001<a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/lib\/rbtree.c\">rbtree.c<\/a>\uff0c\u611f\u5174\u8da3\u7684\u8bdd\u53ef\u79fb\u6b65\u53c2\u8003\u3002<\/p>\n\n\n\n<p>\u7279\u522b\u63d0\u9192\u4e00\u70b9\uff0crbtree\u7684\u590d\u6742\u7a0b\u5ea6\u548c\u524d\u9762\u6211\u4eec\u7814\u7a76\u7684list\u3001hashtable\u4e0d\u662f\u4e00\u4e2a\u91cf\u7ea7\u7684\uff0c\u8bf7\u505a\u597d\u5fc3\u7406\u51c6\u5907\u3002\u53e6\u5916\uff0c\u672c\u7bc7\u6587\u7ae0\u7684\u5b8c\u6574\u4ee3\u7801\u5df2\u7ecf\u4e0a\u4f20\u81f3<a href=\"https:\/\/github.com\/pandawkx\/playground\/tree\/master\/linux_kernel\/rbtree\">\u6211\u7684github<\/a>\u4e86\uff0c\u4f9b\u60a8\u53c2\u8003\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u521d\u59cb\u5316<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u5b9a\u4e49\u7ed3\u6784\u4f53<\/h3>\n\n\n\n<p>\u6309\u7167\u8001\u89c4\u77e9\uff0c\u5148\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct task_node_t\n{\n    int task_id;\n    struct rb_node node;\n};<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u8868\u793a\u4efb\u52a1\u7684\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u4e86\u4e00\u4e2atask_id\u5b57\u6bb5\u548c\u5185\u5d4c\u7684rbree\u8282\u70b9\u5b57\u6bb5\uff08\u5982\u679c\u4e0d\u77e5\u9053\u8fd9\u4e2a\u8282\u70b9\u5143\u7d20\u662f\u5e72\u5565\u7684\uff0c\u5efa\u8bae\u56de\u5934\u7ffb\u7ffb\u524d\u9762\u8bb2list\u548chashtable\u7684\u6587\u7ae0\uff09\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7684\u57fa\u672c\u9700\u6c42\u662f\uff0c\u5c06task\u6309\u7167task_id\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>rbtree\u7684\u666e\u901a\u8282\u70b9\u548c\u6839\u8282\u70b9\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ copy from kernel\/include\/linux\/rbtree.h\nstruct rb_node {\n\tunsigned long  __rb_parent_color;\n\tstruct rb_node *rb_right;\n\tstruct rb_node *rb_left;\n} __attribute__((aligned(sizeof(long))));\n\nstruct rb_root {\n\tstruct rb_node *rb_node;\n};<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6839\u8282\u70b9\u7ed3\u6784\u4f53\u53ea\u5305\u542b\u4e00\u4e2a\u6307\u5411\u666e\u901a\u8282\u70b9\u7684\u6307\u9488<\/li><li>\u666e\u901a\u8282\u70b9\u7ed3\u6784\u4f53\u5305\u542b\u4e86\u6307\u5411\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u7684\u6307\u9488rb_left\u548crb_right<\/li><li>\u666e\u901a\u8282\u70b9\u7ed3\u6784\u4f53\u7684__rb_parent_color\u5b57\u6bb5\u5176\u5b9e\u662f\u4e2a\u590d\u5408\u5b57\u6bb5\uff0c\u53ef\u4ee5\u540c\u65f6\u8868\u793a\u7236\u8282\u70b9\u6307\u9488\u548c<strong>\u672c\u8282\u70b9<\/strong>\u7684\u989c\u8272<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 __rb_parent_color<\/h3>\n\n\n\n<p>__rb_parent_color\u7684\u6700\u540e1\u4f4d\u662f\u8868\u793a<strong>\u672c\u8282\u70b9<\/strong>\u7684\u989c\u8272\u7684\uff0c\u76f8\u5173\u7684\u4ee3\u7801\u6bb5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ copy from kernel\/include\/linux\/rbtree_augmented.h\n#define\tRB_RED\t\t0\n#define\tRB_BLACK\t1\n\n#define __rb_color(pc)     ((pc) &amp; 1)\n#define __rb_is_black(pc)  __rb_color(pc)\n#define __rb_is_red(pc)    (!__rb_color(pc))\n#define rb_is_red(rb)      __rb_is_red((rb)->__rb_parent_color)\n#define rb_is_black(rb)    __rb_is_black((rb)->__rb_parent_color)<\/code><\/pre>\n\n\n\n<p>\u5176\u5b9e\u662f\u53d6\u4e86__rb_parent_color\u7684\u6700\u540e1\u4f4d\uff0c\u4e3a1\u8868\u793a\u9ed1\u8282\u70b9\uff0c\u4e3a0\u8868\u793a\u7ea2\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u53d6\u7236\u8282\u70b9\u5730\u5740\u7684\u64cd\u4f5c\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define rb_parent(r)   ((struct rb_node *)((r)->__rb_parent_color &amp; ~3))<\/code><\/pre>\n\n\n\n<p>3\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u662f0000&#8230;000011\uff0c\u53d6\u53cd\u540e\u8fdb\u884c\u4e0e\u64cd\u4f5c\uff0c\u5373\u53ef\u53bb\u6389\u5c3e\u90e8\u7684\u8868\u793a\u8282\u70b9\u989c\u8272\u7684\u4f4d\u3002<\/p>\n\n\n\n<p>\u770b\u5230\u8fd9\u91cc\u611f\u89c9\u6709\u70b9\u61f5\uff0c\u8fd9\u662f\u4ec0\u4e48\u64cd\u4f5c\uff1f\u8fd8\u80fd\u8fd9\u6837\u73a9\uff1f<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5176\u5b9e\u6709\u4e00\u4e2a\u7cbe\u5de7\u7684\u8bbe\u8ba1\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6ce8\u610f\u5230\u524d\u9762\u5b9a\u4e49rb_node\u65f6\uff0c\u6307\u5b9a\u4e86\u4e00\u4e2a<strong>\u5b57\u8282\u5bf9\u9f50<\/strong>\u64cd\u4f5c\u3002\u572832\u4f4d\u7cfb\u7edf\u4e0a\uff0c\u662f\u6309\u71674\u5b57\u8282\u5bf9\u9f50\uff0c\u572864\u4f4d\u7cfb\u7edf\u4e0a\uff0c\u662f\u6309\u71678\u5b57\u8282\u5bf9\u9f50\u7684\u3002\u8fd9\u5c31\u610f\u5473\u7740\uff0crb_node\u7ed3\u6784\u4f53\u7684\u6307\u9488\u6570\u503c\uff0c<strong>\u540e2\u4f4d\u62163\u4f4d\u662f0<\/strong>\u3002\u4e0e\u5176\u820d\u5f03\u4e0d\u7528\uff0c\u4e0d\u5982\u7528\u6765\u5b58\u653e\u8282\u70b9\u7684\u989c\u8272\u3002\u4e00\u6765\u5e9f\u5f03\u7684\u6bd4\u7279\u4f4d\u7528\u4e0a\u4e86\uff0c \u4e8c\u6765\u8fd8\u80fd\u907f\u514d\u5b9a\u4e49\u65b0\u7684\u53d8\u91cf\u8868\u793a\u8282\u70b9\u989c\u8272\uff0c\u6d6a\u8d39\u5185\u5b58\u3002<\/p>\n\n\n\n<p>\u56de\u5230\u524d\u9762\u7684\u6c42\u7236\u8282\u70b9\u5730\u5740\u7684\u64cd\u4f5c\uff0c\u8fd9\u91cc\u5176\u5b9e\u662f\u7a7a\u4e86\u4e24\u4e2a\u6bd4\u7279\u4f4d\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5373\u4f7f\u662f\u572832\u4f4d\u7cfb\u7edf\u4e0a\u4e5f\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u4e0e__rb_parent_color\u76f8\u5173\u7684\u8fd8\u6709\u4e24\u4e2a\u64cd\u4f5c\uff0c\u4e00\u4e2a\u662f\u6c42\u7ea2\u8282\u70b9\u7236\u8282\u70b9\u7684\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline struct rb_node *rb_red_parent(struct rb_node *red)\n{\n\treturn (struct rb_node *)red->__rb_parent_color;\n}\n<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e\u7ea2\u8282\u70b9\u7684__rb_parent_color\u8868\u793a\u989c\u8272\u7684\u4f4d\u662f0\uff0c\u800c\u5b57\u8282\u5bf9\u9f50\u53c8\u4fdd\u8bc1\u4e86\u5012\u6570\u7b2c\u4e8c\u4f4d\u4e5f\u662f0\uff0c\u56e0\u6b64\u8fd9\u91cc\u76f4\u63a5\u505a\u4e2a\u5f3a\u8f6c\u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\u4e00\u4e2a\u64cd\u4f5c\u662f\u8bbe\u7f6e\u8282\u70b9\u989c\u8272\u7684\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void rb_set_parent_color(struct rb_node *rb,\n\t\t\t\t       struct rb_node *p, int color)\n{\n\trb->__rb_parent_color = (unsigned long)p | color;\n}<\/code><\/pre>\n\n\n\n<p>color\u53c2\u6570\u4f20\u7684\u662f\u4e0a\u9762\u7684RB_BLACK\u6216\u8005RB_RED\u5b8f\uff0c\u53ea\u5360\u4e00\u4e2a\u6bd4\u7279\u4f4d\uff0c\u56e0\u6b64\u8fd9\u91cc\u76f4\u63a5\u4e0e\u4e00\u4e0b\u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 \u5b9a\u4e49rbtree<\/h3>\n\n\n\n<p>\u5b9a\u4e49rbtree\u7684\u4ee3\u7801\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define RB_ROOT\t(struct rb_root) { NULL, }\n\nstruct rb_root task_tree = RB_ROOT;<\/code><\/pre>\n\n\n\n<p>\u5c31\u662f\u5b9a\u4e49\u4e00\u4e2a\u6839\u8282\u70b9\uff0c\u628a\u4ed6\u552f\u4e00\u7684\u5143\u7d20rb_node\u6307\u9488\u7f6e\u7a7a\u5373\u53ef\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u63d2\u5165<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u6d4b\u8bd5\u4ee3\u7801<\/h3>\n\n\n\n<p>\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct rb_root task_tree = RB_ROOT;\nstruct task_node_t *p_task;\n\n\/\/\u63d2\u5165100\u4e2a\u5143\u7d20\nfor (i=0; i &lt; 100; i++) \n{\n\tp_task = (struct task_node_t *)malloc(sizeof(struct task_node_t));\n\tp_task->task_id = 100 -1 - i;\n\ttask_insert(&amp;task_tree, p_task);\n}<\/code><\/pre>\n\n\n\n<p>\u63d2\u5165100\u4e2a\u5143\u7d20\uff0c\u628a\u4ed6\u7684task_id\u5b57\u6bb5\u964d\u5e8f\u8bbe\u7f6e\u4e00\u4e0b\uff08\u56e0\u4e3a\u6211\u4eec\u60f3\u8981\u7684\u529f\u80fd\u662f\u6309\u7167task_id\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u964d\u5e8f\u8bbe\u7f6e\u7684\u8bdd\u53ef\u4ee5\u6d4b\u8bd5\u4e0b\u6392\u5e8f\u529f\u80fd\u662f\u5426\u751f\u6548\uff09\uff0c\u7136\u540e\u8c03\u7528task_insert\u51fd\u6570\u628a\u8282\u70b9\u63d2\u5165\u5230rbtree\u91cc\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 task_insert\u51fd\u6570<\/h3>\n\n\n\n<p>\u8fd9\u4e2atask_insert\u51fd\u6570\u662f\u6211\u4eec\u81ea\u5df1\u5b9e\u73b0\u7684\uff0clinux\u5185\u6838\u7684rbtree\u6ca1\u6709\u63d0\u4f9b\u901a\u7528\u7684\u63d2\u5165\u51fd\u6570\uff0c\u56e0\u6b64\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7684task_insert\u51fd\u6570\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int task_insert(struct rb_root *p_root, struct task_node_t *p_task)\n{\n    struct rb_node **p_new = &amp;p_root->rb_node;\n    struct rb_node *p_parent = NULL;\n    struct task_node_t *p_this;\n\n    while (*p_new) \n    {\n        p_this = container_of(*p_new, struct task_node_t, node);\n        p_parent = *p_new;\n        if (p_task->task_id &lt; p_this->task_id)\n            p_new = &amp;((*p_new)->rb_left);\n        else if (p_task->task_id > p_this->task_id)\n            p_new = &amp;((*p_new)->rb_right);\n        else\n            return 0;\n    }\n\n    rb_link_node(&amp;p_task->node, p_parent, p_new);\n    rb_insert_color(&amp;p_task->node, p_root);\n\n    return 1;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u5bfb\u627e\u63d2\u5165\u4f4d\u7f6e<\/h3>\n\n\n\n<p>\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u662f\u628ap_task\u63d2\u5165\u5230p_root\u91cc\u9762\uff0c\u8fd4\u56de\u503c\u8868\u793a\u662f\u5426\u8fdb\u884c\u4e86\u63d2\u5165\u3002<\/p>\n\n\n\n<p>\u63d2\u5165\u7684\u65f6\u5019\u8981\u5728\u5b50\u6811\u4e2d\u5bfb\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u56e0\u6b64\u5b9a\u4e49\u4e86\u4e09\u4e2a\u6307\u9488\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>p_new\u8868\u793a\u8981\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u4e00\u5f00\u59cb\u662f\u6307\u5411\u6839\u8282\u70b9\u7684rb_node\uff0c\u65e2\u7b2c\u4e00\u4e2a\u666e\u901a\u8282\u70b9\u7684<\/li><li>p_parent\u6307\u5411\u8981\u63d2\u5165\u7684\u4f4d\u7f6e\u7684\u7236\u8282\u70b9\uff0c\u521d\u59cb\u5316\u4e3a\u7a7a\u6307\u9488<\/li><li>p_this\u662fp_new\u6240\u6307\u8282\u70b9\u7684\u5bb9\u5668\u7ed3\u6784\u4f53\u6307\u9488\uff0c\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u662ftask_node_t\u6307\u9488<\/li><\/ul>\n\n\n\n<p>\u7136\u540e\u5f00\u59cb\u63d2\u5165\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f\u4e2a\u5faa\u73af\u7684\u8fc7\u7a0b\uff0c\u4ed6\u4f1a\u6839\u636e\u5f53\u524d\u6240\u6307\u8282\u70b9\u7684task_id\u548c\u6211\u4eec\u8981\u63d2\u5165\u7684\u8282\u70b9\u7684task_id\u7684\u5927\u5c0f\u9009\u62e9\u63d2\u5165\u4f4d\u7f6e\uff0c\u5c0f\u7684\u653e\u5728\u5de6\u5b50\u6811\uff0c\u5927\u7684\u653e\u5728\u53f3\u5b50\u6811\uff0c\u76f8\u7b49\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u4e0d\u518d\u63d2\u5165\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\u4e0a\u8ff0\u6309\u7167\u5927\u5c0f\u6392\u5e8f\u9009\u62e9\u4ee5\u53ca\u76f8\u7b49\u8fd4\u56de\u7684\u63a7\u5236\u90fd\u662f\u7531\u6211\u4eec\u81ea\u5df1\u505a\u7684\uff0c\u5982\u679c\u6709\u9700\u8981\uff0c\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5de6\u5927\u53f3\u5c0f\u7684\u6392\u5e8f\u89c4\u5219\uff0c\u6216\u8005task_id\u76f8\u7b49\u5219\u66ff\u6362\u7684\u63d2\u5165\u89c4\u5219\uff0c\u6216\u8005\u628a\u591a\u4e2a\u5b57\u6bb5\u7efc\u5408\u8d77\u6765\u5224\u65ad\u5927\u5c0f\u7684\u89c4\u5219\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\uff0c\u627e\u5230\u4e86\u4e24\u4e2a\u6307\u9488\uff0c\u4e00\u4e2a\u662f\u8981\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9p_parent\uff0c\u4e00\u4e2a\u662fp_parent\u5bf9\u5e94\u5b50\u6811\u7684\u6307\u9488\u7684\u6307\u9488p_new\u3002\u5373\u4e0a\u8ff0\u8fc7\u7a0b\u53ea\u662f\u627e\u5230\u4e86\u8981\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u5e76\u6ca1\u6709\u628a\u8282\u70b9\u771f\u6b63\u7684\u63d2\u5165\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 \u63d2\u5165\u8282\u70b9<\/h3>\n\n\n\n<p>\u771f\u6b63\u7684\u63d2\u5165\u64cd\u4f5c\u8981\u8c03\u7528\u4e0b\u9762\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,\n\t\t\t\tstruct rb_node **rb_link)\n{\n\tnode->__rb_parent_color = (unsigned long)parent;\n\tnode->rb_left = node->rb_right = NULL;\n\n\t*rb_link = node;\n}<\/code><\/pre>\n\n\n\n<p>\u4e3a\u65b0\u8282\u70b9\u8bbe\u7f6e\u7236\u8282\u70b9\uff0c\u521d\u59cb\u5316\u5de6\u53f3\u5b50\u6811\u4e3a\u7a7a\uff0c\u628a\u7236\u8282\u70b9\u7684\u5bf9\u5e94\u5b50\u6811\u6307\u5411\u65b0\u8282\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.5 \u8282\u70b9\u8c03\u6574<\/h3>\n\n\n\n<p>\u8282\u70b9\u5b8c\u6210\u63d2\u5165\u540e\uff0c\u53ef\u80fd\u5bfc\u81f4\u7ea2\u9ed1\u6811\u7684\u5b9a\u4e49\u88ab\u6253\u7834\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u53d8\u8272\u548c\u65cb\u8f6c\uff0c\u4ee5\u4f7f\u5176\u6ee1\u8db3\u7ea2\u9ed1\u6811\u7684\u5b9a\u4e49\u3002<\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rb_insert_color(&amp;p_task->node, p_root);<\/code><\/pre>\n\n\n\n<p>rb_insert_color\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}\n\nvoid rb_insert_color(struct rb_node *node, struct rb_root *root)\n{\n\t__rb_insert(node, root, dummy_rotate);\n}<\/code><\/pre>\n\n\n\n<p>\u4ed6\u8c03\u7528\u4e86__rb_insert\u51fd\u6570\uff0c\u4f20\u5165\u4e86\u4e00\u4e2a\u65cb\u8f6c\u51fd\u6570\uff08\u4e0d\u8fc7\u662f\u7a7a\u7684\uff09\u3002<\/p>\n\n\n\n<p>__rb_insert\u8fd9\u4e2a\u51fd\u6570\u6bd4\u8f83\u590d\u6742\uff0c\u4e3a\u4e86\u987a\u5229\u7406\u89e3\u4ed6\uff0c\u6211\u4eec\u9700\u8981\u590d\u4e60\u4e00\u4e0b\u7ea2\u9ed1\u6811\u7684\u89c4\u5219\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a) \u7ea2\u9ed1\u6811\u89c4\u5219<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u7ea2\u9ed1\u6811\u9700\u8981\u6ee1\u8db3\u5982\u4e0b\u4e94\u4e2a\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u666e\u901a\u8282\u70b9\u662f\u7ea2\u8272\u6216\u9ed1\u8272<\/li><li>\u6839\u8282\u70b9\u662f\u9ed1\u8272<\/li><li>\u6240\u6709\u7684\u53f6\u5b50\u8282\u70b9\u90fd\u662f\u9ed1\u8272\u7684\uff08\u53f6\u5b50\u662fNIL\u8282\u70b9\uff09<\/li><li>\u6bcf\u4e2a\u7ea2\u8272\u8282\u70b9\u5fc5\u987b\u6709\u4e24\u4e2a\u9ed1\u8272\u7684\u5b50\u8282\u70b9\uff08\u4ece\u6bcf\u4e2a\u53f6\u5b50\u5230\u6839\u7684\u6240\u6709\u8def\u5f84\u4e0a\u4e0d\u80fd\u6709\u4e24\u4e2a\u8fde\u7eed\u7684\u7ea2\u8272\u8282\u70b9\uff09<\/li><li>\u4ece\u4efb\u4e00\u8282\u70b9\u5230\u5176\u6bcf\u4e2a\u53f6\u5b50\u7684\u6240\u6709\u7b80\u5355\u8def\u5f84\u90fd\u5305\u542b\u76f8\u540c\u6570\u76ee\u7684\u9ed1\u8272\u8282\u70b9<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/6\/66\/Red-black_tree_example.svg\/1024px-Red-black_tree_example.svg.png\" alt=\"An example of a red-black tree\"\/><\/figure>\n\n\n\n<p>\u8fd9\u4e2a\u89c4\u5219\u6bd4\u8f83\u590d\u6742\uff0c\u6211\u4eec\u7b80\u5355\u89e3\u91ca\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e0a\u9762\u8bf4\u7684\u53f6\u5b50\u8282\u70b9\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u8282\u70b9\uff0c\u8fd9\u4e2a\u89c4\u5219\u7684\u610f\u601d\u662f\u628a\u53f6\u5b50\u8282\u70b9\u7684\u7a7a\u5b50\u6811\u89c6\u4e3a\u9ed1\u8272\u8282\u70b9\uff08\u5982\u4e0a\u56fe\uff09<\/li><li>\u89c4\u52191\u548c\u89c4\u52193\u662f\u4e00\u5b9a\u6ee1\u8db3\u7684\uff0c\u4ed6\u5176\u5b9e\u53ea\u662f\u4e2a\u7ea6\u5b9a\uff0c\u6211\u4eec\u76f4\u63a5\u9075\u7167\u5373\u53ef<\/li><li>\u89c4\u52194\u53ea\u6709\u5728\u589e\u52a0\u7ea2\u8272\u8282\u70b9\u3001\u628a\u9ed1\u8272\u8282\u70b9\u8bbe\u4e3a\u7ea2\u8272\u3001\u6216\u505a\u65cb\u8f6c\u65f6\u624d\u6709\u53ef\u80fd\u6253\u7834<\/li><li>\u89c4\u52195\u53ea\u6709\u5728\u589e\u52a0\u9ed1\u8272\u8282\u70b9\u3001\u628a\u7ea2\u8272\u8282\u70b9\u8bbe\u4e3a\u9ed1\u8272\u3001\u6216\u505a\u65cb\u8f6c\u65f6\u624d\u6709\u53ef\u80fd\u6253\u7834<\/li><\/ul>\n\n\n\n<p>\u53e6\u5916\uff0c\u6211\u4eec\u53ef\u4ee5\u7ea6\u5b9a\uff0c<strong>\u65b0\u63d2\u5165\u7684\u8282\u70b9\u662f\u7ea2\u8272<\/strong>\u7684\u3002\u56e0\u4e3a\u5982\u679c\u7ea6\u5b9a\u65b0\u63d2\u5165\u7684\u8282\u70b9\u662f\u9ed1\u8272\u7684\u8bdd\uff0c\u4f1a\u5bfc\u81f4\u89c4\u52195\u5931\u6548\uff0c\u8fd9\u662f\u5f88\u96be\u8c03\u6574\u7684\u3002\u4f46\u662f\u5982\u679c\u7ea6\u5b9a\u65b0\u63d2\u5165\u8282\u70b9\u662f\u7ea2\u8272\u7684\uff0c\u90a3\u4e48\u5bf9\u89c4\u52195\u5176\u5b9e\u6ca1\u6709\u5f71\u54cd\uff0c\u53ea\u9700\u8981\u5904\u7406\u89c4\u52194\u7684\u8fde\u7eed\u7ea2\u8272\u8282\u70b9\u51b2\u7a81\u5373\u53ef\uff0c\u6211\u4eec<strong>\u9700\u8981\u5904\u7406\u7684\u60c5\u5f62\u5c31\u4f1a\u5927\u5927\u51cf\u5c11<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">b\uff09\u5206\u60c5\u5f62\u8bf4\u660e<\/h4>\n\n\n\n<p>\u4e3a\u4e86\u8bf4\u660e\u51e0\u79cd\u60c5\u5f62\uff0c\u6211\u4eec\u5148\u5bf9\u8282\u70b9\u7684\u547d\u540d\u4f5c\u51fa\u7ea6\u5b9a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>N<\/strong>ew\u4ee3\u8868\u65b0\u63d2\u5165\u7684\u8282\u70b9<\/li><li><strong>P<\/strong>arent\u4ee3\u8868\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9<\/li><li><strong>G<\/strong>rand parent\u4ee3\u8868\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9<\/li><li><strong>U<\/strong>ncle\u4ee3\u8868\u65b0\u63d2\u5165\u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u5144\u5f1f\u8282\u70b9<\/li><\/ul>\n\n\n\n<p><strong>\u60c5\u5f621\uff1aN\u4f4d\u4e8e\u6811\u7684\u6839\u4e0a\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002<\/strong><\/p>\n\n\n\n<p>\u6b64\u65f6\u53ea\u6253\u7834\u4e86\u89c4\u52192\uff0c\u56e0\u6b64\u6211\u4eec\u628a\u6839\u8282\u70b9\u8bbe\u7f6e\u4e3a\u9ed1\u8272\u5373\u53ef\uff0c\u540c\u65f6\u89c4\u52194\u548c5\u4e5f\u6ca1\u6709\u88ab\u6253\u7834\uff0c\u4ee3\u7801\u5982\u4e0b\uff08\u6839\u8282\u70b9\u7684\u7236\u8282\u70b9\u6307\u9488\u4e3aNULL\uff0c\u81ea\u5df1\u7684\u989c\u8272\u662f\u9ed1\u8272\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct rb_node *parent = rb_red_parent(node);\n\nwhile (1) {\n\t\/*\n\t * Loop invariant: node is red.\n\t *\/\n\tif (!parent) {\n\t\t\/*\n\t\t * The inserted node is root. Either this is the\n\t\t * first node, or we recursed at Case 1 below and\n\t\t * are no longer violating 4).\n\t\t *\/\n\t\trb_set_parent_color(node, NULL, RB_BLACK);\n\t\tbreak;\n\t}<\/code><\/pre>\n\n\n\n<p><strong>\u60c5\u5f622\uff1a\u65b0\u8282\u70b9\u7684\u7236\u8282P\u662f\u9ed1\u8272\u7684<\/strong><\/p>\n\n\n\n<p>\u6b64\u65f6\u63d2\u5165\u65b0\u8282\u70b9\u4e0d\u5f71\u54cd\u4efb\u4f55\u4efb\u4f55\u4e00\u4e2a\u89c4\u5219\uff0c\u56e0\u6b64\u65e0\u9700\u8c03\u6574\u76f4\u63a5\u63d2\u5165\u5373\u53ef\u3002<\/p>\n\n\n\n<p><strong>\u60c5\u5f623\uff1a\u65b0\u8282\u70b9\u7684\u7236\u4eb2P\u548c\u53d4\u7236U\u90fd\u662f\u7ea2\u8272\u7684<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/c\/c8\/Red-black_tree_insert_case_3.png\" alt=\"\u60c5\u5f623\u793a\u610f\u56fe\"\/><\/figure>\n\n\n\n<p>\u5728\u672a\u8c03\u6574\u524d\uff0c\u6839\u636e\u89c4\u52194\uff0c\u7956\u7236\u8282\u70b9G\u4e00\u5b9a\u662f\u9ed1\u8272\u7684\u3002<\/p>\n\n\n\n<p>\u589e\u52a0\u7ea2\u8272\u8282\u70b9N\u540e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bfc\u81f4\u89c4\u52194\u88ab\u6253\u7834\uff0c\u56e0\u6b64\u9700\u5c06\u8282\u70b9P\u6539\u6210\u9ed1\u8272\u7684<\/li><li>\u4e3a\u4e86\u4fdd\u6301N\u6240\u5728\u7684\u94fe\u8def\u4e0a\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u4e0d\u53d8\uff0c\u56e0\u6b64\u9700\u5c06\u7956\u7236\u8282\u70b9\u6539\u6210\u7ea2\u8272\u7684<\/li><li>\u540c\u65f6\u4e3a\u4e86\u4fdd\u6301\u53d4\u7236U\u6240\u5728\u8282\u70b9\u7684\u9ed1\u8272\u8282\u70b9\u6570\u91cf\u4e0d\u53d8\uff0c\u9700\u8981\u5c06\u53d4\u7236\u6539\u6210\u9ed1\u8272\u7684<\/li><li>\u7956\u7236G\u53ef\u80fd\u662f\u6839\u8282\u70b9\uff0c\u56e0\u6b64\u5176\u53d8\u6210\u7ea2\u8272\u540e\u53ef\u80fd\u5bfc\u81f4\u89c4\u52192\u5931\u6548\uff0c\u9700\u8981\u4ee5\u7956\u7236G\u4e3a\u65b0\u8282\u70b9\u9012\u5f52\u6267\u884c\u4e0a\u8ff0\u8fc7\u7a0b<\/li><\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5c0f\u5199\u5b57\u6bcd\u4ee3\u8868\u7ea2\u8272\u8282\u70b9\uff0c\u5927\u5199\u5b57\u6bcd\u4ee3\u8868\u9ed1\u8272\u8282\u70b9<\/li><li>rb_set_parent_color\u662f\u540c\u65f6\u8bbe\u7f6e\u7236\u8282\u70b9\u7684\u6307\u9488\u548c\u672c\u8282\u70b9\u7684\u989c\u8272<ul><li>\u628a\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u7236\u8282\u70b9\u6307\u9488\u8bbe\u7f6e\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570<\/li><li>\u628a\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u989c\u8272\u8bbe\u7f6e\u4e3a\u7b2c\u4e09\u4e2a\u53c2\u6570<\/li><\/ul><\/li><li>tmp\u6307\u5411\u53d4\u7236\u8282\u70b9U<\/li><li>node\u6307\u5411\u65b0\u8282\u70b9n<\/li><li>\u5728\u672b\u5c3e\u7684continue\u4e4b\u524d\uff0c\u5b83\u628anode\u6307\u5411\u4e86\u7956\u7236\u8282\u70b9G\uff0c\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n * Case 1 - node's uncle is red (color flips).\n *\n *       G            g\n *      \/ \\          \/ \\\n *     p   u  -->   P   U\n *    \/            \/\n *   n            n\n *\n * However, since g's parent might be red, and\n * 4) does not allow this, we need to recurse\n * at g.\n *\/\nrb_set_parent_color(tmp, gparent, RB_BLACK);\nrb_set_parent_color(parent, gparent, RB_BLACK);\nnode = gparent;\nparent = rb_parent(node);\nrb_set_parent_color(node, parent, RB_RED);\ncontinue;<\/code><\/pre>\n\n\n\n<p><strong>\u60c5\u5f624\uff1a\u65b0\u8282\u70b9\u7684\u7236\u4eb2P\u662f\u7ea2\u8272\u7684\uff0c\u53d4\u7236U\u90fd\u662f\u9ed1\u8272\u7684(\u6216\u6ca1\u6709)\uff0c\u65b0\u8282\u70b9\u4f4d\u4e8e\u5176\u7236\u4eb2\u8282\u70b9\u7684\u53f3\u5b50\u6811\u4e0a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/5\/56\/Red-black_tree_insert_case_4.png\" alt=\"\u60c5\u5f624\u793a\u610f\u56fe\"\/><\/figure>\n\n\n\n<p>\u5728\u672a\u8c03\u6574\u524d\uff0c\u6839\u636e\u89c4\u52194\uff0c\u7956\u7236\u8282\u70b9G\u4e00\u5b9a\u662f\u9ed1\u8272\u7684\u3002<\/p>\n\n\n\n<p>\u5904\u7406\u8fd9\u4e2a\u60c5\u5f62\uff0c\u8981\u4e48\u6539\u989c\u8272\uff0c\u8981\u4e48\u65cb\u8f6c\u3002<\/p>\n\n\n\n<p>\u5148\u770b\u6539\u989c\u8272\u7684\u65b9\u6848\uff1a\u6211\u4eec\u53ef\u4ee5\u6548\u4eff\u60c5\u5f623\u628aP\u6539\u6210\u9ed1\u8272\uff0c\u7136\u540e\u7531\u4e8eN\u6240\u5728\u94fe\u8def\u4e0a\u591a\u4e86\u4e2a\u9ed1\u8282\u70b9\uff0c\u56e0\u6b64\u9700\u5c06\u7956\u7236G\u6539\u6210\u7ea2\u8272\uff0c\u4f46\u662f\u6b64\u65f6\u53d4\u7236\u8282\u70b9\u6240\u5728\u7684\u94fe\u8def\u4e0a\u4f1a\u5c11\u4e00\u4e2a\u9ed1\u8272\u8282\u70b9\uff0c\u8fd9\u5c31\u5f88\u96be\u5904\u7406\u4e86\u3002\u6b64\u8def\u4e0d\u901a\u3002<\/p>\n\n\n\n<p>\u90a3\u5c31\u53ea\u80fd\u505a\u65cb\u8f6c\u4e86\uff1a\u56e0\u4e3a<strong>\u65cb\u8f6c\u662f\u4e0d\u6539\u53d8\u989c\u8272\u603b\u91cf\uff0c\u53ea\u6539\u53d8\u989c\u8272\u5206\u5e03<\/strong>\u7684\u3002\u4e0a\u56fe\u5de6\u4fa7\u8fd9\u79cd\u6811\u5f62\u662f\u4e0d\u5229\u4e8e\u6211\u4eec\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u65cb\u8f6c\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u5148\u5c06\u4e0a\u56fe\u5de6\u8fb9\u7684\u6811\u5f62\u8c03\u6574\u6210\u53f3\u8fb9\u7684\u6811\u5f62\uff0c\u4f7f\u5176\u7b26\u5408\u60c5\u5f625\u7684\u6761\u4ef6\uff0c\u6ce8\u610f\u4e0a\u8ff0\u8c03\u6574\u540e\u7684\u6811\u5f62\u4f9d\u7136\u4e0d\u7b26\u5408\u89c4\u52194\uff0c\u6211\u4eec\u7ee7\u7eed\u8fd0\u884c\u60c5\u5f625\u7684\u5904\u7406\u8fc7\u7a0b\u6765\u4fee\u6b63\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<p>\u5728\u672c\u60c5\u5f62\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5bf9\u7956\u7236\u8282\u70b9P\u8fdb\u884c\u4e00\u6b21\u5de6\u65cb\uff08N\u6210\u4e3a\u65b0\u7684\u7236\u8282\u70b9\uff0cP\u6210\u4e3aN\u7684\u5de6\u5b50\u6811\uff0cN\u539f\u5148\u7684\u5de6\u5b50\u6811\u6210\u4e3aP\u7684\u53f3\u5b50\u6811\uff09\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-b0702caa-4ca5-40dc-9b3e-53664c6b433b\"><li>node\u6307\u5411\u7684\u662f\u65b0\u8282\u70b9N<\/li><li>\u6ce8\u610f\u4e0b\u9762\u7684\u4e24\u4e2arb_set_parent_color\u7684\u76ee\u7684\u90fd\u662f\u6539\u53d8\u7236\u6307\u9488\uff0c\u800c\u975e\u6539\u53d8\u5f53\u524d\u8282\u70b9\u7684\u989c\u8272<\/li><li>tmp\u6307\u5411\u7684\u662f\u65b0\u8282\u70b9N\u7684\u5de6\u5b50\u6811\uff0c\u65cb\u8f6c\u540e\u4ed6\u6210\u4e86\u7236\u8282\u70b9P\u7684\u53f3\u5b50\u6811\uff0c\u9700\u8981\u4fee\u6539\u7236\u8282\u70b9\u6307\u9488<\/li><li>\u65b0\u8282\u70b9N\u65cb\u8f6c\u540e\u6210\u4e86\u7236\u4eb2\u8282\u70b9P\u7684\u7236\u4eb2\u8282\u70b9\uff0c\u9700\u8981\u4fee\u6539\u7236\u8282\u70b9\u6307\u9488<\/li><li>augment_rotate\u662f\u4e2a\u7a7a\u5b9e\u73b0\uff0c\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c<\/li><li>\u6700\u540e\u66f4\u65b0\u6307\u9488\uff0c\u7236\u8282\u70b9\u6307\u5411\u539f\u5148\u7684\u65b0\u8282\u70b9\uff0ctmp\u6307\u5411\u539f\u5148\u7684\u65b0\u8282\u70b9\u7684\u53f3\u5b50\u6811<\/li><li>\u672b\u5c3e\u6ca1\u6709break\u6216continue\uff0c\u8868\u660e\u4f1a\u7ee7\u7eed\u8fdb\u884c\u4e0b\u4e00\u79cd\u60c5\u5f62\u7684\u5904\u7406<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n * Case 2 - node's uncle is black and node is\n * the parent's right child (left rotate at parent).\n *\n *      G             G\n *     \/ \\           \/ \\\n *    p   U  -->    n   U\n *     \\           \/\n *      n         p\n *\n * This still leaves us in violation of 4), the\n * continuation into Case 3 will fix that.\n *\/\ntmp = node->rb_left;\nparent->rb_right = tmp;\nnode->rb_left = parent;\nif (tmp)\n\trb_set_parent_color(tmp, parent,\n RB_BLACK);\nrb_set_parent_color(parent, node, RB_RED);\naugment_rotate(parent, node);\nparent = node;\ntmp = node->rb_right;<\/code><\/pre>\n\n\n\n<p><strong>\u60c5\u5f625\uff1a\u65b0\u8282\u70b9\u7684\u7236\u4eb2P\u662f\u7ea2\u8272\u7684\uff0c\u53d4\u7236U\u90fd\u662f\u9ed1\u8272\u7684(\u6216\u6ca1\u6709)\uff0c\u65b0\u8282\u70b9\u4f4d\u4e8e\u5176\u7236\u4eb2\u8282\u70b9\u7684\u5de6\u5b50\u6811\u4e0a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/6\/66\/Red-black_tree_insert_case_5.png\" alt=\"\u60c5\u5f625\u793a\u610f\u56fe\"\/><\/figure>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u6709\u53ef\u80fd\u662f\u539f\u751f\u7684\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u7531\u4e0a\u8ff0\u7684\u60c5\u5f624\u8c03\u6574\u800c\u6765\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6539\u53d8\u8282\u70b9\u989c\u8272\u4f9d\u7136\u884c\u4e0d\u901a\uff08\u7406\u7531\u540c\u60c5\u5f624\uff09\uff0c\u56e0\u6b64\u53ea\u80fd\u505a\u65cb\u8f6c\uff0c\u4e0d\u6539\u53d8\u989c\u8272\u7684\u603b\u6570\u91cf\uff0c\u53ea\u6539\u53d8\u4e0d\u540c\u989c\u8272\u5206\u5e03\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u8c03\u6574\u7684\u76ee\u6807\u662f\uff0c\u628a\u5de6\u8fb9\u7684\u8fde\u7eed\u4e24\u4e2a\u7ea2\u8272\u8282\u70b9\u5206\u5f00\uff08\u6ee1\u8db3\u89c4\u52194\uff09\uff0c\u540c\u65f6\u4e0d\u6539\u53d8\u53f3\u8fb9\u7684\u9ed1\u8272\u8282\u70b9\u603b\u6570\uff08\u6ee1\u8db3\u89c4\u52195\uff09\u3002<\/p>\n\n\n\n<p>\u8c03\u6574\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bf9P\u505a\u4e00\u6b21\u53f3\u65cb\uff0c\u628aG\u53d8\u6210P\u7684\u53f3\u5b50\u6811<\/li><li>\u7136\u540e\u5bf9\u8c03P\u548cG\u7684\u989c\u8272<\/li><\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-a2f8242c-1b58-49ae-bcd1-d17a09614437\"><li>tmp\u6307\u5411\u7684\u662f\u7236\u8282\u70b9P\u7684\u53f3\u5b50\u6811<\/li><li>\u7236\u8282\u70b9P\u7684\u53f3\u5b50\u6811\u65cb\u8f6c\u540e\u53d8\u6210\u4e86G\u7684\u5de6\u5b50\u6811\uff0c\u989c\u8272\u4e0d\u53d8\uff08\u4ecd\u4e3a\u9ed1\u8272\uff09<\/li><li>\u7236\u8282\u70b9P\u53d8\u4e3a\u6839\u8282\u70b9\uff0c\u53f3\u5b50\u6811\u53d8\u6210\u4e86\u7956\u7236\u8282\u70b9G<\/li><li>\u7236\u8282\u70b9P\u7684\u989c\u8272\u53d8\u6210\u4e86\u9ed1\u8272\uff0c\u7956\u7236\u8282\u70b9G\u7684\u989c\u8272\u53d8\u6210\u4e86\u7ea2\u8272<\/li><li>augment_rotate\u662f\u4e2a\u7a7a\u5b9e\u73b0\uff0c\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c<\/li><li>\u672b\u5c3e\u6709\u4e2abreak\uff0c\u8868\u660e\u5904\u7406\u7ed3\u675f<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n * Case 3 - node's uncle is black and node is\n * the parent's left child (right rotate at gparent).\n *\n *        G           P\n *       \/ \\         \/ \\\n *      p   U  -->  n   g\n *     \/                 \\\n *    n                   U\n *\/\ngparent->rb_left = tmp; \/* == parent->rb_right *\/\nparent->rb_right = gparent;\nif (tmp)\n\trb_set_parent_color(tmp, gparent, RB_BLACK);\n__rb_rotate_set_parents(gparent, parent, root, RB_RED);\naugment_rotate(gparent, parent);\nbreak;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u641c\u7d22<\/h2>\n\n\n\n<p>\u641c\u7d22\u7684\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/search\nfor(i=0;i&lt;100;i++)\n{\n\tp_task = task_search(&amp;task_tree, i);\n\tprintf(\"%d %d\\n\", i, p_task->task_id);\n}<\/code><\/pre>\n\n\n\n<p>\u4e0e\u63d2\u5165\u7b97\u6cd5\u7c7b\u4f3c\uff0c\u641c\u7d22\u7b97\u6cd5\u4e5f\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct task_node_t *task_search(struct rb_root *p_root, int task_id)\n{\n\tstruct rb_node *p_node = p_root->rb_node;\n    struct task_node_t *p_task;\n\n\twhile (p_node) \n    {\n\t\tp_task = container_of(p_node, typeof(*p_task), node);\n\t\tif (task_id &lt; p_task->task_id)\n\t\t\tp_node = p_node->rb_left;\n\t\telse if (task_id > p_task->task_id)\n\t\t\tp_node = p_node->rb_right;\n\t\telse\n\t\t\treturn p_task;\n\t}\n\treturn NULL;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u8ddf\u63d2\u5165\u7684\u4ee3\u7801task_insert\u975e\u5e38\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e0d\u518d\u91cd\u590d\u89e3\u91ca\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"273\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-1.png\" alt=\"\" class=\"wp-image-1022\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-1.png 349w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-1-300x235.png 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-1-345x270.png 345w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u904d\u5386<\/h2>\n\n\n\n<p>\u904d\u5386\u7684\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u904d\u5386\nstruct rb_node *p_node;\nfor (p_node = rb_first(&amp;task_tree); p_node; p_node = rb_next(p_node))\n    printf(\"task_id=%d\\n\", rb_entry(p_node, struct task_node_t, node)->task_id);<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"164\" height=\"354\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-2.png\" alt=\"\" class=\"wp-image-1023\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-2.png 164w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-2-139x300.png 139w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/12\/image-2-125x270.png 125w\" sizes=\"auto, (max-width: 164px) 100vw, 164px\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53d1\u73b0\uff0c\u867d\u7136\u6211\u4eec\u63d2\u5165\u7684\u65f6\u5019\u662f\u5012\u5e8f\u63d2\u5165\u7684\uff0c\u4f46\u662f\u904d\u5386\u7684\u65f6\u5019\u81ea\u52a8\u53d8\u6210\u6b63\u5e8f\u4e86\uff0c\u6211\u4eec\u7684\u76ee\u7684\u4e5f\u8fbe\u5230\u4e86\u3002<\/p>\n\n\n\n<p>\u5176\u4e2drbfirst\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct rb_node *rb_first(const struct rb_root *root)\n{\n\tstruct rb_node\t*n;\n\n\tn = root->rb_node;\n\tif (!n)\n\t\treturn NULL;\n\twhile (n->rb_left)\n\t\tn = n->rb_left;\n\treturn n;\n}<\/code><\/pre>\n\n\n\n<p>\u5373\u5148\u4ece\u5de6\u5b50\u6811\u7684\u53f6\u5b50\u8282\u70b9\u904d\u5386\u3002<\/p>\n\n\n\n<p>rb_next\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct rb_node *rb_next(const struct rb_node *node)\n{\n\tstruct rb_node *parent;\n\n\tif (RB_EMPTY_NODE(node))\n\t\treturn NULL;\n\n\t\/*\n\t * If we have a right-hand child, go down and then left as far\n\t * as we can.\n\t *\/\n\tif (node->rb_right) {\n\t\tnode = node->rb_right;\n\t\twhile (node->rb_left)\n\t\t\tnode = node->rb_left;\n\t\treturn (struct rb_node *)node;\n\t}\n\n\t\/*\n\t * No right-hand children. Everything down and left is smaller than us,\n\t * so any 'next' node must be in the general direction of our parent.\n\t * Go up the tree; any time the ancestor is a right-hand child of its\n\t * parent, keep going up. First time it's a left-hand child of its\n\t * parent, said parent is our 'next' node.\n\t *\/\n\twhile ((parent = rb_parent(node)) &amp;&amp; node == parent->rb_right)\n\t\tnode = parent;\n\n\treturn parent;\n}<\/code><\/pre>\n\n\n\n<p>\u6c42\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u65f6\u5019\uff0c\u81ea\u5df1\u80af\u5b9a\u662f\u6ca1\u6709\u5de6\u5b50\u6811\u7684\uff0c\u56e0\u6b64\u5f97\u5148\u770b\u770b\u6709\u6ca1\u6709\u53f3\u5b50\u6811\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5982\u679c\u6709\u7684\u8bdd\u53d6\u5176\u5de6\u5b50\u6811\u7684\u53f6\u5b50\u8282\u70b9\u3002<\/li><li>\u5982\u679c\u6ca1\u6709\u7684\u8bdd\uff0c\u8bf4\u660e\u5f53\u524d\u8282\u70b9\u5305\u62ec\u5176\u5de6\u53f3\u5b50\u6811\u90fd\u5df2\u7ecf\u904d\u5386\u5b8c\u4e86\uff0c\u9700\u8981\u5f80\u7236\u8282\u70b9\u8d70<ul><li>\u5982\u679c\u81ea\u5df1\u662f\u7236\u8282\u70b9\u7684\u5de6\u5b50\u6811\uff0c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\u7236\u8282\u70b9\u5373\u53ef<\/li><li>\u5982\u679c\u81ea\u5df1\u662f\u7236\u8282\u70b9\u7684\u53f3\u5b50\u6811\uff0c\u8bf4\u660e\u7236\u8282\u70b9\u4e5f\u904d\u5386\u5b8c\u4e86\uff08\u904d\u5386\u987a\u5e8f\u662f\u5de6\u5b50\u6811\u3001\u8282\u70b9\u672c\u8eab\u3001\u53f3\u5b50\u6811\uff09\u9700\u8981\u518d\u5f80\u4e0a\u8d70\u4e00\u7ea7<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">6.\u5fc3\u5f97\u4f53\u4f1a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 Can I make it simple?<\/h3>\n\n\n\n<p>\u4ecelist\u3001hashtable\u5230\u672c\u7bc7\u6587\u7ae0\u7684rbtree\uff0c\u6211\u4eec\u4e0d\u96be\u53d1\u73b0linux\u5185\u6838\u6570\u636e\u7ed3\u6784\u7684\u4e00\u4e2a\u8bbe\u8ba1\u539f\u5219\uff0c\u5373\u53ea\u5b9e\u73b0\u5fc5\u987b\u5b9e\u73b0\u7684\uff0c\u7528\u6237\u80fd\u5b9e\u73b0\u7684\u5c31\u8ba9\u7528\u6237\u53bb\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982rbtree\uff0clinux\u5185\u6838\u4ec5\u5b9e\u73b0\u4e86\u6700\u6838\u5fc3\u7684\u90e8\u5206\uff0c\u50cf\u63d2\u5165\u641c\u7d22\u67e5\u8be2\u8fd9\u7c7b\u529f\u80fd\u90fd\u8ba9\u7528\u6237\u53bb\u5b9e\u73b0\u4e86\u3002\u8fd9\u4e5f\u662flinux\u5185\u6838\u80fd\u591f\u4fdd\u6301\u7cbe\u7b80\u9ad8\u6548\u7684\u79d8\u8bc0\u4e4b\u4e00\u5427\u3002<\/p>\n\n\n\n<p><strong>\u4fdd\u6301\u7b80\u5355\uff0c\u4f46\u662f\u628a\u7b80\u5355\u7684\u4e8b\u60c5\u505a\u5230\u6781\u81f4\u3002<\/strong>\u6211\u4eec\u505a\u4efb\u4f55\u8f6f\u4ef6\uff0c\u751a\u81f3\u505a\u4efb\u4f55\u51b3\u7b56\u7684\u65f6\u5019\u90fd\u53ef\u4ee5\u501f\u9274\u8fd9\u4e00\u70b9\u3002\u8fd9\u4e2a\u529f\u80fd\u975e\u5f97\u7528\u8fd9\u4e48\u590d\u6742\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e48\uff1f\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u518d\u7b80\u5355\u4e00\u70b9\u4e48\uff1f\u8fd9\u4ef6\u4e8b\u60c5\u6211\u5fc5\u987b\u505a\u4e48\uff1fCan I make it simple\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 \u95ee\u9898\u964d\u7ef4\u80fd\u529b<\/h3>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u4f53\u4f1a\u662f\uff0c\u8fd9\u4e9b\u7b97\u6cd5\u5927\u795e\u548c\u4ee3\u7801\u5927\u795e\u4eec\u603b\u662f\u80fd\u901a\u8fc7\u5206\u6790\uff0c\u53d1\u73b0\u4e00\u4e9b\u9690\u85cf\u7684\u524d\u63d0\u6761\u4ef6\uff0c\u4ece\u800c\u628a\u8981\u505a\u7684\u4e8b\u60c5\u4e0d\u65ad\u5730\u5316\u7b80\uff0c\u6700\u540e\u53d8\u6210\u4e00\u4e2a\u89c4\u6a21\u53c8\u5c0f\uff0c\u96be\u5ea6\u53c8\u4f4e\u7684\u95ee\u9898\uff0c\u8ba9\u666e\u901a\u4eba\u4e5f\u80fd\u7406\u89e3\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u95ee\u9898\u964d\u7ef4\u80fd\u529b\uff0c\u662f\u4e00\u79cd\u9ad8\u7ea7\u80fd\u529b\u3002\u9ad8\u624b\u603b\u662f\u80fd\u628a\u95ee\u9898\u7684\u89c4\u6a21\u548c\u96be\u5ea6\u7684\u7b49\u7ea7\u4e0d\u65ad\u964d\u4f4e\uff0c\u628a\u590d\u6742\u7684\u4e8b\u60c5\u4e0d\u65ad\u5316\u7b80\uff0c\u6700\u540e\u8ba9\u666e\u901a\u4eba\u4e5f\u80fd\u6ee1\u8db3\u5de5\u4f5c\u8981\u6c42\uff0c\u53c2\u4e0e\u5230\u590d\u6742\u7684\u5de5\u4f5c\u4e2d\u53bb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7.\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>rbtree\u7684\u4e2d\u6587\u7ef4\u57fa\u767e\u79d1\u9875\u9762\uff1a<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E7%BA%A2%E9%BB%91%E6%A0%91\">\u94fe\u63a5<\/a><\/li><li>\u65e7\u91d1\u5c71\u5927\u5b66\u7684 David Galles\u505a\u7684\u53ef\u89c6\u5316rbtree\uff1a<a href=\"https:\/\/www.cs.usfca.edu\/~galles\/visualization\/RedBlack.html\">\u94fe\u63a5<\/a><\/li><li>linux rbtree\u5b98\u65b9\u4f7f\u7528\u8bf4\u660e\uff1a<a href=\"https:\/\/www.kernel.org\/doc\/Documentation\/rbtree.txt\">\u94fe\u63a5<\/a><\/li><li>\u300aSTL\u6e90\u7801\u5256\u6790\u300b 5.2\u5c0f\u8282, \u4faf\u6377<\/li><li>\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b \u7b2c\u4e09\u7248 \u7b2c13\u7ae0<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1.\u524d\u8a00 \u8fd9\u662flinux\u5185\u6838\u6570\u636e\u7ed3\u6784\u7684\u7b2c\u4e09\u7bc7\uff0c\u524d\u4e24\u7bc7\u5206\u522b\u8bb2\u4e86list\u548chashtable\uff0c\u6709\u4e0d\u719f\u6089\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u79fb\u6b65\u53c2\u89c2\u4e0b\u3002 \u672c\u7bc7\u6211\u4eec\u5c06\u7814\u7a76\u4e0brbtree\u7684\u5b9e\u73b0\u3002&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=1009\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1009","post","type-post","status-publish","format-standard","hentry","category-03-01-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/1009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1009"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/1009\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1009"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}