Why are the unique pointers annoying to use

This commit is contained in:
2023-03-05 23:13:34 -06:00
parent f3de22da46
commit 8370fa19f1
2 changed files with 298 additions and 50 deletions
+51 -19
View File
@@ -1,37 +1,69 @@
#ifndef TREES_HPP
#define TREES_HPP
#include <memory>
#include <string>
#include <vector>
// Namespace for different implementations of trees
namespace tree_implementation
{
// Base Tree class
class Tree
// General nodes for Tree
struct TreeNode
{
std::string key;
std::string color;
std::unique_ptr<TreeNode> leftChild;
std::unique_ptr<TreeNode> rightChild;
std::unique_ptr<TreeNode> parent;
TreeNode(std::string word);
~TreeNode(void);
TreeNode(const TreeNode& rhs);
TreeNode& operator=(const TreeNode& rhs);
};
// General list for Tree
class TreeList
{
public:
Tree(void);
void Insert(void);
void Search(void);
void InOrderTreeTraversal(void);
std::unique_ptr<TreeNode> head;
TreeList(void);
void InsertAtStart(std::string word);
void InsertAtEnd(std::string word);
void InsertAtPosition(std::string word);
void Remove(std::string word);
void Print(void);
protected:
virtual void PrintParentKey(std::string key) = 0;
virtual void PrintLeftChild(std::string key) = 0;
virtual void PrintRightChild(std::string key) = 0;
virtual void PrintPathToRoot(std::string key) = 0;
;
private:
;
};
// Binary Search Tree operations
// TODO: Implement BST
class BinarySearchTree : public Tree
// Base Tree class
class TreeInterface
{
public:
TreeList tree;
TreeInterface(void);
TreeNode* Search(std::string wordToFind);
bool IsSearchSuccessful(std::unique_ptr<TreeNode> foundNode);
void InOrderTreeTraversal(std::unique_ptr<TreeNode> viewedNode);
void PrintParentKey(std::string key);
void PrintLeftChild(std::string key);
void PrintRightChild(std::string key);
protected:
virtual void Insert(std::unique_ptr<TreeNode> z);
virtual void PrintPathToRoot(std::string key) = 0;
private:
TreeNode* Insert(std::unique_ptr<TreeNode> root, std::unique_ptr<TreeNode> newNode);
TreeNode* Search(std::unique_ptr<TreeNode> viewedNode, std::string wordToFind);
};
// Binary Search Tree operations
class BinarySearchTree : public TreeInterface
{
public:
void Insert(std::string keyToInsert);
void PrintPathToRoot(std::string key);
protected:
;
@@ -40,13 +72,10 @@ namespace tree_implementation
};
// Red-Black Tree operations
// TODO: Implement Red-black tree
class RedBlackTree : public Tree
class RedBlackTree : public TreeInterface
{
public:
void PrintParentKey(std::string key);
void PrintLeftChild(std::string key);
void PrintRightChild(std::string key);
void Insert(std::string keyToInsert);
void PrintPathToRoot(std::string key);
void PrintColor(std::string key);
void PrintParentColor(std::string key);
@@ -54,7 +83,10 @@ namespace tree_implementation
protected:
;
private:
;
void InsertFixup(std::unique_ptr<TreeNode> z);
std::unique_ptr<TreeNode> GetUncleNode(std::unique_ptr<TreeNode> startNode);
void LeftRotate(std::unique_ptr<TreeNode> x);
void RightRotate(std::unique_ptr<TreeNode> x);
};
}