Advertisement
Purianite

Untitled

Sep 22nd, 2011
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. //binarytree.h
  2. #ifndef BINARY_SEARCH_TREE
  3. #define BINARY_SEARCH_TREE
  4. #include <queue>
  5. #include <stack>
  6. #include "player.h"
  7.  
  8. template<class T>
  9. class Queue : public queue<T>
  10. {
  11. public:
  12.     T dequeue()
  13.     {
  14.         T tmp = front();
  15.         queue<T>::pop();
  16.         return tmp;
  17.     }
  18.     void enqueue(const T& el)
  19.     {
  20.         push(el);
  21.     }
  22. };
  23.  
  24. template<class T>
  25. class BSTNode
  26. {
  27. public:
  28.     BSTNode()
  29.     {
  30.         left = right = 0;
  31.     }
  32.     BSTNode(const T& el, BSTNode *l = 0, BSTNode *r = 0)
  33.     {
  34.         key = el; left = l; right = r;
  35.     }
  36.     T key;
  37.     BSTNode *left, *right;
  38. };
  39.  
  40. class PlayerBST
  41. {
  42. public:
  43.     PlayerBST()
  44.     {
  45.         root = 0;
  46.     }
  47.     ~PlayerBST()
  48.     {
  49.         clear();
  50.     }
  51.     void clear()
  52.     {
  53.         clear(root); root = 0;
  54.     }
  55.     bool isEmpty() const
  56.     {
  57.         return root == 0;
  58.     }
  59.     void inorder()
  60.     { //default without input param
  61.         inorder(root); //delegate to overload
  62.     }
  63.     void insert(Player&);
  64.     Player* search(int searchForLevel) const
  65.     {
  66.         return search(root, searchForLevel);
  67.     }
  68.    
  69.     void deleteByMerging(BSTNode<Player>*&);
  70.     void findAndDeleteByMerging(Player&); //Wow, that's an ugly function name :3c
  71.     void breadthFirst();
  72.  
  73. protected:
  74.     BSTNode<Player>* root;
  75.     void clear(BSTNode<Player>*);
  76.     Player* search(BSTNode<Player>* p, int searchForLevel) const;
  77.     void inorder(BSTNode<Player>*); //overloaed
  78.     virtual void visit(BSTNode<Player>* p)
  79.     {
  80.         p->key.playerDisplay() ;
  81.     }
  82. };
  83.  
  84.  
  85. void PlayerBST::clear(BSTNode<Player> *p)
  86. {
  87.     if (p != 0)
  88.     {
  89.         clear(p->left);
  90.         clear(p->right);
  91.         delete p;
  92.     }
  93. }
  94.  
  95.  
  96. void PlayerBST::insert(Player& el)
  97. {
  98.     BSTNode<Player> *p = root, *prev = 0;
  99.     while (p != 0)
  100.     {
  101.         prev = p;
  102.         if (p->key.getLevel() < el.getLevel())
  103.             p = p->right;
  104.         else p = p->left;
  105.     }
  106.     if (root == 0) //tree is empty;
  107.         root = new BSTNode<Player>(el);
  108.     else if (prev->key.getLevel() < el.getLevel())
  109.         prev->right = new BSTNode<Player>(el);
  110.     else prev->left = new BSTNode<Player>(el);
  111. }
  112.  
  113. Player* PlayerBST::search(BSTNode<Player>* p, int searchForLevel) const
  114. {
  115.     while (p != 0)
  116.         if (searchForLevel == p->key.getLevel())
  117.             return &p->key;
  118.         else if (searchForLevel < p->key.getLevel())
  119.             p = p->left;
  120.         else p = p->right;
  121.         return 0;
  122. }
  123.  
  124.  
  125. void PlayerBST::inorder(BSTNode<Player> *p)
  126. {
  127.     if (p != 0)
  128.     {
  129.         inorder (p->left); //note recursive call
  130.         visit(p);
  131.         inorder(p->right); //note recursive call
  132.     }
  133. }
  134.  
  135. void PlayerBST::deleteByMerging(BSTNode<Player>*& node)
  136. {
  137.     BSTNode<Player> *tmp = node;
  138.     if (node != 0)
  139.     {
  140.         if (!node->right) //node has no right child
  141.             node = node-> left; //child (if any) is attached to parent
  142.         else if (node->left == 0) //no left child
  143.             node = node->right; //child is attached to parent
  144.         else
  145.         { //be ready for merging subtrees
  146.             tmp = node->left; //1. move left
  147.             while (tmp->right != 0) //2. and then right as far as possible;
  148.                 tmp = tmp->right;
  149.             tmp->right = node->right; //establish link between rightmost node of left subtree and right subtree
  150.                 tmp = node;
  151.                 node = node->left;
  152.         }
  153.         delete tmp;
  154.     }
  155. }
  156.  
  157. void PlayerBST::findAndDeleteByMerging(Player& el)
  158. {
  159.     BSTNode<Player> *node = root, *prev = 0;
  160.     while(node != 0)
  161.     {
  162.         if (node->key.getLevel() == el.getLevel())
  163.             break;
  164.         prev = node;
  165.         if (node->key.getLevel() < el.getLevel())
  166.             node = node->right;
  167.         else node = node->left;
  168.     }
  169.     if (node != 0 && node->key.getLevel() == el.getLevel())
  170.         if (node == root)
  171.             deleteByMerging(root);
  172.         else if (prev->left == node)
  173.             deleteByMerging(prev->left);
  174.         else deleteByMerging(prev->right);
  175.     else if (root != 0)
  176.         cout << "Key not found in the tree\n";
  177.     else cout << "The tree is empty\n";
  178. }
  179.  
  180. void PlayerBST::breadthFirst()
  181. {
  182.     Queue<BSTNode<Player>*> queue;
  183.     BSTNode<Player> *p = root;
  184.     if (p != 0)
  185.     {
  186.         queue.enqueue(p);
  187.         while (!queue.empty())
  188.         {
  189.             p = queue.dequeue();
  190.             visit(p);
  191.             if (p->left != 0)
  192.                 queue.enqueue(p->left);
  193.             if (p->right != 0)
  194.                 queue.enqueue(p->right);
  195.         }
  196.     }
  197. }
  198.  
  199. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement