Click here to Skip to main content
Licence 
First Posted 28 May 2002
Views 83,536
Bookmarked 30 times

An STL like Tree Class

By | 28 May 2002 | Article
A multi-node Tree class by using map and vector

Sample Image

Introduction

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 tree_iterator 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.

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

Jack Hui

Web Developer

Hong Kong Hong Kong

Member

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.

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
Generalremove node Pinmemberfilippov.anton1:35 25 Sep '07  
QuestionHow to delete a leaf-node? Pinmembercaijianshan21:46 17 Oct '06  
GeneralInteresting Solution PinmemberDave_H12:24 13 Aug '04  
GeneralRe: Interesting Solution PinsussAnonymous18:12 13 Aug '04  
GeneralRe: Interesting Solution PinmemberDave_H0:48 14 Aug '04  
GeneralRe: Interesting Solution Pinmemberrajas6:59 1 Sep '04  
GeneralRe: Interesting Solution PinmemberDave_H9:58 1 Sep '04  
Questionnot work in VC.net 2003? Pinmemberlad23:44 5 Aug '04  
AnswerRe: not work in VC.net 2003? PinmemberJovan Cakic11:34 24 Oct '04  
GeneralRe: not work in VC.net 2003? PinmemberScott McCain16:50 1 Jun '06  
Generaliterator AddChild (const Key parent, const Key nm, const T x) PinmemberMaRuiYun18:15 15 Jan '04  
GeneralRe: Sample Test PinmemberMaRuiYun18:21 15 Jan '04  
Questionmanaged C++? PinmemberK2O311:56 28 Jul '03  
QuestionDo you have a C# version of this? Pinmemberfetcher7:08 27 Jul '03  
AnswerRe: Do you have a C# version of this? PinmemberJack Hui7:30 27 Jul '03  
GeneralMoving nodes Pinsusssuzz..20:39 8 Aug '02  
GeneralRe: Moving nodes PinsussAnonymous20:29 9 Aug '02  
GeneralRe: Moving nodes PinmemberTrevor Lewis20:52 29 Jul '03  
Questionwhy take chances with bugs? Pinmemberkokie18:55 29 May '02  
AnswerRe: why take chances with bugs? PinmemberAnonymous17:26 31 May '02  

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.120517.1 | Last Updated 29 May 2002
Article Copyright 2002 by Jack Hui
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid