#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class t=""> class BinaryTree { struct Node { T data; Node* lChildptr; Node* rChildptr; Node(T dataNew) { data = dataNew; lChildptr = NULL; rChildptr = NULL; } }; private: Node* root; void Insert(T newData, Node* &theRoot) { if(theRoot == NULL) { theRoot = new Node(newData); return; } if(newData < theRoot->data) Insert(newData, theRoot->lChildptr); else Insert(newData, theRoot->rChildptr); } void PrintTree(Node* theRoot) { if(theRoot != NULL) { PrintTree(theRoot->lChildptr); cout<< theRoot->data<<" , "; PrintTree(theRoot->rChildptr); } } T Largest( Node* theRoot) { if ( root == NULL ){ cout<<"There is no tree"; return -1; } if (theRoot->rChildptr != NULL) return Largest(theRoot->rChildptr); else { cout<<"\n Highest priority: "<<theRoot->data<<"\n"; theRoot->data=NULL; return theRoot->data; } }; public: BinaryTree() { root = NULL; } void AddItem(T newData) { Insert(newData, root); } void PrintTree() { PrintTree(root); } T Largest() { return Largest(root); } }; int main() { BinaryTree<int> *myBT = new BinaryTree<int>(); for(int i = 0; i < 10; i++) myBT->AddItem(rand() % 100); myBT->PrintTree(); myBT->Largest(); } </int></int></class></cstdlib></string></iostream>
delete
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)