Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define endl '\n';
- using namespace std;
- struct bst{
- string nome;
- bst* left;
- bst* right;
- bst* up;
- bst* down;
- bool isDirectory;
- void setRoot(){
- this->nome = "/";
- this->up = nullptr;
- this->down = nullptr;
- this->left = nullptr;
- this->right = nullptr;
- isDirectory = true;
- }
- };
- bst *touch(bst *root, bst* up, string nome){
- if(root == nullptr){
- bst *n = new bst;
- n->nome = nome;
- n->left = nullptr;
- n->right = nullptr;
- n->down = nullptr;
- n->up = up;
- n->isDirectory = false;
- return n;
- }else if(nome < root->nome){
- root->left = touch(root->left, up,nome);
- return root;
- }else{
- root->right = touch(root->right,up,nome);
- return root;
- }
- }
- bst *mkdir(bst *root, bst* up, string nome){
- if(root == nullptr){
- bst *n = new bst;
- n->nome = nome;
- n->left = nullptr;
- n->right = nullptr;
- n->down = nullptr;
- n->up = up;
- n->isDirectory = true;
- return n;
- }else if(nome < root->nome){
- root->left = mkdir(root->left, up,nome);
- return root;
- }else{
- root->right = mkdir(root->right,up,nome);
- return root;
- }
- }
- string pwd(bst *root){
- return root->nome;
- }
- string pre_order(bst* root, string indentacao){
- if(root == nullptr || root->up == nullptr){
- return "";
- }
- cout << indentacao << root->nome << endl;
- if(root->isDirectory ){
- pre_order(root->down, indentacao+" ");
- }
- pre_order(root->left,indentacao);
- pre_order(root->right,indentacao);
- }
- string in_order(bst* root, string indentacao){
- if(root == nullptr || root->up == nullptr){
- return "";
- }
- in_order(root->left, indentacao);
- cout << indentacao << root->nome << endl;
- if(root->isDirectory ){
- indentacao += " ";
- in_order(root->down, indentacao);
- indentacao = "";
- }
- in_order(root->right,indentacao);
- }
- string pos_order(bst* root, string indentacao){
- if(root == nullptr || root->up == nullptr){
- return "";
- }
- pos_order(root->left,indentacao);
- pos_order(root->right,indentacao);
- cout << indentacao << root->nome << endl;
- if(root->isDirectory ){
- pos_order(root->down, indentacao+" ");
- }
- }
- string ls(bst* root){
- if(root == nullptr || root->up == nullptr){
- return "";
- }
- ls(root->left);
- cout << root->nome << endl;
- ls(root->right);
- }
- int busca_previa(bst *root, string nome){
- if(root == nullptr){
- return 0;
- }else if(nome == root->nome){
- if(root->isDirectory){
- return 1;
- }
- return 2;
- }else if(nome < root->nome){
- return busca_previa(root->left, nome);
- }else{
- return busca_previa(root->right,nome);
- }
- }
- bst* cd(bst* root, string nome){
- if(root == nullptr){
- return nullptr ;
- }else if(nome == root->nome){
- return root;
- }else if(nome < root->nome){
- return cd(root->left, nome);
- }else{
- return cd(root->right,nome);
- }
- }
- struct bst_min{
- bst* root;
- string nome;
- };
- bst_min* bst_delete_min(bst* root){
- if(root->left == nullptr){
- bst_min* opa = new bst_min;
- opa->nome = root->nome;
- opa->root = root->right;
- delete root;
- return opa;
- }else{
- bst_min* rm = bst_delete_min(root -> left);
- root->left = rm->root;
- root->nome = rm->nome;
- return rm;
- }
- }
- bst* rm(bst* root, string nome){
- if(root == nullptr){
- cout << "No such file or directory" << endl;
- return nullptr;
- }else if(nome < root->nome){
- root->left = rm(root->left,nome);
- return root;
- }else if(nome > root->nome){
- root->right = rm(root->right,nome);
- return root;
- }else{
- if(root->left == nullptr){
- bst* r = root->right;
- delete root;
- return r;
- }else if(root->right == nullptr){
- bst* l = root->left;
- delete root;
- return l;
- }else{
- bst_min* rm = bst_delete_min(root -> right);
- root->right = rm->root;
- root->nome = rm->nome;
- return root;
- }
- }
- }
- int main(){
- bst *pimba = new bst;
- pimba->setRoot();
- bst* current = pimba;
- // string indentacao = "";
- //
- // cout << pwd(current) << endl;
- // current->down = mkdir(current->down,current,"Q");
- // current->down = mkdir(current->down,current,"A");
- // current->down = mkdir(current->down,current,"Z");
- // current = cd(current->down, "A");
- // current->down = touch(current->down,current, "W");
- // current->down = mkdir(current->down,current, "S");
- // current->down = touch(current->down,current, "X");
- // current = cd(current->down, "S");
- // current->down = touch(current->down,current, "1");
- // current->down = touch(current->down,current, "0");
- // current->down = touch(current->down,current, "2");
- // current->down = touch(current->down,current, "9");
- // current = current->up;
- // if(busca_previa(current->down,"P") == 0){
- // cout << "No such file or directory" << endl;
- // }else if(busca_previa(current->down, "P") == 2){
- // cout << "Not a directory" << endl;
- // }else if(busca_previa(current->down,"P") == 1){
- // current = cd(current->down,"P");
- // }
- // ls(current->down);
- // if(busca_previa(current->down,"X") != 0){
- // cout << "File exists" << endl;
- // }else{
- // current->down = mkdir(current->down,current,"X");
- // }
- // current = current->up;
- // current = cd(current->down, "Q");
- // current->down = touch(current->down,current, "E");
- // current->down = touch(current->down,current, "D");
- // current = current->up;
- // current = cd(current->down, "Z");
- // current->down = touch(current->down, current, "X");
- // current = current->up;
- // in_order(current->down,indentacao);
- string comando;
- string d;
- while(cin >> comando){
- string indentacao;
- if(comando == "cd"){
- cin >> d;
- if(d == ".."){
- if(current->up != nullptr){
- current = current->up;
- }
- }else{
- if(busca_previa(current->down,d) == 0){
- cout << "No such file or directory" << endl;
- }else if(busca_previa(current->down, d) == 2){
- cout << "Not a directory" << endl;
- }else if(busca_previa(current->down,d) == 1){
- current = cd(current->down,d);
- }
- }
- }else if(comando == "ls"){
- ls(current->down);
- }else if(comando == "touch"){
- string f;
- cin >> f;
- if(busca_previa(current->down,f) != 0){
- cout << "File exists" << endl;
- }else{
- current->down = touch(current->down,current,f);
- }
- }else if(comando == "mkdir"){
- string nD;
- cin >> nD;
- if(busca_previa(current->down,nD) != 0){
- cout << "File exists" << endl;
- }else{
- current->down = mkdir(current->down,current,nD);
- }
- }else if(comando == "pwd"){
- cout << pwd(current) << endl;
- }else if(comando == "tree"){
- string arg;
- cin >> arg;
- if(arg == "--pre-order"){
- pre_order(current->down,indentacao);
- }else if(arg == "--in-order"){
- in_order(current->down,indentacao);
- }else if(arg == "--post-order"){
- pos_order(current->down,indentacao);
- }else{
- cout << "Illegal option" << endl;
- }
- }else if(comando == "rm"){
- string f;
- cin >> f;
- current->down = rm(current,f);
- }else{
- cout << "Command not found" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement