Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/BST/bst.cpp

Go to the documentation of this file.
00001 // BST.cpp - Binary Search Tree Class Implementation
00002 #include "BST.hpp"
00003 
00004 void BinSearchTree::display() const {           // With preorder
00005   cout << "DISPLAY:"<<endl;
00006   preorder( root );
00007   cout << "End of DISPLAY:"<<endl;
00008 }
00009 
00010 void BinSearchTree::preorder( BinNode* rt ) const {  // rt is the root of a subtree
00011 
00012   if ( rt == NULL ) return;                   // Empty subtree
00013   
00014   cout << rt->value() << " ";                 // Visit performs whatever action is desired
00015 
00016   preorder( rt->left  );
00017   preorder( rt->right );
00018 }
00019 
00020 BinSearchTree::BinSearchTree() { 
00021   NbElements = 0;
00022   searchStep = 0;
00023   root       = NULL; 
00024   current    = NULL;
00025   currElem   = NULL;
00026 }
00027 
00028 BinSearchTree::~BinSearchTree() { 
00029   clear( root );
00030 }
00031 
00032 void BinSearchTree::clear() { 
00033   clear( root ); 
00034     root = NULL;
00035   NbElements = 0;
00036   searchStep = 0;
00037 }
00038 
00039 void BinSearchTree::insert( DBase* val ) { 
00040   insert( root, val ); 
00041 }
00042 
00043 void BinSearchTree::remove() {
00044 //  cout << "This will be remove:" << current << endl;
00045   remove( root, current->value() );
00046 }
00047 
00048 void BinSearchTree::remove( DBase& val ) { 
00049   remove( root, val ); 
00050 }
00051 
00052 int BinSearchTree::find( DBase& val ) {
00053   searchStep = 0;
00054     return find( root, val ); 
00055 }
00056 
00057 int BinSearchTree::findID( String& ID ) {
00058   searchStep = 0;
00059     return findID( root, ID ); 
00060 }
00061 
00062 bool BinSearchTree::isEmpty() const { 
00063   return ( root == NULL ); 
00064 }
00065 
00066 void BinSearchTree::print() const {
00067     if ( root == NULL ) {
00068     cout << "The BinSearchTree is empty.\n";
00069   } else {
00070     print( root, 0 );
00071   }
00072 }
00073 
00074 BinNode*  BinSearchTree::getCurrent() {
00075   return current;
00076 }
00077 
00078 DBase*    BinSearchTree::getValue() {
00079   return currElem;
00080 }
00081 
00082 int BinSearchTree::length()   const {
00083   return NbElements;
00084 }
00085 
00086 int BinSearchTree::search()   const {
00087   return searchStep;
00088 }
00089 
00090 // Clear all nodes from the BST
00091 void BinSearchTree::clear( BinNode* rt ) {
00092   if ( rt == NULL ) return;
00093   clear( rt->left  );
00094   clear( rt->right );
00095   NbElements--;
00096   current  = NULL;
00097   currElem = NULL;
00098   delete rt;
00099 }
00100 
00101 void BinSearchTree::remove( BinNode*& rt, DBase& val ) {
00102   if ( rt == NULL ) {
00103     cout << val << " is not in the tree.\n";
00104 
00105   } else {
00106 
00107   if ( val < rt->value() ) {              // Check left
00108        remove( rt->left,  val );
00109 
00110   } else if ( val > rt->value() ) {       // Check right
00111        remove( rt->right, val );
00112 
00113   } else {                                // Found it: remove it
00114 
00115        BinNode* temp = rt;
00116 
00117        if ( rt->left == NULL ) {            // Only a right child
00118          rt = rt->right;                    //   so point to right
00119 
00120        } else if ( rt->right == NULL ) {    // Only a left child
00121          rt = rt->left;                     //   so point to left
00122     
00123      } else {                             // Both children are non-empty
00124          temp = deletemin( rt->right );     // Replace with min value
00125 
00126          rt->setValue( temp->value() );     //  in right subtree  
00127        }
00128 
00129      NbElements--;
00130 
00131      if ( temp == current ) {
00132       current  = NULL;
00133       currElem = NULL;
00134      }
00135 
00136        delete temp;                         // Return node to free store
00137   }
00138   }
00139 }
00140 
00141 BinNode* BinSearchTree::deletemin( BinNode*& rt ) {
00142 
00143   if ( rt == NULL ) {                  // There must be a node to delete
00144     cout << "Cannot delete a node inside an empty Binary Search Tree ! ";
00145     return NULL;
00146   }
00147 
00148   if ( rt->left != NULL ) {            // Continue left
00149     return deletemin( rt->left );
00150 
00151   } else {                             // Found it
00152     BinNode* temp = rt; 
00153   rt = rt->right; 
00154   NbElements--;
00155   current = NULL;
00156   return temp; 
00157   }
00158 }
00159 
00160 void BinSearchTree::insert( BinNode*& rt, DBase* val ) {
00161   
00162   if ( rt == NULL ) {                  // Empty tree: create node
00163   NbElements++;
00164   current = rt;
00165     rt = new BinNode( val, NULL, NULL );
00166 
00167   } else if ( *val < rt->value() ) {
00168     insert( rt->left, val );                // Check left
00169 
00170   } else {
00171   insert( rt->right, val );               // Check right
00172   }
00173 }
00174 
00175 // Print the nodes of a BST; use indentation to indicate tree shape
00176 void BinSearchTree::print( BinNode *rt, int level ) const {
00177 
00178   if ( rt == NULL ) return;                 // Empty tree
00179 
00180   print( rt->left, level+1 );               // Print left subtree
00181   
00182   if ( level == 0 ) {
00183     cout << "  Root  ";
00184   } else {
00185     if ( level < 10 )  { cout << " ";  }
00186     if ( level < 100 ) { cout << " ";  }
00187 
00188     cout << level << "     " ;
00189   }
00190 
00191   cout << rt->value() << "\n";                  // Print node value
00192 
00193   print( rt->right, level+1 );                  // Print right subtree
00194 }
00195 
00196 int BinSearchTree::find( BinNode* node, DBase& val ) {
00197   searchStep++;
00198   if ( node == NULL ) {                      // Empty Tree
00199     cout << "Can't search an element from a NOWHERE location" << endl;
00200     return -50;
00201 
00202   } else if ( NbElements < 1 ) {
00203 //    cout << "Can't search inside an empty binary search tree" << endl;
00204     return -50;
00205   
00206   } else if ( node->CompareID( val ) < 0  ) {    // Check left
00207     return find( node->left, val );
00208 
00209   } else if ( node->CompareID( val ) == 0 ) {
00210     current  = node;
00211     currElem = node->getElem();
00212   // node->value();
00213     return 100;                            // Found Flag
00214   
00215   } else {
00216     return find( node->right, val );          // Check right
00217   }
00218 }
00219   
00220 int BinSearchTree::findID( BinNode* node, String& ID ) {
00221  searchStep++;
00222 
00223  if ( node == NULL ) {
00224 //    cout << "The element wasn't found in the tree. " << ID << endl;
00225     return -50;
00226  } else  {
00227 
00228 //  if ( node->valid() ) {  cout << node << endl; }
00229 
00230   if ( NbElements < 1 ) {
00231     cout << "Can't search inside an empty binary search tree" << endl;
00232     return -50;
00233   
00234   } else if ( node->CompareID( ID ) < 0  ) {    // Check left
00235     return findID( node->left, ID );
00236 
00237   } else if ( node->CompareID( ID ) == 0 ) {
00238     current  = node;
00239     currElem = node->getElem();
00240   // node->value();
00241     return 100;                            // Found Flag
00242   
00243   } else {
00244     return findID( node->right, ID );         // Check right
00245   }
00246 
00247  }
00248 }

Generated on Sun Oct 14 18:46:17 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001