Discussion: Tree driving me nuts!
Afficher un message
Vieux 18/02/2008, 00h08   #10
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Tree driving me nuts!

In article <269b4ba8-b244-4dcb-a03f-
65d9d5504cba@i7g2000prf.googlegroups.com>, travis.bowers@gmail.com
says...
> So this is a driving me nuts. I need some more eyes on this. I have a
> written a Tree structure that mimics a Menu tree for a kiosk (no
> sorting, infinite children per node). Each node contains a pointer to
> its parent and a vector of children nodes. Here is a snippet of the
> pertinent bits driving me crazy.


Why not have each node literally contain a vector of child nodes?

struct node {
std::vector<node> children;

std::string name_;
node *parent_;

node(node *parent, std::string const &name)
: parent_(parent), name_(name)
{}
};

Of course, the code you posted isn't sufficient to figure out exactly
what a node should really support, but from your description, it sounds
like you have a problem with the memory management. That's fairly common
when manually managing dynamic memory, especially for a semi-complex
structure like an N-way tree. Eliminating the manual memory management
tends to keep the complexity under control, though until or unless
you describe the remainder of what you want the code to do, it's
impossible to say what else will be needed or where your problem may be
arising.

--
Later,
Jerry.

The universe is a figment of its own imagination.
  Réponse avec citation
 
Page generated in 0,05110 seconds with 9 queries