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

Simple STL tree

Simple way to create tree structure using basic STL classes
34 votes, 81.0%
1
2 votes, 4.8%
2
4 votes, 9.5%
3
1 vote, 2.4%
4
1 vote, 2.4%
5
1.04/5 - 42 votes
6 removed
μ 1.35, σa 1.64 [?]

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_xox16:16 12 Feb '11  
GeneralTraversing the tree and adding new children Pinmemberrobocupper19:49 5 Aug '08  
GeneralRe: Traversing the tree and adding new children Pinmemberrobocupper21:52 5 Aug '08  
Generalbad programming style PinsussMeister Eder und sein Pumuckl5:51 12 Sep '04  
GeneralRe: bad programming style Pinmemberzirani2:47 28 Jun '06  
GeneralThank you PinsussAnonymous15:37 7 Jun '04  
GeneralRe: Thank you PinsussAnonymous19:47 16 Jun '04  
GeneralRe: Thank you PinsussAnonymous3:59 10 Oct '05  
GeneralCould be of value. PinmemberWREY11:01 3 Dec '03  
GeneralRe: Could be of value. PinmemberPavel Molchanov14:04 3 Dec '03  
GeneralRe: Could be of value. PinmemberWREY20:25 3 Dec '03  
GeneralRe: Could be of value. PinmemberPavel Molchanov6:34 4 Dec '03  
GeneralRe: Could be of value. PinsussAnonymous23:38 5 Mar '04  
GeneralRe: Could be of value. PinsussAnonymous23:39 5 Mar '04  
GeneralWorthless Pinmemberdog_spawn12:59 2 Dec '03  
GeneralRe: Worthless PinmemberPavel Molchanov14:46 3 Dec '03  
GeneralRe: Worthless Pinmemberdog_spawn4:41 4 Dec '03  
GeneralRe: Worthless PinsussAnonymous0:54 13 Apr '05  
GeneralRe: Worthless Pinmembersinukus12:12 9 Dec '03  
GeneralRe: Worthless PinmemberXML_LYP4: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
Web01 | 2.5.120210.1 | Last Updated 2 Dec 2003
Article Copyright 2003 by Pavel Molchanov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid