Binary tree in pattern

so I want to make code that creates a binary tree containing data, for example, ints, for example 1,6,2,10,8, and pop, I get the largest number, and after that it is deleted from the tree, and on push I can insert a new item. And that should be in the template, so I can easily change the data type that I want to save in the tree. Now I got the tree so far, it works fine without a template, I can add elements and I can print them, but when I try to put it in a template, I get the following error: using the template class requires a list of template arguments . What could be the problem? Perhaps I am doing this completely wrong. Any suggestions are welcome.

So far I have received the following code:

#include <iostream> 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); } } public: BinaryTree() { root = NULL; } void AddItem(T newData) { Insert(newData, root); } void PrintTree() { PrintTree(root); } }; int main() { BinaryTree<int> *myBT = new BinaryTree(); myBT->AddItem(1); myBT->AddItem(7); myBT->AddItem(1); myBT->AddItem(10); myBT->AddItem(4); myBT->PrintTree(); } 
+4
source share
1 answer

In expression

 new BinaryTree() 

the BinaryTree identifier is a template, not a class. You probably meant

 new BinaryTree<int>() 
+5
source

Source: https://habr.com/ru/post/1381944/


All Articles