Chapter 2: Trees, Heaps, and Graphs
Activity 4: Create a Data Structure for a Filesystem
In this activity, we will create a data structure using N-ary tree for a file system. Follow these steps to complete the activity:
- First, let's include the required headers:
#include <iostream> #include <vector> #include <algorithm>
- Now, let's write a node to store the data of a directory/file:
struct n_ary_node { std::string name; bool is_dir; std::vector<n_ary_node*> children; };
- Now, let's wrap this node in a tree structure for a good interface, and also add a static member so that we can store the current directory:
struct file_system { using node = n_ary_node; using node_ptr = node*; private: node_ptr root; node_ptr cwd;
- Now, let's add a constructor so that we can...