[转载]红黑树 rbtree 实际使用例子一个

原地址:http://www.linuxforum.net/forum/showthreaded.php?Cat=&Board=program&Number=556347&page=&view=&sb=&o=

Code:
  1. rbtree.h  
  2.   
  3. /* 
  4.  
  5.   Red Black Trees 
  6.  
  7.   (C) 1999  Andrea Arcangeli <andrea@suse.de> 
  8.  
  9.    
  10.  
  11.   This program is free software; you can redistribute it and/or modify 
  12.  
  13.   it under the terms of the GNU General Public License as published by 
  14.  
  15.   the Free Software Foundation; either version 2 of the License, or 
  16.  
  17.   (at your option) any later version. 
  18.  
  19.  
  20.  
  21.   This program is distributed in the hope that it will be useful, 
  22.  
  23.   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  24.  
  25.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  26.  
  27.   GNU General Public License for more details. 
  28.  
  29.  
  30.  
  31.   You should have received a copy of the GNU General Public License 
  32.  
  33.   along with this program; if not, write to the Free Software 
  34.  
  35.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  36.  
  37.  
  38.  
  39.   To use rbtrees you'll have to implement your own insert and search cores. 
  40.  
  41.   This will avoid us to use callbacks and to drop drammatically performances. 
  42.  
  43.   I know it's not the cleaner way,  but in C (not in C++) to get 
  44.  
  45.   performances and genericity... 
  46.  
  47.  
  48.  
  49.   Some example of insert and search follows here. The search is a plain 
  50.  
  51.   normal search over an ordered tree. The insert instead must be implemented 
  52.  
  53.   int two steps: as first thing the code must insert the element in 
  54.  
  55.   order as a red leaf in the tree, then the support library function 
  56.  
  57.   rb_insert_color() must be called. Such function will do the 
  58.  
  59.   not trivial work to rebalance the rbtree if necessary. 
  60.  
  61.  
  62.  
  63. ----------------------------------------------------------------------- 
  64. */  
  65. static inline struct page * rb_search_page_cache(struct inode * inode,  
  66.   
  67.                          unsigned long offset)  
  68.   
  69. {  
  70.   
  71.     rb_node_t * n = inode->i_rb_page_cache.rb_node;  
  72.   
  73.     struct page * page;  
  74.   
  75.   
  76.   
  77.     while (n)  
  78.   
  79.     {  
  80.   
  81.         page = rb_entry(n, struct page, rb_page_cache);  
  82.   
  83.   
  84.   
  85.         if (offset < page->offset)  
  86.   
  87.             n = n->rb_left;  
  88.   
  89.         else if (offset > page->offset)  
  90.   
  91.             n = n->rb_right;  
  92.   
  93.         else  
  94.   
  95.             return page;  
  96.   
  97.     }  
  98.   
  99.     return NULL;  
  100.   
  101. }  
  102.   
  103.   
  104.   
  105. static inline struct page * __rb_insert_page_cache(struct inode * inode,  
  106.   
  107.                            unsigned long offset,  
  108.   
  109.                            rb_node_t * node)  
  110.   
  111. {  
  112.   
  113.     rb_node_t ** p = &inode->i_rb_page_cache.rb_node;  
  114.   
  115.     rb_node_t * parent = NULL;  
  116.   
  117.     struct page * page;  
  118.   
  119.   
  120.   
  121.     while (*p)  
  122.   
  123.     {  
  124.   
  125.         parent = *p;  
  126.   
  127.         page = rb_entry(parent, struct page, rb_page_cache);  
  128.   
  129.   
  130.   
  131.         if (offset < page->offset)  
  132.   
  133.             p = &(*p)->rb_left;  
  134.   
  135.         else if (offset > page->offset)  
  136.   
  137.             p = &(*p)->rb_right;  
  138.   
  139.         else  
  140.   
  141.             return page;  
  142.   
  143.     }  
  144.   
  145.   
  146.   
  147.     rb_link_node(node, parent, p);  
  148.   
  149.   
  150.   
  151.     return NULL;  
  152.   
  153. }  
  154.   
  155.   
  156.   
  157. static inline struct page * rb_insert_page_cache(struct inode * inode,  
  158.   
  159.                          unsigned long offset,  
  160.   
  161.                          rb_node_t * node)  
  162.   
  163. {  
  164.   
  165.     struct page * ret;  
  166.   
  167.     if ((ret = __rb_insert_page_cache(inode, offset, node)))  
  168.   
  169.         goto out;  
  170.   
  171.     rb_insert_color(node, &inode->i_rb_page_cache);  
  172.   
  173.  out:  
  174.   
  175.     return ret;  
  176.   
  177. }  
  178.   
  179. -----------------------------------------------------------------------  
  180.   
  181. teawater:This file is come from the Linux Kernel.  
  182.   
  183. */  
  184.   
  185.   
  186.   
  187. #ifndef _LINUX_RBTREE_H  
  188.   
  189. #define _LINUX_RBTREE_H  
  190.   
  191.   
  192.   
  193. typedef struct rb_node_s {  
  194.   
  195.     struct rb_node_s * rb_parent;  
  196.   
  197.     int rb_color;  
  198.   
  199. #define RB_RED      0  
  200.   
  201. #define RB_BLACK    1  
  202.   
  203.     struct rb_node_s * rb_right;  
  204.   
  205.     struct rb_node_s * rb_left;  
  206.   
  207. }rb_node_t;  
  208.   
  209.   
  210.   
  211. typedef struct rb_root_s {  
  212.   
  213.     struct rb_node_s * rb_node;  
  214.   
  215. }rb_root_t;  
  216.   
  217.   
  218.   
  219. #define RB_ROOT             (rb_root_t){ NULL, }  
  220.   
  221. #define rb_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned  
  222. long)(&((type *)0)->member)))  
  223.   
  224.   
  225.   
  226.   
  227.   
  228. static inline void  
  229.   
  230. rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)  
  231.   
  232. {  
  233.   
  234.     node->rb_parent = parent;  
  235.   
  236.     node->rb_color = RB_RED;  
  237.   
  238.     node->rb_left = node->rb_right = NULL;  
  239.   
  240.   
  241.   
  242.     *rb_link = node;  
  243.   
  244. }  
  245.   
  246.   
  247.   
  248. static void  
  249.   
  250. __rb_rotate_left(rb_node_t * node, rb_root_t * root)  
  251.   
  252. {  
  253.   
  254.     rb_node_t * right = node->rb_right;  
  255.   
  256.   
  257.   
  258.     if ((node->rb_right = right->rb_left))  
  259.   
  260.         right->rb_left->rb_parent = node;  
  261.   
  262.     right->rb_left = node;  
  263.   
  264.   
  265.   
  266.     if ((right->rb_parent = node->rb_parent))  
  267.   
  268.     {  
  269.   
  270.         if (node == node->rb_parent->rb_left)  
  271.   
  272.             node->rb_parent->rb_left = right;  
  273.   
  274.         else  
  275.   
  276.             node->rb_parent->rb_right = right;  
  277.   
  278.     }  
  279.   
  280.     else  
  281.   
  282.         root->rb_node = right;  
  283.   
  284.     node->rb_parent = right;  
  285.   
  286. }  
  287.   
  288.   
  289.   
  290. static void  
  291.   
  292. __rb_rotate_right(rb_node_t * node, rb_root_t * root)  
  293.   
  294. {  
  295.   
  296.     rb_node_t * left = node->rb_left;  
  297.   
  298.   
  299.   
  300.     if ((node->rb_left = left->rb_right))  
  301.   
  302.         left->rb_right->rb_parent = node;  
  303.   
  304.     left->rb_right = node;  
  305.   
  306.   
  307.   
  308.     if ((left->rb_parent = node->rb_parent))  
  309.   
  310.     {  
  311.   
  312.         if (node == node->rb_parent->rb_right)  
  313.   
  314.             node->rb_parent->rb_right = left;  
  315.   
  316.         else  
  317.   
  318.             node->rb_parent->rb_left = left;  
  319.   
  320.     }  
  321.   
  322.     else  
  323.   
  324.         root->rb_node = left;  
  325.   
  326.     node->rb_parent = left;  
  327.   
  328. }  
  329.   
  330.   
  331.   
  332. static void  
  333.   
  334. rb_insert_color(rb_node_t * node, rb_root_t * root)  
  335.   
  336. {  
  337.   
  338.     rb_node_t * parent, * gparent;  
  339.   
  340.   
  341.   
  342.     while ((parent = node->rb_parent) && parent->rb_color == RB_RED)  
  343.   
  344.     {  
  345.   
  346.         gparent = parent->rb_parent;  
  347.   
  348.   
  349.   
  350.         if (parent == gparent->rb_left)  
  351.   
  352.         {  
  353.   
  354.             {  
  355.   
  356.                 register rb_node_t * uncle = gparent->rb_right;  
  357.   
  358.                 if (uncle && uncle->rb_color == RB_RED)  
  359.   
  360.                 {  
  361.   
  362.                     uncle->rb_color = RB_BLACK;  
  363.   
  364.                     parent->rb_color = RB_BLACK;  
  365.   
  366.                     gparent->rb_color = RB_RED;  
  367.   
  368.                     node = gparent;  
  369.   
  370.                     continue;  
  371.   
  372.                 }  
  373.   
  374.             }  
  375.   
  376.   
  377.   
  378.             if (parent->rb_right == node)  
  379.   
  380.             {  
  381.   
  382.                 register rb_node_t * tmp;  
  383.   
  384.                 __rb_rotate_left(parent, root);  
  385.   
  386.                 tmp = parent;  
  387.   
  388.                 parent = node;  
  389.   
  390.                 node = tmp;  
  391.   
  392.             }  
  393.   
  394.   
  395.   
  396.             parent->rb_color = RB_BLACK;  
  397.   
  398.             gparent->rb_color = RB_RED;  
  399.   
  400.             __rb_rotate_right(gparent, root);  
  401.   
  402.         } else {  
  403.   
  404.             {  
  405.   
  406.                 register rb_node_t * uncle = gparent->rb_left;  
  407.   
  408.                 if (uncle && uncle->rb_color == RB_RED)  
  409.   
  410.                 {  
  411.   
  412.                     uncle->rb_color = RB_BLACK;  
  413.   
  414.                     parent->rb_color = RB_BLACK;  
  415.   
  416.                     gparent->rb_color = RB_RED;  
  417.   
  418.                     node = gparent;  
  419.   
  420.                     continue;  
  421.   
  422.                 }  
  423.   
  424.             }  
  425.   
  426.   
  427.   
  428.             if (parent->rb_left == node)  
  429.   
  430.             {  
  431.   
  432.                 register rb_node_t * tmp;  
  433.   
  434.                 __rb_rotate_right(parent, root);  
  435.   
  436.                 tmp = parent;  
  437.   
  438.                 parent = node;  
  439.   
  440.                 node = tmp;  
  441.   
  442.             }  
  443.   
  444.   
  445.   
  446.             parent->rb_color = RB_BLACK;  
  447.   
  448.             gparent->rb_color = RB_RED;  
  449.   
  450.             __rb_rotate_left(gparent, root);  
  451.   
  452.         }  
  453.   
  454.     }  
  455.   
  456.   
  457.   
  458.     root->rb_node->rb_color = RB_BLACK;  
  459.   
  460. }  
  461.   
  462.   
  463.   
  464. static void  
  465.   
  466. __rb_erase_color(rb_node_t * node, rb_node_t * parent, rb_root_t * root)  
  467.   
  468. {  
  469.   
  470.     rb_node_t * other;  
  471.   
  472.   
  473.   
  474.     while ((!node || node->rb_color == RB_BLACK) && node != root->rb_node)  
  475.   
  476.     {  
  477.   
  478.         if (parent->rb_left == node)  
  479.   
  480.         {  
  481.   
  482.             other = parent->rb_right;  
  483.   
  484.             if (other->rb_color == RB_RED)  
  485.   
  486.             {  
  487.   
  488.                 other->rb_color = RB_BLACK;  
  489.   
  490.                 parent->rb_color = RB_RED;  
  491.   
  492.                 __rb_rotate_left(parent, root);  
  493.   
  494.                 other = parent->rb_right;  
  495.   
  496.             }  
  497.   
  498.             if ((!other->rb_left ||  
  499.   
  500.                  other->rb_left->rb_color == RB_BLACK)  
  501.   
  502.                 && (!other->rb_right ||  
  503.   
  504.                 other->rb_right->rb_color == RB_BLACK))  
  505.   
  506.             {  
  507.   
  508.                 other->rb_color = RB_RED;  
  509.   
  510.                 node = parent;  
  511.   
  512.                 parent = node->rb_parent;  
  513.   
  514.             }  
  515.   
  516.             else  
  517.   
  518.             {  
  519.   
  520.                 if (!other->rb_right ||  
  521.   
  522.                     other->rb_right->rb_color == RB_BLACK)  
  523.   
  524.                 {  
  525.   
  526.                     register rb_node_t * o_left;  
  527.   
  528.                     if ((o_left = other->rb_left))  
  529.   
  530.                         o_left->rb_color = RB_BLACK;  
  531.   
  532.                     other->rb_color = RB_RED;  
  533.   
  534.                     __rb_rotate_right(other, root);  
  535.   
  536.                     other = parent->rb_right;  
  537.   
  538.                 }  
  539.   
  540.                 other->rb_color = parent->rb_color;  
  541.   
  542.                 parent->rb_color = RB_BLACK;  
  543.   
  544.                 if (other->rb_right)  
  545.   
  546.                     other->rb_right->rb_color = RB_BLACK;  
  547.   
  548.                 __rb_rotate_left(parent, root);  
  549.   
  550.                 node = root->rb_node;  
  551.   
  552.                 break;  
  553.   
  554.             }  
  555.   
  556.         }  
  557.   
  558.         else  
  559.   
  560.         {  
  561.   
  562.             other = parent->rb_left;  
  563.   
  564.             if (other->rb_color == RB_RED)  
  565.   
  566.             {  
  567.   
  568.                 other->rb_color = RB_BLACK;  
  569.   
  570.                 parent->rb_color = RB_RED;  
  571.   
  572.                 __rb_rotate_right(parent, root);  
  573.   
  574.                 other = parent->rb_left;  
  575.   
  576.             }  
  577.   
  578.             if ((!other->rb_left ||  
  579.   
  580.                  other->rb_left->rb_color == RB_BLACK)  
  581.   
  582.                 && (!other->rb_right ||  
  583.   
  584.                 other->rb_right->rb_color == RB_BLACK))  
  585.   
  586.             {  
  587.   
  588.                 other->rb_color = RB_RED;  
  589.   
  590.                 node = parent;  
  591.   
  592.                 parent = node->rb_parent;  
  593.   
  594.             }  
  595.   
  596.             else  
  597.   
  598.             {  
  599.   
  600.                 if (!other->rb_left ||  
  601.   
  602.                     other->rb_left->rb_color == RB_BLACK)  
  603.   
  604.                 {  
  605.   
  606.                     register rb_node_t * o_right;  
  607.   
  608.                     if ((o_right = other->rb_right))  
  609.   
  610.                         o_right->rb_color = RB_BLACK;  
  611.   
  612.                     other->rb_color = RB_RED;  
  613.   
  614.                     __rb_rotate_left(other, root);  
  615.   
  616.                     other = parent->rb_left;  
  617.   
  618.                 }  
  619.   
  620.                 other->rb_color = parent->rb_color;  
  621.   
  622.                 parent->rb_color = RB_BLACK;  
  623.   
  624.                 if (other->rb_left)  
  625.   
  626.                     other->rb_left->rb_color = RB_BLACK;  
  627.   
  628.                 __rb_rotate_right(parent, root);  
  629.   
  630.                 node = root->rb_node;  
  631.   
  632.                 break;  
  633.   
  634.             }  
  635.   
  636.         }  
  637.   
  638.     }  
  639.   
  640.     if (node)  
  641.   
  642.         node->rb_color = RB_BLACK;  
  643.   
  644. }  
  645.   
  646.   
  647.   
  648. static void  
  649.   
  650. rb_erase(rb_node_t * node, rb_root_t * root)  
  651.   
  652. {  
  653.   
  654.     rb_node_t * child, * parent;  
  655.   
  656.     int color;  
  657.   
  658.   
  659.   
  660.     if (!node->rb_left)  
  661.   
  662.         child = node->rb_right;  
  663.   
  664.     else if (!node->rb_right)  
  665.   
  666.         child = node->rb_left;  
  667.   
  668.     else  
  669.   
  670.     {  
  671.   
  672.         rb_node_t * old = node, * left;  
  673.   
  674.   
  675.   
  676.         node = node->rb_right;  
  677.   
  678.         while ((left = node->rb_left))  
  679.   
  680.             node = left;  
  681.   
  682.         child = node->rb_right;  
  683.   
  684.         parent = node->rb_parent;  
  685.   
  686.         color = node->rb_color;  
  687.   
  688.   
  689.   
  690.         if (child)  
  691.   
  692.             child->rb_parent = parent;  
  693.   
  694.         if (parent)  
  695.   
  696.         {  
  697.   
  698.             if (parent->rb_left == node)  
  699.   
  700.                 parent->rb_left = child;  
  701.   
  702.             else  
  703.   
  704.                 parent->rb_right = child;  
  705.   
  706.         }  
  707.   
  708.         else  
  709.   
  710.             root->rb_node = child;  
  711.   
  712.   
  713.   
  714.         if (node->rb_parent == old)  
  715.   
  716.             parent = node;  
  717.   
  718.         node->rb_parent = old->rb_parent;  
  719.   
  720.         node->rb_color = old->rb_color;  
  721.   
  722.         node->rb_right = old->rb_right;  
  723.   
  724.         node->rb_left = old->rb_left;  
  725.   
  726.   
  727.   
  728.         if (old->rb_parent)  
  729.   
  730.         {  
  731.   
  732.             if (old->rb_parent->rb_left == old)  
  733.   
  734.                 old->rb_parent->rb_left = node;  
  735.   
  736.             else  
  737.   
  738.                 old->rb_parent->rb_right = node;  
  739.   
  740.         } else  
  741.   
  742.             root->rb_node = node;  
  743.   
  744.   
  745.   
  746.         old->rb_left->rb_parent = node;  
  747.   
  748.         if (old->rb_right)  
  749.   
  750.             old->rb_right->rb_parent = node;  
  751.   
  752.         goto color;  
  753.   
  754.     }  
  755.   
  756.   
  757.   
  758.     parent = node->rb_parent;  
  759.   
  760.     color = node->rb_color;  
  761.   
  762.   
  763.   
  764.     if (child)  
  765.   
  766.         child->rb_parent = parent;  
  767.   
  768.     if (parent)  
  769.   
  770.     {  
  771.   
  772.         if (parent->rb_left == node)  
  773.   
  774.             parent->rb_left = child;  
  775.   
  776.         else  
  777.   
  778.             parent->rb_right = child;  
  779.   
  780.     }  
  781.   
  782.     else  
  783.   
  784.         root->rb_node = child;  
  785.   
  786.   
  787.   
  788.  color:  
  789.   
  790.     if (color == RB_BLACK)  
  791.   
  792.         __rb_erase_color(child, parent, root);  
  793.   
  794. }  
  795.   
  796.   
  797.   
  798. #endif  /* _LINUX_RBTREE_H */  
  799.   
  800.   
  801.   
  802. test.c  
  803.   
  804. #include <sys/types.h>  
  805.   
  806. #include <stdio.h>  
  807.   
  808. #include <stdlib.h>  
  809.   
  810. #include <string.h>  
  811.   
  812. #include <unistd.h>  
  813.   
  814. #include <errno.h>  
  815.   
  816. #include "rbtree.h"  
  817.   
  818.   
  819.   
  820. typedef unsigned int    CORE_ADDR;  
  821.   
  822. typedef struct rb_addr_s {  
  823.   
  824.     rb_node_t   rb_node;  
  825.   
  826.     CORE_ADDR   addr;  
  827.   
  828. }rb_addr_t;  
  829.   
  830.   
  831.   
  832. static inline rb_addr_t *  
  833.   
  834. rb_find_addr_struct(CORE_ADDR addr, rb_root_t *root, rb_node_t **ret_parent, rb_node_t  
  835. ***ret_link)  
  836.   
  837. {  
  838.   
  839.     rb_node_t   **link = &root->rb_node;  
  840.   
  841.     rb_node_t   *parent = NULL;  
  842.   
  843.     rb_addr_t   *rb_addr = NULL;  
  844.   
  845.     rb_addr_t   *ret_rb_addr = NULL;  
  846.   
  847.   
  848.   
  849.     while (*link) {  
  850.   
  851.         parent = *link;  
  852.   
  853.         rb_addr = rb_entry(parent, rb_addr_t, rb_node);  
  854.   
  855.   
  856.   
  857.         if (addr < rb_addr->addr) {  
  858.   
  859.             link = &(*link)->rb_left;  
  860.   
  861.         }  
  862.   
  863.         else if (addr > rb_addr->addr) {  
  864.   
  865.             link = &(*link)->rb_right;  
  866.   
  867.         }  
  868.   
  869.         else {  
  870.   
  871.             ret_rb_addr = rb_addr;  
  872.   
  873.             break;  
  874.   
  875.         }  
  876.   
  877.     }  
  878.   
  879.     if (ret_parent) {  
  880.   
  881.         *ret_parent = parent;  
  882.   
  883.     }  
  884.   
  885.     if (ret_link) {  
  886.   
  887.         *ret_link = link;  
  888.   
  889.     }  
  890.   
  891.   
  892.   
  893.     return(ret_rb_addr);  
  894.   
  895. }  
  896.   
  897.   
  898.   
  899. int  
  900.   
  901. rb_insert_addr(CORE_ADDR addr, rb_root_t *root)  
  902.   
  903. {  
  904.   
  905.     rb_node_t   **rb_link;  
  906.   
  907.     rb_node_t   *rb_parent;  
  908.   
  909.     rb_addr_t   *rb_addr = NULL;  
  910.   
  911.   
  912.   
  913.     if (rb_find_addr_struct(addr, root, &rb_parent, &rb_link)) {  
  914.   
  915.         return(-1);  
  916.   
  917.     }  
  918.   
  919.     rb_addr = (rb_addr_t *)malloc(sizeof(rb_addr_t));  
  920.   
  921.     if (!rb_addr) {  
  922.   
  923.         return(-1);  
  924.   
  925.     }  
  926.   
  927.     rb_addr->addr = addr;  
  928.   
  929.     rb_link_node(&rb_addr->rb_node, rb_parent, rb_link);  
  930.   
  931.     rb_insert_color(&rb_addr->rb_node, root);  
  932.   
  933.   
  934.   
  935.     return(0);  
  936.   
  937. }  
  938.   
  939.   
  940.   
  941. int  
  942.   
  943. rb_remove_addr(CORE_ADDR addr, rb_root_t *root)  
  944.   
  945. {  
  946.   
  947.     rb_addr_t   *rb_addr = rb_find_addr_struct(addr, root, NULL, NULL);  
  948.   
  949.   
  950.   
  951.     if (!rb_addr) {  
  952.   
  953.         return(-1);  
  954.   
  955.     }  
  956.   
  957.     rb_erase(&rb_addr->rb_node, root);  
  958.   
  959.     free(rb_addr);  
  960.   
  961.   
  962.   
  963.     return(0);  
  964.   
  965. }  
  966.   
  967.   
  968.   
  969. static inline int  
  970.   
  971. rb_find_addr(CORE_ADDR addr, rb_root_t *root)  
  972.   
  973. {  
  974.   
  975.     rb_node_t   **link = &root->rb_node;  
  976.   
  977.     rb_node_t   *parent = NULL;  
  978.   
  979.     rb_addr_t   *rb_addr;  
  980.   
  981.   
  982.   
  983.     while (*link) {  
  984.   
  985.         parent = *link;  
  986.   
  987.         rb_addr = rb_entry(parent, rb_addr_t, rb_node);  
  988.   
  989.   
  990.   
  991.         if (addr < rb_addr->addr) {  
  992.   
  993.             link = &(*link)->rb_left;  
  994.   
  995.         }  
  996.   
  997.         else if (addr > rb_addr->addr) {  
  998.   
  999.             link = &(*link)->rb_right;  
  1000.   
  1001.         }  
  1002.   
  1003.         else {  
  1004.   
  1005.             return(1);  
  1006.   
  1007.         }  
  1008.   
  1009.     }  
  1010.   
  1011.   
  1012.   
  1013.     return(0);  
  1014.   
  1015. }  
  1016.   
  1017.   
  1018.   
  1019. int  
  1020.   
  1021. main(int argc,char *argv[], char *envp[])  
  1022.   
  1023. {  
  1024.   
  1025.     rb_root_t   addr_root;  
  1026.   
  1027.   
  1028.   
  1029.     addr_root = RB_ROOT;  
  1030.   
  1031.   
  1032.   
  1033.     //right insert  
  1034.   
  1035.     printf("right insert/n");  
  1036.   
  1037.     if (rb_insert_addr(0x1, &addr_root)) {  
  1038.   
  1039.         printf("rb_insert_addr error/n");  
  1040.   
  1041.     }  
  1042.   
  1043.     if (rb_insert_addr(0xc0028018, &addr_root)) {  
  1044.   
  1045.         printf("rb_insert_addr error/n");  
  1046.   
  1047.     }  
  1048.   
  1049.     if (rb_insert_addr(0xd014cb68, &addr_root)) {  
  1050.   
  1051.         printf("rb_insert_addr error/n");  
  1052.   
  1053.     }  
  1054.   
  1055.     if (rb_insert_addr(0x74697277, &addr_root)) {  
  1056.   
  1057.         printf("rb_insert_addr error/n");  
  1058.   
  1059.     }  
  1060.   
  1061.     if (rb_insert_addr(0xc00959f8, &addr_root)) {  
  1062.   
  1063.         printf("rb_insert_addr error/n");  
  1064.   
  1065.     }  
  1066.   
  1067.     if (rb_insert_addr(0x959f8, &addr_root)) {  
  1068.   
  1069.         printf("rb_insert_addr error/n");  
  1070.   
  1071.     }  
  1072.   
  1073.     if (rb_insert_addr(0xe3740ffa, &addr_root)) {  
  1074.   
  1075.         printf("rb_insert_addr error/n");  
  1076.   
  1077.     }  
  1078.   
  1079.     if (rb_insert_addr(0x31321, &addr_root)) {  
  1080.   
  1081.         printf("rb_insert_addr error/n");  
  1082.   
  1083.     }  
  1084.   
  1085.     if (rb_insert_addr(0x765702, &addr_root)) {  
  1086.   
  1087.         printf("rb_insert_addr error/n");  
  1088.   
  1089.     }  
  1090.   
  1091.     if (rb_insert_addr(0xf123, &addr_root)) {  
  1092.   
  1093.         printf("rb_insert_addr error/n");  
  1094.   
  1095.     }  
  1096.   
  1097.   
  1098.   
  1099.     //wrong insert  
  1100.   
  1101.     printf("wrong insert/n");  
  1102.   
  1103.     if (rb_insert_addr(0x1, &addr_root)) {  
  1104.   
  1105.         printf("rb_insert_addr error/n");  
  1106.   
  1107.     }  
  1108.   
  1109.     if (rb_insert_addr(0xc0028018, &addr_root)) {  
  1110.   
  1111.         printf("rb_insert_addr error/n");  
  1112.   
  1113.     }  
  1114.   
  1115.     if (rb_insert_addr(0xd014cb68, &addr_root)) {  
  1116.   
  1117.         printf("rb_insert_addr error/n");  
  1118.   
  1119.     }  
  1120.   
  1121.     if (rb_insert_addr(0x74697277, &addr_root)) {  
  1122.   
  1123.         printf("rb_insert_addr error/n");  
  1124.   
  1125.     }  
  1126.   
  1127.     if (rb_insert_addr(0xc00959f8, &addr_root)) {  
  1128.   
  1129.         printf("rb_insert_addr error/n");  
  1130.   
  1131.     }  
  1132.   
  1133.     if (rb_insert_addr(0x959f8, &addr_root)) {  
  1134.   
  1135.         printf("rb_insert_addr error/n");  
  1136.   
  1137.     }  
  1138.   
  1139.     if (rb_insert_addr(0xe3740ffa, &addr_root)) {  
  1140.   
  1141.         printf("rb_insert_addr error/n");  
  1142.   
  1143.     }  
  1144.   
  1145.     if (rb_insert_addr(0x31321, &addr_root)) {  
  1146.   
  1147.         printf("rb_insert_addr error/n");  
  1148.   
  1149.     }  
  1150.   
  1151.     if (rb_insert_addr(0x765702, &addr_root)) {  
  1152.   
  1153.         printf("rb_insert_addr error/n");  
  1154.   
  1155.     }  
  1156.   
  1157.     if (rb_insert_addr(0xf123, &addr_root)) {  
  1158.   
  1159.         printf("rb_insert_addr error/n");  
  1160.   
  1161.     }  
  1162.   
  1163.   
  1164.   
  1165.     //right find  
  1166.   
  1167.     printf("right find/n");  
  1168.   
  1169.     if (rb_find_addr(0x1, &addr_root)) {  
  1170.   
  1171.         printf("rb_find_addr ok/n");  
  1172.   
  1173.     }  
  1174.   
  1175.     if (rb_find_addr(0xc0028018, &addr_root)) {  
  1176.   
  1177.         printf("rb_find_addr ok/n");  
  1178.   
  1179.     }  
  1180.   
  1181.     if (rb_find_addr(0xd014cb68, &addr_root)) {  
  1182.   
  1183.         printf("rb_find_addr ok/n");  
  1184.   
  1185.     }  
  1186.   
  1187.     if (rb_find_addr(0x74697277, &addr_root)) {  
  1188.   
  1189.         printf("rb_find_addr ok/n");  
  1190.   
  1191.     }  
  1192.   
  1193.     if (rb_find_addr(0xc00959f8, &addr_root)) {  
  1194.   
  1195.         printf("rb_find_addr ok/n");  
  1196.   
  1197.     }  
  1198.   
  1199.     if (rb_find_addr(0x959f8, &addr_root)) {  
  1200.   
  1201.         printf("rb_find_addr ok/n");  
  1202.   
  1203.     }  
  1204.   
  1205.     if (rb_find_addr(0xe3740ffa, &addr_root)) {  
  1206.   
  1207.         printf("rb_find_addr ok/n");  
  1208.   
  1209.     }  
  1210.   
  1211.     if (rb_find_addr(0x31321, &addr_root)) {  
  1212.   
  1213.         printf("rb_find_addr ok/n");  
  1214.   
  1215.     }  
  1216.   
  1217.     if (rb_find_addr(0x765702, &addr_root)) {  
  1218.   
  1219.         printf("rb_find_addr ok/n");  
  1220.   
  1221.     }  
  1222.   
  1223.     if (rb_find_addr(0xf123, &addr_root)) {  
  1224.   
  1225.         printf("rb_find_addr ok/n");  
  1226.   
  1227.     }  
  1228.   
  1229.   
  1230.   
  1231.     //wrong find  
  1232.   
  1233.     printf("wrong find/n");  
  1234.   
  1235.     if (rb_find_addr(0x0, &addr_root)) {  
  1236.   
  1237.         printf("rb_find_addr error/n");  
  1238.   
  1239.     }  
  1240.   
  1241.     if (rb_find_addr(0xc002801, &addr_root)) {  
  1242.   
  1243.         printf("rb_find_addr error/n");  
  1244.   
  1245.     }  
  1246.   
  1247.     if (rb_find_addr(0xd014cb6, &addr_root)) {  
  1248.   
  1249.         printf("rb_find_addr error/n");  
  1250.   
  1251.     }  
  1252.   
  1253.     if (rb_find_addr(0x7469727, &addr_root)) {  
  1254.   
  1255.         printf("rb_find_addr error/n");  
  1256.   
  1257.     }  
  1258.   
  1259.     if (rb_find_addr(0xc00959f, &addr_root)) {  
  1260.   
  1261.         printf("rb_find_addr error/n");  
  1262.   
  1263.     }  
  1264.   
  1265.     if (rb_find_addr(0x959f, &addr_root)) {  
  1266.   
  1267.         printf("rb_find_addr error/n");  
  1268.   
  1269.     }  
  1270.   
  1271.     if (rb_find_addr(0xe3740ff, &addr_root)) {  
  1272.   
  1273.         printf("rb_find_addr error/n");  
  1274.   
  1275.     }  
  1276.   
  1277.     if (rb_find_addr(0x3132, &addr_root)) {  
  1278.   
  1279.         printf("rb_find_addr error/n");  
  1280.   
  1281.     }  
  1282.   
  1283.     if (rb_find_addr(0x76570, &addr_root)) {  
  1284.   
  1285.         printf("rb_find_addr error/n");  
  1286.   
  1287.     }  
  1288.   
  1289.     if (rb_find_addr(0xf12, &addr_root)) {  
  1290.   
  1291.         printf("rb_find_addr error/n");  
  1292.   
  1293.     }  
  1294.   
  1295.   
  1296.   
  1297.     //right remove  
  1298.   
  1299.     printf("right remove/n");  
  1300.   
  1301.     if (rb_remove_addr(0x1, &addr_root)) {  
  1302.   
  1303.         printf("rb_remove_addr error/n");  
  1304.   
  1305.     }  
  1306.   
  1307.     if (rb_remove_addr(0xc0028018, &addr_root)) {  
  1308.   
  1309.         printf("rb_remove_addr error/n");  
  1310.   
  1311.     }  
  1312.   
  1313.     if (rb_remove_addr(0xd014cb68, &addr_root)) {  
  1314.   
  1315.         printf("rb_remove_addr error/n");  
  1316.   
  1317.     }  
  1318.   
  1319.     if (rb_remove_addr(0x74697277, &addr_root)) {  
  1320.   
  1321.         printf("rb_remove_addr error/n");  
  1322.   
  1323.     }  
  1324.   
  1325.     if (rb_remove_addr(0xc00959f8, &addr_root)) {  
  1326.   
  1327.         printf("rb_remove_addr error/n");  
  1328.   
  1329.     }  
  1330.   
  1331.     if (rb_remove_addr(0x959f8, &addr_root)) {  
  1332.   
  1333.         printf("rb_remove_addr error/n");  
  1334.   
  1335.     }  
  1336.   
  1337.     if (rb_remove_addr(0xe3740ffa, &addr_root)) {  
  1338.   
  1339.         printf("rb_remove_addr error/n");  
  1340.   
  1341.     }  
  1342.   
  1343.     if (rb_remove_addr(0x31321, &addr_root)) {  
  1344.   
  1345.         printf("rb_remove_addr error/n");  
  1346.   
  1347.     }  
  1348.   
  1349.     if (rb_remove_addr(0x765702, &addr_root)) {  
  1350.   
  1351.         printf("rb_remove_addr error/n");  
  1352.   
  1353.     }  
  1354.   
  1355.     if (rb_remove_addr(0xf123, &addr_root)) {  
  1356.   
  1357.         printf("rb_remove_addr error/n");  
  1358.   
  1359.     }  
  1360.   
  1361.   
  1362.   
  1363.     //wrong remove  
  1364.   
  1365.     printf("wrong remove/n");  
  1366.   
  1367.     if (rb_remove_addr(0x1, &addr_root)) {  
  1368.   
  1369.         printf("rb_remove_addr error/n");  
  1370.   
  1371.     }  
  1372.   
  1373.     if (rb_remove_addr(0xc0028018, &addr_root)) {  
  1374.   
  1375.         printf("rb_remove_addr error/n");  
  1376.   
  1377.     }  
  1378.   
  1379.     if (rb_remove_addr(0xd014cb68, &addr_root)) {  
  1380.   
  1381.         printf("rb_remove_addr error/n");  
  1382.   
  1383.     }  
  1384.   
  1385.     if (rb_remove_addr(0x74697277, &addr_root)) {  
  1386.   
  1387.         printf("rb_remove_addr error/n");  
  1388.   
  1389.     }  
  1390.   
  1391.     if (rb_remove_addr(0xc00959f8, &addr_root)) {  
  1392.   
  1393.         printf("rb_remove_addr error/n");  
  1394.   
  1395.     }  
  1396.   
  1397.     if (rb_remove_addr(0x959f8, &addr_root)) {  
  1398.   
  1399.         printf("rb_remove_addr error/n");  
  1400.   
  1401.     }  
  1402.   
  1403.     if (rb_remove_addr(0xe3740ffa, &addr_root)) {  
  1404.   
  1405.         printf("rb_remove_addr error/n");  
  1406.   
  1407.     }  
  1408.   
  1409.     if (rb_remove_addr(0x31321, &addr_root)) {  
  1410.   
  1411.         printf("rb_remove_addr error/n");  
  1412.   
  1413.     }  
  1414.   
  1415.     if (rb_remove_addr(0x765702, &addr_root)) {  
  1416.   
  1417.         printf("rb_remove_addr error/n");  
  1418.   
  1419.     }  
  1420.   
  1421.     if (rb_remove_addr(0xf123, &addr_root)) {  
  1422.   
  1423.         printf("rb_remove_addr error/n");  
  1424.   
  1425.     }  
  1426.   
  1427.   
  1428.   
  1429.     return(0);  
  1430.   
  1431. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值