Hvis du vil bruge pointere i din template, kan du f.eks sende en compareTo() metode med som parameter, og bruge den i stedet for > <. For at få > < til at virke efter hensigten, ville det være nødvendigt at overloade dem, og det kan man ikke med pointere.
Hvis det kun er primitiver du vil bruge, kan du lave et typecheck, og hvis parameteret er en pointer, kan du dereferentiere den, før du sammenligner.
/** * Remove x from the tree. */ template <class Comparable> void SplayTree<Comparable>::remove( Comparable & x ) { BinaryNode<Comparable> *newTree;
// If x is found, it will be at the root splay( x, root ); if( root->element != x ) return; // Item not found; do nothing
if( root->left == nullNode ) newTree = root->right; else { // Find the maximum in the left subtree // Splay it to the root; and then attach right child newTree = root->left; splay( x, newTree ); newTree->right = root->right; } delete root; root = newTree; }
/** * Find the smallest item in the tree. * Not the most efficient implementation (uses two passes), but has correct * amortized behavior. * A good alternative is to first call Find with parameter * smaller than any item in the tree, then call findMin. * Return the smallest item or ITEM_NOT_FOUND if empty. */ template <class Comparable> const Comparable & SplayTree<Comparable>::findMin( ) { if( isEmpty( ) ) return ITEM_NOT_FOUND;
/** * Find the largest item in the tree. * Not the most efficient implementation (uses two passes), but has correct * amortized behavior. * A good alternative is to first call Find with parameter * larger than any item in the tree, then call findMax. * Return the largest item or ITEM_NOT_FOUND if empty. */ template <class Comparable> const Comparable & SplayTree<Comparable>::findMax( ) { if( isEmpty( ) ) return ITEM_NOT_FOUND;
/** * Find item x in the tree. * Return the matching item or ITEM_NOT_FOUND if not found. */ template <class Comparable> const Comparable & SplayTree<Comparable>::find( Comparable & x ) { if( isEmpty( ) ) return ITEM_NOT_FOUND; splay( x, root ); if( root->element != x ) return ITEM_NOT_FOUND;
return root->element; }
/** * Make the tree logically empty. */ template <class Comparable> void SplayTree<Comparable>::makeEmpty( ) { /****************************** * Comment this out, because it is prone to excessive * recursion on degenerate trees. Use alternate algorithm.
/** * Test if the tree is logically empty. * @return true if empty, false otherwise. */ template <class Comparable> bool SplayTree<Comparable>::isEmpty( ) const { return root == nullNode; }
/** * Internal method to perform a top-down splay. * The last accessed node becomes the new root. * This method may be overridden to use a different * splaying algorithm, however, the splay tree code * depends on the accessed item going to the root. * x is the target item to splay around. * t is the root of the subtree to splay. */ template <class Comparable> void SplayTree<Comparable>::splay( Comparable & x, BinaryNode<Comparable> * & t ) const { BinaryNode<Comparable> *leftTreeMax, *rightTreeMin; static BinaryNode<Comparable> header;
/** * Internal method to print a subtree t in sorted order. * WARNING: This is prone to running out of stack space. */ template <class Comparable> void SplayTree<Comparable>::printTree( BinaryNode<Comparable> *t ) const { if( t != t->left ) { printTree( t->left ); cout << t->element.name << ": " << t->element.error << endl; printTree( t->right ); } }
/** * Internal method to reclaim internal nodes in subtree t. * WARNING: This is prone to running out of stack space. */ template <class Comparable> void SplayTree<Comparable>::reclaimMemory( BinaryNode<Comparable> * t ) const { if( t != t->left ) { reclaimMemory( t->left ); reclaimMemory( t->right ); delete t; } }
/** * Internal method to clone subtree. * WARNING: This is prone to running out of stack space. */ template <class Comparable> BinaryNode<Comparable> * SplayTree<Comparable>::clone( BinaryNode<Comparable> * t ) const { if( t == t->left ) // Cannot test against nullNode!!! return nullNode; else return new BinaryNode<Comparable>( t->element, clone( t->left ), clone( t->right ) ); }
Jeg tror nu nok, at jeg ville vælge at sende en compareTo metode med som parameter til SplayTree konstruktoren.
Så kan du lave en compareTo metode, der passer til din type, og derved få en større alsidighed. Da det nu ikke er muligt at overloade operatorer på alle typer.
Jeg skylder vist lige at nævne at koden til klassen SplayTreee er skrevet af Mark Allen Weiss, og kan downloades fra han website http://www.cs.fiu.edu/~weiss/
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.