// see apset3.h template apset::apset() : myRoot(NULL), myCount(0) { } template Node * copy(Node * root) { if (root == NULL) return NULL; else return new Node(root->info, copy(root->left),copy(root->right)); } template const apset & apset::operator = (const apset & rhs) { myRoot = copy(rhs.myRoot); myCount = rhs.myCount; return *this; } template bool apset::contains(const itemType & item) const { return find(myRoot,item) != NULL; } template int apset::size() const { return myCount; } template void apset::print(ostream & output) const { // not implemented } template void apset::add(const itemType & item) { Node * loc = find(myRoot,item); if (loc == NULL) { if (myRoot == NULL) { myRoot = new Node(item,NULL,NULL); myCount++; return; } // must have a root start there loc = myRoot; bool found = false; while (! found) { if (item < loc->info) { if (loc->left == NULL) { loc->left = new Node(item,NULL,NULL); found = true; } else { loc = loc->left; } } else { if (loc->right == NULL) { loc->right = new Node(item,NULL,NULL); found = true; } else { loc = loc->right; } } } myCount++; } } template void apset::remove(const itemType & item) { // not implemented } template void apset::makeEmpty() { myRoot = NULL; myCount = 0; } template Node * apset::find(Node * root, const itemType & item) const { while (root != NULL) { if (item == root->info) return root; else if (item < root->info) root = root->left; else root = root->right; } return NULL; } template const apset& apset::operator *= (const apset & rhs) { apset result; if (size() < rhs.size()) { interHelp(result,rhs.myRoot,myRoot); } else { interHelp(result,myRoot,rhs.myRoot); } // swap roots so that when result goes // out of scope the destructor deletes // what used to be me Node * temp = myRoot; myRoot = result.myRoot; result.myRoot = temp; myCount = result.myCount; return *this; } template const apset& apset::operator += (const apset & rhs) { unionHelp(rhs.myRoot); return *this; } template void apset::unionHelp(Node * rightRoot) { if (rightRoot != NULL) { unionHelp(rightRoot->left); unionHelp(rightRoot->right); if (! contains(rightRoot->info)) { add(rightRoot->info); } } } template void apset::interHelp(apset & result, Node * leftRoot, Node * rightRoot) { if (rightRoot != NULL) { interHelp(result,leftRoot,rightRoot->left); if (find(leftRoot,rightRoot->info) != NULL) { result.add(rightRoot->info); } interHelp(result,leftRoot,rightRoot->right); } }