Advertisement
goldlinux

bug in return

Mar 6th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class BTNode    // CLASS BINAR TREE
  6. {
  7. private:
  8.     int data;
  9.     BTNode* pLeft;
  10.     BTNode* pRight;
  11. public:
  12.     BTNode(int _data = 0, BTNode* _pLeft = nullptr, BTNode* _pRight = nullptr)  // CONSTR
  13.     {
  14.         data = _data;
  15.         pLeft = _pLeft;
  16.         pRight = _pRight;
  17.     }
  18.     void setData(int _data) // SET DATA
  19.     {
  20.         data = _data;  
  21.     }
  22.     int getData() const // GET DATA
  23.     {
  24.         return data;   
  25.     }
  26.     void setpLeft(BTNode* _pLeft)   // SET pLeft
  27.     {
  28.         pLeft = _pLeft;
  29.     }
  30.     BTNode* getpLeft()  // GET pLeft
  31.     {
  32.         return pLeft;
  33.     }
  34.     const BTNode* getpLeft() const  // CONST GET pLeft
  35.     {
  36.         return pLeft;
  37.     }
  38.     void setpRight(BTNode* _pRight) // SET pRight
  39.     {
  40.         pRight = _pRight;
  41.     }
  42.     BTNode* getpRight() // GET pRight
  43.     {
  44.         return pRight;
  45.     }
  46.     const BTNode* getpRight() const // CONST GET pRight
  47.     {
  48.         return pRight;
  49.     }
  50. };
  51. //----------------------------------------------------------------------
  52. void treePrint(BTNode* root)    // TREE PRINT
  53. {
  54.     if (root!=nullptr)
  55.     {
  56.         treePrint(root->getpLeft());
  57.         cout<<root->getData()<<" & "<<root<<endl;
  58.         treePrint(root->getpRight());
  59.     }
  60. }
  61. void addBTNode(int data, BTNode* root)  // ADD NODE
  62. {
  63.     if (root != nullptr)
  64.     {
  65.         if (data == root->getData())
  66.             cout<<"\n[!] DATA ALREADY EXIST !!!\n";
  67.            
  68.         else if (data > root->getData()) // go right
  69.         {
  70.             if (root->getpRight() == nullptr)
  71.             {
  72.                 //cout<<"\n[!] I FOUND PLACE FOR ("<<data<<")! RIGHT FOR "<<root->getData()<<"\n"<<endl;
  73.                 root->setpRight(new BTNode(data));
  74.             }
  75.             else
  76.                 addBTNode(data,root->getpRight());
  77.         }
  78.        
  79.         else // go left
  80.         {
  81.             if (root->getpLeft() == nullptr)
  82.             {
  83.                 //cout<<"\n[!] I FOUND PLACE FOR ("<<data<<")! LEFT FOR "<<root->getData()<<"\n"<<endl;
  84.                 root->setpLeft(new BTNode(data));
  85.             }
  86.             else
  87.                 addBTNode(data,root->getpLeft());
  88.         }
  89.     }
  90.     else
  91.         cout<<"\n[!] NOT FOUND DATA PLACE FOR 'NULL'. TRY AGAIN !\n"<<endl;
  92. }
  93. BTNode* treeFindMax(BTNode* root) // FIND MAX NODE
  94. {
  95.     cout<<"FUNCROOT: "<<root<<endl;
  96.     if (root!=nullptr) // if siso
  97.     {
  98.         if (root->getpRight() != nullptr)
  99.             treeFindMax(root->getpRight());
  100.         else
  101.             cout<<"\n[!] FOUND MAXIMUM DATA, IS: "<<root->getData()<<" & "<<root<<"\n"<<endl;
  102.             return root;
  103.     }
  104.        
  105.     else
  106.         cout<<"\n[!] NOT FOUND DATA FOR 'NULL'. TRY AGAIN !\n"<<endl;
  107.         return nullptr;
  108. }
  109. int main()
  110. {
  111.     BTNode* root = new BTNode(25);
  112.     addBTNode(10,root);
  113.     addBTNode(41,root);
  114.     addBTNode(7,root);
  115.     addBTNode(15,root);
  116.     addBTNode(36,root);
  117.     addBTNode(54,root);
  118.     addBTNode(60,root);
  119.    
  120.     addBTNode(34,root);
  121.     addBTNode(37,root);
  122.  
  123.     cout<<"MAINROOT: "<<root<<endl<<endl;
  124.     cout<<"RETURNED: "<<treeFindMax(root)<<endl<<endl;
  125.     treePrint(root);
  126.    
  127.    
  128.     return 0;  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement