Click here to Skip to main content
13,000,324 members (58,030 online)
Click here to Skip to main content


51 bookmarked
Posted 12 Jan 2002

Tree data class for C++

, 12 Jan 2002
Simple class to represent tree data structures in C++

#pragma warning(disable:4786)
#include "vector"
using namespace std;
#include "datakeeper.h"

/* template <class Type> class NodeVector : private vector<Type> {
	Type &operator[](int index) { return at(index);};
	const Type &operator[](int index) const { return at(index);};
	__declspec( property( get=get_Count) ) int Count;
	int get_Count() { return size();};
}; */
template <class Type,class DataType2=NodeData<Type> > class Tree;
template <class Type> class NodeData {
	typedef Ref<NodeData> NodeRef;
	NodeData(const Type &Data) : tData(Data){};
	NodeData(Type *pData) : tData(pData) {};
	NodeData() : Parent((NodeData*)NULL) {};
	virtual ~NodeData() {};
	Ref<Type> tData;
	std::vector<NodeRef> vChilds;
	Ref<NodeData> Parent;
	friend class Tree<Type>;
	friend class Ref<NodeData>;
	friend _Datakeeper::Data<NodeData>;

template <class T> class CScrollImpl;
template <class T, class TBase = CWindow, class TWinTraits = CContro1lWinTraits> class CScrollWindowImpl;
template <class T> class CMapScrollImpl;
template <class T, class TBase = CWindow, class TWinTraits = CControlWinTraits> class CMapScrollWindowImpl;


template <class Type,class DataType2=NodeData<Type> > class Tree : private Ref<DataType2> {
	typedef Ref<DataType2> NodeBase;
	typedef Tree<Type> Node;
//	Node node;
	// vector<Node> &Nodes() {	return vChilds;	};
	Node Parent() { if(!NodeBase::get_Data().IsNull()) return NodeBase::get_Data().Parent; return *this;};
	operator Type&() { return (NodeBase::get_Data().tData.get_Data()); };
	Type *operator->() {return &(NodeBase::get_Data().tData.get_Data());};
	__declspec( property( get=get_Data) ) Ref<Type> Data;
	Ref<Type> get_Data() { return NodeBase::get_Data().tData;};

	bool IsLeaf() { return NodeBase::get_Data().vChilds.size()==0;};
	bool IsNode() { return NodeBase::get_Data()vChilds.size()>0;};
	bool IsRoot() { return NodeBase::get_Data().Parent.IsNull();};
	bool operator ==(const Node &rhs) { return *(NodeBase*)this==(NodeBase &)rhs;};

	__declspec( property( get=get_Nodes) ) Node Nodes[];
	Node get_Nodes(int nIndex) { return (Node)NodeBase::get_Data().vChilds[nIndex]; };
	__declspec( property( get=get_Count) ) int Count;
	int get_Count() { return NodeBase::get_Data().vChilds.size();};
	__declspec( property( get=get_Key) ) unsigned long Key;
	unsigned long get_Key() { return NodeBase::Key();};

	Node AddNode(const Type &t) { Node n(t); ((NodeBase &)n).get_Data().Parent=(*this); NodeBase::get_Data().vChilds.push_back(n); return n;};
	void Delete() {
		if(!NodeBase::get_Data().Parent->IsNull()) NodeBase::get_Data().Parent->DeleteNode(*this);
	void DeleteNode(int nIndex) {
	void DeleteNode(Node &node) {
		vector<NodeBase>::iterator = find(NodeBase::get_Data().vChilds.begin(),NodeBase::get_Data().vChilds.end(),node);
		if(iterator!=NodeBase::get_Data().vChilds.end()) {
	Tree(const Type &Data) : NodeBase(Data) {};
	Tree(Type *Data) : NodeBase(Data) {};
	Tree() {};
	Tree(const Tree<Type> &rhs) : NodeBase(rhs) {};
	Tree(const NodeBase &rhs) : NodeBase(rhs) {};
	Tree(KEY key) : NodeBase(key) {};
	virtual ~Tree() 
	{	// if RefCount==ChildsCount+1, now is the time for release this (i.e. remove all refs to parent from childs

	void ReleaseNode() {
		if(NodeBase::IsNull()) return;
		ULONG nRefs=NodeBase::GetRefCount();
		int nChilds=NodeBase::get_Data().vChilds.size();
		if(nRefs==nChilds+1) {
			std::vector<NodeBase> &vChilds=NodeBase::get_Data().vChilds;
			for(int n=0;n<vChilds.size();n++) {
				((Node &)vChilds[n]).ReleaseNode();

// typedef Tree<t> Node<t>
// usage:


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

Alexander Kovachev
Web Developer
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 13 Jan 2002
Article Copyright 2002 by Alexander Kovachev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid