Update datastructs/binarysearchtree example

+ Utilize copy-swap idiom, miscellaneous clean-up of conditions and return values
This commit is contained in:
2021-06-09 11:00:02 -04:00
parent a8b6627135
commit 8f211b1603
4 changed files with 177 additions and 173 deletions

View File

@@ -1,10 +1,10 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a binary search tree implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example of a binary search tree implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
################################################################################
## bst.h
*/
@@ -16,41 +16,42 @@
// TODO: Add balance() method to balance overweight branches
class BinarySearchTree {
public:
BinarySearchTree() : root(NULL) {};
BinarySearchTree(const BinarySearchTree &rhs) : root(rhs.clone(rhs.root)) {};
const BinarySearchTree& operator=(const BinarySearchTree& rhs);
~BinarySearchTree();
const int & findMin() const;
const int & findMax() const;
bool contains(const int &x) const;
bool isEmpty() const;
void insert(const int &x);
void remove(const int &x);
void makeEmpty();
void printInOrder() const;
void printPostOrder() const;
void printPreOrder() const;
public:
BinarySearchTree() : root(nullptr) {};
BinarySearchTree(const BinarySearchTree &rhs) : root(clone(rhs.root)) {};
BinarySearchTree& operator=(BinarySearchTree rhs);
~BinarySearchTree() { makeEmpty(root);};
int findMin() const;
int findMax() const;
bool contains(const int &x) const;
bool isEmpty() const;
void insert(const int &x);
void remove(const int &x);
void makeEmpty();
void printInOrder() const;
void printPostOrder() const;
void printPreOrder() const;
private:
struct BinaryNode{
int element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const int &el, BinaryNode *lt, BinaryNode *rt)
:element(el), left(lt), right(rt) {};
};
BinaryNode *root;
BinaryNode * clone(BinaryNode *t) const;
void insert(const int &x, BinaryNode *&t) const;
void remove(const int &x, BinaryNode *&t) const;
BinaryNode * findMin(BinaryNode *t) const;
BinaryNode * findMax(BinaryNode *t) const;
bool contains(const int &x, BinaryNode *t) const;
void makeEmpty(BinaryNode * & t);
void printInOrder(BinaryNode *t) const;
void printPostOrder(BinaryNode *t) const;
void printPreOrder(BinaryNode *t) const;
private:
struct BinaryNode{
int element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const int &el, BinaryNode *lt, BinaryNode *rt)
:element(el), left(lt), right(rt) {};
};
BinaryNode *root;
BinaryNode * clone(BinaryNode *t) const;
void insert(const int &x, BinaryNode *&t) const;
void remove(const int &x, BinaryNode *&t) const;
BinaryNode * findMin(BinaryNode *t) const;
BinaryNode * findMax(BinaryNode *t) const;
bool contains(const int &x, BinaryNode *t) const;
void makeEmpty(BinaryNode * & t);
void printInOrder(BinaryNode *t) const;
void printPostOrder(BinaryNode *t) const;
void printPreOrder(BinaryNode *t) const;
};
#endif //BST_H