1 | /* |
---|
2 | Copyright (c) 2003-2006, Troy Aaron Johnson |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Redistribution and use in source and binary forms, with or without |
---|
6 | modification, are permitted provided that the following conditions |
---|
7 | are met: |
---|
8 | |
---|
9 | * Redistributions of source code must retain the above copyright |
---|
10 | notice, this list of conditions and the following disclaimer. |
---|
11 | * Redistributions in binary form must reproduce the above copyright |
---|
12 | notice, this list of conditions and the following disclaimer in the |
---|
13 | documentation and/or other materials provided with the distribution. |
---|
14 | * Neither the name of Troy Aaron Johnson nor the names of any |
---|
15 | contributors may be used to endorse or promote products derived from |
---|
16 | this software without specific prior written permission. |
---|
17 | |
---|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | POSSIBILITY OF SUCH DAMAGE. |
---|
29 | */ |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | #include <cassert> |
---|
33 | #include <iostream> |
---|
34 | #include <list> |
---|
35 | #include <string> |
---|
36 | |
---|
37 | #include "tree.h" |
---|
38 | |
---|
39 | using namespace taj; |
---|
40 | |
---|
41 | bool pruning_predicate(const tree<std::string>::iterator& node) |
---|
42 | { |
---|
43 | return (*node == "B"); |
---|
44 | } |
---|
45 | |
---|
46 | template <class TreeType> |
---|
47 | void print(const tree<TreeType>& t) |
---|
48 | { |
---|
49 | std::cout << "preorder traversal: " << std::endl; |
---|
50 | for (typename tree<TreeType>::const_preorder_iterator i = t.beginPre(); i != t.endPre(); ++i) |
---|
51 | { |
---|
52 | std::cout << *i << std::endl; |
---|
53 | } |
---|
54 | std::cout << std::endl; |
---|
55 | |
---|
56 | std::cout << "postorder traversal: " << std::endl; |
---|
57 | for (typename tree<TreeType>::const_postorder_iterator i = t.beginPost(); i != t.endPost(); ++i) |
---|
58 | { |
---|
59 | std::cout << *i << std::endl; |
---|
60 | } |
---|
61 | std::cout << std::endl; |
---|
62 | |
---|
63 | std::cout << "bfs traversal: " << std::endl; |
---|
64 | for (typename tree<TreeType>::const_bfs_iterator i = t.beginBfs(); i != i.endBfs(); ++i) |
---|
65 | { |
---|
66 | std::cout << *i << std::endl; |
---|
67 | } |
---|
68 | std::cout << std::endl; |
---|
69 | } |
---|
70 | |
---|
71 | void pointer_specialization_test(void) |
---|
72 | { |
---|
73 | tree<int*> pint_tree; |
---|
74 | |
---|
75 | int x = 1, y = 2, z = 3; |
---|
76 | |
---|
77 | tree<int*>::iterator i(pint_tree.setRoot(&x)); |
---|
78 | |
---|
79 | i.push_back(&y); |
---|
80 | i.push_back(&z); |
---|
81 | |
---|
82 | std::cout << "int* tree root is " << **i << std::endl; |
---|
83 | |
---|
84 | tree<int*>::sibling_iterator si(i.beginChildren()); |
---|
85 | |
---|
86 | tree<char*> t1; |
---|
87 | tree<long*> t2; |
---|
88 | tree<float*> t3; |
---|
89 | tree<double*> t4; |
---|
90 | tree<std::string*> t5; |
---|
91 | tree<unsigned char*> t6; |
---|
92 | tree<unsigned long*> t7; |
---|
93 | } |
---|
94 | |
---|
95 | int main(int argc, char* argv[]) |
---|
96 | { |
---|
97 | tree<std::string> t; |
---|
98 | |
---|
99 | assert(t.empty()); |
---|
100 | assert(t.size() == 0); |
---|
101 | |
---|
102 | tree<std::string>::sibling_iterator i(t.setRoot("root")); |
---|
103 | |
---|
104 | assert(!t.empty()); |
---|
105 | assert(t.size() == 1); |
---|
106 | assert(*i == "root"); |
---|
107 | |
---|
108 | *i = "new root"; |
---|
109 | |
---|
110 | assert(*i == "new root"); |
---|
111 | |
---|
112 | tree<std::string>::sibling_iterator j(i.push_back("left")); |
---|
113 | assert(t.size() == 2); |
---|
114 | tree<std::string>::sibling_iterator k(i.push_back("right")); |
---|
115 | assert(t.size() == 3); |
---|
116 | |
---|
117 | assert(*j == "left"); |
---|
118 | assert(*k == "right"); |
---|
119 | |
---|
120 | j.push_back("A"); |
---|
121 | j.push_back("B"); |
---|
122 | |
---|
123 | k.push_back("C"); |
---|
124 | k.push_back("D"); |
---|
125 | |
---|
126 | assert(find(t.beginPre(), t.endPre(), "C") != t.endPre()); |
---|
127 | assert(find(t.beginPre(), t.endPre(), "Z") == t.endPre()); |
---|
128 | |
---|
129 | std::cout << "tree height is " << t.height() << std::endl; |
---|
130 | |
---|
131 | print(t); |
---|
132 | |
---|
133 | tree<std::string> copy_of_t(t); |
---|
134 | |
---|
135 | print(copy_of_t); |
---|
136 | |
---|
137 | std::list<int> int_list; |
---|
138 | for (int i = 1; i < 20; ++i) |
---|
139 | int_list.push_back(i); |
---|
140 | |
---|
141 | tree<int> int_tree(int_list.begin(), int_list.end(), 2); |
---|
142 | |
---|
143 | print(int_tree); |
---|
144 | |
---|
145 | pointer_specialization_test(); |
---|
146 | } |
---|