The best and secure way to implement a tree is using STL MAP container:
map<Key, Element> Tree
say you want a state/city tree:
class statecity
{
string _state // redundant
string _city;
int _ areacode // assuming there's only one area code
public:
statecity(){
_state = ""; // init variables
_city = "";
_areacode = 0;
}
virtual ~statecity(){};
}
// create an empty container
map<string, statecity > StateCityTree;
// create a new instance of your class (node)
statecity * sc = new statecity;
sc->_state = "CT";
sc->_city = "Hartford";
sc->_areacode = 860;
// create you node ( entry) in your tree
StateCityTree["CT"] = *sc;
// cleanup
delete sc;
if you want to know the details dig up the STL header files.
Jalapeno
|