-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
1913 lines (1706 loc) · 62 KB
/
Copy pathbst.cpp
File metadata and controls
1913 lines (1706 loc) · 62 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
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bst.h"
//used as a filling slot when using breadth first save/load
string filling_string = "NULL" ;
//helper functions to take right input
//if user enters a character while expecting an integer or vice versa it doesn't cause issues
bool getInput(char& choice) {
cin>>choice;
if(cin.fail()) {
cin.clear(); // clear the error state
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the bad input
return false;
}
return true;
}
bool getInput(int& choice) {
cin>>choice;
if(cin.fail()) {
cin.clear(); // clear the error state
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the bad input
return false;
}
return true;
}
// Function to open a file for writing
bool openFileForWriting(const string& filePath) {
ofstream file(filePath);
if (!file) {
return false; // Failed to open the file
}
// File is now open for writing
return true;
}
// Function to open a file for reading
bool openFileForReading(const string& filePath) {
std::ifstream file(filePath);
if (!file) {
return false; // Failed to open the file
}
// File is now open for reading
return true;
}
//turns a tree bst into a string and returns it
//used in cout
template<typename DataType>
string to_string(const bst<DataType>&tree){
string str;
tree.to_string(str) ;
return str ;
}
string to_string(const int &data){
return std::to_string(data);
}
string to_string(const unsigned int&data){
return std::to_string(static_cast<int>(data));
}
string to_string(const float &data){
return std::to_string(data);
}
string to_string(const double &data){
return std::to_string(data);
}
string to_string(const char &data){
return std::string(1,data);
}
string to_string(const long &data){
return std::to_string(static_cast<int>(data));
}
string to_string(const bool &data){
return data ? "1" : "0";
}
string to_string(const string&data){
return data;
}
template<typename DataType>
string to_string(const vector<DataType>&vec){
string str= "" ;
for(int i = 0 ; i<vec.size();i++){
str.append(::to_string(vec[i])+' ');
}
return str ;
}
template<typename unique_data_type>
string to_string(const custom_data<unique_data_type>&p){
return p.to_string() ;
}
//recursively add data in-order to a string str
template <typename DataType>
void bst<DataType>::to_string(string& str, node<DataType>* ptr) const {
if (!ptr) {
ptr = root ;
}
if(ptr){
if (ptr->left) {
to_string(str, ptr->left);
}
//this '\0' is actually important when saving the bst
//data in file might seem weird but it loads correctly
str.append(::to_string(ptr->data)+'\0');
if (ptr->right) {
to_string(str, ptr->right);
}
}
}
//helper function to allocate memory for a new node
template <typename DataType>
node<DataType>*bst<DataType>::get_node(const DataType & _data){
node<DataType>*new_node=new node<DataType> ;
new_node->data =_data ;
new_node->left = NULL;
new_node->right = NULL;
new_node->parent = NULL;
return new_node;
}
//empty tree
template<typename DataType>
bst<DataType>::bst(void){
root = NULL ;
traverser= NULL ;
size=0;
}
//tree filled from an array
//if data is sorted its filled in O(N) else O(Nlog2N)
//note if data is sorted and there are duplicates they aren't deleted
template <typename DataType>
bst<DataType>::bst(const DataType* arr, const long long _size) {
size = 0;
root = NULL ;
traverser=NULL;
if (is_sorted(arr,_size)) {
size =_size;
root = fill_sorted(arr, 0, size-1,root);
root->parent = NULL;
}
else {
for (long long i = 0; i <_size; i++) {
insert(arr[i]);
}
traverser= root;
root->parent = NULL;
}
}
template <typename DataType>
bst<DataType>::bst(const bst<DataType>& src) {
root = copy_bst(src.root);
size = src.size;
}
template <typename DataType>
void bst<DataType>::operator=(const bst<DataType>&src) {
if(this!=&src&&src.root){
if(root){
del_tree(root) ;
root=NULL;
}
root=copy_bst(src.root) ;
size= src.size;
}
else{
del_tree(root) ;
root =NULL;
}
}
//tree filled from an array
//if data is sorted its filled in O(N) else O(Nlog2N)
template<typename DataType>
bst<DataType>::bst(const vector<DataType>&arr) {
size = 0;
root = NULL ;
traverser=NULL;
if (is_sorted(arr)){
size =arr.size();
root = fill_sorted(arr, 0, size-1,root);
root->parent = NULL;
}
else {
for (long long i = 0; i <arr.size(); i++) {
insert(arr[i]);
}
traverser= root;
root->parent = NULL;
}
}
//destructor
template <typename DataType>
bst<DataType>::~bst() {
del_tree(root);
root = NULL;
traverser=NULL;
size = 0;
}
//functions that remove the whole tree or a subtree
template <typename DataType>//removes the whole bst
bool bst<DataType>:: remove_tree(void){
if(root){
del_tree(root) ;
root =NULL;
traverser=NULL;
size = 0 ;
return 1;
}
return 0;
}
//remove the subtree beneath root containing data
template <typename DataType>
bool bst<DataType>:: remove_subtree(const DataType&data){
if(search(data)){
fix_parent(traverser,NULL) ;
del_tree(traverser);
traverser= root ;
return 1 ;
}
return 0 ;
}
//checks if data in an array is sorted
template <typename DataType>
bool bst<DataType>::is_sorted(const DataType*arr,long long _size){
for(long long i = 0 ; i <_size-1;i++){
if(arr[i]>arr[i+1]){
return 0;
}
}
return 1 ;
}
//checks if vector of datatype is sorted
template<typename DataType>
bool bst<DataType>::is_sorted(const vector<DataType>&arr){
for(long long i = 0 ; i <arr.size()-1;i++){
if(arr[i]>arr[i+1]){
return 0;
}
}
return 1 ;
}
//checks if data in a file is sorted
template<typename DataType>
bool bst<DataType>::is_sorted(ifstream& file){
if (!file) return false; // Check if the file is open
DataType d1, d2;
char ch;
while (file.get(ch)) {
if (ch == '\0') continue; // Skip null characters
file.putback(ch); // Put the character back into the stream
if (!(file >> d1)) return true; // If the file is empty, it's considered sorted
while (file.get(ch)) {
if (ch == '\0') continue; // Skip null characters
file.putback(ch); // Put the character back into the stream
if (!(file >> d2)) return true; // If there are no more elements, it's considered sorted
if (d1 > d2) {
return false;
}
d1 = d2; // Update d1 to be the current element for the next comparison
}
}
return true;
}
template <typename DataType>
node<DataType>*bst<DataType>::fill_sorted(const vector<DataType>&arr,long long beg,long long end,node<DataType>*ptr){
if(ptr==NULL){
if(beg<=end){
long long mid =(end-beg)/2+beg;
ptr = get_node(arr[mid]) ;
ptr->left = fill_sorted(arr,beg,mid-1,ptr->left) ;
if(ptr->left){
ptr->left->parent=ptr;
}
ptr->right=fill_sorted(arr,mid+1,end,ptr->right);
if(ptr->right){
ptr->right->parent=ptr;
}
return ptr ;
}
else{
return NULL;
}
}
return NULL ;
}//passed
/*
these functions fill a bst from sorted data in linear time (O(N))
and returns the root of the tree
one from an array and the other one from a file but the indexing file must exist(used in load)
if indexing file doesn't exist and data is sorted it loads the data in a vector of the same data type
and then puts it in linear time in the bst
*/
template <typename DataType>
node<DataType>* bst<DataType>:: fill_sorted(const DataType*arr,long long beg ,long long end ,node<DataType>*ptr){
if(ptr==NULL){
if(beg<=end){
long long mid =(end-beg)/2+beg;
ptr = get_node(arr[mid]) ;
ptr->left = fill_sorted(arr,beg,mid-1,ptr->left) ;
if(ptr->left){
ptr->left->parent=ptr;
}
ptr->right=fill_sorted(arr,mid+1,end,ptr->right);
if(ptr->right){
ptr->right->parent=ptr;
}
return ptr ;
}
else{
return NULL;
}
}
return NULL ;
}
template<typename DataType>
node<DataType>* bst<DataType>::fill_sorted(ifstream& file, ifstream& index_file, long long beg, long long end, node<DataType>* ptr) {
if (ptr == NULL) {
if (beg <= end) {
// Calculate the middle line number
long long mid = (end - beg) / 2 + beg;
// Calculate the position to seek to in the index_file
std::streamoff index_pos = mid * sizeof(std::streampos);
// Seek to the position in index_file
index_file.clear(); // Clear the EOF flag
index_file.seekg(index_pos, std::ios::beg);
// Read the position from the index_file
std::streampos mid_pos;
index_file.read(reinterpret_cast<char*>(&mid_pos), sizeof(mid_pos));
// Seek to the position in file_path
file.seekg(mid_pos);
// Read the data from file_path
DataType data;
file >> data;
// Create a new node with the data
ptr = get_node(data);
ptr->left = fill_sorted(file, index_file, beg, mid - 1, ptr->left);
if (ptr->left) {
ptr->left->parent = ptr;
}
ptr->right = fill_sorted(file, index_file, mid + 1, end, ptr->right);
if (ptr->right) {
ptr->right->parent = ptr;
}
return ptr;
}
else {
return NULL;
}
}
return NULL;
}
//deletes a binary tree where root is ptr
template <typename DataType>//no recusion
void bst<DataType>:: del_tree(node<DataType>*ptr){
if(ptr){
queue<node<DataType>*>q ;
q.push(ptr) ;
if(ptr->parent){
fix_parent(ptr,NULL);
}
long long counter =1 ;
while(!q.empty()){
node<DataType>*temp= q.front() ;
q.pop() ;
if(temp->left){
q.push(temp->left);
counter++;
}
if(temp->right){
q.push(temp->right);
counter++ ;
}
if(temp){
delete temp;
temp=NULL;
}
}
if(ptr==root){
root=NULL;
}
ptr=NULL;
size-=counter;
}
}
//data inserted must have the id or the feature of comparison not repeated
//ex: id of a person...etc
//no recursion
template <typename DataType>
bool bst<DataType>::insert(const DataType&_data){
node<DataType>*ptr= root;
if(root==NULL){
root=get_node(_data);
size=1;
return 1 ;
}
else{
size++;
while(1){
if(_data==ptr->data){
size--;
return 0 ;
}
else if(_data<ptr->data){
if(ptr->left){
ptr=ptr->left;
}
else{
ptr->left= get_node(_data) ;
ptr->left->parent=ptr;
return 1 ;
}
}
else{
if(ptr->right){
ptr=ptr->right;
}
else{
ptr->right= get_node(_data) ;
ptr->right->parent=ptr;
return 1 ;
}
}
}
}
}
//this function puts traverser pointer on the node containing data if found
//and returns true
template <typename DataType> //no recursion
bool bst<DataType>::search(const DataType&_data,node<DataType>*ptr)const{
traverser=(ptr==NULL)?root:ptr;
if(traverser==NULL){
return 0 ;
}
else{
while(1){
if(_data==traverser->data){
return 1 ;
}
else if(_data<traverser->data){
if(traverser->left){
traverser=traverser->left;
}
else{
return 0 ;
}
}
else{
if(traverser->right){
traverser=traverser->right;
}
else{
return 0 ;
}
}
}
}
}
/*
next set of functions are used in deletion of a node in a bst
*/
//returns node with max data where root is ptr
template <typename DataType>
node<DataType>*bst<DataType>::get_max(node<DataType>*ptr)const {
if(ptr){
while(ptr->right){
ptr=ptr->right;
}
return ptr ;
}
}
//returns node with min data where root is ptr
template <typename DataType>
node<DataType>* bst<DataType>::get_min(node<DataType>*ptr)const {
if(ptr){
while(ptr->left){
ptr=ptr->left;
}
return ptr ;
}
}
//checks if ptr exists and its the left child
template <typename DataType>
bool bst<DataType>::is_left(node<DataType>*ptr)const{
return(ptr&&ptr->parent&&ptr->parent->left==ptr);
}
//checks if ptr exists and its the right child
template <typename DataType>
bool bst<DataType>::is_right(node<DataType>*ptr)const {
return(ptr&&ptr->parent&&ptr->parent->right==ptr);
}
/*
this function makes ptr's parent's link pointing at ptr point at another destiniation
for example ptr is left child of a parent left and we want it to point to another node
this function takes care of it
*/
template <typename DataType>
void bst<DataType>::fix_parent(node<DataType>*ptr,node<DataType>*dest){
if(is_left(ptr)){
ptr->parent->left=dest;
}
else if(is_right(ptr)){
ptr->parent->right=dest ;
}
}
//checks if ptr is the root
template <typename DataType>
bool bst<DataType>::is_root(node<DataType>*ptr)const {
return (ptr==root) ;
}
//deletes a node from a bst
template <typename DataType>//no recursion
bool bst<DataType>::remove(const DataType&data,node<DataType>*ptr){
if(root){
//if data exists traverser sits on it now
if(search(data,ptr)){
size--;//decrement if data is found
//leaf node condition
if(traverser->left==NULL&&traverser->right==NULL){
if(is_root(traverser)){
delete root ;
traverser=NULL;
root= NULL;
}
else{
//update paren't link to point to null
fix_parent(traverser,NULL) ;
delete traverser ;
traverser = NULL;
}
}
//no right child condition
else if(traverser->right==NULL){
traverser->left->parent=traverser->parent;
if(!is_root(traverser)){
fix_parent(traverser,traverser->left);
}
else{
root =root->left ;
}
delete traverser;
traverser=NULL;
}
//no left child condition
else if(traverser->left==NULL){
traverser->right->parent=traverser->parent;
if(!is_root(traverser)){
fix_parent(traverser,traverser->right);
}
else{
root =root->right ;
}
delete traverser;
traverser=NULL;
}
//2 children condition
else{
node<DataType>*temp=(traverser->left)?get_max(traverser->left):get_min(traverser->right);
//save data in temp_data
//and saves traverser location in a temp pointer
//so that after removing temp data
//the temp_ptr is assigned the value
DataType temp_data=temp->data;
node<DataType>*temp_ptr= traverser;
remove(temp_data,temp);
temp_ptr->data=temp_data ;
}
traverser=root;
return 1 ;
}
}
else{
traverser=root;
}
return 0;
}
//copies a tree where ptr is the root of that tree
template <typename DataType> //no recursion
node<DataType>*bst<DataType>::copy_bst(const node<DataType>* ptr) {
if (ptr) {
node<DataType>* new_root = get_node(ptr->data);
queue<const node<DataType>*> src;
queue<node<DataType>*> dest;
size = 1 ;
src.push(ptr);
dest.push(new_root);
while (!src.empty()) {
const node<DataType>* src_node = src.front();
node<DataType>* dest_node = dest.front();
src.pop();
dest.pop();
if (src_node->left) {
dest_node->left = get_node(src_node->left->data);
dest_node->left->parent = dest_node;
src.push(src_node->left);
dest.push(dest_node->left);
size++;
}
if (src_node->right) {
dest_node->right = get_node(src_node->right->data);
dest_node->right->parent = dest_node;
src.push(src_node->right);
dest.push(dest_node->right);
size++;
}
}
return new_root;
}
//if src is null the tree is deleted
else{
del_tree(root) ;
root = NULL;
}
return NULL;
}
//inorder print the data of the bst where root is ptr
//by default its root of the phantom
template <typename DataType>//recursion used
void bst<DataType>::inorder(node<DataType>*ptr)const {
if(!ptr){
ptr = root ;
}
if(ptr){
if(ptr->left){
inorder(ptr->left);
}
cout<<ptr->data<<" , ";
if(ptr->right){
inorder(ptr->right) ;
}
}
}
template <typename DataType>//recursion used
void bst<DataType>::preorder(node<DataType>*ptr)const {
if(!ptr){
ptr = root ;
}
if(ptr){
cout<<ptr->data<<" , " ;
if(ptr->left){
preorder(ptr->left);
}
if(ptr->right){
preorder(ptr->right) ;
}
}
}
template <typename DataType>//recursion used
void bst<DataType>:: postorder(node<DataType>*ptr)const {
if(!ptr){
ptr = root ;
}
if(ptr){
if(ptr->left){
postorder(ptr->left);
}
if(ptr->right){
postorder(ptr->right) ;
}
cout<<ptr->data<<" , " ;
}
}
//prints the tree in level order or breadth-first
template <typename DataType>//no recursion
void bst<DataType>::breadth_first(node<DataType>*ptr)const{
if(root){
if(!ptr){
ptr = root ;
}
queue<const node<DataType>*>q ;
q.push(ptr) ;
while(!q.empty()){
const node<DataType>*temp= q.front() ;
q.pop() ;
if(temp){
if(temp->left){
q.push(temp->left);
}
if(temp->right){
q.push(temp->right);
}
if(temp){
cout<<temp->data<<" , ";
}
}
}
}
}
//interface with the tree
template <typename DataType>
void bst<DataType>:: move(void){
if(root){
traverser= root;
while(1){
cout<<"\n(Enter->stops at current)\n(w->go up)\n(a->go left)\n(d->go right)\n(s->show the whole tree)\n(q->quit)\n>>";
cout<<"\n\nCurrent : "<<traverser->data<<"\n\n";
cout<<"\n>>";
char ch = getch();
switch(ch){
case 'w':{
if(traverser->parent)
traverser =traverser->parent;
}break;
case 'd':{
if(traverser->right)
traverser =traverser->right;
}break;
case 'a':{
if(traverser->left)
traverser =traverser->left;
}break;
case 's':{
cout<<"\n\nLevel Order\n\n";
breadth_first(root);
}break;
case 'r':{
traverser=root;
}break;
case 'q':{
traverser=root;
return ;
}break;
case '\r' :{
return ;
}
}
}
}
else{
cout<<"\nTree is empty\n" ;
}
}
/*
load and save from a file functions
*/
/*
this function saves the bst in a file with an index_file (inorder)
index_file contains a bunch of row where each contains the starting point of data in the saved file
think of computer organization how stack pointer works with stack
data is pushed and the stack pointer is pointing at the new data (or at first empty place in memory)
so for row one it contains 0 for row 2 it contains sizeof(data1)
for row3 it contains sizeof(data1+data2) and so on
if index file is removed or the name is altered (the .bin file )
data may not be loaded correctly or it would take O(NlogN)
since it assumes that its not sorted
if the .bin doesn't exist
*/
template<typename DataType>
void bst<DataType>::inorder_save(node<DataType>*ptr, ofstream& file, ofstream& index_file) const {
// If the node pointer is null, set it to the root of the tree
if(!ptr){
ptr=root;
}
// If there is a left child, recursively call the function on the left child
if(ptr->left){
inorder_save(ptr->left, file, index_file);
}
// Get the current position in the file
std::streampos pos = file.tellp();
// Write the position to the index file
index_file.write(reinterpret_cast<const char*>(&pos), sizeof(pos));
// Write the data of the current node to the file, followed by a null character
file<<ptr->data<<'\0';
// If there is a right child, recursively call the function on the right child
if(ptr->right){
inorder_save(ptr->right, file, index_file);
}
}
/*
this function saves the bst in a file with an index_file (inorder)
index_file contains for each row it contains the starting point of data in the saved file
think of computer organization how stack pointer works with stack
data is pushed and the stack pointer is pointing at the new data
so for row one it contains 0 for row 2 it contains sizeof(data1)
for row3 it contains sizeof(data1+data2) and so on
if index file is removed or the name is altered (the .bin file )
data may not be loaded correctly or it would take O(NlogN) since it assumes that its not sorted
if the .bin doesn't exist
*/
template<typename DataType>
bool bst<DataType>::save(void) const {
if (root) {
string file_path;
string index_path ;
cout << "\nEnter file's path please make sure the file is created\n";
cout << "\n>>";
getline(cin, file_path);
if (openFileForWriting(file_path)) {
ofstream file(file_path, std::ios::trunc); // Open file in write mode, which clears it
index_path.append(file_path).append(".bin");
ofstream index_file(index_path, std::ios::binary);
inorder_save(root, file, index_file);
cout << "\nSaved!\n";
file.close(); // Close the file when done
index_file.close() ;
return 1;
}
return 0;
}
return 0 ;
}
//if user wants to load from a file and bst contains data
//it promts the user to to save the tree if needed before it gets deleted
template<typename DataType>
void bst<DataType>::del_to_load(void){
if(root){
char choice ;
cout<<"\nCurrent tree contains data do you want to save it first?(y/n)\n";
cin>>choice;
if(choice!='n'){
cin.ignore();
save();
}
del_tree(root) ;
size=0 ;
root= NULL;
traverser =NULL;
cin.ignore();
}
}
//load data from a file where data isn't sorted
//either spaced by ' ' or line by line
//if your custom >> operator works correctly this functions loads unsorted data
//successfully
template<typename DataType>
bool bst<DataType>::load_unorder(ifstream& file) {
if (file.is_open()) { //if file exists
DataType data;
char ch;
while (file.get(ch)) {
if (!file.eof()) {
if (ch == '\0') continue; // Skip null characters
file.putback(ch); // Put the character back into the stream
}
if (file >> data) {
insert(data);
}
}
file.close();
return true;
}
return false;
}
//load data that's previously!!!! saved by the bst
//if data is sorted and the index_file is altered this function won't be used
//correctly
template<typename DataType>
bool bst<DataType>::load_inorder(ifstream&file,ifstream&index_file) {
// Get the size of the index_file
index_file.seekg(0, std::ios::end);
std::streamoff index_size = index_file.tellg();
index_file.seekg(0, std::ios::beg);
// Calculate the number of entries in the index_file
long long count = index_size / sizeof(std::streampos);
root = fill_sorted(file, index_file, 0, count-1, root);
traverser = root;
size = count ;
file.close();
index_file.close();
return 1;
}
/*
this function has 2 features one is loading from a sorted file which takes O(N)
and the other is loading from unordered file which takes O(NlogN)
if the .bin file (index_file) doesn't exist then it loads the data into a vector
and then loads it in O(N)
*/
template<typename DataType>
bool bst<DataType>::load(void) {
bool logic =0;
del_to_load();
string file_path;
string index_path ;
cout << "\nEnter file's path please make sure the file is created\n";
cout << ">>";
getline(cin, file_path);
ifstream file(file_path); // Open file in read mode
//note how index file is same name as file_path
//with the .bin added since its a binary file
index_path.append(file_path).append(".bin");
ifstream index_file(index_path, std::ios::binary);
if(file.is_open() && index_file.is_open()) {
//if both files exist it loads in O(N)
load_inorder(file,index_file) ;
logic= 1;
}
else if(file.is_open()){
//if no index file then this is random data or unsorted
vector<DataType>vec;
if(is_sorted(file)){
DataType d ;
file.clear();
file.seekg(0);
if (file.is_open()) { //if file exists
DataType data;
vector<DataType>vec;
char ch;
while (file.get(ch)) {
if (ch == '\0') continue; // Skip null characters
file.putback(ch); // Put the character back into the stream
if (file >> data) {
vec.push_back(data);
}
}