home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / adrule10.zip / TREE.H < prev   
C/C++ Source or Header  |  1994-06-04  |  1KB  |  58 lines

  1. // Tree.h
  2.  
  3. /* Notes:
  4.  
  5.     This is a simple binary tree class.  It was a template, but my DOS
  6.     compiler doesn't support templates so I took them out
  7.  
  8.     */
  9. #include <string.h>
  10. #include <iostream.h>
  11.  
  12. class linked_node {
  13.  
  14.  
  15.    char                 *Key;
  16.    char*                 Info;
  17.  
  18. public:
  19.    linked_node    *Right,*Left;
  20.  
  21.    linked_node ( void ) : Right(NULL), Left(NULL), Key(NULL),
  22.                           Info(NULL){};
  23.    char*& GetInfo ( void ) { return Info; };
  24.    void SetKey(char *AssignedValue) { Key = strdup(AssignedValue);};
  25.    char* GetKey ( void ) { return Key; };
  26.    ~linked_node ( void ) { delete Key; };
  27. };
  28.  
  29. class binary_tree {
  30.  
  31.    linked_node    *Root;
  32.  
  33.    char*& FindKeyOrAdd ( linked_node  *&Parent, char* key ){
  34.  
  35.        int RC;
  36.  
  37.        if ( Parent == NULL ){
  38.           Parent = new linked_node;
  39.           Parent->SetKey( key );
  40.           return Parent->GetInfo();
  41.        } else {
  42.           if (( RC = strcmp( key, Parent->GetKey())) == 0){
  43.              return Parent->GetInfo();
  44.           } else if ( RC > 0){
  45.              return FindKeyOrAdd ( Parent->Left, key );
  46.           } else {
  47.              return FindKeyOrAdd ( Parent->Right, key );
  48.           }
  49.        }
  50.     };
  51.  
  52.  
  53.    public:
  54.       binary_tree ( void ) : Root(NULL) {};
  55.       char* &operator [](char *key) { return FindKeyOrAdd( Root, key); };
  56.  
  57. };
  58.