Click here to Skip to main content
15,881,204 members
Articles / Desktop Programming / MFC

Tree Container Library

Rate me:
Please Sign up or sign in to vote.
4.85/5 (29 votes)
22 Aug 2007Zlib10 min read 227.9K   7.2K   111  
A generic template class library for storing data in a tree-like structure.
<html>
<head>
<title>
Tree Container Library: Polymorphic Example Code
</title>
</head>
<body>
<pre>
<code>
  1 //#include "stdafx.h"
  2 innclude &lt;string&gt;
  3 #include &lt;iostream&gt;
  4 #include &lt;sstream&gt;
  5 #include "unique_tree.h"
  6
  7 // forward declaration
  8 class cRental;
  9
 10 namespace utility
 11 {
 12 	void populate_tree(unique_tree&lt;cRental&gt;&amp; rental_tree);
 13 	void print_tree(const unique_tree&lt;cRental&gt;&amp; rental_tree, const int depth);
 14 	bool is_last_child(const unique_tree&lt;cRental&gt;* node);
 15 }
 16 
 17 class cRental
 18 {
 19 public:
 20 	cRental() : rental_no(0) {}
 21 	cRental(const int rental_no_) : rental_no(rental_no_) {}
 22 	cRental(const int rental_no_, const std::string&amp; rental_name_)
 23 		: rental_no(rental_no_), rental_name(rental_name_) {}
 24 	virtual ~cRental() {}
 25
 26 	virtual std::string get_rental_type() const { return "unknown"; }
 27 	virtual cRental* clone() const { return new cRental(*this); }
 28 
 29 	friend bool operator &lt; (const cRental&amp; lhs, const cRental&amp; rhs) { return lhs.rental_no &lt; rhs.rental_no; }
 30 	int get_rental_no() const { return rental_no; }
 31 	const std::string&amp; get_name() const { return rental_name; }
 32 	std::string print() const
 33 	{
 34 		std::ostringstream ostr;
 35 		ostr &lt;&lt; get_rental_type() &lt;&lt; ": " &lt;&lt; get_name();
 36 		return ostr.str();
 37 	}
 38 
 39 private:
 40 	int rental_no;
 41 	std::string rental_name;
 42 };
 43
 44 class cProperty : public cRental
 45 {
 46 public:
 47 	cProperty() : acreage(0.0), land_value(0.0) {}
 48 	cProperty(	int rental_no_, 
 49 				const std::string&amp; property_name_,
 50 				double acreage_,
 51 				double land_value_)
 52 				:	cRental(rental_no_, property_name_),
 53 					acreage(acreage_),
 54 					land_value(land_value_)  {}
 55 	virtual ~cProperty() {}
 56 
 57 	virtual std::string get_rental_type() const { return "Property"; }
 58 	virtual cRental* clone() const { return new cProperty(*this); }
 59 
 60 private:
 61 	double acreage;
 62 	double land_value;
 63 };
 64
 65 class cBuilding : public cRental
 66 {
 67 public:
 68 	cBuilding()  {}
 69 	cBuilding(	int rental_no_, 
 70 				const std::string&amp; building_name_,
 71 				const std::string&amp; address_)
 72 				:	cRental(rental_no_, building_name_),
 73 					address(address_)  {}
 74 	virtual ~cBuilding() {}
 75
 76 	virtual std::string get_rental_type() const { return "Building"; }
 77 	virtual cRental* clone() const { return new cBuilding(*this); }
 78
 79 private:
 80 	std::string address;
 81 };
 82
 83 class cUnit : public cRental
 84 {
 85 public:
 86 	cUnit() : rent_amt(0.0) {}
 87 	cUnit(	int rental_no_, 
 88 				const std::string&amp; unit_name_,
 89 				double rent_amt_)
 90 				:	cRental(rental_no_, unit_name_),
 91 					rent_amt(rent_amt_)  {}
 92 	~cUnit() {}
 93 
 94 	virtual std::string get_rental_type() const { return "Unit"; }
 95 	virtual cRental* clone() const { return new cUnit(*this); }
 96
 97 private:
 98 	double rent_amt;
 99 	std::string unit_code;
100 };
101
102 cRental* clone_fcn(const cRental&amp; rental_obj)
103 {
104 	return rental_obj.clone();
105 }
106
107 int main(int argc, char* argv[])
108 {
109 	unique_tree&lt;cRental&gt; rental_tree(cRental(0, "My Properties"));
110 	
111 
112 	// populate and print tree without supplying clone function
113 	std::cout &lt;&lt; "Tree without clone function" &lt;&lt; std::endl &lt;&lt; std::endl;
114 	utility::populate_tree(rental_tree);
115 	utility::print_tree(rental_tree, 0);
116 	std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; std::endl;
117
118 	// populate and print tree supplying a clone function
119 	std::cout &lt;&lt; "Tree with clone function" &lt;&lt; std::endl &lt;&lt; std::endl;
120 	rental_tree.clear();
121 	rental_tree.set_clone(&clone_fcn);
122 	utility::populate_tree(rental_tree);
123 	utility::print_tree(rental_tree, 0);
124 	std::cout &lt;&lt; std::endl &lt;&lt; std::endl;
125
126 	return 0;
127 }
128
129 void utility::populate_tree(unique_tree&lt;cRental&gt;&amp; rental_tree)
130 {
131 	// insert properties
132 	rental_tree.insert( cRental(0), cProperty(100, "Riverside Property", 35.5, 1000.00));
133 	rental_tree.insert( 0, cProperty(200, "Main Street Property", 21.6, 2500.00));
134 	rental_tree.insert( 0, cProperty(300, "Boston Ave Property", 52.1, 4300.00));
135 
136 	// insert buildings
137 	rental_tree.insert( 100, cBuilding(110, "Tech Building", "4636 Riverside Road" ));
138 	rental_tree.insert( 100, cBuilding(120, "Riverside Hotel", "4690 Riverside Road"));
139
140 	rental_tree.insert( 200, cBuilding(210, "Main Street Building", "322 Main Street"));
141
142 	rental_tree.insert( 300, cBuilding(310, "Boston Ave Tech Center", "3200 Boston Ave"));
143 	rental_tree.insert( 300, cBuilding(320, "Mike's Pool Hall", "3350 Boston Ave"));
144 	rental_tree.insert( 300, cBuilding(330, "Porter Apartment Bldg", "3300 Boston Ave"));
145
146 	// insert units
147 	rental_tree.insert( 110, cUnit(111, "Suite G1", 1400.00));
148 	rental_tree.insert( 110, cUnit(112, "Suite G2", 2150.00));
149 	rental_tree.insert( 110, cUnit(113, "Suite G3", 1530.00));
150 	rental_tree.insert( 110, cUnit(114, "Suite G4", 2300.00));
151 	rental_tree.insert( 110, cUnit(115, "Suite G5", 2640.00));
152
153 	rental_tree.insert( 120, cUnit(121, "Room 1A", 600.00));
154 	rental_tree.insert( 120, cUnit(122, "Room 1B", 550.00));
155 	rental_tree.insert( 120, cUnit(123, "Room 1C", 650.00));
156 	rental_tree.insert( 120, cUnit(124, "Room 2A", 700.00));
157 	rental_tree.insert( 120, cUnit(125, "Room 2B", 630.00));
158 	rental_tree.insert( 120, cUnit(126, "Room 2C", 800.00));
159
160 	rental_tree.insert( 210, cUnit(211, "Apt 1", 550.00));
161 	rental_tree.insert( 210, cUnit(212, "Apt 2", 600.00));
162 	rental_tree.insert( 210, cUnit(213, "Apt 3", 730.00));
163 	rental_tree.insert( 210, cUnit(214, "Apt 4", 660.00));
164
165 	rental_tree.insert( 310, cUnit(311, "Suite A", 2200.00));
166 	rental_tree.insert( 310, cUnit(312, "Suite B", 1900.00));
167 	rental_tree.insert( 310, cUnit(313, "Suite C", 2500.00));
168 
169 	rental_tree.insert( 330, cUnit(331, "Apt 101", 900.00));
170 	rental_tree.insert( 330, cUnit(332, "Apt 102", 680.00));
171 	rental_tree.insert( 330, cUnit(333, "Apt 103", 800.00));
172 	rental_tree.insert( 330, cUnit(334, "Apt 201", 880.00));
173 	rental_tree.insert( 330, cUnit(335, "Apt 202", 930.00));
174 }
175
176 void utility::print_tree(const unique_tree&lt;cRental&gt;&amp; rental_tree, const int depth )
177 {
178 	std::cout &lt;&lt; rental_tree.get()-&gt;print() &lt;&lt; std::endl;
179
180 	unique_tree&lt;cRental&gt;::const_iterator it = rental_tree.begin(), it_end = rental_tree.end();
181 	for ( ; it != it_end; ++it ) {
182 		for ( int i = 0; i &lt; depth; ++i ) {
183 			const unique_tree&lt;cRental&gt;* parent = &rental_tree;
184 			for ( int j = depth - 1; j &gt; i; --j )
185 				parent = parent-&gt;parent();
186
187 			std::cout &lt;&lt;  (is_last_child(parent) ? " " : "|");
188 
189 			std::cout &lt;&lt; std::string(2, ' ');
190 		}
191 		std::cout &lt;&lt; "|" &lt;&lt; std::endl;
192 		std::cout &lt;&lt; std::string(depth * 3 + 1, ' ') &lt;&lt; "- ";
193 		print_tree(*it.node(), depth + 1);
194 	}
195 }
196 
197 bool utility::is_last_child(const unique_tree&lt;cRental&gt;* node)
198 {
199 	const unique_tree&lt;cRental&gt;* parent = node-&gt;parent();
200 	unique_tree&lt;cRental&gt;::const_iterator it = parent-&gt;end();
201 	--it;
202 	return it-&gt;get_rental_no() == node-&gt;get()-&gt;get_rental_no();
203 }

</code>
</pre>


<script type="text/javascript" src="/stats/tracker.js.php"></script>
<noscript>
<p>
	<img style="display: none" src="/stats/static.php" alt='site stats'/>
</p>
</noscript>		


</body>
</html>

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



Comments and Discussions