Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class BTNode // CLASS BINAR TREE
- {
- private:
- int data;
- BTNode* pLeft;
- BTNode* pRight;
- public:
- BTNode(int _data = 0, BTNode* _pLeft = nullptr, BTNode* _pRight = nullptr) // CONSTR
- {
- data = _data;
- pLeft = _pLeft;
- pRight = _pRight;
- }
- void setData(int _data) // SET DATA
- {
- data = _data;
- }
- int getData() const // GET DATA
- {
- return data;
- }
- void setpLeft(BTNode* _pLeft) // SET pLeft
- {
- pLeft = _pLeft;
- }
- BTNode* getpLeft() // GET pLeft
- {
- return pLeft;
- }
- const BTNode* getpLeft() const // CONST GET pLeft
- {
- return pLeft;
- }
- void setpRight(BTNode* _pRight) // SET pRight
- {
- pRight = _pRight;
- }
- BTNode* getpRight() // GET pRight
- {
- return pRight;
- }
- const BTNode* getpRight() const // CONST GET pRight
- {
- return pRight;
- }
- };
- //----------------------------------------------------------------------
- void treePrint(BTNode* root) // TREE PRINT
- {
- if (root!=nullptr)
- {
- treePrint(root->getpLeft());
- cout<<root->getData()<<" & "<<root<<endl;
- treePrint(root->getpRight());
- }
- }
- void addBTNode(int data, BTNode* root) // ADD NODE
- {
- if (root != nullptr)
- {
- if (data == root->getData())
- cout<<"\n[!] DATA ALREADY EXIST !!!\n";
- else if (data > root->getData()) // go right
- {
- if (root->getpRight() == nullptr)
- {
- //cout<<"\n[!] I FOUND PLACE FOR ("<<data<<")! RIGHT FOR "<<root->getData()<<"\n"<<endl;
- root->setpRight(new BTNode(data));
- }
- else
- addBTNode(data,root->getpRight());
- }
- else // go left
- {
- if (root->getpLeft() == nullptr)
- {
- //cout<<"\n[!] I FOUND PLACE FOR ("<<data<<")! LEFT FOR "<<root->getData()<<"\n"<<endl;
- root->setpLeft(new BTNode(data));
- }
- else
- addBTNode(data,root->getpLeft());
- }
- }
- else
- cout<<"\n[!] NOT FOUND DATA PLACE FOR 'NULL'. TRY AGAIN !\n"<<endl;
- }
- BTNode* treeFindMax(BTNode* root) // FIND MAX NODE
- {
- cout<<"FUNCROOT: "<<root<<endl;
- if (root!=nullptr) // if siso
- {
- if (root->getpRight() != nullptr)
- treeFindMax(root->getpRight());
- else
- cout<<"\n[!] FOUND MAXIMUM DATA, IS: "<<root->getData()<<" & "<<root<<"\n"<<endl;
- return root;
- }
- else
- cout<<"\n[!] NOT FOUND DATA FOR 'NULL'. TRY AGAIN !\n"<<endl;
- return nullptr;
- }
- int main()
- {
- BTNode* root = new BTNode(25);
- addBTNode(10,root);
- addBTNode(41,root);
- addBTNode(7,root);
- addBTNode(15,root);
- addBTNode(36,root);
- addBTNode(54,root);
- addBTNode(60,root);
- addBTNode(34,root);
- addBTNode(37,root);
- cout<<"MAINROOT: "<<root<<endl<<endl;
- cout<<"RETURNED: "<<treeFindMax(root)<<endl<<endl;
- treePrint(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement