Advertisement
HimikoWerckmeister

RedBlackTree.h

Aug 2nd, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. #ifndef CMPT225RBTREE
  2. #define CMPT225RBTREE
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. /*
  8.     The node class contains the following attributes
  9.         a data attribute
  10.         a left child pointer
  11.         a right child pointer
  12.         a pointer to its parent
  13.         a boolean flag representing the colour bit, if the boolean is true then the node is
  14.         black and
  15.         if it is false
  16.         the node is red
  17. */
  18. template <class T>
  19. class Node
  20. {
  21.     public:
  22.         T data;
  23.         Node *left;
  24.         Node *right;
  25.         Node *parent;
  26.         bool isBlack;
  27.         // Node constructors
  28.         //Node();
  29.         Node(T data);
  30.         Node(T data, Node* pL, Node *pR, Node* pParent);
  31.         Node(T data, Node *pL, Node* pR, Node *pParent, bool bColour);
  32.         Node(T data, Node *pL, Node* pR, bool bColour);
  33.  
  34. };
  35. /*
  36.     Red Black tree class interface
  37. */
  38. template <class T>
  39. class RedBlackTree
  40. {
  41.     public:
  42.         /*
  43.             Constructor, copy C'tor, D'tor, and overloaded assignment operator
  44.         */
  45.         RedBlackTree();
  46.         RedBlackTree(const RedBlackTree<T> &rhs);
  47.         ~RedBlackTree();
  48.         RedBlackTree & operator=(const RedBlackTree<T> &rhs);
  49.         // searches if a particular datum exists in the tree
  50.         bool search(T data) const;
  51.         // inserts and removes datums from the tree checking if the node we want to insert is already there or not
  52.         // and if the node we want to remove exists
  53.         bool insert(T data);
  54.         bool remove(T data);
  55.         // finds the values between the specified bounds and returns an array countaining those values with an
  56.         // integer reference parameter denoting the size of the returned array
  57.         T *search(T firstParem, T secondParem, int &iNumElements);
  58.         // clears all of the values in the tree
  59.         void removeAll();
  60.         // returns an array containing all of the datums in the tree
  61.         T *dump(int& iSize);
  62.         // returns amount of nodes in the tree
  63.         int size() const;
  64.         // returns height of the tree
  65.         int height() const;
  66.         // returns a pointer to the root of the tree
  67.         Node<T> *getRoot();
  68.         // allows us to print the tree so we can see if our remove/insert methods are working as we expected
  69.         void Print();
  70.         // finds the minimum of the tree
  71.         T findMin();
  72.        
  73.  
  74.     private:
  75.         Node<T> *root;
  76.         // member variables that will enable us to perform memory allocations for dump and search
  77.         T *aDumpData;
  78.         T *aDumpTemp;
  79.         // member variables for second search function to store the values for the first and second parameters respectively
  80.         // so we can perform the comparison when we recursively call the associated helper function that gets called by the
  81.         // public search function with the parameters and return value as follows T*search( T fval, T secondval, int &n);
  82.         T firstvalue;
  83.         T secondvalue;
  84.         int iNodePos; // keeps track of index book keeping for dump function
  85.  
  86.         //int &iaPos;
  87.         // private helper function to determine if the value we are searching for exists or not
  88.         void PrintRecurse(Node<T> *pTr);
  89.         bool bPrivSearch(T data, Node<T> *pTr) const;
  90.         // helper function that will allow us find the pointer to a node that we are looking for
  91.         Node<T> *pSearchPriv(T data, Node<T> *pTr);
  92.         // Private helper that clones all of the nodes
  93.         Node<T> *CloneTree(const Node<T>* pTr);
  94.         // an internal helper function that will allow us to pass in a newly allocated node with the client's requested datum as the node's data
  95.         bool bInsert(Node<T>* pNode);
  96.  
  97.     private:
  98.         // private remove function that gets called by its public counterpart
  99.         bool bRemove(Node<T> *pNode);
  100.     private:
  101.         // Helper function that fixes violations when deleting a node from the  tree
  102.         void vPrivDeleteFix(Node<T> *pNode);
  103.         // Helper function to fix any RB tree violations that we may encounter in inserting the node in the tree
  104.         void vInsertFixViolation(Node<T> *pNode);
  105.         // Rotation helper methods
  106.         void LeftRotation(Node<T> *pNode);
  107.         void RightRotation(Node<T> *pNode);
  108.         // helper function to deallocate memory from the tree
  109.         void DeleteTreePrivate(Node<T>* pNode);
  110.         // helper to allow us to find the successor node
  111.         Node<T> * findMinPriv(Node<T>* pNode);
  112.         // helper function to help us find the height of the tree
  113.         int heightHelper(Node<T> *pNode) const;
  114.         // helper function to help us to find the amount of nodes in the tree
  115.         int sizeHelper(Node<T> *pNode) const;
  116.         // Helper function that gets called by our public dump function to return an array containing all of the nodes in the tree
  117.         void dumpPrivate(Node<T> *pNode,int &iPos);
  118.         // Helper function that gets called by our public searching the values between the bounds function to return an array containing those items
  119.         // works by recursively searching the left and right subtrees with extra constraints
  120.         void searchPrivate(Node<T>*pTr1, int &iNdex);
  121.          
  122. };
  123. #include "RedBlackTree.cpp"
  124. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement