-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.cpp
849 lines (607 loc) · 20.6 KB
/
test.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#include <iostream>
// TODO: установите здесь ссылки на дополнительные заголовки, требующиеся для программы.
#include <string>
#include <vector>
#include <utility>
#include <chrono>
#include <iostream>
#include <fstream>
#include <iterator> /** for ostream_iterator */
#include <stack> /** for stack */
#include <queue> /** for queue */
#include <cstring> /** for strcpy() */
using namespace std;
using namespace chrono;
using write_sequence = vector<string>;
using test_pair = pair<uint64_t, string>;
using modify_sequence = vector<test_pair>;
using read_sequence = vector<test_pair>;
ifstream& operator >> (ifstream& _is, test_pair& _value)
{
_is >> _value.first;
_is >> _value.second;
return _is;
}
template <typename S>
S get_sequence(const string& _file_name)
{
ifstream infile(_file_name);
S result;
typename S::value_type item;
while (infile >> item)
{
result.emplace_back(move(item));
}
return result;
}
class storage
{
public:
static const uint64_t Shirina_Massive = 128;
//Node structure.
struct TreeNode
{
char data[Shirina_Massive]; //data field
TreeNode* l, * r; //left and right childs.
TreeNode* parent;
uint64_t LeftCounter;
uint64_t RightCounter;
};
//Variables
TreeNode* pRoot; //pointer to the root of a tree
uint64_t treeTotal; //The number of nodes in the tree
storage()
{
treeTotal = 0;
pRoot = nullptr;
cout << "DEBUG: Constructor called... " << endl;
}/* storage()*/
~storage()
{
/** free allocated memory */
cout << "DEBUG: Destructor called... " << endl;
DeleteTree_no_recursive(pRoot);
cout << "Our Binary Tree Removed From Memory: treeTotal = " << treeTotal << endl << endl;
}/* ref. to ~storage() */
void push_no_recursive(const char* a, TreeNode** troot)
{
TreeNode** t = troot;
TreeNode* pParent = nullptr;
while ((*t) != NULL) {
if (strcmp(a, (*t)->data) < 0) {
(*t)->LeftCounter++;
pParent = (*t);
t = &(*t)->l;
continue;
}
else if (strcmp(a, (*t)->data) > 0) {
(*t)->RightCounter++;
pParent = (*t);
t = &(*t)->r;
continue;
}
}
if ((*t) == NULL)
{
if (pRoot == nullptr) {
(*t) = new TreeNode;
strcpy((*t)->data, a);
(*t)->l = (*t)->r = NULL;
(*t)->parent = nullptr;
(*t)->LeftCounter = 0;
(*t)->RightCounter = 0;
cout << "Starting initialization. " << "(*t)->data = " << (*t)->data << endl;
}
else {
(*t) = new TreeNode;
strcpy((*t)->data, a);
(*t)->l = (*t)->r = NULL;
(*t)->parent = pParent;
(*t)->LeftCounter = 0;
(*t)->RightCounter = 0;
}
treeTotal++;
return;
}
}/*end of push_no_recursive() */
/*
* Delete the tree,
* Uses queue to delete all the tree.
*/
uint64_t DeleteTree_no_recursive(TreeNode* tRoot)
{
// Base Case
if (tRoot == nullptr)
return 0;
// Create an empty queue for level order traversal
queue<TreeNode*> q;
// Do level order traversal starting from root
q.push(tRoot);
while (!q.empty())
{
TreeNode* node = q.front();
q.pop();
if (node->l != nullptr)
q.push(node->l);
if (node->r != nullptr)
q.push(node->r);
delete node;
}
return 0;
}/*end DeleteTree_no_recursive()*/
/*
* Can show tree's elements.
* Useful for debug purposes.
* InOrder Traversal.
*
*/
void Show_Simmetrich_no_recursive(TreeNode* troot)
{
stack<TreeNode* > s;
TreeNode* curr = troot;
uint64_t deb_cc = 0;
while (curr != nullptr || s.empty() == false)
{
/* Reach the left most Node of the
curr Node */
while (curr != nullptr)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
s.push(curr);
curr = curr->l;
}
/* Current must be nullptr at this point */
curr = s.top();
s.pop();
if (curr->parent == nullptr) {
cout << "curr ptr= " << curr << " " << curr->data << " index = " << deb_cc++ << " curr->parent = " << "nullptr" << endl;
}
else {
cout << "curr ptr= " << curr << " " << curr->data << " index = " << deb_cc++ << " curr->parent = " << curr->parent << " curr->parent->data = " << curr->parent->data << endl;
}
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->r;
} /* end of while */
}/*end Show_Simmetrich_no_recursive() */
/*
* SUPPORT INDICES.
* Delete node with current pointer _root
* and parent _prev.
*
*/
void delete_elem(TreeNode* _root, TreeNode* _prev)
{
TreeNode* curr = _root;
TreeNode* prev = _prev;
// Base case
if (_root == nullptr) {
return;
}
if (curr->l == nullptr && curr->r == nullptr) { /**this is a leaf */
if (prev != nullptr) { /** if remains the last node in the tree - root node ( i.e. prev=nullptr). */
if (curr == prev->l) {
prev->l = nullptr;
}
else if (curr == prev->r) {
prev->r = nullptr;
}
delete curr;
}
else {
delete curr;
pRoot = nullptr;
}
}/*ref. to if (curr->l == nullptr && curr->r == nullptr) */
// Check if the node to be
// deleted has at most one child.
else if (curr->l == nullptr || curr->r == nullptr) {
// newCurr will replace
// the node to be deleted.
TreeNode* newCurr;
// if the left child does not exist.
if (curr->l == nullptr) {
newCurr = curr->r;
}
else {
newCurr = curr->l;
}
// check if the node to
// be deleted is the root.
if (prev == nullptr) {
if (curr->r != nullptr) {
newCurr = curr->r;
newCurr->parent = nullptr;
pRoot = newCurr;
}
else { /** closest left element must become the root. */
if (curr->l != nullptr) {
newCurr = curr->l;
newCurr->parent = nullptr;
pRoot = newCurr;
}
}
delete curr;
return;
}
// check if the node to be deleted
// is prev's left or right child
// and then replace this with newCurr
if (curr == prev->l) {
prev->l = newCurr;
}
else {
prev->r = newCurr;
}
// free memory of the
// node to be deleted.
newCurr->parent = prev;
delete curr;
}
// node to be deleted has
// two children.
else {
TreeNode* p = nullptr;
TreeNode* temp;
// Compute the inorder successor
temp = curr->r;
while (temp->l != nullptr) {
p = temp;
if (p->LeftCounter > 0) {
p->LeftCounter--;
}
temp = temp->l;
}
// check if the parent of the inorder
// successor is the curr or not(i.e. curr=
// the node which has the same data as
// the given data by the user to be
// deleted). if it isn't, then make the
// the left child of its parent equal to
// the inorder successor'd right child.
if (p != nullptr) {
p->l = temp->r;
if (temp->r == nullptr) { /** No right subtree of the left minimum node. */
p->LeftCounter = 0;
}
else { /** if there is right subtree of the left minimum node. */
p->LeftCounter = temp->RightCounter;
temp->r->parent = p;
}
if (curr->RightCounter > 0 ) {
curr->RightCounter--;
}
}
// if the inorder successor was the
// curr (i.e. curr = the node which has the
// same data as the given data by the
// user to be deleted), then make the
// right child of the node to be
// deleted equal to the right child of
// the inorder successor.
else {
curr->r = temp->r;
if (temp->r != nullptr) {
temp->r->parent = curr;
}
curr->RightCounter = temp->RightCounter;
}
strcpy(curr->data, temp->data);
delete temp;
}
}/* end delete_elem() */
/*
* Classic version. Traversal through the tree.
* Find wanted element by its number in sorted order
* and return it.
* Use InOrder Traversal.
*
*/
char* get_by_num_no_recursive(TreeNode* troot, uint64_t num)
{
uint64_t OrderNum = 0;
stack<TreeNode* > s;
TreeNode* curr = troot;
//int count = 1;
while (curr != nullptr || s.empty() == false)
{
/* Reach the left most Node of the
curr Node */
while (curr != nullptr)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
s.push(curr);
curr = curr->l;
}
/* Current must be nullptr at this point */
curr = s.top();
s.pop();
if (OrderNum == num) {
return curr->data;
}
OrderNum++;
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->r;
} /* end of while */
return nullptr;
}/*end get_by_num_no_recursive() */
/*
* Classic version. Traversal through the tree.
* Find wanted element to delete by its number in sorted order
* and delete it.
* use InOrder Traversal.
*
*/
void del_by_num_no_recursive(TreeNode* troot, uint64_t num)
{
uint64_t OrderNum = 0;
struct __NODE {
TreeNode* curr;
TreeNode* prev;
}node;
stack<struct __NODE> s;
TreeNode* curr = troot;
TreeNode* prev = nullptr;
while (curr != nullptr || s.empty() == false)
{
/* Reach the left most Node of the
curr Node */
while (curr != NULL)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
node.curr = curr;
node.prev = prev;
s.push(node);
if (curr->l != nullptr) {
prev = curr;
}
curr = curr->l;
}
/* Current must be NULL at this point */
node = s.top();
s.pop();
curr = node.curr;
prev = node.prev;
if (OrderNum == num) {
delete_elem(node.curr, node.prev);
break;
}
OrderNum++;
if (curr->r != nullptr) {
prev = curr;
}
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->r;
} /* end of while */
}/*end del_by_num_no_recursive() - Classic version. Traversal through the tree.*/
/*
* Support indices.
* Get an element by index from the tree.
*
*/
char* get_elem_by_index(TreeNode* proot, uint64_t ind)
{
char* pRet = nullptr;
TreeNode* ptr = proot;
uint64_t sought_for_ind = ind;
if (proot == nullptr) {
cout << "The Tree is Emty !!! ";
return 0;
}
if (ind > (treeTotal-1) ) {
cout << "Too big index to get, index = " << ind << " treeTotal = " << treeTotal << " Stop. " << endl << endl;
return 0;
}
while (ptr != nullptr) {
if (sought_for_ind < ptr->LeftCounter) { /** work out left branch */
ptr = ptr->l;
if (ptr == nullptr) {
cout << " DEBUG POINT XXX L: ptr = nullptr from Left ind = " << ind << endl;
}
}
else if (sought_for_ind == ptr->LeftCounter) {
return ptr->data;
}
else if (sought_for_ind > ptr->LeftCounter) { /** work out right branch */
sought_for_ind = sought_for_ind - ptr->LeftCounter - 1;
if (ptr->r == nullptr) {
cout << " --DEBUG POINT XXX R: ptr->data=" << ptr->data << " ptr->LeftCounter=" <<
ptr->LeftCounter << " ptr->RightCounter=" << ptr->RightCounter << endl;
}
ptr = ptr->r;
if (ptr == nullptr) {
cout << " DEBUG POINT XXX R: ptr = nullptr from Right ind = " << ind << endl;
}
}
}
return pRet;
}/*ref. to get_elem_by_index() */
/*
* Support indices.
* Delete an element by index from the tree.
*
*/
char* delete_elem_by_index(TreeNode* proot, uint64_t ind)
{
char* pRet = nullptr;
TreeNode* ptr = proot;
uint64_t sought_for_ind = ind;
if (proot == nullptr) {
cout << "Can not delete. The Tree is Emty !!! ";
return 0;
}
if (ind > (treeTotal - 1)) {
cout << "Too big index to delete, index = " << ind << " treeTotal = " << treeTotal << " Stop. " << endl << endl;
return 0;
}
while (ptr != nullptr) {
if (sought_for_ind < ptr->LeftCounter) { /** work out left branch */
if (ptr->LeftCounter > 0) {
ptr->LeftCounter--;
}
ptr = ptr->l;
}
else if (sought_for_ind == ptr->LeftCounter) {
delete_elem(ptr, ptr->parent);
break;
}
if (sought_for_ind > ptr->LeftCounter) { /** work out right branch */
sought_for_ind = sought_for_ind - ptr->LeftCounter - 1;
if (ptr->RightCounter > 0) {
ptr->RightCounter--;
}
ptr = ptr->r;
}
}
return pRet;
}/*ref. to delete_elem_by_index() */
public:
void insert(const string& _str)
{
//TODO insert str with sorting
push_no_recursive(_str.c_str(), &pRoot); //put a node in the tree
}/*ref. to insert() */
void erase(uint64_t _index)
{
//TODO erase string via index
//del_by_num_no_recursive(pRoot, _index); // - classic func.
delete_elem_by_index(pRoot, _index);
treeTotal--;
}/** erase() */
const string& get(uint64_t _index)
{
static string retStr;
char* pGet;
//TODO return string via index
retStr.clear();
//pGet=get_by_num_no_recursive(pRoot, _index); // - classic func.
pGet = get_elem_by_index(pRoot, _index);
if (pGet != nullptr) {
retStr.assign(pGet, std::strlen(pGet));
}
return retStr;
}
/*
* Can print tree's elements as
* sorted array.
* Useful for debug.
*/
void print_container(TreeNode* container)
{
cout << "\nprint_container: " << endl;
//Show_Simmetrich_no_recursive(pRoot);
cout << "Get all elements in the tree by index: \n";
cout << "treeTotal = " << treeTotal << endl << endl;;
char* resNum = nullptr;
for (int i = 0; i < static_cast<int>(treeTotal); i++) {
resNum = get_elem_by_index(pRoot, i);
cout << "[" << i << "] = " << resNum << endl;
}
}/* end of print_container() */
/*
* Recursive function.
* Return true if the tree is BST
* and false if the tree is not BST.
*/
bool IsItBinarysearchTree(TreeNode* troot)
{
static TreeNode* prev = nullptr;
if (troot == nullptr)
{
return true;
}
if (!IsItBinarysearchTree(troot->l))
{
return false;
}
if ((prev != nullptr) && (strcmp(troot->data, prev->data) < 0))
{
return false;
}
prev = troot;
return IsItBinarysearchTree(troot->r);;
}/*end of IsItBinarysearchTree() */
};/* ref. to class storage */
int main()
{
write_sequence write = get_sequence<write_sequence>("write.txt");
modify_sequence modify = get_sequence<modify_sequence>("modify.txt");
read_sequence read = get_sequence<read_sequence>("read.txt");
storage st;
time_point<system_clock> time1;
nanoseconds total_time1(0);
time1 = system_clock::now();
for (const string& item : write)
{
st.insert(item);
}
cout << "Filling complete." << " The Tree size = " << st.treeTotal << " nodes." << endl;
total_time1 += system_clock::now() - time1;
cout << "Filling time: " << duration_cast<milliseconds>(total_time1).count() << "ms" << endl;
uint64_t progress = 0;
uint64_t percent = modify.size() / 100;
////uint64_t percent = modify.size() / 10000;
time_point<system_clock> time;
nanoseconds total_time(0);
modify_sequence::const_iterator mitr = modify.begin();
read_sequence::const_iterator ritr = read.begin();
int deb_count = 0;
for (; mitr != modify.end() && ritr != read.end(); ++mitr, ++ritr)
{
time = system_clock::now();
st.erase(mitr->first);
st.insert(mitr->second);
const string& str = st.get(ritr->first);
total_time += system_clock::now() - time;
if (ritr->second != str)
{
cout << "test failed" << endl;
//============= DEBUG
cout << "mitr->first = " << mitr->first << " mitr->second = " << mitr->second <<
" ritr->first = " << ritr->first << endl;
cout << "ritr->second = " << ritr->second << " str = " << str << endl;
cout << "deb_count = " << deb_count << endl; // FOR DEBUG ONLY.
cout << "ritr->second = " << ritr->second << " ritr->second.size() = " << ritr->second.size() << endl;
cout << "str = " << str << " str.size() = " << str.size() << endl;
//============= END: DEBUG
return 1;
}
// if size() / 100 = 0
if (percent == 0) {
percent = 1;
}
if (++progress % (5 * percent) == 0)
{
cout << "time: " << duration_cast<milliseconds>(total_time).count()
<< "ms progress: " << progress << " / " << modify.size() << "\n";
}
// cout << "deb_count = " << deb_count << " progress = " << progress
// << " percent = " << percent << endl;
deb_count++;
}
cout << endl;
cout << "===========" << endl;
cout << "Test OK!" << endl;
cout << "===========" << endl;
/** Examine if the tree is BST */
bool IsTheTreeBST = st.IsItBinarysearchTree(st.pRoot);
if (IsTheTreeBST) {
cout << "\n\n This Tree Is The TRUE BST !!! \n\n";
}
else {
cout << "\n\n This Tree Is FALSE ??? \n\n";
}
/** END: Examine if the tree is BST */
return 0;
}