一般情況下還沒有發現bug.
#include <stdio.h>
#include <stdlib.h>
typedef enum _color
{
RED,
BLACK
}color;
typedef struct _data
{
int value;
}data;
typedef struct _redblack
{
color m_color;
data *m_data;
struct _redblack *parent;
struct _redblack *left;
struct _redblack *right;
}redblack;
typedef struct _wrapdata
{
redblack *root;
int size;
}wrapdata;
int compare ( redblack *left ,redblack *right)
{
if(left->m_data->value > right->m_data->value)
{
return 1;
}
else
{
return 0;
}
}
redblack * newstruct ( int value)
{
redblack *newnode;
newnode=(redblack *)calloc (1 ,sizeof (redblack));
newnode->m_color=RED;
newnode->m_data =(data *)calloc ( 1 ,sizeof (data ) );
newnode->m_data->value=value;
return newnode;
}
void rotate_right ( redblack * left ,redblack *right )
{
right->left=left->right;
if(left->right)
{
left->right->parent=right;
}
left->right=right;
left->parent=right->parent;
if(right->parent)
{
if(right->parent->left==right)
{
right->parent->left=left;
}
else
{
right->parent->right=left;
}
}
right->parent=left;
}
void rotate_left ( redblack * left ,redblack *right )
{
left->right=right->left;
if (right->left)
{
right->left->parent=left;
}
right->left=left;
right->parent=left->parent;
if(left->parent)
{
if(left->parent->right==left)
{
left->parent->right=right;
}
else
{
left->parent->left=right;
}
}
left->parent=right;
}
void mid_print (redblack *root,int level)
{
if (!root)
{
return;
}
else
{
mid_print (root ->left,level+1);
printf("{color=%s,level=%d ,value=%d} ",root->m_color==RED?"red":"black",level ,root->m_data->value);
// printf(" %d ",root->m_data->value);
mid_print (root ->right,level+1);
}
}
void left_print (redblack *root)
{
if (!root)
{
return;
}
else
{
printf("%d ",root->m_data->value);
left_print (root ->left);
left_print (root ->right);
}
}
void right_print (redblack *root)
{
if (!root)
{
return;
}
else
{
right_print (root ->left);
right_print (root ->right);
printf("%d ",root->m_data->value);
}
}
int depth(redblack *root)
{
int left,right;
if (!root)
{
return 0;
}
else
{
left=depth(root->left);
right=depth(root->right);
return left>right ? ( left+1):(right+1);
}
}
void insert_fixup (wrapdata *node , redblack *newnode)
{
redblack *curnode;
redblack *parent;
redblack *grandparent;
redblack *tmp;
curnode=newnode;
parent=curnode->parent;
while( curnode->m_color==RED && parent ->m_color==RED)
{
grandparent=parent->parent;
if ( curnode == parent->left)
{
if ( parent == grandparent->left)
{
curnode->m_color=BLACK;
rotate_right ( parent , grandparent);
curnode=parent;
parent=curnode->parent;
if(!parent )
{
node->root=curnode;
break;
}
}
else
{
// printf("nothing");
rotate_right (curnode, parent );
tmp=parent;
parent=curnode;
curnode=tmp;
curnode->m_color=BLACK;
rotate_left (grandparent ,parent );
curnode=parent;
parent=curnode->parent;
if(!parent )
{
node->root=curnode;
break;
}
}
}
else
{
if ( parent== grandparent->right)
{
curnode->m_color=BLACK;
rotate_left (grandparent,parent );
curnode=parent;
parent=curnode->parent;
if(!parent )
{
node->root=curnode;
break;
}
}
else
{
// printf("nothing");
rotate_left ( parent ,curnode);
tmp=parent;
parent=curnode;
curnode=tmp;
curnode->m_color=BLACK;
rotate_right (parent,grandparent );
curnode=parent;
parent=curnode->parent;
if(!parent )
{
node->root=curnode;
break;
}
}
}
}
}
redblack * find(redblack *root ,int data)
{
if (! root)
{
return NULL;
}
else
{
if ( data == root->m_data->value)
{
return root;
}
else if ( data > root->m_data->value )
{
return find (root ->right ,data);
}
else
{
return find (root->left ,data );
}
}
}
redblack * next (redblack * mostleft)
{
// if
if(! mostleft->left)
{
return mostleft;
}
else
{
return next ( mostleft->left);
}
}
void delete_fixup (wrapdata *node, redblack *curnode ,int mode)
{
redblack *parent;
redblack *uncle;
redblack *uncle_left;
redblack *uncle_right;
while(1)
{
if (curnode->m_color==RED)
{
curnode->m_color=BLACK;
parent=curnode->parent;
if(!parent)
{
node->root=curnode;
}
return ;
}
else www.2cto.com
{
parent=curnode->parent;
if(!parent)
{
node->root=curnode;
return ;
}
if(0==mode)
{
uncle=parent->right;
if(!uncle)
{
return ;
}
if (uncle->m_color== RED)
{
uncle->m_color=BLACK;
parent->m_color=RED;
rotate_left (parent,uncle );
}
else
{
uncle_left=uncle->left;
uncle_right=uncle->right;
if( (!uncle_left || uncle_left->m_color==BLACK )
&&
(!uncle_right || uncle_right->m_color==BLACK))
{
uncle->m_color=RED;
curnode=parent;
}
else
{
if( !uncle_right || uncle_right->m_color==RED)
{
uncle->m_color=parent->m_color;
parent->m_color=BLACK;
uncle_right->m_color=BLACK;
rotate_left( parent,uncle);
curnode=uncle;
}
}
}
}
else
{
uncle=parent->left;
if(!uncle)
{
return ;
}
if (uncle->m_color== RED)
{
uncle->m_color=BLACK;
parent->m_color=RED;
rotate_right (uncle ,parent);
}
else
{
uncle_left=uncle->left;
uncle_right=uncle->right;
if( (!uncle_left || uncle_left->m_color==BLACK )
&&
(!uncle_right || uncle_right->m_color==BLACK))
{
uncle->m_color=RED;
curnode=parent;
}
else
{
if( !uncle_left || uncle_left->m_color==RED)
{
uncle->m_color=parent->m_color;
parent->m_color=BLACK;
uncle_left->m_color=BLACK;
rotate_right( uncle , parent);
curnode=uncle;
}
}
}
}
}
}
}
void delete (wrapdata *node ,int data)
{
redblack * drop;
redblack *suc;
redblack *curnode;
int value;
int mode;
printf("will delete %d \n",data);
if ( drop=find ( node->root ,data))
{
if(!drop->left && !drop->right )
{
if (!drop->parent)
{
free(drop);
}
else
{
if (drop->parent->left==drop)
{
drop->parent->left=0;
}
else
{
drop->parent->right=0;
}
free (drop);
}
}
else if (!drop->right )
{
if(!drop ->parent)
{
}
else
{
if (drop->parent->left==drop)
{
drop->parent->left=drop->left;
mode=0;
}
else
{
drop->parent->right=drop->left;
mode=1;
}
}
curnode=drop->left;
curnode->parent=drop->parent;
free (drop);
delete_fixup (node, curnode,mode);
}
else
{
suc=next ( drop->right);
value=drop->m_data->value;
drop->m_data->value=suc->m_data->value;
suc->m_data->value=value;
if(suc->parent->left==suc)
{
mode=0;
}
else
{
mode=1;
}
if(!suc->right)
{
if(0==mode)
{
suc->parent->left=0;
}
else
{
suc->parent->right=0;
}
free(suc);
}
else
{
if(0==mode)
{
suc->parent->left=suc->right;
}
else
{
suc->parent->right=suc->right;
}
curnode=suc->right;
curnode->parent=suc->parent;
free(suc);
delete_fixup (node ,curnode,mode);
}
}
}
else
{
}
node->root->m_color=BLACK;
}
void insert ( wrapdata *node , int data )
{
redblack *newnode;
redblack *curnode;
printf("will insert %d \n",data);
newnode=newstruct (data);
node->size++;
if ( ! node->root)
{
node->root= newnode;
}
else
{
curnode=node->root;
while(1)
{
if ( compare ( newnode ,curnode))
{
if (!curnode->right)
{
curnode->right=newnode;
newnode->parent=curnode;
break;
}
else
{
curnode=curnode->right;
}
}
else
{
if (!curnode->left)
{
curnode->left=newnode;
newnode->parent=curnode;
break;
}
else
{
curnode=curnode->left;
}
}
}
insert_fixup ( node , newnode);
}
node->root->m_color=BLACK;
}
int main()
{
int i;
wrapdata *node;
data *newdata;
/*
int value[]={12,24,25,9,4,91,2,100,29,888,10,22,5,11,111,7,13,1,99,222,98,17,8,55,44,33,21,77,199,2345,46,49,51,53,54,52,50,48,47,45,43,42,41,40,39,38,37,78,79,80,81,82,83,84,85};
*/
int value[]={12,24,25,9,4,91,2,100,98,95,93,96,99,120,13};
// int value[]={12,24,25,9,4,91,2};
node=(wrapdata*)calloc (1 ,sizeof (wrapdata));
for (i=0;i<sizeof (value)/sizeof(int );i++)
{
insert (node ,value[i]);
}
printf("%d \n",depth (node->root));
mid_print (node->root,0);
printf("\n");
while(1)
{
printf("into delete mode\n");
while( scanf("%d",&i) && i)
{
delete (node ,i);
printf("%d \n",depth (node->root));
mid_print (node->root,0);
printf("\n");
}
printf("into insert mode\n");
while( scanf("%d",&i) && i)
{
insert (node ,i);
printf("%d \n",depth (node->root));
mid_print (node->root,0);
printf("\n");
}
}
/*
left_print (node->root);
printf("\n");
right_print (node->root);
printf("\n");
*/
return 0;
}
摘自chenbingchenbing的專欄