insert string array elements in a tree

Hi Guys,

I am creating one tree. i am having one string array

char * arr[] = {"a", "a/b", "a/c", "a/d","a/b/e","a/b/f","a/b/g","a/c/h","a/c/i", "a/c/h/j",a/c/h/k");

tree should be like...

a is having b,c,d as children.
b is having e,f,g children.
c is having h,i children.
again h is having j and k children.

we have to write logic for inserting nodes to tree. arr string have to seprate.that we can do it with token operation- strtok().

tree can any no of nodes, and node can any no of children.
I'd do it like this:
The string S is passed to the tree, which in turn passes it to the root' function F():
Copy S from its start to before the first '/', or to its end, into S2. If a child named S2 doesn't exist, create it. If S doesn't contain a '/', return. Copy S from after the first '/' to the end into S3, and pass S3 to (the child named S2)->F(). Free the memory for S2 and S3 and return.

Of course, the nodes should have a map using char * as keys, and pointers to nodes as values.
Alternatively, they could have a char * as the name and a set to store its children.
Last edited on
can you provide code for this
Probably not. The purpose of schooling is to learn to take a thought and turn it into code. The reference documentation gives very good example code you can study:
http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
Topic archived. No new replies allowed.