Click here to Skip to main content
6,594,432 members and growing! (16,243 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » STL » General     Intermediate

Simple STL tree

By Pavel Molchanov

Simple way to create tree structure using basic STL classes
VC6, VC7, VC7.1Win2K, WinXP, Win2003, Visual Studio, STL, Dev
Posted:1 Dec 2003
Views:64,491
Bookmarked:12 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
42 votes for this article.
Popularity: 2.19 Rating: 1.35 out of 5
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

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


Member

Location: Dominican Republic Dominican Republic

Other popular STL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Dec 2003
Editor:
Copyright 2003 by Pavel Molchanov
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project