#include "AATree.h" #include /** * Construct the tree. */ template AATree::AATree( const Comparable & notFound ) : ITEM_NOT_FOUND( notFound ) { nullNode = new AANode; nullNode->left = nullNode->right = nullNode; nullNode->level = 0; root = nullNode; } /** * Copy constructor. */ template AATree::AATree( const AATree & rhs ) : ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ) { nullNode = new AANode; nullNode->left = nullNode->right = nullNode; nullNode->level = 0; root = clone( rhs.root ); } /** * Destructor for the tree. */ template AATree::~AATree( ) { makeEmpty( ); delete nullNode; } /** * Insert x into the tree; duplicates are ignored. */ template void AATree::insert( const Comparable & x ) { insert( x, root ); } /** * Remove x from the tree. Nothing is done if x is not found. */ template void AATree::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 & AATree::findMin( ) const { if( isEmpty( ) ) return ITEM_NOT_FOUND; AANode *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 & AATree::findMax( ) const { if( isEmpty( ) ) return ITEM_NOT_FOUND; AANode *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 & AATree:: find( const Comparable & x ) const { AANode *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 AATree::makeEmpty( ) { makeEmpty( root ); } /** * Test if the tree is logically empty. * Return true if empty, false otherwise. */ template bool AATree::isEmpty( ) const { return root == nullNode; } /** * Print the tree contents in sorted order. */ template void AATree::printTree( ) const { if( root == nullNode ) cout << "Empty tree" << endl; else printTree( root ); } /** * Deep copy. */ template const AATree & AATree::operator=( const AATree & 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 AATree:: insert( const Comparable & x, AANode * & t ) { if( t == nullNode ) t = new AANode( x, nullNode, nullNode ); else if( x < t->element ) insert( x, t->left ); else if( t->element < x ) insert( x, t->right ); else return; // Duplicate; do nothing skew( t ); split( t ); } /** * 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 AATree:: remove( const Comparable & x, AANode * & t ) { static AANode *lastNode, *deletedNode = nullNode; if( t != nullNode ) { // Step 1: Search down the tree and set lastNode and deletedNode lastNode = t; if( x < t->element ) remove( x, t->left ); else { deletedNode = t; remove( x, t->right ); } // Step 2: If at the bottom of the tree and // x is present, we remove it if( t == lastNode ) { if( deletedNode == nullNode || x != deletedNode->element ) return; // Item not found; do nothing deletedNode->element = t->element; deletedNode = nullNode; t = t->right; delete lastNode; } // Step 3: Otherwise, we are not at the bottom; rebalance else if( t->left->level < t->level - 1 || t->right->level < t->level - 1 ) { if( t->right->level > --t->level ) t->right->level = t->level; skew( t ); skew( t->right ); skew( t->right->right ); split( t ); split( t->right ); } } } /** * Internal method to make subtree empty. */ template void AATree::makeEmpty( AANode * & 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 AATree::printTree( AANode *t ) const { if( t != nullNode ) { printTree( t->left ); cout << t->element << endl; printTree( t->right ); } } /** * Rotate binary tree node with left child. */ template void AATree::rotateWithLeftChild( AANode * & k2 ) const { AANode *k1 = k2->left; k2->left = k1->right; k1->right = k2; k2 = k1; } /** * Rotate binary tree node with right child. */ template void AATree::rotateWithRightChild( AANode * & k1 ) const { AANode *k2 = k1->right; k1->right = k2->left; k2->left = k1; k1 = k2; } /** * Skew primitive for AA-trees. * t is the node that roots the tree. */ template void AATree::skew( AANode * & t ) const { if( t->left->level == t->level ) rotateWithLeftChild( t ); } /** * Split primitive for AA-trees. * t is the node that roots the tree. */ template void AATree::split( AANode * & t ) const { if( t->right->right->level == t->level ) { rotateWithRightChild( t ); t->level++; } } /** * Internal method to clone subtree. */ template AANode * AATree::clone( AANode * t ) const { if( t == t->left ) // Cannot test against nullNode!!! return nullNode; else return new AANode( t->element, clone( t->left ), clone( t->right ), t->level ); }