-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBST.cpp
197 lines (167 loc) · 5.18 KB
/
RBST.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "RBST.hpp"
/***********************************************************/
/******************* PROVIDED FUNCTIONS ********************/
/***********************************************************/
int RBST::add(const Key& key, bool verbose) {
unsigned int oldSize = m_size;
m_head = randomAdd(m_head, key);
if (m_size > oldSize) {
if (verbose) {
cout<<"Node "<<key<< " is added into the tree."<<endl;
}
return 1;
} else {
if (verbose) {
cout<<"Node "<<key<< " is already in the tree."<<endl;
}
return 0;
}
if (verbose) cout<<endl;
};
int RBST::del(const Key& key, bool verbose) {
unsigned oldSize= m_size;
m_head = del(m_head, key);
if (m_size < oldSize) {
if (verbose) {
cout<<"Node "<<key<< " is deleted from the tree."<<endl;
}
return 1;
} else {
if (verbose) {
cout<< "Node "<<key<< " is not in the tree."<<endl;
}
return 0;
}
};
int RBST::find(const Key& key, bool verbose) {
RBSTNode* ret = find(m_head, key);
if (ret == NULL) {
if (verbose) {
cout<< "Node "<<key<< " is not in the tree."<<endl;
}
return 0;
} else {
if (verbose) {
cout<<"Node "<<key<< " is in the tree."<<endl;
}
return 1;
}
};
int RBST::dump(char sep) {
int ret = dump(m_head, sep);
cout<<"SIZE: " <<ret<<endl;
return ret;
};
int RBST::dump(RBSTNode* target, char sep) {
if (target == NULL) {
return 0;
}
int ret = dump(target->left(), sep);
cout<< *target<<sep;
ret ++;
ret += dump(target->right(), sep);
return ret;
};
/***********************************************************/
/**************** FUNCTIONS TO BE COMPLETED ***************/
/***********************************************************/
/////////////////////////////////////////////////////////////
///////////////////// ADD FUNCTIONS ////////////////////////
/////////////////////////////////////////////////////////////
RBSTNode* RBST::rightRotate(RBSTNode* target) {
////////////// Write your code below ////////////////////////
RBSTNode *tempNode = target->left();
target->setLeft( tempNode->right() );
tempNode->setRight( target );
return tempNode;
};
RBSTNode* RBST::leftRotate(RBSTNode* target) {
////////////// Write your code below ////////////////////////
RBSTNode *tempNode = target->right();
target->setRight( tempNode->left() );
tempNode->setLeft( target );
return tempNode;
};
RBSTNode* RBST::addRoot(RBSTNode* target, const Key& key) {
countAdd++;
////////////// Write your code below ////////////////////////
if ( target == NULL ) {
++m_size;
return new RBSTNode(key);
}
if ( key < *target ) {
target->setLeft( addRoot( target->left(), key ) );
return rightRotate( target );
} else {
target->setRight( addRoot( target->right(), key ) );
return leftRotate( target );
}
};
RBSTNode* RBST::randomAdd(RBSTNode* target, const Key& key) {
countAdd++;
////////////// Write your code below ////////////////////////
if( target == NULL ) {
++m_size;
return new RBSTNode(key);
}
int r = rand() % m_size;
if ( r == 1 ) {
return addRoot( target, key );
}
if ( key < *target ) {
target->setLeft( randomAdd( target->left(), key ) );
} else {
target->setRight( randomAdd( target->right(), key ) );
}
return target;
};
/////////////////////////////////////////////////////////////
///////////////////// FIND FUNCTIONS ///////////////////////
/////////////////////////////////////////////////////////////
RBSTNode* RBST::find(RBSTNode* target, const Key& key) {
countFind++;
////////////// Write your code below ////////////////////////
if( target == NULL ) {
return NULL;
}
if( key == *target ) {
return target;
} else if ( key < *target ) {
return find( target->left(), key );
} else {
return find( target->right(), key );
}
};
/////////////////////////////////////////////////////////////
///////////////////// DEL FUNCTIONS ////////////////////////
/////////////////////////////////////////////////////////////
RBSTNode* RBST::del(RBSTNode* target, const Key& key) {
countDelete++;
////////////// Write your code below ////////////////////////
if ( target == NULL ) {
return NULL;
}
if ( key == *target) {
m_size--;
if ( target->right() == NULL ) {
return target->left();
}
RBSTNode *temp = delHelper(target->right());
del_store->setLeft( target->left() );
del_store->setRight( temp );
return del_store;
} else if ( key < *target ) {
target->setLeft( del( target->left(), key ) );
} else {
target->setRight( del( target->right(), key ) );
}
return target;
};
RBSTNode* RBST::delHelper(RBSTNode* target) {
if ( target->left() == NULL ) {
del_store = target;
return target->right();
}
target->setLeft( delHelper( target->left() ) );
return target;
};