home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / OOPWLD.ZIP / BINTREE / FULLBIN.CPP next >
Encoding:
C/C++ Source or Header  |  1990-05-10  |  1.5 KB  |  48 lines

  1. // FULLBIN.CPP
  2. #include "fullbin.hpp"
  3.  
  4. void tree::add(char * dat) {
  5.   if(*data == 0) { dup(dat); return; }  // use current node if empty
  6.   int compare = strcmp(dat, data);
  7.   if( compare > 0) { // dat > data -- right tree
  8.     if(right) right->add(dat);  // if subtree is there, recursive call
  9.     else right = new tree(dat); // if not, make a new subtree
  10.   } else if(compare < 0) {  // put it in left tree
  11.     if(left) left->add(dat); // if subtree is there, recursive call
  12.     else left = new tree(dat);  // if not, make a new subtree
  13.   }
  14.   // do nothing if it's already in the tree (compare == 0)
  15. }
  16.  
  17. tree * tree::find(char * dat) {
  18.   int cmp = strcmp(dat,data);
  19.   if(cmp == 0) return this;  // return this subtree if dat == data
  20.   if(cmp > 0) { // search right subtree if dat > data
  21.     if(right) return right->find(dat);
  22.     else return NULL;
  23.   }
  24.   if(cmp < 0) { // search left subtree if dat < data
  25.     if(left) return left->find(dat);
  26.     else return NULL;
  27.   }
  28. }
  29.  
  30. void rotate_left(tree ** tp) {
  31. //  tree * t = *tp;
  32.   if(!(*tp)->right) return;  // no place to rotate from
  33.   tree * tmp = (*tp)->right->left;
  34.   (*tp)->right->left = (*tp);
  35.   *tp = (*tp)->right;  // change the external pointer value
  36.   (*tp)->left->right = tmp;
  37. }
  38.  
  39. void rotate_right(tree ** tp) {
  40. //  tree * t = *tp;
  41.   if(!(*tp)->left) return;  // no place to rotate from
  42.   tree * tmp = (*tp)->left->right;
  43.   (*tp)->left->right = (*tp);
  44.   *tp = (*tp)->left;  // change the external pointer value
  45.   (*tp)->right->left = tmp;
  46. }
  47.  
  48.