Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在红黑树上插入一个共同的哨兵_C_Algorithm - Fatal编程技术网

C 在红黑树上插入一个共同的哨兵

C 在红黑树上插入一个共同的哨兵,c,algorithm,C,Algorithm,我在c中实现了两个函数: Client*insertABR(Client*sentinelle,int numeroTel,int prixAppel):这允许我在插入二叉搜索树时插入一个红黑树,而不需要考虑颜色。(它工作得很好) Client*insert(Client*sentinelle、int numeroTel、int prixAppel):此函数允许我修复以前的插入 我的客户结构如下: typedef enum { RED=0, BLACK=1 } Color;

我在c中实现了两个函数:

  • Client*insertABR(Client*sentinelle,int numeroTel,int prixAppel):这允许我在插入二叉搜索树时插入一个红黑树,而不需要考虑颜色。(它工作得很好)
  • Client*insert(Client*sentinelle、int numeroTel、int prixAppel):此函数允许我修复以前的插入
  • 我的客户结构如下:

         typedef enum {  RED=0, BLACK=1 } Color; 
         struct sClient
        {
        int num_tel;
        int nbr_appel;
        double cout;
        struct sClient *fg; // left
        struct sClient *fd; //right 
        Color col;
        struct sClient *pere; //parent
        };
        typedef struct sClient Client;
    
    这棵红黑树的特点是它有一个共同的哨兵,如图所示: 树根(Racine)是sentinal的左儿子 菲尔斯·高切=左儿子,菲尔斯·德罗伊特=右儿子

        Client* insert(Client* sentinelle, int numeroTel, int prixAppel){
        Client *s,*c,*y;
        s=sentinelle;
        if(sentinelle == NULL){
                       sentinelle= createNode(0,0,0,1);
                       c= createNode(numeroTel, 1, prixAppel,1);
                       sentinelle->fg=c;
                       c->pere=sentinelle; 
                       c->fg=sentinelle;
                       c->fd=sentinelle;
                       return sentinelle;
                       }
        else{  
    
    
    
            c=insertABR(sentinelle, numeroTel, prixAppel); 
    
             while(((c->pere != s) && (c->pere->col==0))  ){
    
               if(grand_pere(c)->fg == c->pere){
                            //grand_pere= grand_father      
                            y= grand_pere(c)->fd;
    
    
                            if(y->col ==0){
    
                            c->pere->col =1;      
                            y->col =1;
                            grand_pere(c)->col =0;
                            c=grand_pere(c); 
                            }
                            else{
                                 if(c==c->pere->fd) {
                                                    c=c->pere; 
                                                    left_rotate(c); 
                                                    }
                                  c->pere->col =1;
                                  grand_pere(c)->col= 0;
                                  right_rotate(grand_pere(c));             
                                 }
                            }
    
    
    
                            else{
                              y=grand_pere(c)->fg; 
                                 if(y->col ==0){
                                   c->pere->col =1;
                                   y->col =1;
                                   grand_pere(c)->col =0;
                                   c=grand_pere(c); 
                            } 
                            else{
                                 if(c==c->pere->fg) {
                                                    c=c->pere; 
                                                    right_rotate(c); 
                                                    }
                                  c->pere->col =1;
                                  grand_pere(c)->col= 0;
                                  left_rotate(grand_pere(c));                   
                                 }                   
    
                                 }
    
    
        } 
        sentinelle->fg->col=1;
    
        return sentinelle;
    
        }
        }
    
    我的问题是,当我尝试插入8,18,10,19,29,15时,我得到了以下结果:

    根10(黑色)是8(黑色)和19(红色)的父代。 19是29(黑人)和18(黑人)的父母。 18是15的父母(红色)。 颜色很好,但树不再平衡,就像错过了旋转,但我不知道在哪里添加它。


    您的问题就在这里(在相应的情况下,当父母是正确的孩子时):

    它需要:

    if(c == c->pere->fd)
                            left_rotate(c);
                        else
                            c = c->pere;
    
                        c->col = 1;
                        c->pere->col = 0;
                        right_rotate(c)->pere;
    

    现在,在相反方向的情况下更改c点的位置,当这两个动作需要向后执行时,如果c点相同(即左子项的左子项),则不使用c点。

    我仍然有相同的结果此代码适用于右子项(fd)当我试图更新左边的子部分时,这个过程停止了。我想我在旋转过程中遇到了问题
    if(c == c->pere->fd)
                            left_rotate(c);
                        else
                            c = c->pere;
    
                        c->col = 1;
                        c->pere->col = 0;
                        right_rotate(c)->pere;