Click here to Skip to main content
12,244,489 members (47,519 online)
Click here to Skip to main content

Stats

168.6K views
6.5K downloads
110 bookmarked
Posted

Tree Container Library

, 22 Aug 2007 Zlib
A generic template class library for storing data in a tree-like structure.
TCL.chm
TCL.chm
basic_tree.inl
associative_tree.inl
descendant_iterator.inl
ordered_iterator.inl
multitree.inl
tree.inl
child_iterator.inl
unique_tree.inl
sequential_tree.inl
sequential_tree.inl
tree.inl
unique_tree.inl
associative_tree.inl
basic_tree.inl
descendant_iterator.inl
descendant_node_iterator.inl
multitree.inl
associative_tree.inl
basic_tree.inl
descendant_iterator.inl
descendant_node_iterator.inl
multitree.inl
sequential_tree.inl
tree.inl
unique_tree.inl
TCL.chm
descendant_iterator_tester.inl
modifying_algorithm_tester.inl
nonmodifying_algorithm_tester.inl
ordered_iterator_checker.inl
sequential_tree_tester.inl
stl_algorithm_tester.inl
unique_tree_tester.inl
associative_tree_tester.inl
basic_tree_tester.inl
child_iterator_tester.inl
child_sequential_iterator_tester.inl
polymorphic_example_results.jpg
sequential_tree_example_diagram.jpg
sequential_tree_example_explanation.rtf
sequential_tree_example_results.jpg
unique_tree_example_diagram.jpg
unique_tree_example_explation.rtf
unique_tree_example_results.jpg
generic_example_diagram.jpg
generic_example_explanation.rtf
generic_example_results.jpg
polymorphic_example_diagram.jpg
polymorphic_example_explanation.rtf
generic_example_diagram.jpg
generic_example_results.jpg
polymorphic_example_diagram.jpg
polymorphic_example_results.jpg
sequential_tree_example_diagram.jpg
sequential_tree_example_results.jpg
unique_tree_example_diagram.jpg
unique_tree_example_results.jpg
multitree.inl
basic_tree.inl
Tree.inl
unique_tree.inl
basic_tree.inl
associative_tree.inl
unique_tree.inl
Tree.inl
sequential_tree.inl
multitree.inl
#pragma once
#include "basic_tree.h"
#include <set>


// forward declaration for deref comparison functor
template<typename stored_type, typename node_compare_type >
class tree;

/************************************************************************************************
**	This library was designed and developed by Mitchel Haas
**	This software library is provided �as is� and any express or implied warranties, including, 
**	but not limited to, the implied warranties of merchantability and fitness for a particular 
**	purpose are disclaimed.  The use of this software library is at the user's own risk.  
**	In no event shall the software designer/developer be liable for any direct, indirect, 
**	incidental, special, exemplary, or consequential damages (including, but not limited to, 
**	procurement of substitute goods or services; loss of use, data, or profits; 
**	or business interruption) however caused and on any theory of liability, 
**	whether in contract, strict liability (including negligence or otherwise) 
**	arising in any way out of the use of this software library.
**
**	This library may be used freely, and redistributed, 
**	provided it is kept and distributed in it's entirety,
**	and that all of it's contained files remain unmodified, 
**	in their original state, including this notice and the authors name.
*************************************************************************************************/

// deref comparison functor, derive from binary function per Scott Meyer
template<typename stored_type, typename node_compare_type >
struct tree_deref_less : public std::binary_function<const tree<stored_type, node_compare_type>*, const tree<stored_type, node_compare_type>*, bool>
{
	bool operator () (const tree<stored_type, node_compare_type>* lhs, const tree<stored_type, node_compare_type>* rhs) const 
	{
		// call < on actual object
		return (*lhs < *rhs);
	}
};

// node object type.  forwards most operations to base_tree_type, 
// instanciates base_tree_type with type of container (set of unique_tree ptrs) to use for node and key comparisons
template<typename stored_type, typename node_compare_type = std::less<stored_type> >
class tree : public basic_tree<stored_type, tree<stored_type, node_compare_type>,  std::set<tree<stored_type, node_compare_type>*, tree_deref_less<stored_type, node_compare_type> > >
{
public:
	// typedefs
	typedef tree<stored_type, node_compare_type> tree_type;
	typedef basic_tree<stored_type, tree<stored_type, node_compare_type>,  std::set<tree<stored_type, node_compare_type>*, tree_deref_less<stored_type, node_compare_type> > > basic_tree_type;
	friend class basic_tree<stored_type, tree<stored_type, node_compare_type>,  std::set<tree<stored_type, node_compare_type>*, tree_deref_less<stored_type, node_compare_type> > >;

	// constructors/destructor
	tree() : basic_tree_type(stored_type()) {}
	tree( const stored_type& stored_obj ) : basic_tree_type(stored_obj) {}
	tree( const tree_type& rhs ); // copy constructor
	~tree();

	// less operator.  since data's stored as a ptr, use a dereference less
	friend bool operator < (const tree_type& lhs, const tree_type& rhs) { return node_compare_type()(*lhs.pData, *rhs.pData); }

	// public interface
public:
	typename basic_tree_type::iterator insert(const stored_type& stored_obj) { return basic_tree_type::insert(stored_obj, this); }
	typename basic_tree_type::iterator insert(const tree_type& tree_obj ) { return basic_tree_type::insert(tree_obj, this); }
	void set(const tree_type& tree_obj) { basic_tree_type::set(tree_obj, this); }
	void clear();
};

#include "tree.inl"

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.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License

Share

About the Author

Mitchel Haas
Software Developer Datasoft Solutions
United States United States
I'm a c++ programmer in the midwest, now using VC7 at work and at home. I enjoy creating generic libraries, and template based programming.

I also enjoy web development (xhtml, css, javascript, php).

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 22 Aug 2007
Article Copyright 2006 by Mitchel Haas
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid