Group: comp.lang.c++
From: Jerry Coffin
Date: Sunday, February 17, 2008 5:08 PM
Subject: 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 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 help 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.