#include #include using namespace std; #include "c:\buoy.h" #include "c:\emp.h" template class Tree{ private: T * _b; long zip; // key object Tree * left; Tree * right; static Tree * top; static int count; public: static int Count() {return count;}; static Tree * Top() {return top;}; string Key() {return _b->Name();}; void Key(string _v) {_b->Name(_v);}; T * anObject() { return _b;}; Tree() : left(NULL), right(NULL) {Key(0);}; Tree(T * _v) : left(NULL), right(NULL) ,_b(_v){ Key(_v->Name());if (top == NULL) cout<< " TOP NODE INSERTED\n";}; static void Insert(Tree * _t) { Tree * temp; if (top == NULL) { top = _t; return; } temp = top; while (1) { if (_t->Key() < temp->Key()) { if (temp->left == NULL) { temp->left = _t; count++; return; } else { temp = temp->left; } } else // greater { if (temp->right == NULL) { temp->right = _t; count++; return; } else { temp = temp->right ; } } } // end while }; // end Insert method static void Navigate(Tree * _t) { static int count = 0; if (_t) { Navigate(_t->left); cout << _t->Key() << endl; Navigate(_t->right); } }; // end Navigate method static string Lowest() { Tree * temp = top; while (temp->left) temp = temp->left; return temp->Key(); } static string Highest() { Tree * temp = top; while (temp->right) temp = temp->right; return temp->Key(); }; static T * Find (string _v) { Tree * _t; _t = Tree::top; while (1) { if (_t->Key() == _v) return _t->_b; if (_t->Key() < _v) _t = _t->right; else _t = _t->left; if (!(_t)) break; // cout << _t->Key() << endl; } return (T *) NULL ; }; // end method Find() }; // end class Tree templateTree * Tree::top = NULL; template int Tree::count = 1; void main() { Buoy aBuoy(39.6,40.5,"CHUCK"); Employee aEmp("WASHINGTON",40,10); if (Tree::Top()) cout << " TOP IS SET\n"; else cout << " TOP IS EMPTY\n"; Tree::Insert(new Tree(&aEmp)); Tree::Insert(new Tree(&aBuoy)); Tree::Insert(new Tree(new Buoy(40.0,75.0,"JOE"))); Tree::Insert(new Tree(new Buoy(40.0,75.0,"ALBERT"))); Tree::Insert(new Tree(new Buoy(40.0,75.0,"FRED"))); Tree::Insert(new Tree(new Buoy(40.0,75.0,"ROLLO"))); Tree::Insert(new Tree(new Buoy(40.0,75.0,"PICO"))); Tree::Insert(new Tree(new Buoy(40.0,75.0,"ALVERADO"))); Tree::Navigate(Tree::Top()); Tree::Navigate(Tree::Top()); Buoy * _pt = Tree::Find("ANDY PANDA"); if (_pt) cout << _pt->Name()<< endl; else cout << " NOT FOUND\n"; }