How to build a folder tree structure?

S

shuisheng

Dear All,

I'd like to build a structure similar to the folder tree which

1. Has a root.
2. Nodes can be folder or files.
3. Folder can contain folders and files.
4. Name of folders and files under a folder can not be same.

Anybody can give me some hint?

I appreciate your help.

Shuisheng
 
V

Victor Bazarov

shuisheng said:
I'd like to build a structure similar to the folder tree which

1. Has a root.
2. Nodes can be folder or files.
3. Folder can contain folders and files.
4. Name of folders and files under a folder can not be same.

Anybody can give me some hint?

class FolderEntry
{
std::string name;
public:
FolderEntry(std::string const&);
};

class Folder : public FolderEntry
{
std::vector<FolderEntry*> entries; // can be empty
...
void fillEntries(); // scanning the file system
};

class File : public FolderEntry
{
...
};

What other hints do you expect? Scanning folders and filling in
the 'entries' for any 'Folder' is platform-specific.

V
 
P

PasalicZaharije

It is tree structure with parent and sibling hierarchy:

struct node
{
node* first_sibling;
node* first_child;
// other data (like it is file or folder, etc)
};

For tree:

root
app
test1
test2
src
frm
usr1
usr2
usr3

root will be first allocated node struct.
root->first_sibling will point to first node at same level (so it will be
NULL), root->first_child will point to 'app'. app node (let say anode)
will be like: anode->first_sibling point to src, anode->first_child point
to test1, and so ...

Most operation will be recursive:

void dump(node* n)
{
dump(n->first_sibling);
dump(n->first_child);

}
 
H

hhsaez

What you need here is a typical example of a Composite design patter.
As Victor explained, you'll need a base abstract class that handle
common operation (like a function that returns the size of the element)
and then derived classes for both files and folders (each class will
implement the size function by returning its actual size or the sum of
the sizes of each child).

Verifying that there are no duplicates in the same folder is trivial
here.

Rolf Magnus ha escrito:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top