Click here to Skip to main content
13,347,168 members (56,725 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 28 May 2002

An STL like Tree Class

, 28 May 2002
Rate this:
Please Sign up or sign in to vote.
A multi-node Tree class by using map and vector

Sample Image


For more than a year and a half I have had to use some kind of multi-node tree to do something. However, all I found from the Internet are binary trees or equivalents. I therefore decided to write my own. Because I had to finish it pretty fast, I spent only two days for it, so it may be buggy.

In the project, it has three classes working like in STL container-iterator style.
template <class Key, class T> class Tree
template <class Key, class T> class Tree_iterator
template <class Key, class T> class TreeNode

A tree contains many tree nodes, while tree nodes contain data. Tree nodes of a tree are called children. In the design each node is assigned a level and the root node is of level 1. Tree_iterator is for navigation within the tree. Two tree-walk styles are defined. One walks down from the root node, while the other one use post-style to walk through all children of the tree. For each walk action, a

is returned, so the user can use the iterator to reference the tree node and get the data.

I wrapped all the classes under the namespace Tiffany. For example, declaration of the tree with key type of wstring, and node data type of string, and add two levels of element is like:

Tiffany::Tree<wstring, string> x(L"Node 1", "Root");

px = x.AddChild(L"Key1","A");
px1 = x.AddChild(px, L"Key2", "1");

For a walk down action, you can limit it to a part of a tree ("sub-tree") by using the function SetSubTreePivot(px), and you can set the pivot to point back to the root node by calling SetWalkDownRootPivot. Try it out!. You can even delete part of the tree by calling DelSubTree().

Unfortunately I didn't have much time to prepare this document, but it should be obvious how to the classes.


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

Jack Hui
Web Developer
Hong Kong Hong Kong
I'm a guy situated in Hong Kong with some knowledges in Java, VC++, C#, database, client-server, distributed, and mutithreaded computing and so on. I've been working in various companies as engineer, consultant, programmer.

Lately I was mainly working in banking & financial industries. Personally, I'm working on a trading application on my own now.

You may also be interested in...

Comments and Discussions

Generalremove node Pin
filippov.anton25-Sep-07 2:35
memberfilippov.anton25-Sep-07 2:35 
QuestionHow to delete a leaf-node? Pin
caijianshan17-Oct-06 22:46
membercaijianshan17-Oct-06 22:46 
GeneralInteresting Solution Pin
Dave_H13-Aug-04 13:24
memberDave_H13-Aug-04 13:24 
GeneralRe: Interesting Solution Pin
Anonymous13-Aug-04 19:12
sussAnonymous13-Aug-04 19:12 
GeneralRe: Interesting Solution Pin
Dave_H14-Aug-04 1:48
memberDave_H14-Aug-04 1:48 
GeneralRe: Interesting Solution Pin
rajas1-Sep-04 7:59
memberrajas1-Sep-04 7:59 
GeneralRe: Interesting Solution Pin
Dave_H1-Sep-04 10:58
memberDave_H1-Sep-04 10:58 
Questionnot work in 2003? Pin
lad6-Aug-04 0:44
memberlad6-Aug-04 0:44 
AnswerRe: not work in 2003? Pin
Jovan Cakic24-Oct-04 12:34
memberJovan Cakic24-Oct-04 12:34 
GeneralRe: not work in 2003? Pin
Scott McCain1-Jun-06 17:50
memberScott McCain1-Jun-06 17:50 
I'm working on an unrelated project but happened to get this article in a google search. I just wanted to say thanks to the poster above for that valuable information. I have been banging my head for 6 hours to get some old VC6 code to compile in Visual Studio 8. You saved me a sleepless night. Thanks! Cool | :cool:

(Who would've thought that Microsoft would actually write an ISO compliant compiler! There is a lot of broken code out there thanks to their crappy implementations in the past)
Generaliterator AddChild (const Key parent, const Key nm, const T x) Pin
MaRuiYun15-Jan-04 19:15
memberMaRuiYun15-Jan-04 19:15 
GeneralRe: Sample Test Pin
MaRuiYun15-Jan-04 19:21
memberMaRuiYun15-Jan-04 19:21 
Questionmanaged C++? Pin
K2O328-Jul-03 12:56
memberK2O328-Jul-03 12:56 
QuestionDo you have a C# version of this? Pin
fetcher27-Jul-03 8:08
memberfetcher27-Jul-03 8:08 
AnswerRe: Do you have a C# version of this? Pin
Jack Hui27-Jul-03 8:30
memberJack Hui27-Jul-03 8:30 
GeneralMoving nodes Pin
suzz..8-Aug-02 21:39
susssuzz..8-Aug-02 21:39 
GeneralRe: Moving nodes Pin
Anonymous9-Aug-02 21:29
sussAnonymous9-Aug-02 21:29 
GeneralRe: Moving nodes Pin
Trevor Lewis29-Jul-03 21:52
memberTrevor Lewis29-Jul-03 21:52 
Questionwhy take chances with bugs? Pin
kokie29-May-02 19:55
memberkokie29-May-02 19:55 
AnswerRe: why take chances with bugs? Pin
Anonymous31-May-02 18:26
memberAnonymous31-May-02 18:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 29 May 2002
Article Copyright 2002 by Jack Hui
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid