Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- template <typename T>
- class BTNode
- {
- T data;
- BTNode*pLeft;
- BTNode*pRigt;
- public:
- BTNode(const T& _data = T(), BTNode *_pLeft = nullptr, BTNode *_pRigt = nullptr) : data(_data), pLeft(_pLeft), pRigt(_pRigt) {}
- void setData(const T&_data)
- {
- T data = _data;
- }
- T getData()
- {
- return data;
- }
- void setpRight(BTNode*_pRigt)
- {
- pRigt = _pRigt;
- }
- void setpLeft(BTNode*_pLeft)
- {
- pLeft = _pLeft;
- }
- const BTNode *getpRight()const
- {
- return pRigt;
- }
- BTNode *getpRight()
- {
- return pRigt;
- }
- const BTNode *getpLeft()const
- {
- return pLeft;
- }
- BTNode *getpLeft()
- {
- return pLeft;
- }
- bool isLeaf()const
- {
- if (pLeft == nullptr&&pRigt == nullptr)
- {
- return true;
- }
- }
- };
- void fillTree(BTNode<string>*&root)
- {
- root = new BTNode<string>("Оно Живет в воде?");
- BTNode<string>*temp = new BTNode <string>("У него есть жабры?");
- temp->setpLeft(new BTNode<string>("Щука"));
- temp->setpRight(new BTNode<string>("Лягушка"));
- root->setpLeft(temp);
- temp = new BTNode <string>("у него 4 лапы?");
- temp->setpLeft(new BTNode<string>("Кот"));
- temp->setpRight(new BTNode<string>("Змея"));
- root->setpRight(temp);
- }
- template <typename T>
- void printTree(BTNode <T>*root)
- {
- if (root != nullptr)
- {
- printTree(root->getpLeft());
- //cout << root->getData() << endl;
- printTree(root->getpRight());
- cout << root->getData() << endl;
- //cout << root->getData() << endl;
- }
- }
- void main()
- {
- setlocale(LC_ALL, "Russian");
- BTNode <string>*root = nullptr;
- fillTree(root);
- printTree(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement