Pay attention to the data sequence in the main function.
# 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 (data * newdata)
{
Redblack * newnode;
Newnode = (redblack *) calloc (1, sizeof (redblack ));
Newnode-> m_color = RED;
Newnode-> m_data = newdata;
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)
{
If (! Root)
{
Return;
}
Else
{
Mid_print (root-> left );
Printf ("% d", root-> m_data-> value );
Mid_print (root-> right );
}
}
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;
}
}
}
}
}
Void insert (wrapdata * node, data * newdata)
{
Redblack * newnode;
Redblack * curnode;
Newnode = newstruct (newdata );
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 [] = {2,100, 29,888, 99,222, 54,52, 50,48, 47,45, 43,42, 41,40, 39,38, 37,78, 79,80, 81,82, 83,84, 85 };
Node = (wrapdata *) calloc (1, sizeof (wrapdata ));
For (I = 0; I <sizeof (value)/sizeof (int); I ++)
{
Newdata = (data *) calloc (1, sizeof (data ));
Newdata-> value = value [I];
Insert (node, newdata );
} Www.2cto.com
Printf ("% d \ n", depth (node-> root ));
Mid_print (node-> root );
Printf ("\ n ");
Left_print (node-> root );
Printf ("\ n ");
Right_print (node-> root );
Printf ("\ n ");
Return 0;
}
From chenbingchenbing's column