diff --git a/src/11/helloworld/helloworld.cpp b/src/11/helloworld/helloworld.cpp index f04de7a..06b3d03 100755 --- a/src/11/helloworld/helloworld.cpp +++ b/src/11/helloworld/helloworld.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/src/12/c6/BinarySearchTree.hpp b/src/12/c6/BinarySearchTree.hpp new file mode 100644 index 0000000..9cabfcf --- /dev/null +++ b/src/12/c6/BinarySearchTree.hpp @@ -0,0 +1,112 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace zt { + template + class BinarySearchTree { + public: + using valueType = std::remove_cv_t; + private: + struct Node { + valueType val; + Node* leftSon{nullptr}; + Node* rightSon{nullptr}; + }; + Node* root{nullptr}; + + void _deleteTree(Node* node){ + if(node == nullptr) return; + _deleteTree(node->leftSon); + _deleteTree(node->rightSon); + delete node; + } + + void _inorderTraversal(Node* now, void(*func)(Node* now)) const { + if(now->leftSon != nullptr){ + _inorderTraversal(now->leftSon, func); + } + func(now); + if(now->rightSon != nullptr){ + _inorderTraversal(now->rightSon, func); + } + } + + void _toString(const Node* now, std::stringstream& ss) const { + if(now->leftSon != nullptr){ + _toString(now->leftSon, ss); + } + ss << now->val << ", "; + if(now->rightSon != nullptr){ + _toString(now->rightSon, ss); + } + } + + public: + BinarySearchTree() = default; + + void insert(const valueType& val) { + Node* newNode = new Node{}; + newNode->val=val; + if(root == nullptr){ + root = newNode; + return; + } + Node* current = root; + Node* parent = nullptr; + while(current != nullptr){ + parent = current; + if(val < current->val){ + current = current->leftSon; + } else { + current = current->rightSon; + } + } + if(val < parent->val){ + parent->leftSon = newNode; + } else { + parent->rightSon = newNode; + } + } + + valueType findMin() const { + if(root == nullptr){ + throw std::runtime_error("Tree is empty"); + } + Node* current = root; + while(current->leftSon != nullptr){ + current = current->leftSon; + } + return current->val; + } + + valueType findMax() const { + if(root == nullptr){ + throw std::runtime_error("Tree is empty"); + } + Node* current = root; + while(current->rightSon != nullptr){ + current = current->rightSon; + } + return current->val; + } + + std::string toString() const { + std::stringstream ss; + ss << "{ "; + if(root != nullptr){ + _toString(root, ss); + } + ss << "}"; + return ss.str(); + } + + ~BinarySearchTree(){ + _deleteTree(root); + } + }; +} diff --git a/src/12/c6/CartesianTree.hpp b/src/12/c6/CartesianTree.hpp new file mode 100644 index 0000000..61541d1 --- /dev/null +++ b/src/12/c6/CartesianTree.hpp @@ -0,0 +1,98 @@ +// #pragma once + +// #include +// #include +// #include +// #include +// #include +// #include +// #include "Stack.hpp" + +// namespace zt { +// template +// class CartesianTree{ +// public: +// using valueType = std::remove_cv_t; +// private: +// struct Node{ +// valueType val; +// Node* leftSon{nullptr}; +// Node* rightSon{nullptr}; +// }; +// Stack stack; +// Node* root{nullptr}; +// void _dfs(Node *now,void(*func)(Node* now)){ +// if(now->leftSon!=nullptr){ +// _dfs(now->leftSon, func); +// } +// if(now->rightSon!=nullptr){ +// _dfs(now->rightSon, func); +// } +// func(now); +// } +// void _dfs(const Node *const now,void(*func)(Node*const now))const{ +// if(now->leftSon!=nullptr){ +// _dfs(now->leftSon, func); +// } +// if(now->rightSon!=nullptr){ +// _dfs(now->rightSon, func); +// } +// func(now); +// } +// void _toString(const Node*const now,std::stringstream&ss)const{ +// if(now->leftSon!=nullptr){ +// _toString(now->leftSon, ss); +// } +// ss<val<<", "; +// if(now->rightSon!=nullptr){ +// _toString(now->rightSon, ss); +// } +// } +// void dfs(void(*func)(Node*const now))const{ +// if(root!=nullptr){ +// _dfs(root,func); +// } +// } +// void dfs(void(*func)(Node* now)){ +// if(root!=nullptr){ +// _dfs(root,func); +// } +// } +// public: +// constexpr explicit CartesianTree(){} +// constexpr void insert(valueType const& val){ +// Node *const newNode {new Node{}}; +// newNode->val=val; +// while(!stack.empty()&&valval){ +// newNode->leftSon=stack.top(); +// stack.pop(); +// } +// if(!stack.empty()){ +// stack.top()->rightSon=newNode; +// }else{ +// root = newNode; +// } +// stack.push(newNode); +// } +// constexpr valueType top()const{ +// if(root==nullptr){ +// throw std::runtime_error("the root of the Cartesian is nullptr"); +// } +// return root->val; +// } +// ~CartesianTree(){ +// dfs([](Node* now){ +// delete now; +// }); +// } +// std::string toString()const{ +// std::stringstream ss; +// ss<<"{ "; +// if(root!=nullptr){ +// _toString(root, ss); +// } +// ss<<" }"; +// return std::string(ss.str()); +// } +// }; +// } \ No newline at end of file diff --git a/src/12/c6/Stack.hpp b/src/12/c6/Stack.hpp new file mode 100644 index 0000000..07f7924 --- /dev/null +++ b/src/12/c6/Stack.hpp @@ -0,0 +1,59 @@ +#pragma once + +#include +#include +#include +namespace zt { + template + class Stack{ + private: + using valueType = std::remove_cv_t; + size_t stackSize{}; + struct Node{ + valueType value; + Node* previous; + }; + Node *topNode{nullptr}; + public: + constexpr explicit Stack(){} + constexpr size_t size()const noexcept{ + return stackSize; + } + constexpr valueType top()const{ + if(topNode==nullptr){ + throw std::runtime_error("the top of the struct stack is nullptr"); + } + return topNode->value; + } + constexpr bool empty()const noexcept{ + return topNode == nullptr; + } + constexpr Stack& push(valueType const& val)noexcept{ + Node* newNode = new Node{}; + newNode->value = val; + stackSize++; + if(topNode==nullptr){ + topNode = newNode; + }else{ + newNode->previous = topNode; + topNode = newNode; + } + return *this; + } + constexpr Stack& pop(){ + if(topNode==nullptr){ + throw std::runtime_error("the top of the stack is nullptr"); + } + stackSize--; + Node* oldNode{topNode}; + topNode=topNode->previous; + delete oldNode; + return *this; + } + ~Stack(){ + while (topNode!=nullptr) { + pop(); + } + } + }; +} \ No newline at end of file diff --git a/src/12/c6/c6_bst.cpp b/src/12/c6/c6_bst.cpp new file mode 100644 index 0000000..f0638ee --- /dev/null +++ b/src/12/c6/c6_bst.cpp @@ -0,0 +1,33 @@ +#include "BinarySearchTree.hpp" +#include "Stack.hpp" +#include + +int main(){ + zt::Stack stk; + std::cout< bst; + bst.insert(9); + std::cout<