-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathYRedBlackTree.cpp
More file actions
163 lines (145 loc) · 3.75 KB
/
YRedBlackTree.cpp
File metadata and controls
163 lines (145 loc) · 3.75 KB
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
# include "stdafx.h"
# include "YRedBlackTree.h"
bool YRedBlack::Insert( POINT X, YRedBlackNode * & T )
{
if( T == NULL ) // Create a one node tree.
{
T = new YRedBlackNode( X );
assert(T!=NULL);// "Out of space"
return true;
}
//Going Into Recursion
if( T->Left!=NULL && T->Right!=NULL)
if (T->Left->branchColor == red && T->Right->branchColor == red)
{ T->branchColor = red;
T->Left->branchColor = black;
T->Right->branchColor = black;
}
if( X.y < T->Element.y )
{
if (Insert( X, T->Left ))
{
if( TooRed( T ) == Left )
{ Srotateright( T );
//cout<<"Single Rotation Right"<<endl;
}
else if( TooRed( T ) == LeftRight )
{ Drotateright( T );
//cout<<"Double Rotation Right"<<endl;
}
return true;
}
return false;
}
if( X.y > T->Element.y )
{
if(Insert( X, T->Right ))
{
if( TooRed( T ) == Right )
{ Srotateleft( T );
//cout<<"Single Rotation Left"<<endl;
}
else if( TooRed( T ) == RightLeft )
{ Drotateleft( T );
//cout<<"Double Rotation Left"<<endl;
}
return true;
}
}
// Else Y is in the tree already, so we'll do nothing.
if( X.y == T->Element.y )
{ T->useCount++;
return true;
}
return false;
}
//Returns the side that is heavy in Red
RBDirection YRedBlack::TooRed( YRedBlackNode * T)
{
if( T->Left !=NULL && T->Left->branchColor == red)
{
if( T->Left->Left != NULL && T->Left->Left->branchColor == red)
return Left;
if( T->Left->Right != NULL && T->Left->Right->branchColor == red)
return LeftRight;
}
if( T->Right != NULL && T->Right->branchColor == red)
{
if( T->Right->Right != NULL && T->Right->Right->branchColor == red)
return Right;
if( T->Right->Left != NULL && T->Right->Left->branchColor == red)
return RightLeft;
}
return NoDirection;
}
// Perform a left rotation at the tree rooted at k2
void YRedBlack::Srotateleft( YRedBlackNode * & k2 )
{ assert(k2!= NULL);
//cout << "Srotateleft " << k2->Element << endl;
YRedBlackNode *k1 = k2->Right;
assert(k1 != NULL);
k2->Right = k1->Left;
k1->Left = k2;
TreeNodeColor temp=k2->branchColor;
k2->branchColor=k1->branchColor;
k1->branchColor=temp;
k2 = k1;
}
void YRedBlack::Srotateright( YRedBlackNode * & k2 )
{ assert(k2!=NULL);
//cout << "Srotateright " << k2->Element << endl;
YRedBlackNode *k1 = k2->Left;
assert(k1 != NULL);
k2->Left = k1->Right;
k1->Right = k2;
TreeNodeColor temp=k2->branchColor;
k2->branchColor=k1->branchColor;
k1->branchColor=temp;
k2 = k1;
}
void YRedBlack::Drotateleft( YRedBlackNode * & k3 )
{
Srotateright ( ( YRedBlackNode * & ) k3->Right);
Srotateleft ( k3 );
}
// Perform a double right rotation at the node rooted at k3
void YRedBlack::Drotateright( YRedBlackNode * & k3 )
{
Srotateleft( ( YRedBlackNode * & ) k3->Left );
Srotateright( k3 );
}
/*
void YRedBlack::PrintTree( YRedBlackNode * T,int indent )
{
if (T==NULL) return;
PrintTree (T->Right,indent+1);
cout << setw(indent*3) << " ";
cout << T->Element.y << "(";
if(T->branchColor == red)
cout<<"Red)"<< endl;
else
cout<<"Black)"<<endl;
PrintTree (T->Left,indent+1);
}
*/
YRedBlackNode * YRedBlack::GetNode( POINT P, YRedBlackNode * T )
{
if( T == NULL )
return NULL;
else if( T->Element.y > P.y )
return GetNode( P, T->Left );
else if( T->Element.y < P.y )
return GetNode( P, T->Right );
else if( T->Element.y == P.y )
return T;
return NULL;
}
void YRedBlack::Delete( YRedBlackNode *& T )
{
if( T == NULL )
return;
Delete( T->Left );
Delete( T->Right );
delete T;
T = NULL;
}