View Single Post
  #8 (permalink)  
Old June 22nd, 2006, 11:14 PM
Jalapeno Jalapeno is offline
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote