home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
adrule10.zip
/
TREE.H
< prev
Wrap
C/C++ Source or Header
|
1994-06-04
|
1KB
|
58 lines
// Tree.h
/* Notes:
This is a simple binary tree class. It was a template, but my DOS
compiler doesn't support templates so I took them out
*/
#include <string.h>
#include <iostream.h>
class linked_node {
char *Key;
char* Info;
public:
linked_node *Right,*Left;
linked_node ( void ) : Right(NULL), Left(NULL), Key(NULL),
Info(NULL){};
char*& GetInfo ( void ) { return Info; };
void SetKey(char *AssignedValue) { Key = strdup(AssignedValue);};
char* GetKey ( void ) { return Key; };
~linked_node ( void ) { delete Key; };
};
class binary_tree {
linked_node *Root;
char*& FindKeyOrAdd ( linked_node *&Parent, char* key ){
int RC;
if ( Parent == NULL ){
Parent = new linked_node;
Parent->SetKey( key );
return Parent->GetInfo();
} else {
if (( RC = strcmp( key, Parent->GetKey())) == 0){
return Parent->GetInfo();
} else if ( RC > 0){
return FindKeyOrAdd ( Parent->Left, key );
} else {
return FindKeyOrAdd ( Parent->Right, key );
}
}
};
public:
binary_tree ( void ) : Root(NULL) {};
char* &operator [](char *key) { return FindKeyOrAdd( Root, key); };
};