Question: heree is the code #ifndef ASSIGNMENT_VERSION1_DATA_STRUCT_H #define ASSIGNMENT_VERSION1_DATA_STRUCT_H #endif //ASSIGNMENT_VERSION1_DATA_STRUCT_H #include #include #include #include // for setw and setfill stream manipulators #include // for invalid_argument
heree is the code
#ifndef ASSIGNMENT_VERSION1_DATA_STRUCT_H #define ASSIGNMENT_VERSION1_DATA_STRUCT_H
#endif //ASSIGNMENT_VERSION1_DATA_STRUCT_H
#include #include #include #include // for setw and setfill stream manipulators #include // for invalid_argument exception class #include // for ostringstream class #include #include #include
using namespace std;
class data_struct {
private: // data members
int id, size_file, parent_id; char folder; string file_folder_name;
public:
data_struct *left; data_struct *right;
// constructor with 4 parameters data_struct(int i, string nm, int l, char m, int n) {
id = i; file_folder_name = nm; size_file = l; folder = m; parent_id = n;
left = NULL; right = NULL; //initialize a pointer variable when that pointer variable isnt assigned any valid memory address yet.
}
// function which will be used to display void display() { cout << "[" << id << "][" << file_folder_name << "][" << size_file << "][" << folder << "][" << parent_id << "]" << endl; // inorder(head); }
vector file;// vector of string which stores the file data_struct *head; // pointer of type data_struct which ofstream fout;// decraling an output file stream
data_struct() {
head = NULL; int id, size_file, parent_id; string file_folder_name; char folder;
ifstream fin("file.txt");// reading from file if (fin) { while (!fin.eof()) { string line; fin >> line; line = line.substr(1, line.length() - 2); int y = line.find("]["); stringstream ss(line.substr(0, y)); ss >> id; line.erase(0, y + 2); y = line.find("]["); file_folder_name = line.substr(0, y); line.erase(0, y + 2); y = line.find("]["); stringstream ss2(line.substr(0, y)); ss2 >> size_file; line.erase(0, y + 2); y = line.find("]["); stringstream ss3(line.substr(0, y)); ss3 >> parent_id; file.push_back(new data_struct(id, file_folder_name, size_file, folder, parent_id));
}
// now constructing BST loadTree(); } else { cout << "no prior saved file!" << endl; } }
// constructing tree
void loadTree() {
for (int i = 0; i < file.size(); i++) if (file[i]->id == 0) { head = file[i]; break; } for(int i=0;i if(file[i]->id!=0) insert(head, file[i]); } // }
}
// for updation in file void finorder(data_struct *node){ if (node){ finorder(node->left); fout<<"["right); } }
//inorder traversal of tree void inorder(data_struct *node){ if(node){ inorder(node->left); node->display(); inorder(node->right); } }
//void display(){ // inorder(head); //}
//updations void updateFile(){ fout.open("file.txt"); finorder(head); fout.close(); } data_struct* insert(data_struct * node, data_struct *key){ if(!node) return key; if(key->idid) node->left = insert(node->left,key); if(key->id>node->id) node->right = insert(node->right,key); return node; }
void search(data_struct * node, int id){ if (!node){ cout << "element not found!! "; return; } if (node->id == id){ node ->display(); return; } if (idid) search (node ->left,id); if(id>node->id) search(node->right,id); } data_struct *minNode(data_struct* t){ while(t->left){ t= t->left; } return t; } data_struct* Delete(int id, data_struct *node){ if (!node) return node; if(id < node->id) node->left = Delete(id, node->left); else if (id>node->id) node-> right = Delete(id,node->right); else{ if (!node->left){ data_struct *temp =node->right; free(node); return temp; } data_struct * temp =minNode(node->right); node->id = temp-> id; node ->file_folder_name = temp ->file_folder_name; node->size_file = temp->size_file; node->folder = temp->folder; node->parent_id = temp->parent_id; node->right = Delete(temp->id,node->right); } return node; } void menu(){
cout<<"| M E N U |"< cout<<"| 1 | Add |"< cout<<"| 2 | Delete |"< cout<<"| 3 | List |"< cout<<"| 4 | Search |"< cout<<"| 5 | Exit |"< cout<<"|_____|______| "<
while(true){
int choice; cout<<"enter your choice: "; cin>> choice; if(choice ==1){ int id, size_file,parent_id; string file_folder_name,line; char folder; cin>>line; line=line.substr(1,line.length()-2); int y = line.find("]["); stringstream ss(line.substr(0,y)); ss>>id; line.erase(0,y+2); y= line.find("]["); stringstream ss2(line.substr(0,y)); ss2>>size_file; line.erase(0,y+2); y=line.find("]["); folder=line[0]; line.erase(0,y+2); y=line.find("]["); stringstream ss3(line.substr(0,y)); ss3>>parent_id; head=insert(head,new data_struct(id,file_folder_name,size_file,folder,parent_id)); updateFile(); } if (choice ==3){ display(); } if (choice ==4){ int id; cout<<"enter the id:"; cin>>id; search(head,id); } if(choice ==5) { break; } cout< } } };
#include #include #include #include "data_struct.h" #include // for setw and setfill stream manipulators #include // for invalid_argument exception class #include // for ostringdtream class #include
using namespace std;
int main() {
data_struct().menu();
return 0; }
the scenario of this program is to first read data from a text file, the data in the text file is about files and foldes stored in a storage device.here is a sample of the data from a text file that must be included:
[0][Root][0][T][0] [1][file.txt][45643][F][0] [2][folder1][0][T][0] [3][file2.txt][7346][F][2]
this information should be represented in an adequate data structure. the data structure should then be loaded into a binary search tree. the program must provide listing searching deleting and adding files or folders through the binary search tree. finally the information must be exported in a text file. the programm must be in c++ object oriented as you can see i have implented the binary search tree but it is not functioning well so i will be very thankful if you could help me out.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
