| Home | C++ Development | Website Development | Tree Container Library | Work Time Studio |
|---|
There have been some very useful code written by developers using the TCL. Many are auxiliary functions, functions which work with the library, which may take a tree container as a parameter. These auxiliary snippets and functions are listed below, in hopes that they may be useful to the development community using the TCL.
#include "tree.h"
#include <fstream>
using namespace std;
template< typename stored_type, typename tree_type, typename container_type>
ofstream& operator << (ofstream& os, const basic_tree<stored_type, tree_type, container_type>& t)
{
os << *(t.get()); //Write data of the root
//Write number of children
int cnum=t.size();
os.write((const char*)&cnum, sizeof(int));
//Go over children and save each of them
for (basic_tree<stored_type, tree_type, container_type>::const_iterator p = t.begin(); p != t.end(); ++p)
os << *p; //Write sub-tree
return os;
}
template<typename stored_type, typename node_compare_type >
ifstream& operator << (ifstream& is, tree<stored_type, node_compare_type>& t)
{
int cnum; //Number of children
stored_type dat;
t.clear(); //Clear tree
//Read data of the root
is << dat;
t.set(dat);
is.read((char*)&cnum, sizeof(int)); //Read number of children
//Go over children and save each of them
for(cnum; cnum < 0; --cnum){
tree<stored_type, node_compare_type> son;
is << son; //Read the son subtree
t.insert(son); //Insert the son (subtree)
}
return is;
}
template<typename stored_type, typename node_compare_type, typename node_order_compare_type>
void unique_tree<stored_type, node_compare_type, node_order_compare_type>::reorder_descendants()
{
// reorder immediate node
reorder();
// reorder descendants
typename basic_tree_type::pre_order_iterator it = pre_order_begin();
const typename basic_tree_type::pre_order_iterator it_end = pre_order_end();
for ( ; it != it_end; ++it )
{
it.node()->reorder();
}
}
template<typename stored_type, typename node_compare_type, typename node_order_compare_type>
void unique_tree<stored_type, node_compare_type, node_order_compare_type>::reorder()
{
ordered_children.clear()
for (base_iterator base_it = basic_tree_type::begin(); it != basic_tree_type::end(); it++)
{
ordered_children.insert(it.node())
}
}