-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.c
168 lines (136 loc) · 3.83 KB
/
3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
struct node{
int key ;
struct node *left;
struct node *right;
char color ;
};
struct rbtree{
struct node * root ;
};
struct node* create_node (int key){
struct node *p = (struct node *)malloc(sizeof(struct node));
p->key = key ;
p->left = NULL;
p->right = NULL ;
p->color = 'R';
return p ;
}
struct node * ll( struct node * x )
{
struct node * y = x->right;
x->right = y->left;
y->left = x;
return y;
}
struct node * rr ( struct node * x)
{
struct node * y = x->left;
x->left = y->right;
y->right = x;
return y;
}
char color(struct node *x)
{
if ( x == NULL )
return 'B';
return x->color ;
}
struct node * rbtreeinsert( struct node *r , struct node * x )
{
if (r == NULL )
return x ;
else if (r->key < x->key)
{
r->right = rbtreeinsert(r->right , x);
if (color(r->right)== 'B')
return r ;
else
{
if (color(r->right->left) == 'R' || color(r->right->right) == 'R')
{
if(color(r->left )== 'R') // Case1 : Uncle is red - simply recolor
{
r->right->color = 'B';
r->left->color = 'B';
r->color = 'R';
return r ;
}
else // Case2 and Case3 -- Uncle is black
{
//So, let's convert Case3 into Case2
if(color(r->right->left )== 'R')
r->right = rr(r->right);
struct node * y = ll(r);
y->left->color = 'R' ;
y->color = 'B' ;
return y ;
}
}
return r ;
}
}
else
{
r->left = rbtreeinsert(r->left, x);
if (color(r->left) == 'B')
return r ;
else
{
if (color(r->left->left)== 'R' || color(r->left->right) == 'R')
{
if (color(r->right) == 'R') // Case1 : Uncle is Red , simply recolor
{
r->right->color = 'B';
r->left->color = 'B';
r->color = 'R';
return r ;
}
else //Case2 and case3 :: Uncle is Black
{
if (color(r->left->right) == 'R')
r->left = ll(r->left);
struct node *y = rr(r);
y->right->color = 'R';
y->color = 'B';
return y ;
}
}
return r ;
}
}
}
struct node * Insert(struct node * r , struct node * x)
{
r = rbtreeinsert(r,x);
if(r->color == 'R')
r->color = 'B';
return r ;
}
void Print_Tree (struct node * root)
{
printf("( ");
if (root != NULL)
{
printf("%d %c ",root->key ,root->color);
Print_Tree(root->left);
Print_Tree(root->right);
}
printf(") ");
}
int main()
{
int k ;
struct rbtree * t = (struct rbtree*)malloc(sizeof(struct rbtree));
t->root = (struct node *)malloc(sizeof(struct node)) ;
t->root = NULL ;
while (scanf("%d",&k) == 1)
{
struct node *a = create_node(k);
t->root = Insert(t->root , a );
Print_Tree(t->root);
printf("\n");
}
return 0 ;
}