cgal/SearchStructures/doc_tex/SearchStructures/segment_tree_examples.tex

112 lines
4.0 KiB
TeX

\subsection{Example of Segment Tree on Map-like Data}
\label{sec:segment_tree_ex}
The following example program uses the predefined \ccc{
Segment_tree_2} data structure together with the predefined traits
class \ccc{Segment_tree_map_traits_2} which has two template arguments
specifying the
type of the point data in each dimension
(\ccc{CGAL::Cartesian<double>}) and the value type of the
2-dimensional point data (\ccc{char}). Therefore the \ccc{
Segment_tree_2} is defined on 2-dimensional point data
(\ccc{CGAL::Point_2<Cartesian<double> >}) each of which is
associated with a character.
Then, a few data items are created and put into a list. After
that the tree is constructed according to that list, a window
query is performed, and the query elements are given out.
\begin{verbatim}
#include <CGAL/Cartesian.h>
#include <CGAL/Segment_tree_k.h>
#include <CGAL/Range_segment_tree_traits.h>
typedef CGAL::Cartesian<double> K;
typedef CGAL::Segment_tree_map_traits_2<K, char> Traits;
typedef CGAL::Segment_tree_2<Traits > Segment_tree_2_type;
int main()
{
typedef Traits::Interval Interval;
typedef Traits::Pure_interval Pure_interval;
typedef Traits::Key Key;
std::list<Interval> InputList, OutputList1, OutputList2;
InputList.push_back(Interval(Pure_interval(Key(1,5), Key(2,7)),'a'));
InputList.push_back(Interval(Pure_interval(Key(2,7), Key(3,8)),'b'));
InputList.push_back(Interval(Pure_interval(Key(6,9), Key(9,13)),'c'));
InputList.push_back(Interval(Pure_interval(Key(1,3), Key(3,9)),'d'));
Segment_tree_2_type Segment_tree_2(InputList.begin(),InputList.end());
Interval a=Interval(Pure_interval(Key(3,6), Key(7,12)),'e');
Segment_tree_2.window_query(a,std::back_inserter(OutputList1));
std::list<Interval>::iterator j = OutputList1.begin();
std::cout << "\n window_query (3,6),(7,12)\n";
while(j!=OutputList1.end()){
std::cout << (*j).first.first.x() << "-" << (*j).first.second.x() << " "
<< (*j).first.first.y() << "-" << (*j).first.second.y() << std::endl;
j++;
}
Interval b=Interval(Pure_interval(Key(6,10),Key(7,11)), 'f');
Segment_tree_2.enclosing_query(b,std::back_inserter(OutputList2));
j = OutputList2.begin();
std::cout << "\n enclosing_query (6,10),(7,11)\n";
while(j!=OutputList2.end()){
std::cout << (*j).first.first.x() << "-" << (*j).first.second.x() << " "
<< (*j).first.first.y() << "-" << (*j).first.second.y() << std::endl;
j++;
}
return 0;
}
\end{verbatim}
\subsection{Example of Segment Tree on Set-like Data}
This example illustrates the use of the predefined segment tree
on 3-dimensional interval data (with no value associated). After
the definition of the traits type and tree type, some intervals
are constructed and the tree is build according to the
intervals. Then, a window query is performed and the query
elements are given out.
\begin{verbatim}
#include <CGAL/Cartesian.h>
#include <CGAL/Segment_tree_k.h>
#include <CGAL/Range_segment_tree_traits.h>
typedef CGAL::Cartesian<int> K;
typedef CGAL::Range_segment_tree_set_traits_3<K> Traits;
typedef CGAL::Segment_tree_3<Traits > Segment_tree_3_type;
int main()
{
typedef Traits::Interval Interval;
typedef Traits::Key Key;
std::list<Interval> InputList, OutputList;
InputList.push_back(Interval(Key(1,5,7), Key(2,7,9)));
InputList.push_back(Interval(Key(2,7,6), Key(3,8,9)));
InputList.push_back(Interval(Key(6,9,5), Key(9,13,8)));
InputList.push_back(Interval(Key(1,3,4), Key(3,9,8)));
Segment_tree_3_type Segment_tree_3(InputList.begin(),InputList.end());
Interval a(Key(3,6,5), Key(7,12,8));
Segment_tree_3.window_query(a,std::back_inserter(OutputList));
std::list<Interval>::iterator j = OutputList1.begin();
std::cout << "\n window_query (3,6,5),(7,12,8) \n";
while(j!=OutputList.end()){
std::cout << (*j).first.x() << "," << (*j).first.y() << ",";
std::cout << (*j).first.z() <<", " << (*j).second.x() << ",";
std::cout << (*j).second.y() << "," << (*j).second.z() << std::endl;
j++;
}
}
\end{verbatim}