#include #include #include #include #include #include "book.h" template class GTNode { private: Elem element; GTNode* rent; GTNode* leftchild; GTNode* rightsib; public: GTNode(const Elem&); // Constructor GTNode(const Elem&, GTNode*, GTNode*, GTNode*); ~GTNode(); // Destructor Elem value(); // Return node's value bool isLeaf(); // TRUE if node is a leaf GTNode* parent(); // Return node's parent GTNode* leftmost_child(); // Return node's first child GTNode* right_sibling(); // Return node's right sibling void setValue(Elem&); // Set node's value void insert_first(GTNode* n); // Insert as the first child void insert_next(GTNode* n); // Insert as the right sibling void remove_first(); // Remove first child from tree void remove_next(); // Remove right sibling from tree }; template GTNode::GTNode(const Elem& value) { rent = leftchild = rightsib = NULL; element = value; } template GTNode::GTNode(const Elem& value, GTNode* par, GTNode* leftc, GTNode* rights) { element = value; rent = par; leftchild = leftc; rightsib = rights; } template GTNode::~GTNode() { } template Elem GTNode::value() { return element; } template bool GTNode::isLeaf() { return leftchild == NULL; } template GTNode* GTNode::parent() { return rent; } template GTNode* GTNode::leftmost_child() { return leftchild; } template GTNode* GTNode::right_sibling() { return rightsib; } template void GTNode::setValue(Elem& value) { element = value; } template void GTNode::insert_first(GTNode* n) { n->rightsib = leftchild; n->rent = this; leftchild = n; } template void GTNode::insert_next(GTNode* n) { n->rightsib = rightsib; n->rent = rent; rightsib = n; } template void GTNode::remove_first() { if (leftchild == NULL) return; GTNode* temp = leftchild; leftchild = leftchild->rightsib; delete temp; // BAD -- lose all its subtree! } template void GTNode::remove_next() { if (rightsib == NULL) return; GTNode* temp = rightsib; rightsib = rightsib->rightsib; delete temp; // BAD -- lose all its subtree! } ///////////////////////////////////////////// template class GenTree { private: GTNode* rt; void printhelp(GTNode*); // Print helper function public: GenTree(); // Constructor ~GenTree(); // Destructor void clear(); // Send all nodes to free store GTNode* root(); // Return the root of the tree void newroot(const Elem&, GTNode*, GTNode*); // Combine two trees void print() { printhelp(rt); } // Print a tree }; template GenTree::GenTree() { rt = NULL; } template GenTree::~GenTree() { rt = NULL; } // AWFUL! Throw away the storage template void GenTree::clear() { rt = NULL; } // AWFUL! Throw away the storage template GTNode* GenTree::root() { return rt; } template void GenTree::newroot(const Elem& value, GTNode* first, GTNode* sib) { clear(); rt = new GTNode(value, (GTNode*)NULL, first, sib); } template // Print using a preorder traversal void GenTree::printhelp(GTNode* subroot) { if (subroot->isLeaf()) cout << "Leaf: "; else cout << "Internal: "; cout << subroot->value() << "\n"; for (GTNode* temp = subroot->leftmost_child(); temp != NULL; temp = temp->right_sibling()) printhelp(temp); } main() { GenTree tree; GTNode* ptr; GenTree tree2; GTNode* ptr2; tree.newroot(1, NULL, NULL); ptr = tree.root(); cout << "Print the tree with one node\n"; tree.print(); ptr->insert_first(new GTNode(2)); cout << "Print the tree with two nodes\n"; tree.print(); ptr = ptr->leftmost_child(); cout << "ptr now at node " << ptr->value() << "\n"; ptr->insert_next(new GTNode(3)); cout << "Print the tree with three nodes\n"; tree.print(); ptr->insert_next(new GTNode(4)); cout << "Print the tree with four nodes\n"; tree.print(); ptr = ptr->right_sibling(); cout << "ptr now at node " << ptr->value() << "\n"; ptr->insert_first(new GTNode(5)); cout << "Print the tree with 5 nodes\n"; tree.print(); tree2.newroot(11, NULL, NULL); ptr2 = tree2.root(); ptr2->insert_first(new GTNode(12)); ptr2 = ptr2->leftmost_child(); ptr2->insert_next(new GTNode(13)); return(0); }