update
This commit is contained in:
		
							parent
							
								
									04c2bd2ff2
								
							
						
					
					
						commit
						4cb35a1835
					
				| @ -1,4 +1,3 @@ | |||||||
| #include <array> |  | ||||||
| #include <cstdint> | #include <cstdint> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <utility> | #include <utility> | ||||||
|  | |||||||
							
								
								
									
										112
									
								
								src/12/c6/BinarySearchTree.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								src/12/c6/BinarySearchTree.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,112 @@ | |||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include <ostream> | ||||||
|  | #include <sstream> | ||||||
|  | #include <stdexcept> | ||||||
|  | #include <string> | ||||||
|  | #include <type_traits> | ||||||
|  | 
 | ||||||
|  | namespace zt { | ||||||
|  |     template<class T> | ||||||
|  |     class BinarySearchTree { | ||||||
|  |     public: | ||||||
|  |         using valueType = std::remove_cv_t<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); | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  | } | ||||||
							
								
								
									
										98
									
								
								src/12/c6/CartesianTree.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								src/12/c6/CartesianTree.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,98 @@ | |||||||
|  | // #pragma once
 | ||||||
|  | 
 | ||||||
|  | // #include <iostream>
 | ||||||
|  | // #include <ostream>
 | ||||||
|  | // #include <sstream>
 | ||||||
|  | // #include <stdexcept>
 | ||||||
|  | // #include <string>
 | ||||||
|  | // #include <type_traits>
 | ||||||
|  | // #include "Stack.hpp"
 | ||||||
|  | 
 | ||||||
|  | // namespace zt {
 | ||||||
|  | //     template<class T>
 | ||||||
|  | //     class CartesianTree{
 | ||||||
|  | //     public:
 | ||||||
|  | //         using valueType = std::remove_cv_t<T>;
 | ||||||
|  | //     private:
 | ||||||
|  | //         struct Node{
 | ||||||
|  | //             valueType val;
 | ||||||
|  | //             Node* leftSon{nullptr};
 | ||||||
|  | //             Node* rightSon{nullptr};
 | ||||||
|  | //         };
 | ||||||
|  | //         Stack<Node*> 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<<now->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()&&val<stack.top()->val){
 | ||||||
|  | //                 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());
 | ||||||
|  | //         }
 | ||||||
|  | //     };
 | ||||||
|  | // }
 | ||||||
							
								
								
									
										59
									
								
								src/12/c6/Stack.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/12/c6/Stack.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,59 @@ | |||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include <cstddef> | ||||||
|  | #include <stdexcept> | ||||||
|  | #include <type_traits> | ||||||
|  | namespace zt { | ||||||
|  |     template<class T> | ||||||
|  |     class Stack{ | ||||||
|  |     private: | ||||||
|  |         using valueType = std::remove_cv_t<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(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  | } | ||||||
							
								
								
									
										33
									
								
								src/12/c6/c6_bst.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/12/c6/c6_bst.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | |||||||
|  | #include "BinarySearchTree.hpp" | ||||||
|  | #include "Stack.hpp" | ||||||
|  | #include <iostream> | ||||||
|  | 
 | ||||||
|  | int main(){ | ||||||
|  |     zt::Stack<int> stk; | ||||||
|  |     std::cout<<stk.empty()<<'\n'; | ||||||
|  |     stk.push(1); | ||||||
|  |     stk.push(2); | ||||||
|  |     std::cout<<stk.top()<<" "<<stk.size()<<'\n'; | ||||||
|  |     stk.pop(); | ||||||
|  |     std::cout<<stk.size()<<'\n'; | ||||||
|  |     stk.push(10).push(100); | ||||||
|  |     std::cout<<std::boolalpha<<stk.size()<<" "<<stk.empty()<<'\n'; | ||||||
|  |     // try {
 | ||||||
|  |     //     std::cout<<stk.pop().pop().pop().size()<<'\n';
 | ||||||
|  |     // } catch (...) {
 | ||||||
|  |      | ||||||
|  |     // }
 | ||||||
|  |     zt::BinarySearchTree<int> bst; | ||||||
|  |     bst.insert(9); | ||||||
|  |     std::cout<<bst.findMin()<<'\n'<<bst.toString()<<'\n'; | ||||||
|  |     bst.insert(3); | ||||||
|  |     std::cout<<bst.findMin()<<'\n'<<bst.toString()<<'\n'; | ||||||
|  |     bst.insert(7); | ||||||
|  |     std::cout<<bst.findMin()<<'\n'<<bst.toString()<<'\n'; | ||||||
|  |     bst.insert(1); | ||||||
|  |     std::cout<<bst.findMin()<<'\n'<<bst.toString()<<'\n'; | ||||||
|  |     bst.insert(12); | ||||||
|  |      | ||||||
|  |      | ||||||
|  |     std::cout<<bst.findMin()<<'\n'<<bst.toString()<<'\n'; | ||||||
|  | } | ||||||
							
								
								
									
										
											BIN
										
									
								
								vgcore.9671
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								vgcore.9671
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Loading…
	
		Reference in New Issue
	
	Block a user