Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.63 KB | None | 0 0
  1.  
  2. #include <vector>
  3. #ifndef BST_H
  4. #define BST_H
  5.  
  6.  
  7. using namespace std;
  8.  
  9. template <class T>
  10. class LumpOfDataStuff
  11. {
  12.     public:
  13.  
  14.         LumpOfDataStuff() {
  15.             leftbit = 0;
  16.             rightbit = 0;
  17.         } /**< Empty Constructor for LumpOfDataStuff, intiates leftbit and rightbit to 0 */
  18.  
  19.  
  20.         LumpOfDataStuff(T data) {
  21.             dat = data;
  22.             leftbit = 0;
  23.             rightbit = 0;
  24.          } /**< Constructor for LumpOfDataStuff, takes data of type T */
  25.         virtual ~LumpOfDataStuff() {} /**<  */
  26.         const T data() {return dat; } /**< Returns a const reference to data */
  27.         void setdata(T data) { dat = data; } /**< Sets internal data to passed in value */
  28.  
  29.         void SetleftbitLumpOfDataStuff(LumpOfDataStuff<T> * LumpOfDataStuff){ leftbit = LumpOfDataStuff; } /**< Sets the leftbit LumpOfDataStuff to the passed in LumpOfDataStuff pointer */
  30.  
  31.  
  32.         void SetrightbitLumpOfDataStuff(LumpOfDataStuff<T> * LumpOfDataStuff) { rightbit = LumpOfDataStuff; } /**< Sets the leftbit LumpOfDataStuff to the passed in LumpOfDataStuff pointer */
  33.         LumpOfDataStuff<T>* leftbitLumpOfDataStuff() { return leftbit; }/**< Returns a Pointer to the leftbit LumpOfDataStuff */
  34.         LumpOfDataStuff<T>* rightbitLumpOfDataStuff() {return rightbit;} /**< Returns a Pointer to the rightbit LumpOfDataStuff */
  35.  
  36.  
  37.     protected:
  38.  
  39.     private:
  40.         T dat; /**< Private value of Type T used to store data for the LumpOfDataStuff */
  41.         LumpOfDataStuff* leftbit; /**< Pointer to the leftbit LumpOfDataStuff */
  42.         LumpOfDataStuff* rightbit; /**< Pointer to the rightbit LumpOfDataStuff */
  43. };
  44.  
  45. #endif // LumpOfDataStuff_H
  46.  
  47.  
  48. template <class T>
  49. class BST
  50. {
  51.     public:
  52.         BST(); /**< BST Constructor */
  53.         virtual ~BST(); /**< BST Destructor */
  54.  
  55.         /** \brief Inserts an item into the Binary Search Tree
  56.          * The type selected must at least have a less than '<' operator that can compare between items
  57.          * \param T key The item to be inserted
  58.          *
  59.          */
  60.  
  61.         void insert(T key);
  62.  
  63.         /** \brief Searches the BST in order and returns the associated LumpOfDataStuff.
  64.          * In order searches occurs by searching and processing the leftbit side, then itself and then the rightbit side.
  65.          * \param T key The Item to be found in the tree. T must have a less than '<' operator for comparison purposes
  66.          * \return Returns a pointer of type T if the LumpOfDataStuff is found. Otherwise returns 0
  67.          *
  68.          */
  69.  
  70.  
  71.         LumpOfDataStuff<T> * searchInOrder(T key);
  72.  
  73.  
  74.          */
  75.         LumpOfDataStuff<T> * searchPostOrder(T key);
  76.  
  77.  
  78.         LumpOfDataStuff<T> * searchPreOrder(T key);
  79.  
  80.  
  81.         void destroy_tree(T key);
  82.  
  83.  
  84.         void sortedInsertation(Vector<T> sarr);
  85.  
  86.  
  87.     private:
  88.  
  89.    
  90.  
  91.         LumpOfDataStuff<T> * searchInOrder(T key,LumpOfDataStuff<T> * parent);
  92.  
  93.      
  94.         LumpOfDataStuff<T> * searchPostOrder(T key,LumpOfDataStuff<T> * parent);
  95.  
  96.  
  97.         LumpOfDataStuff<T> * searchPreOrder(T key,LumpOfDataStuff<T> * parent);
  98.  
  99.    
  100.  
  101.         LumpOfDataStuff<T> * LumpOfDataStuffParent(T key,LumpOfDataStuff<T> * parent);
  102.  
  103.  
  104.  
  105.         void insertR(LumpOfDataStuff<T> * newLumpOfDataStuff, LumpOfDataStuff<T> * parent);
  106.  
  107.  
  108.         LumpOfDataStuff<T>* root; /**< The root LumpOfDataStuff */
  109.         void destroy_tree(LumpOfDataStuff<T> * leaf);
  110.         void dividedInsert(Vector<T> vec, int leftbit, int rightbit);
  111. };
  112.  
  113. template <class T>
  114. BST<T>::BST()
  115. {
  116.     root = 0;
  117.     //ctor
  118. }
  119.  
  120. template <class T>
  121. void BST<T>::insert(T newdata)
  122. {
  123.     LumpOfDataStuff<T>* newLumpOfDataStuff = new LumpOfDataStuff<T>(newdata);
  124.     newLumpOfDataStuff->setdata(newdata);
  125.     if(root == 0)
  126.     {
  127.         root = newLumpOfDataStuff;
  128.     }
  129.     else
  130.     {
  131.         insertR(newLumpOfDataStuff,root);
  132.     }
  133.  
  134. }
  135.  
  136. template <class T>
  137. void BST<T>::sortedInsertation(Vector<T> sarr)
  138. {
  139.     if(sarr.size() > 0)
  140.         dividedInsert(sarr,0,sarr.size()-1);
  141. }
  142.  
  143. template<class T>
  144. void BST<T>::dividedInsert(Vector<T> vec, int leftbit,int rightbit)
  145. {
  146.     if(leftbit == rightbit)
  147.     {
  148.         //cout << endl << leftbit << " " << rightbit << endl;
  149.         insert(vec[leftbit]);
  150.     }
  151.     else
  152.     {
  153.         //work out current middle and insert that
  154.         int midPOS = leftbit +((rightbit-leftbit)/2);
  155.  
  156.         //cout << endl << leftbit << " " << midPOS << " " << rightbit << endl;
  157.  
  158.         insert(vec[midPOS]);
  159.         //call leftbit insert
  160.         if(midPOS != leftbit)
  161.             dividedInsert(vec,leftbit,midPOS-1);
  162.         //call rightbit insert
  163.         if(midPOS+1 == rightbit)
  164.             dividedInsert(vec,rightbit,rightbit);
  165.         else
  166.         {
  167.             dividedInsert(vec,midPOS+1,rightbit);
  168.         }
  169.  
  170.     }
  171. }
  172.  
  173. template <class T>
  174. void BST<T>::insertR(LumpOfDataStuff<T> * newLumpOfDataStuff,LumpOfDataStuff<T> * parent)
  175. {
  176.     if(newLumpOfDataStuff->data() < parent->data())
  177.     {
  178.         if(parent->leftbitLumpOfDataStuff() == 0)
  179.         {
  180.             parent->SetleftbitLumpOfDataStuff(newLumpOfDataStuff);
  181.         }
  182.         else
  183.         {
  184.             insertR(newLumpOfDataStuff,parent->leftbitLumpOfDataStuff());
  185.         }
  186.     }
  187.     else
  188.     {
  189.         if(parent->rightbitLumpOfDataStuff() == 0)
  190.         {
  191.             parent->SetrightbitLumpOfDataStuff(newLumpOfDataStuff);
  192.         }
  193.         else
  194.         {
  195.             insertR(newLumpOfDataStuff,parent->rightbitLumpOfDataStuff());
  196.         }
  197.     }
  198. }
  199.  
  200. template <class T>
  201. LumpOfDataStuff<T> * BST<T>::searchInOrder(T key)
  202. {
  203.     return searchInOrder(key,root);
  204. }
  205.  
  206. template <class T>
  207. LumpOfDataStuff<T> * BST<T>::searchPreOrder(T key)
  208. {
  209.     return searchPreOrder(key,root);
  210. }
  211.  
  212. template <class T>
  213. LumpOfDataStuff<T> * BST<T>::searchPostOrder(T key)
  214. {
  215.     return searchPreOrder(key,root);
  216. }
  217.  
  218. template <class T>
  219. LumpOfDataStuff<T> * BST<T>::searchInOrder(T key,LumpOfDataStuff<T> * parent)
  220. {
  221.  
  222.     if(parent != 0)
  223.     {
  224.         if(key < parent->data())
  225.             return searchInOrder(key,parent->leftbitLumpOfDataStuff());
  226.         if(key == parent->data())
  227.             return parent;
  228.         else
  229.             return searchInOrder(key,parent->rightbitLumpOfDataStuff());
  230.     }
  231.     else
  232.         return 0;
  233. }
  234.  
  235. template <class T>
  236. LumpOfDataStuff<T> * BST<T>::searchPreOrder(T key, LumpOfDataStuff<T> * parent)
  237. {
  238.     if(parent != 0)
  239.     {
  240.         if(key == parent->data())
  241.             return parent;
  242.         if(key < parent->data())
  243.             return searchPreOrder(key,parent->leftbitLumpOfDataStuff());
  244.         else
  245.             return searchPreOrder(key,parent->rightbitLumpOfDataStuff());
  246.     }
  247.     else
  248.         return 0;
  249. }
  250.  
  251. template <class T>
  252. LumpOfDataStuff<T> * BST<T>::searchPostOrder(T key, LumpOfDataStuff<T> * parent)
  253. {
  254.     if(parent != 0)
  255.     {
  256.  
  257.         if(key < parent->data())
  258.             return searchPostOrder(key,parent->leftbitLumpOfDataStuff());
  259.         else if(key > parent->data())
  260.             return searchPostOrder(key,parent->rightbitLumpOfDataStuff());
  261.         if(key == parent->data())
  262.             return parent;
  263.     }
  264.     return 0;
  265. }
  266.  
  267.  
  268. template <class T>
  269. LumpOfDataStuff<T> * BST<T>::LumpOfDataStuffParent(T key, LumpOfDataStuff<T> * parent)
  270. {
  271.     if(parent == 0)
  272.         return 0;
  273.     if(parent->leftbitLumpOfDataStuff() == 0 && parent->rightbitLumpOfDataStuff() == 0)
  274.         return 0;
  275.     if((parent->leftbitLumpOfDataStuff() != 0 && parent->leftbitLumpOfDataStuff()->data() == key) || (parent->rightbitLumpOfDataStuff() != 0 && parent->rightbitLumpOfDataStuff()->data() == key))
  276.         return parent;
  277.     if(parent->data() > key)
  278.         return LumpOfDataStuffParent(key,parent->leftbitLumpOfDataStuff());
  279.     if(parent->data() < key)
  280.         return LumpOfDataStuffParent(key,parent->rightbitLumpOfDataStuff());
  281. }
  282.  
  283. template<class T>
  284. void BST<T>::destroy_tree(T key)
  285. {
  286.     LumpOfDataStuff<T> * del = searchInOrder(key);
  287.     if(del != 0)
  288.     {
  289.         LumpOfDataStuff<T> * delpar = LumpOfDataStuffParent(key,root);
  290.         bool leftbit = true;
  291.         if(delpar->rightbitLumpOfDataStuff() != 0 && delpar->rightbitLumpOfDataStuff()->data() == key)
  292.             leftbit = false;
  293.         destroy_tree(del);
  294.         if(leftbit)
  295.             delpar->SetleftbitLumpOfDataStuff(0);
  296.         else
  297.             delpar->SetrightbitLumpOfDataStuff(0);
  298.     }
  299. }
  300.  
  301. template <class T>
  302. void BST<T>::destroy_tree(LumpOfDataStuff<T> * leaf)
  303. {
  304.     if(leaf != 0)
  305.     {
  306.         destroy_tree(leaf->leftbitLumpOfDataStuff());
  307.         destroy_tree(leaf->rightbitLumpOfDataStuff());
  308.         delete leaf;
  309.     }
  310. }
  311.  
  312.  
  313.  
  314. template <class T>
  315. BST<T>::~BST()
  316. {
  317.     destroy_tree(root);
  318.     //dtor
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement