#include "Treap.h" #include /** * Implements an unbalanced binary search tree. * Note that all "matching" is based on the compares method. * @author Mark Allen Weiss */ /** * Construct the treap. */ template Treap::Treap( const Comparable & notFound ) : ITEM_NOT_FOUND( notFound ) { nullNode = new TreapNode; nullNode->left = nullNode->right = nullNode; nullNode->priority = INT_MAX; root = nullNode; } /** * Copy constructor. */ template Treap::Treap( const Treap & rhs ) : ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ) { nullNode = new TreapNode; nullNode->left = nullNode->right = nullNode; nullNode->priority = INT_MAX; root = nullNode; *this = rhs; } /** * Destructor for the tree. */ template Treap::~Treap( ) { makeEmpty( ); delete nullNode; } /** * Insert x into the tree; duplicates are ignored. */ template void Treap::insert( const Comparable & x ) { insert( x, root ); } /** * Remove x from the tree. Nothing is done if x is not found. */ template void Treap::remove( const Comparable & x ) { remove( x, root ); } /** * Find the smallest item in the tree. * Return smallest item or ITEM_NOT_FOUND if empty. */ template const Comparable & Treap::findMin( ) const { if( isEmpty( ) ) return ITEM_NOT_FOUND; TreapNode *ptr = root; while( ptr->left != nullNode ) ptr = ptr->left; return ptr->element; } /** * Find the largest item in the tree. * Return the largest item of ITEM_NOT_FOUND if empty. */ template const Comparable & Treap::findMax( ) const { if( isEmpty( ) ) return ITEM_NOT_FOUND; TreapNode *ptr = root; while( ptr->right != nullNode ) ptr = ptr->right; return ptr->element; } /** * Find item x in the tree. * Return the matching item or ITEM_NOT_FOUND if not found. */ template const Comparable & Treap:: find( const Comparable & x ) const { TreapNode *current = root; nullNode->element = x; for( ; ; ) { if( x < current->element ) current = current->left; else if( current->element < x ) current = current->right; else if( current != nullNode ) return current->element; else return ITEM_NOT_FOUND; } } /** * Make the tree logically empty. */ template void Treap::makeEmpty( ) { makeEmpty( root ); } /** * Test if the tree is logically empty. * Return true if empty, false otherwise. */ template bool Treap::isEmpty( ) const { return root == nullNode; } /** * Print the tree contents in sorted order. */ template void Treap::printTree( ) const { if( isEmpty( ) ) cout << "Empty tree" << endl; else printTree( root ); } /** * Deep copy. */ template const Treap & Treap::operator=( const Treap & rhs ) { if( this != &rhs ) { makeEmpty( ); root = clone( rhs.root ); } return *this; } /** * Internal method to insert into a subtree. * x is the item to insert. * t is the node that roots the tree. * Set the new root. */ template void Treap:: insert( const Comparable & x, TreapNode * & t ) { if( t == nullNode ) t = new TreapNode( x, nullNode, nullNode, randomNums.randomInt( ) ); else if( x < t->element ) { insert( x, t->left ); if( t->left->priority < t->priority ) rotateWithLeftChild( t ); } else if( t->element < x ) { insert( x, t->right ); if( t->right->priority < t->priority ) rotateWithRightChild( t ); } // else duplicate; do nothing } /** * Internal method to remove from a subtree. * x is the item to remove. * t is the node that roots the tree. * Set the new root. */ template void Treap::remove( const Comparable & x, TreapNode * & t ) { if( t != nullNode ) { if( x < t->element ) remove( x, t->left ); else if( t->element < x ) remove( x, t->right ); else { // Match found if( t->left->priority < t->right->priority ) rotateWithLeftChild( t ); else rotateWithRightChild( t ); if( t != nullNode ) // Continue on down remove( x, t ); else { delete t->left; t->left = nullNode; // At a leaf } } } } /** * Internal method to make subtree empty. */ template void Treap::makeEmpty( TreapNode * & t ) { if( t != nullNode ) { makeEmpty( t->left ); makeEmpty( t->right ); delete t; } t = nullNode; } /** * Internal method to print a subtree in sorted order. * @param t the node that roots the tree. */ template void Treap::printTree( TreapNode *t ) const { if( t != nullNode ) { printTree( t->left ); cout << t->element << endl; printTree( t->right ); } } /** * Internal method to clone subtree. */ template TreapNode * Treap::clone( TreapNode * t ) const { if( t == t->left ) // Cannot test against nullNode!!! return nullNode; else return new TreapNode( t->element, clone( t->left ), clone( t->right ), t->priority ); } /** * Rotate binary tree node with left child. */ template void Treap::rotateWithLeftChild( TreapNode * & k2 ) const { TreapNode *k1 = k2->left; k2->left = k1->right; k1->right = k2; k2 = k1; } /** * Rotate binary tree node with right child. */ template void Treap::rotateWithRightChild( TreapNode * & k1 ) const { TreapNode *k2 = k1->right; k1->right = k2->left; k2->left = k1; k1 = k2; }