Click here to Skip to main content
Licence 
First Posted 1 Dec 2003
Views 78,412
Bookmarked 14 times

Simple STL tree

By | 1 Dec 2003 | Article
Simple way to create tree structure using basic STL classes

Introduction

One day I was searching for a easy and simple tree implementation using STL. I didn't find anything useful and invented a very simple way to do this.

Background (optional)

Basic idea is to use a standard tree structure:

// Simple tree

class CTreeItem
{
   // Tree Item properties
   ...
   //
   vector<CTreeItem*> children;
};

We can expand simple tree definition using templates to:

// Simple tree
#include <vector>

// Template definition

template<class CItem> class CTree
{
public:
   // Tree Item properties
   CItem item;
   
   // Children items
   std::vector<CTree*> children;
};

Using the code

Using this simple template we can create tree structures and operate with them easily:

// Example

class Location
{
   public:
      std::string name;
      long code;
};

// Create tree items

CTree<Location> Country, Washington, NewYork;

Country.item.name = "USA";
Country.item.code = 1;

Washington.item.name = "Washington";
Washington.item.code = 2;

NewYork.item.name = "New York";
NewYork.item.code = 1;

Country.children.push_back(&Washington);
Country.children.push_back(&NewYork);

That's it. This class can be extended to destroy all children objects during parent destruction.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Pavel Molchanov



Dominican Republic Dominican Republic

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYup Just Another NOOB Pinmemberxox_c0bra_xox15:16 12 Feb '11  
GeneralTraversing the tree and adding new children Pinmemberrobocupper18:49 5 Aug '08  
GeneralRe: Traversing the tree and adding new children Pinmemberrobocupper20:52 5 Aug '08  
Generalbad programming style PinsussMeister Eder und sein Pumuckl4:51 12 Sep '04  
GeneralRe: bad programming style Pinmemberzirani1:47 28 Jun '06  
GeneralThank you PinsussAnonymous14:37 7 Jun '04  
GeneralRe: Thank you PinsussAnonymous18:47 16 Jun '04  
GeneralRe: Thank you PinsussAnonymous2:59 10 Oct '05  
being a noob concerning to STL i'ts very useful to understand it.
GeneralCould be of value. PinmemberWREY10:01 3 Dec '03  
GeneralRe: Could be of value. PinmemberPavel Molchanov13:04 3 Dec '03  
GeneralRe: Could be of value. PinmemberWREY19:25 3 Dec '03  
GeneralRe: Could be of value. PinmemberPavel Molchanov5:34 4 Dec '03  
GeneralRe: Could be of value. PinsussAnonymous22:38 5 Mar '04  
GeneralRe: Could be of value. PinsussAnonymous22:39 5 Mar '04  
GeneralWorthless Pinmemberdog_spawn11:59 2 Dec '03  
GeneralRe: Worthless PinmemberPavel Molchanov13:46 3 Dec '03  
GeneralRe: Worthless Pinmemberdog_spawn3:41 4 Dec '03  
GeneralRe: Worthless PinsussAnonymous23:54 12 Apr '05  
GeneralRe: Worthless Pinmembersinukus11:12 9 Dec '03  
GeneralRe: Worthless PinmemberXML_LYP3:26 23 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 2 Dec 2003
Article Copyright 2003 by Pavel Molchanov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid