-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBST_Integrated_Operations.cpp
227 lines (192 loc) · 8.7 KB
/
CBST_Integrated_Operations.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <string>
#include "CBST_Tree_Company.h"
int CBST::resetCBST(Info_collection *info)//reset the CBST
{//resetCBST
int accounter = 0;
int item = the_overallinfo[Min_Extremum]; if(item <= 0) return 0;
while(item > 0){//loop
info->Kind = item; accounter ++;
if(!checkexitence(item)) return -1;
int node = get_info(item, Cousin); info->Name = item;
while(node > 0){//doing in CL
info->Kind = node;
if(!checkexitence(node)) return -2;
int next = get_info(node, Cousin);
resetAnItemInCL(node); updateItemInfo(node, Out, InOrOut); accounter ++;
if(next <= 0) break; else node = next;
}
node = get_info(item, Successor); resetAUnit(item);//reset info of BST and ARA
item = node;
}//loop
resetWholeInfo();
return accounter ++;
}//resetCBST
bool CBST::InitializeAllItems()
{//resetAllItem
if(Scalar <= 0) return false;
for(int n = 1; n <= Scalar; n++) resetAUnit(n);
resetWholeInfo();
return true;
}//resetAllItem
int CBST::gain_Min_Extremum(Info_collection *info)
{//gain_Min_Extremum
int extremum = get_MinEtremum(); if(extremum <= 0) return 0;//declear the tree is empty
info -> Name = 0; info -> Parent_Port = 0; info -> Port = 0; info->Kind = 0;
int type = deletion(extremum, info);
if(type < 0){
cout<<"\n deleting error in functor gain_Min_Extremum. "<<endl;
return type;
}
return extremum;
}//gain_Min_Extremum
int CBST::gain_theMinCL(Info_collection *info)
{//gain_theMinCL
/* method introduction
1. get and check the extremum
2. the likelihood we will have to settle:
(1) extremum as a terminal, conversely (2) as a PK
(3) as a root, on contrary (4) as an average item.
----------------------+-----------------------+------------------------------------+------------*/
if(the_overallinfo[Root] <= 0) return 0;//declear the tree is empty
int extremum = get_MinEtremum();
//* 1st step: estimate that extremum.
info -> Name = 0; info -> Parent_Port = 0; info -> Port = 0;
int type = estimateItem(extremum, info);
if(type < 0){
cout<<"\n estimateItem, the position => "<<type<<"; "<<info->Kind<<endl;
return type;
}
else if(type >= Complete_Knot){cout<<"\n error type => "<<type<<endl; return -1;}
//* 2nd step: in ARA model
int right_member = get_info(extremum, Successor);
if(right_member <= 0){//(1) + (3)
resetWholeInfo(); the_overallinfo[num_Agencies] --; updateItemInfo(extremum, Out, InOrOut);
return extremum;// not need addressing in BST model
}else{
//(2) + ((3) || (4)) or (1) + (4)
info->Kind = right_member; if(!judge_agency(right_member)) return -1;//check right_member
updateItemInfo(extremum, 0, Successor); updateItemInfo(right_member, 0, Predecessor);//deleted off ARA
the_overallinfo[Min_Extremum] = right_member;// update the overall information
info->Length ++;
}
//* 3rd step: in BST model
if(info -> Name > 0){//(4) + ((1) || (2))
BST_delete_aBSTRelation(info -> Name, extremum, info->Parent_Port);//delete extremum off father if terminal then off BST
info->Length ++;
}
if(type == Partial_Knot){//(2)
int son = get_info(extremum, Right_child);
if(info -> Name)//+ (4)
BST_connection(info -> Name, son, Left_child);//father <->grandson
else{//+ (3)
the_overallinfo[Root] = son;
updateItemInfo(son, 0, Parent); // child as the new root.
}
info -> Length ++;
}
the_overallinfo[num_Agencies] --; updateItemInfo(extremum, Out, InOrOut);
return extremum;
}//gain_theMinCL
bool CBST::insertion(int item, Info_collection *info)throw(Exception_about_memory)
{//insertion
/*
1. if CBST is empty, then appoint item as being a root
2.1 nonempty, seek appropriate position
2.2 insertion in BST model
2.3 in ARA model if will as an average terminal in BST.
----------------------+--------------------+---------------------------------*/
info->Kind = item; int effort = 0;
if(judge_inorout(item)) return false; //check item
if(the_overallinfo[Root] <= 0){//empty tree
updateItemInfo(item, In, InOrOut);//for itself
the_overallinfo[Root] = item; // for BST
the_overallinfo[Max_Extremum] = item; the_overallinfo[Min_Extremum] = item; //for ARA
the_overallinfo[num_Agencies] ++; //for overall info
effort ++; //for statistices
}
else{
int father = standard_seeking(get_TheKey(item), info); // find the position
if(father <= 0){
cout<<"\n standard_seeking in insertion; position => "<<info->Kind<<"; return => "<<father<<endl; return false;
}
//1. if in CL
if(info->Port == Cousin){
int cousin = get_info(father, Cousin); info->Kind = cousin;
if(cousin > 0){//with another cousin
if(!judge_inorout(cousin)) return false;
else{
BST_insertion(item, get_info(father, Cousin), info->Port); //will be insert into the between of agency and agency's cousin
effort ++;
}
}//another
}//CL
else{// as a child
int c = ARA_interpolation(father, item, info->Port);//in ARA model
if(c <= 0){Exception_about_memory e(0); string str = "ARA, function insertion\n"; e.adding_info(str); throw e;}
effort = effort + c; the_overallinfo[num_Agencies] ++;
}//child
//3. connet father and item in BST model, finally.
//Remark: at this step, the father's information should be updated when in CL, wherefore this step at last.
BST_insertion(father, item, info->Port); effort ++;// father <--> item.
}
info->Port = 0; info->Name = - 1; info->Length = effort;
the_overallinfo[num_Items] ++;
return true;
}//insertion
int CBST::insertionAList(int *item_list, Info_collection *info)throw(Exception_about_memory)
{//insertionAList
if(item_list[0] <= 0) return 0;
info -> initialize(); int counter = 0;
if(item_list[0] == 1)
try{
info->other = item_list[1]; if(insertion(item_list[1], info) < 0) return - 2;
updateItemInfo(item_list[1], 0, Auxiliary); counter ++;
}catch(Exception_about_memory &e){string str = "\n single insertion, function insertionAList"; e.adding_info(str); throw e;}
else{
while(item_list[0] > 0){//loop
int i = item_list[0]; item_list[0] --;
try{
info->other = item_list[i]; if(insertion(item_list[i], info) < 0) return - 2;
updateItemInfo(item_list[i], 0, Auxiliary); counter ++;
}catch(Exception_about_memory &e){string str = "\n a list insertion, function insertionAList"; e.adding_info(str); throw e;}
}//loop
}
item_list[0] = 0;
return counter ++;
}//insertionAList
int CBST::deletion(int item, Info_collection *info)throw(Exception_about_memory)
{//deletion
info->Kind = -1;
if(!judge_inorout(item) || the_overallinfo[Root] <= 0) return 0; //check relation
info -> Name = -1; info -> Port = 0; info -> Parent_Port = 0;
int type = estimateItem(item, info);
if(type < 0){
cout<<"\n estimateItem position => "<<info->Kind<<endl;
return -1;
}
int value = 0;
switch(type){
case Terminal: value = deleting_terminal(item, info); break;
case Partial_Knot: value = deleting_PK(item, info); break;
case Complete_Knot: value = deleting_CK(item, info); break;
case Juxta_Position: value = deleting_cousin(item, info); break;
default: value = -1;
}
if(value < 0){
switch(type){
case Terminal: cout<<"\n deleting_terminal, position => "<<info->Kind<<endl; break;
case Partial_Knot: cout<<"\n deleting_PK, position => "<<info->Kind<<endl; break;
case Complete_Knot: cout<<"\n deleting_CK, position => "<<info->Kind<<endl; break;
case Juxta_Position: cout<<"\n deleting_cousin, position => "<<info->Kind<<endl; break;
default: value = -1;
}
Exception_about_memory e(0);
string str ="deletion, function deletion\n"; e.adding_info(str); throw e;
}
return type;
}//deletion
bool CBST::set_AuxiliaryOnItem(int item, int value)
{//set_AuxiliaryOnItem
if(!checkexitence(item)) return false; updateItemInfo(item, value, Auxiliary);
return true;
}//set_AuxiliaryOnItem