Update example of red-black and binary tree algorithms

+ Use copy-swap idiom for assignment operators
+ Update and organize CMakeLists subdirectories for algorithm examples
This commit is contained in:
2021-06-09 10:16:24 -04:00
parent 202953de49
commit a8b6627135
6 changed files with 51 additions and 60 deletions

View File

@@ -17,51 +17,48 @@
*******************************************************************************/
/** BinarySearchTree Copy Assignment Operator
* @brief Empty the calling object's root BinaryNode, and copy the rhs data
* @brief Empty the calling object's root BinaryNode, and swap the rhs data
* + Utilizes the copy-swap-idiom
*
* Runs in O( n ) time, since we visit each node in the BST once
* + Where n is the total number of nodes within the BST
*
* makeEmpty() and clone() are both O( n ), and we call each sequentially
* + This would appear to be O( 2n ), but we drop the constant of 2
* Runs in O( n ) time, since we call makeEmpty() which runs is O( n )
*
* @param rhs The BST to copy, beginning from its root BinaryNode
* @return const BinarySearchTree& The copied BinarySearchTree object
* @return BinarySearchTree The copied BinarySearchTree object
*/
BinarySearchTree& BinarySearchTree::operator=(const BinarySearchTree &rhs)
BinarySearchTree& BinarySearchTree::operator=(BinarySearchTree rhs)
{
// If the objects are already equal, do nothing
if (this == &rhs) return *this;
// Empty this->root
makeEmpty(root);
// Copy rhs to this->root
root = clone(rhs.root);
std::swap(root, rhs.root);
return *this;
}
/* BinaryNode Copy Constructor
* @brief Recursively copy rhs node and all child nodes
*
* Runs in O( n ) time, since we visit each node in the BST once
* + Where n is the total number of nodes within the BST
*
* @param rhs An existing BST to initialize this node (and children) with
*/
BinarySearchTree::BinaryNode::BinaryNode(BinaryNode * toCopy)
BinarySearchTree::BinaryNode::BinaryNode(const BinaryNode &rhs)
{
// Base case, breaks recursion when we hit a null node
// + Returns to the previous call in the stack
if (toCopy == nullptr) return;
if (isEmpty(this)) return;
// Set the element of this BinaryNode to the value in toCopy->element
element = toCopy->element;
element = rhs.element;
// If there is a left / right node, copy it using recursion
// + If there is no left / right node, set them to nullptr
if (toCopy->left != nullptr) {
left = new BinaryNode(toCopy->left);
if (rhs.left != nullptr) {
left = new BinaryNode(*rhs.left);
left->parent = this;
}
if (toCopy->right != nullptr) {
right = new BinaryNode(toCopy->right);
if (rhs.right != nullptr) {
right = new BinaryNode(*rhs.right);
right->parent = this;
}
}
@@ -113,19 +110,6 @@ void BinarySearchTree::makeEmpty(BinarySearchTree::BinaryNode * & tree)
}
}
/** isEmpty
* @brief Determine whether or not the calling BST object is empty
*
* Runs in constant time, O( 1 )
*
* @return true If this->root node points to an empty tree (nullptr)
* @return false If this->root node points to a constructed BinaryNode
*/
bool BinarySearchTree::isEmpty() const
{
return root == nullptr;
}
/** insert
* @brief Insert a value into the tree starting at a given BinaryNode
* + Uses recursion
@@ -268,6 +252,7 @@ BinarySearchTree::BinaryNode *BinarySearchTree::search(
if (start == nullptr || start->element == value) return start;
else if (start->element < value) return search(value, start->right);
else if (start->element > value) return search(value, start->left);
else return nullptr;
}
/** findMin
@@ -382,7 +367,7 @@ BinarySearchTree::BinaryNode * BinarySearchTree::clone(BinaryNode *start)
if (start == nullptr) return nullptr;
// Construct all child nodes through recursion, return root node
return new BinaryNode(start);
return new BinaryNode(*start);
}
/** transplant

View File

@@ -27,12 +27,13 @@ public:
BinaryNode(const int &el, BinaryNode *lt, BinaryNode *rt, BinaryNode *p)
:element(el), left(lt), right(rt), parent(p) {};
// Ctor for a node and any downstream nodes
explicit BinaryNode(BinaryNode * toCopy);
BinaryNode(const BinaryNode &rhs);
};
BinarySearchTree() : root(nullptr) {};
BinarySearchTree(const BinarySearchTree &rhs) : root(rhs.clone(rhs.root)) {};
BinarySearchTree& operator=(const BinarySearchTree& rhs);
BinarySearchTree(const BinarySearchTree &rhs) :
root(BinarySearchTree::clone(rhs.root)) {};
BinarySearchTree& operator=(BinarySearchTree rhs);
~BinarySearchTree() { makeEmpty(root);};
inline BinaryNode * getRoot() const { return root;}
@@ -44,7 +45,8 @@ public:
inline void makeEmpty() { makeEmpty(root);}
void makeEmpty(BinaryNode *&tree);
// Checks if this BST is empty
bool isEmpty() const;
inline bool isEmpty() const { return isEmpty(root);}
static inline bool isEmpty(const BinaryNode *rhs) { return rhs == nullptr;}
// Insert and remove values from a tree or subtree
inline void insert(const int &x) { insert(x, root, nullptr);}
@@ -70,7 +72,11 @@ public:
BinaryNode * findMin(BinaryNode *start) const;
BinaryNode * findMax(BinaryNode *start) const;
inline BinaryNode * predecessor(const int &value) const
{ return predecessor(search(value));}
BinaryNode * predecessor(BinaryNode *startNode) const;
inline BinaryNode * successor(const int &value) const
{ return successor(search(value));}
BinaryNode * successor(BinaryNode *startNode) const;
private: