Complementary task for topic: 12

M Nemeth · 2023-08-29 15:21:04.635218'

Binary trees: Directory tree

Binary trees: Directory tree

Implement a simple file system directory manager using a binary tree data structure. The directory manager should allow users to create, delete, and search for directories and files in the file system. Each node in the binary tree will represent a directory, and its left and right children will represent its subdirectories or files.

Hint: struct Node {
char name[100];
int isDirectory;
struct Node* left;
struct Node* right;
};
The name field stores the name of the directory or file. The isDirectory field is a flag to indicate whether the node represents a directory (1) or a file (0). The left and right pointers are used to link the node to its left and right subdirectories, respectively.

Solution
// Function to create a new node with the given name and type (directory or file)
struct Node* createNode(const char* name, int isDirectory) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    strcpy(newNode->name, name);
    newNode->isDirectory = isDirectory;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

// Function to insert a new directory or file into the binary tree
struct Node* insertNode(struct Node* root, const char* name, int isDirectory) {
    if (root == NULL) {
        return createNode(name, isDirectory);
    }

    int compareResult = strcmp(name, root->name);

    if (compareResult < 0) {
        root->left = insertNode(root->left, name, isDirectory);
    } else if (compareResult > 0) {
        root->right = insertNode(root->right, name, isDirectory);
    } else {
        // If the name already exists, display an error message and return the root unchanged.
        printf("Error: The name '%s' already exists.\n", name);
    }

    return root;
}

// Function to search for a directory or file in the binary tree
struct Node* search(struct Node* root, const char* name) {
    if (root == NULL || strcmp(name, root->name) == 0) {
        return root;
    }

    int compareResult = strcmp(name, root->name);

    if (compareResult < 0) {
        return search(root->left, name);
    } else {
        return search(root->right, name);
    }
}

// Function to destroy the entire binary tree and free memory
void destroyTree(struct Node* root) {
    if (root != NULL) {
        destroyTree(root->left);
        destroyTree(root->right);
        free(root);
    }
}


int main() {
    struct Node* root = NULL;

    // Create directories and files in the file system
    root = insertNode(root, "documents", 1);
    root = insertNode(root, "photos", 1);
    root = insertNode(root, "music", 1);

    insertNode(root, "report.pdf", 0);
    insertNode(root, "presentation.ppt", 0);
    insertNode(root, "summer_vacation.jpg", 0);
    insertNode(root, "birthday_party.jpg", 0);
    insertNode(root, "song1.mp3", 0);
    insertNode(root, "song2.mp3", 0);

    // Search for a directory or file in the file system
    const char* searchName = "photos";
    struct Node* foundNode = search(root, searchName);
    if (foundNode != NULL) {
        printf("'%s' found in the file system.\n", searchName);
    } else {
        printf("'%s' not found in the file system.\n", searchName);
    }

    // Destroy the binary tree and free memory
    destroyTree(root);

    return 0;
}



Explanation
    We define a structure struct Node to represent a node in the binary tree. Each node has a name field to store the name of the directory or file, an isDirectory field to indicate whether it is a directory (1) or a file (0), and pointers left and right to its left and right children, respectively.

    The createNode function creates a new node with the given name and type (directory or file).

    The insertNode function inserts a new directory or file into the binary tree. It traverses the tree to find the appropriate position for insertion based on the name and maintains the binary search tree property.

    The search function searches for a directory or file in the binary tree based on its name. It recursively traverses the tree, comparing the names of nodes to find the target node.

    In the main function, we demonstrate the usage of the directory manager. We create a file system by inserting directories and files into the binary tree. Then, we search for a specific directory (photos) to check its existence in the file system.

    Finally, we destroy the binary tree to free the memory used by its nodes.

In real-life scenarios, the binary tree-based file system directory manager can be extended to include additional functionalities such as deleting directories or files, updating file metadata, displaying the directory structure, and handling more complex file system operations. The concept of binary trees is widely used in file systems to efficiently organize and manage directory structures and facilitate fast file searching and retrieval.

< < previous