1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#include <iostream>
using namespace std;
// InspectableContainer
template <typename Object>
class InspectableContainer {
public:
int size();
bool isEmpty() const;
};
// Code Fragment: InspectablePositionalContainer
template <typename Object>
class InspectablePositionalContainer
: public InspectableContainer<Object> {
public:
class Position; // node position type
class PositionIterator; // position iterator
PositionIterator positions() const; // get position iterator
};
// Code Fragment: PositionalContainer
template <typename Object>
class PositionalContainer :
public InspectablePositionalContainer<Object> {
public:
typedef typename InspectablePositionalContainer<Object>::Position Position;
void swapElements(const Position& v, const Position& w);
Object& replaceElement(const Position& v, const Object& e);
};
// Code Fragment: InspectableTree
template <typename Object>
class InspectableTree
: public InspectablePositionalContainer<Object> {
public:
typedef typename InspectablePositionalContainer<Object>::Position Position;
typedef typename InspectablePositionalContainer<Object>::PositionIterator
PositionIterator;
Position root() const; // get root of tree
Position parent(const Position& v) const; // get parent of v
PositionIterator children(const Position& v) const; // iterator for children
bool isInternal(const Position& v) const; // internal node?
bool isExternal(const Position& v) const; // external node?
bool isRoot(const Position& v) const; // the root?
};
// Code Fragment: Tree
template <typename Object>
class Tree
: public InspectableTree<Object>, PositionalContainer<Object> {
};
template <typename Object>
class LinkedBinaryTree
{
protected:
struct Node // a node in the tree
{
Object element; // the element
Node* parent; // parent
Node* left; // left child
Node* right; // right child
Node() : element(Object()) // default constructor
{
parent = left = right = NULL;
}
Node* sibling() const // get our sibling
{
return (this == parent->left ? parent->right : parent->left);
}
};
typedef Node* NodePtr; // a node pointer
public:
class Position // position in the tree
{
private:
NodePtr node; // pointer to the node
public:
Position(NodePtr n = NULL) // constructor
{
node = n;
}
Object& element() const // get element
{
return node->element;
}
bool isNull() const // null position?
{
return node == NULL;
}
friend LinkedBinaryTree; // allow access
};
private: // member data
NodePtr theRoot; // pointer to the root
int sz; // number of nodes
protected: // protected utilities
NodePtr nodePtr(const Position& v) const // convert to NodePtr
{
return v.node;
}
bool isExternal(NodePtr n) const // is node external?
{
return (n->left == NULL && n->right == NULL);
}
bool isInternal(NodePtr n) const // is node internal?
{
return ! isExternal(n);
}
bool isRoot(NodePtr n) const // is node the root?
{
return (n == theRoot);
}
void setRoot(NodePtr r) // make r the root
{
theRoot = r;
r->parent = NULL;
}
void replaceElement(NodePtr n, const Object& o) // replace element
{
n->element = o;
}
void swapElements(NodePtr n, NodePtr w) // swap elements
{
Object temp = w->element;
w->element = n->element;
n->element = temp;
}
void expandExternal(NodePtr n) // expand external node
{
n->left = new Node;
n->left->parent = n;
n->right = new Node;
n->right->parent = n;
sz += 2;
}
NodePtr removeAboveExternal(NodePtr n) // remove n and parent
{
NodePtr p = n->parent;
NodePtr s = n->sibling();
if (isRoot(p)) setRoot(s); // p was root; now s is
else
{
NodePtr g = p->parent; // the grandparent
if (p == g->left) g->left = s; // replace parent by sibling
else g->right = s;
s->parent = g;
}
delete n;
delete p; // delete removed nodes
sz -= 2; // two fewer nodes
return s;
}
public:
LinkedBinaryTree() // constructor
{
theRoot = new Node;
sz = 1;
}
int size() const // size of tree
{
return sz;
}
bool isEmpty() const // is tree empty?
{
return (sz == 0);
}
Position root() const // returns root
{
return Position(theRoot);
}
Position leftChild(const Position& v) const // returns left child
{
return Position(nodePtr(v)->left);
}
Position rightChild(const Position& v) const // returns right child
{
return Position(nodePtr(v)->right);
}
// ... parent(), and sibling() are omitted but similar)
bool isRoot(const Position& v) const // is v the root?
{
return isRoot(nodePtr(v));
}
bool isInternal(const Position& v) const // is v internal?
{
return isInternal(nodePtr(v));
}
bool isExternal(const Position& v) const // is v external?
{
return isExternal(nodePtr(v));
}
void replaceElement(const Position& v, const Object& o)
{
replaceElement(nodePtr(v), o); // replace element
}
void swapElements(const Position& v, const Position& w)
{
swapElements(nodePtr(v), nodePtr(w)); // swap elements
}
void expandExternal(const Position& v)
{
expandExternal(nodePtr(v)); // expand external node
}
Position removeAboveExternal(const Position& v) // remove v and parent
{
return Position(removeAboveExternal(nodePtr(v)));
}
// ... (housekeeping and iterator functions omitted)
};
class Int
{
public:
Int()
{
counter++;
num = counter;
}
private:
static int counter;
int num;
friend ostream& operator<<(ostream& out, Int i);
};
ostream& operator<<(ostream& out, Int i)
{
out << i.num << " ";
return out;
}
typedef LinkedBinaryTree<Int> Tree1;
typedef Tree1::Position Position;
int depth(const Tree1& T, const Position& v);
void binaryInorderPrint(const Tree1& T, const Position& v)
{
if (T.isInternal(v)) // visit left child
binaryInorderPrint(T, T.leftChild(v));
cout << v.element(); // print element
if (T.isInternal(v)) // visit right child
binaryInorderPrint(T, T.rightChild(v));
}
int Int::counter = 0;
int height1(const Tree1& T)
{
int h = 0;
PositionIterator nodes = T.positions();
while (nodes.hasNext())
{
Position v = nodes.next();
if (T.isExternal(v)) h = max(h, depth(T, v));
}
return h;
}
int height2(const Tree1& T, const Position& v)
{
if (T.isExternal(v))
return 0; // leaf has height 0
else
{
int h = 0;
PositionIterator children = T.children(v);
while (children.hasNext())
h = max(h, height2(T, children.next()));
return 1 + h; // 1 + (max height)
}
}
int depth(const Tree& T, const Position& v)
{
if (T.isRoot(v))
return 0; // root has depth 0
else
return 1 + depth(T, T.parent(v)); // 1 + (depth of parent)
}
int main()
{
LinkedBinaryTree<Int> btree;
btree.expandExternal(btree.root());
binaryInorderPrint(btree, btree.root());
cout << endl;
btree.expandExternal(btree.leftChild(btree.root()));
binaryInorderPrint(btree, btree.root());
cout << depth(btree, btree.root());
cout << height1(btree);
cout << height2(btree, btree.root());
return 0;
}
|