├── .gitattributes ├── .gitignore ├── Algorithm.h ├── Algorithm ├── Char │ └── CharMatch.h ├── Geometry │ ├── ConvexHull.cpp │ ├── ConvexHull.h │ ├── Geometry.cpp │ ├── Geometry.h │ ├── MinDistance.cpp │ ├── MinDistance.h │ ├── SegmentIntersect.cpp │ └── SegmentIntersect.h ├── Graph │ ├── BreadthFirstVisit.h │ ├── DepthFirstVisit.h │ ├── MaxStream.h │ ├── MinGenerateTree.h │ ├── ReverseGraph.h │ ├── ShorestPath.h │ ├── StrongConnectGraph.h │ └── TopologySort.h ├── Search │ └── Find.h └── Sort │ └── Sort.h ├── DataStruct ├── Array │ └── DynArray.h ├── Graph │ └── Graph.h ├── Hash │ └── ListHash.h ├── Heap │ └── MinHeap.h ├── List │ └── DoubleList.h ├── Queue │ ├── DynQueue.h │ └── MinPriorityQueue.h ├── Stack │ └── DynStack.h └── Tree │ ├── SortedBalanceBinaryTree.h │ └── SortedBinaryTree.h ├── Design ├── Divide │ └── Divide.txt ├── DynamicPlanning │ ├── DynamicPlanning.txt │ ├── MaxProfit.cpp │ ├── MaxProfit.h │ ├── MinExpectTimes.cpp │ └── MinExpectTimes.h ├── Greedy │ ├── ActionsPlanning.cpp │ ├── ActionsPlanning.h │ ├── GreedyAlgorithm.txt │ ├── HufManAlgorithm.h │ ├── ZeroOnePack.cpp │ └── ZeroOnePack.h └── Iterator │ └── Iteration.txt ├── External ├── Math │ ├── Data │ │ ├── ConstValue.h │ │ ├── Helper.h │ │ ├── Point.h │ │ └── Vector.h │ ├── Math.h │ └── Stdafx.h └── PlaneGeometry │ ├── External │ └── Math │ │ ├── Data │ │ ├── Point.h │ │ └── Vector.h │ │ ├── Math.h │ │ └── Stdafx.h │ ├── Geometry │ ├── Geometry.h │ ├── LineGeometry.h │ └── PathGeometry.h │ ├── PlaneGeometry.h │ ├── Segment │ ├── ArcSegment.h │ ├── LineSegment.h │ └── Segment.h │ └── Stdafx.h ├── LICENSE ├── README.md ├── stdafx.cpp └── stdafx.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | DataStruct/Heap/FiboHeap.h 34 | DataStruct/Hash/NoListHash.h 35 | DataStruct/Heap/MaxHeap.h 36 | DataStruct/Stack/MinHeap.h 37 | DataStruct/Tree/BinaryTree.h 38 | DataStruct/Tree/SortedBalanceMultiTree.h 39 | Algorithm/Planning/LinearPlanning.h 40 | app.ico 41 | app.rc 42 | Debug/Algorithm.Build.CppClean.log 43 | Debug/Algorithm.log 44 | Debug/Algorithm.tlog/Algorithm.lastbuildstate 45 | Debug/Algorithm.tlog/cl.command.1.tlog 46 | Debug/Algorithm.tlog/metagen.write.1.tlog 47 | Debug/Algorithm.tlog/unsuccessfulbuild 48 | Debug/Algorithm.vcxprojResolveAssemblyReference.cache 49 | Design/Divide/CalculateInversionNumAndMergeSort.h 50 | Design/Divide/MaxSubArray.h 51 | Design/DynamicPlanning/MaxSubArray.cpp 52 | Design/DynamicPlanning/MaxSubArray.h 53 | -------------------------------------------------------------------------------- /Algorithm.h: -------------------------------------------------------------------------------- 1 | #ifndef ALGORITHM_ALGORITHM_H 2 | #define ALGORITHM_ALGORITHM_H 3 | #include "Algorithm/Search/Find.h" 4 | #include "Algorithm/Sort/Sort.h" 5 | #include "DataStruct/Array/DynArray.h" 6 | #include "DataStruct/Stack/DynStack.h" 7 | #include "DataStruct/List/DoubleList.h" 8 | #include "DataStruct/Queue/DynQueue.h" 9 | #include "DataStruct/Heap/MinHeap.h" 10 | #include "DataStruct/Queue/MinPriorityQueue.h" 11 | #include "DataStruct/Hash/ListHash.h" 12 | #include "DataStruct/Tree/SortedBinaryTree.h" 13 | #include "DataStruct/Tree/SortedBalanceBinaryTree.h" 14 | #include "Algorithm/Graph/BreadthFirstVisit.h" 15 | #include "Algorithm/Graph/DepthFirstVisit.h" 16 | #include "Algorithm/Graph/TopologySort.h" 17 | #include "Algorithm/Graph/StrongConnectGraph.h" 18 | #include "Algorithm/Graph/ShorestPath.h" 19 | #include "Algorithm/Graph/MinGenerateTree.h" 20 | #include "Algorithm/Graph/MaxStream.h" 21 | #include "Algorithm/Char/CharMatch.h" 22 | //#include "HighLevelAlgorithm/CharactersMatch/CharacterMatch.h" 23 | //#include "HighLevelAlgorithm/LinearPlanning/LinearPlanning.h" 24 | // 25 | //#include "BaseStruct/DynArray.h" 26 | //#include "BaseStruct/Hash.h" 27 | //#include "BaseStruct/List.h" 28 | //#include "BaseStruct/MaxHeap.h" 29 | //#include "BaseStruct/MinHeap.h" 30 | //#include "BaseStruct/Queue.h" 31 | //#include "BaseStruct/Stack.h" 32 | // 33 | //#include "HighLevelStruct/Tree/BinaryTree/BinaryTree.h" 34 | //#include "HighLevelStruct/Tree/SortedBinaryTree/SortedBinaryTree.h" 35 | //#include "HighLevelStruct/Tree/SortedAndBalancedBinaryTree/SortedBalanceBinaryTree.h" 36 | //#include "HighLevelStruct/Tree/SortedAndBalancedMuitiTree/SortedBalanceMultiTree.h" 37 | //#include "HighLevelStruct/Graph/Graph.h" 38 | // 39 | //#include "GraphAlgorithms/BreadthFirstVisit.h" 40 | //#include "GraphAlgorithms/DepthFirstVisit.h" 41 | //#include "GraphAlgorithms/MaxStream.h" 42 | //#include "GraphAlgorithms/MinGenerateTree.h" 43 | //#include "GraphAlgorithms/ReverseGraph.h" 44 | //#include "GraphAlgorithms/ShorestPath.h" 45 | //#include "GraphAlgorithms/StrongConnectGraph.h" 46 | //#include "GraphAlgorithms/TopologySort.h" 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /Algorithm/Char/CharMatch.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Char/CharMatch.h -------------------------------------------------------------------------------- /Algorithm/Geometry/ConvexHull.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Geometry/ConvexHull.cpp -------------------------------------------------------------------------------- /Algorithm/Geometry/ConvexHull.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_ALGORITHM_GEOMETRY_CONVEXHULL_H 7 | #define AILIB_ALGORITHM_GEOMETRY_CONVEXHULL_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\DataStruct\Array\DynArray.h" 10 | #include "..\..\DataStruct\Stack\DynStack.h" 11 | #include "Geometry.h" 12 | 13 | namespace AlLib 14 | { 15 | namespace Algorithm 16 | { 17 | namespace Geo 18 | { 19 | extern "C" class ALGORITHMLIB ConvexHull 20 | { 21 | public: 22 | ConvexHull(); 23 | ~ConvexHull(); 24 | 25 | DataStruct::Stack::DynStack> Run(const DataStruct::Array::DynArray>& poArr_); 26 | }; 27 | } 28 | } 29 | } 30 | #endif -------------------------------------------------------------------------------- /Algorithm/Geometry/Geometry.cpp: -------------------------------------------------------------------------------- 1 | #define ALGORITHMLIB __declspec(dllexport) 2 | #include "Geometry.h" 3 | namespace AlLib 4 | { 5 | namespace Algorithm 6 | { 7 | namespace Geo 8 | { 9 | Geometry::Geometry() 10 | { 11 | 12 | } 13 | 14 | Geometry::~Geometry() 15 | { 16 | 17 | } 18 | 19 | Geometry::ROTATE_DIRECTION Geometry::TestDirection( 20 | const Math::Vector<2>& vector1_, 21 | const Math::Vector<2>& vector2_) 22 | { 23 | double _nRet = vector1_.m_nValues[0] * vector2_.m_nValues[1] - vector2_.m_nValues[0] * vector1_.m_nValues[1]; 24 | if (_nRet > 0.0) 25 | { 26 | return ROTATE_DIRECTION::ANTICLOCK; 27 | } 28 | else if (_nRet < 0.0) 29 | { 30 | return ROTATE_DIRECTION::CLOCK; 31 | } 32 | else 33 | { 34 | return ROTATE_DIRECTION::NO_ROTATE; 35 | } 36 | } 37 | 38 | bool Geometry::IsPointOnLine( 39 | const PlaneGeometry::LineGeometry& line_, 40 | const Math::Point<2> po_) 41 | { 42 | double _nMinX = Math::Helper::Min(line_.m_poStart.m_nPos[0], line_.m_poEnd.m_nPos[0]); 43 | double _nMaxX = Math::Helper::Max(line_.m_poStart.m_nPos[0], line_.m_poEnd.m_nPos[0]); 44 | double _nMinY = Math::Helper::Min(line_.m_poStart.m_nPos[1], line_.m_poEnd.m_nPos[1]); 45 | double _nMaxY = Math::Helper::Max(line_.m_poStart.m_nPos[1], line_.m_poEnd.m_nPos[1]); 46 | bool _bOn = true; 47 | if (!(po_.m_nPos[0] >= _nMinX 48 | && po_.m_nPos[0] <= _nMaxX)) 49 | { 50 | _bOn = false; 51 | } 52 | else if (!(po_.m_nPos[1] >= _nMinY 53 | && po_.m_nPos[1] <= _nMaxY)) 54 | { 55 | _bOn = false; 56 | } 57 | 58 | return _bOn; 59 | } 60 | 61 | double Geometry::GetAngle(const Math::Vector<2>& vec_) 62 | { 63 | Math::Vector<2> _vecV = vec_; 64 | if (_vecV.GetLength() == 0.0) 65 | { 66 | throw "0 vector has no angle"; 67 | } 68 | 69 | _vecV.Normalize(); 70 | double _nAngle = std::asin(_vecV.m_nValues[1]); 71 | if (_vecV.m_nValues[0] >= 0.0 && _vecV.m_nValues[1] >= 0.0) 72 | { 73 | return _nAngle; 74 | } 75 | else if (_vecV.m_nValues[0] <= 0.0 && _vecV.m_nValues[1] >= 0.0) 76 | { 77 | assert(_nAngle >= 0.0 && _nAngle <= PI / 2.0); 78 | return (PI - _nAngle); 79 | } 80 | else if (_vecV.m_nValues[0] <= 0.0 && _vecV.m_nValues[1] <= 0.0) 81 | { 82 | assert(_nAngle >= -(PI / 2.0) && _nAngle <= 0.0); 83 | return (PI - _nAngle); 84 | } 85 | else 86 | { 87 | assert(_nAngle >= -(PI / 2.0) && _nAngle <= 0.0); 88 | return (2 * PI + _nAngle); 89 | } 90 | } 91 | 92 | double Geometry::GetDistance( 93 | const Math::Point<2>& po1_, 94 | const Math::Point<2>& po2_) 95 | { 96 | Math::Vector<2> _vecV(po1_, po2_); 97 | return _vecV.GetLength(); 98 | } 99 | } 100 | } 101 | } -------------------------------------------------------------------------------- /Algorithm/Geometry/Geometry.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_ALGORITHM_GEOMETRY_GEOMETRY_H 7 | #define AILIB_ALGORITHM_GEOMETRY_GEOMETRY_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\DataStruct\Array\DynArray.h" 10 | 11 | namespace AlLib 12 | { 13 | namespace Algorithm 14 | { 15 | namespace Geo 16 | { 17 | extern "C" class ALGORITHMLIB Geometry 18 | { 19 | public: 20 | enum ROTATE_DIRECTION 21 | { 22 | NO_ROTATE, 23 | CLOCK, 24 | ANTICLOCK, 25 | }; 26 | 27 | Geometry(); 28 | ~Geometry(); 29 | static ROTATE_DIRECTION TestDirection(const Math::Vector<2>& vector1_, const Math::Vector<2>& vector2_); 30 | static bool IsPointOnLine(const PlaneGeometry::LineGeometry& line_, const Math::Point<2> po_); 31 | static double GetAngle(const Math::Vector<2>& vec_); 32 | static double GetDistance(const Math::Point<2>& po1_, const Math::Point<2>& po2_); 33 | }; 34 | } 35 | } 36 | } 37 | #endif -------------------------------------------------------------------------------- /Algorithm/Geometry/MinDistance.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Geometry/MinDistance.cpp -------------------------------------------------------------------------------- /Algorithm/Geometry/MinDistance.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_ALGORITHM_GEOMETRY_MINDISTANCE_H 7 | #define AILIB_ALGORITHM_GEOMETRY_MINDISTANCE_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\DataStruct\Array\DynArray.h" 10 | #include "..\..\DataStruct\Stack\DynStack.h" 11 | #include "Geometry.h" 12 | 13 | namespace AlLib 14 | { 15 | namespace Algorithm 16 | { 17 | namespace Geo 18 | { 19 | extern "C" class ALGORITHMLIB MinDistance 20 | { 21 | public: 22 | enum Flag 23 | { 24 | LEFT, 25 | RIGHT, 26 | }; 27 | 28 | class FlagPoint 29 | { 30 | public: 31 | FlagPoint(); 32 | FlagPoint(Math::Point<2>* pPo_, Flag nFlag_); 33 | ~FlagPoint(); 34 | 35 | public: 36 | Math::Point<2> m_poP; 37 | Flag m_nFlag; 38 | }; 39 | 40 | MinDistance(); 41 | ~MinDistance(); 42 | 43 | double Run(const DataStruct::Array::DynArray>& poArr_); 44 | private: 45 | double CalculateMinDistance( 46 | const DataStruct::Array::DynArray& arrPos_, 47 | const DataStruct::Array::DynArray& arrSortedByX_, 48 | const DataStruct::Array::DynArray& arrSortedByY_); 49 | }; 50 | } 51 | } 52 | } 53 | #endif -------------------------------------------------------------------------------- /Algorithm/Geometry/SegmentIntersect.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Geometry/SegmentIntersect.cpp -------------------------------------------------------------------------------- /Algorithm/Geometry/SegmentIntersect.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Geometry/SegmentIntersect.h -------------------------------------------------------------------------------- /Algorithm/Graph/BreadthFirstVisit.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Graph/BreadthFirstVisit.h -------------------------------------------------------------------------------- /Algorithm/Graph/DepthFirstVisit.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Graph/DepthFirstVisit.h -------------------------------------------------------------------------------- /Algorithm/Graph/MaxStream.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Graph/MaxStream.h -------------------------------------------------------------------------------- /Algorithm/Graph/MinGenerateTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Graph/MinGenerateTree.h -------------------------------------------------------------------------------- /Algorithm/Graph/ReverseGraph.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_ALGORITHM_GRAPH_REVERSEGRAPH_H 7 | #define AILIB_ALGORITHM_GRAPH_REVERSEGRAPH_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\DataStruct\Graph\Graph.h" 10 | 11 | namespace AlLib 12 | { 13 | namespace Algorithm 14 | { 15 | namespace Graph 16 | { 17 | template 18 | class ReverseGraph 19 | { 20 | public: 21 | typename typedef DataStruct::GraphStruct::Graph InnerGraph; 22 | ReverseGraph(const InnerGraph& nGraph_); 23 | ~ReverseGraph(); 24 | 25 | InnerGraph Run(); 26 | private: 27 | ReverseGraph(const ReverseGraph& nGraph_) = default; 28 | ReverseGraph& operator=(const ReverseGraph& nGraph_) = default; 29 | 30 | private: 31 | const InnerGraph& m_nGraph; 32 | }; 33 | 34 | template 35 | ReverseGraph::ReverseGraph(const InnerGraph& nGraph_) 36 | : m_nGraph(nGraph_) 37 | { 38 | 39 | } 40 | 41 | template 42 | ReverseGraph::~ReverseGraph() 43 | { 44 | 45 | } 46 | 47 | template 48 | typename DataStruct::GraphStruct::Graph ReverseGraph::Run() 49 | { 50 | InnerGraph _nGraph; 51 | DataStruct::Array::DynArray _arrNodes = m_nGraph.GetNodesArray(); 52 | for (int _i = 0; _i < _arrNodes.GetSize(); _i++) 53 | { 54 | _nGraph.AddNode(_arrNodes[_i]->GetPair()); 55 | } 56 | 57 | DataStruct::Array::DynArray _arrEdges = m_nGraph.GetEdgesArray(); 58 | for (int _i = 0; _i < _arrEdges.GetSize(); _i++) 59 | { 60 | _nGraph.AddEdge(_arrEdges[_i]->GetIdentity().Reverse()); 61 | } 62 | 63 | return _nGraph; 64 | } 65 | } 66 | } 67 | } 68 | #endif 69 | -------------------------------------------------------------------------------- /Algorithm/Graph/ShorestPath.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Graph/ShorestPath.h -------------------------------------------------------------------------------- /Algorithm/Graph/StrongConnectGraph.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_ALGORITHM_GRAPH_STRONGCONNECTGRAPH_H 7 | #define AILIB_ALGORITHM_GRAPH_STRONGCONNECTGRAPH_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\DataStruct\Graph\Graph.h" 10 | #include "DepthFirstVisit.h" 11 | #include "ReverseGraph.h" 12 | 13 | namespace AlLib 14 | { 15 | namespace Algorithm 16 | { 17 | namespace Graph 18 | { 19 | template 20 | class StrongConnectGraph 21 | { 22 | public: 23 | typename typedef DataStruct::GraphStruct::Graph InnerGraph; 24 | StrongConnectGraph(const InnerGraph& nGraph_); 25 | ~StrongConnectGraph(); 26 | 27 | DataStruct::Array::DynArray::Node*> Run(); 28 | private: 29 | StrongConnectGraph(const StrongConnectGraph& nSCG_) = default; 30 | StrongConnectGraph& operator=(const StrongConnectGraph& nSCG_) = default; 31 | private: 32 | const InnerGraph& m_nGraph; 33 | InnerGraph* m_pReverseGraph; 34 | typename DepthFirstVisit* m_pDepthFirstVisit; 35 | }; 36 | 37 | template 38 | StrongConnectGraph::StrongConnectGraph(const InnerGraph& nGraph_) 39 | : m_nGraph(nGraph_), m_pReverseGraph(nullptr), m_pDepthFirstVisit(nullptr) 40 | { 41 | 42 | } 43 | 44 | template 45 | StrongConnectGraph::~StrongConnectGraph() 46 | { 47 | if (m_pReverseGraph != nullptr) 48 | { 49 | delete m_pReverseGraph; 50 | m_pReverseGraph = nullptr; 51 | } 52 | 53 | if (m_pDepthFirstVisit == nullptr) 54 | { 55 | delete m_pDepthFirstVisit; 56 | m_pDepthFirstVisit = nullptr; 57 | } 58 | } 59 | 60 | template 61 | DataStruct::Array::DynArray::Node*> StrongConnectGraph::Run() 62 | { 63 | DepthFirstVisit _alDepthFirstVisit(m_nGraph); 64 | DataStruct::Array::DynArray::Node*> _arrNodes = _alDepthFirstVisit.Run(); 65 | _arrNodes.Sort([](typename DepthFirstVisit::Node* pNode1_, typename DepthFirstVisit::Node* pNode2_)->int 66 | { 67 | int _nRet = pNode1_->GetLastVisitTime() - pNode2_->GetLastVisitTime(); 68 | if (_nRet > 0) 69 | { 70 | return -1; 71 | } 72 | else if (_nRet < 0) 73 | { 74 | return 1; 75 | } 76 | else 77 | { 78 | return 0; 79 | } 80 | }); 81 | 82 | DataStruct::Array::DynArray _arrKeys; 83 | for (int _i = 0; _i < _arrNodes.GetSize(); _i++) 84 | { 85 | typename InnerGraph::Pair _nPair = _arrNodes[_i]->GetGraphNode()->GetPair(); 86 | _arrKeys.Add(_nPair.m_nKey); 87 | } 88 | 89 | ReverseGraph _alReverseGraph(m_nGraph); 90 | if (m_pReverseGraph != nullptr) 91 | { 92 | delete m_pReverseGraph; 93 | m_pReverseGraph = nullptr; 94 | } 95 | 96 | if (m_pDepthFirstVisit == nullptr) 97 | { 98 | delete m_pDepthFirstVisit; 99 | m_pDepthFirstVisit = nullptr; 100 | } 101 | 102 | try 103 | { 104 | m_pReverseGraph = new InnerGraph(_alReverseGraph.Run()); 105 | m_pDepthFirstVisit = new DepthFirstVisit(*m_pReverseGraph); 106 | } 107 | catch (...) 108 | { 109 | throw "out of memory"; 110 | m_pReverseGraph = nullptr; 111 | m_pDepthFirstVisit = nullptr; 112 | } 113 | 114 | return m_pDepthFirstVisit->Run(_arrKeys); 115 | } 116 | } 117 | } 118 | } 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /Algorithm/Graph/TopologySort.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_ALGORITHM_GRAPH_TOPOLOGYSORT_H 7 | #define AILIB_ALGORITHM_GRAPH_TOPOLOGYSORT_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\DataStruct\Graph\Graph.h" 10 | #include "DepthFirstVisit.h" 11 | #include "..\Sort\Sort.h" 12 | 13 | namespace AlLib 14 | { 15 | namespace Algorithm 16 | { 17 | namespace Graph 18 | { 19 | template 20 | class TopologySort 21 | { 22 | public: 23 | typename typedef DataStruct::GraphStruct::Graph InnerGraph; 24 | TopologySort(const InnerGraph& nGraph_); 25 | ~TopologySort(); 26 | 27 | DataStruct::Array::DynArray::Node*> Run(); 28 | private: 29 | TopologySort(const TopologySort& nTopoAl_) = default; 30 | TopologySort& operator=(const TopologySort& nTopoAl_) = default; 31 | 32 | private: 33 | typename DepthFirstVisit m_alDepthFirstVisit; 34 | }; 35 | 36 | template 37 | TopologySort::TopologySort(const InnerGraph& nGraph_) 38 | : m_alDepthFirstVisit(nGraph_) 39 | { 40 | 41 | } 42 | 43 | template 44 | TopologySort::~TopologySort() 45 | { 46 | 47 | } 48 | 49 | template 50 | DataStruct::Array::DynArray::Node*> TopologySort::Run() 51 | { 52 | DataStruct::Array::DynArray::Node*> _arrNodes = m_alDepthFirstVisit.Run(); 53 | _arrNodes.Sort([](DepthFirstVisit::Node* pNode1_, DepthFirstVisit::Node* pNode2_) 54 | { 55 | int _nRet = pNode1_->GetLastVisitTime() - pNode2_->GetLastVisitTime(); 56 | if (_nRet > 0) 57 | { 58 | return -1; 59 | } 60 | else if (_nRet < 0) 61 | { 62 | return 1; 63 | } 64 | else 65 | { 66 | return 0; 67 | } 68 | }); 69 | 70 | return _arrNodes; 71 | } 72 | } 73 | } 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /Algorithm/Search/Find.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Search/Find.h -------------------------------------------------------------------------------- /Algorithm/Sort/Sort.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Algorithm/Sort/Sort.h -------------------------------------------------------------------------------- /DataStruct/Array/DynArray.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef ALLIB_DATASTRUCT_ARRAY_DYNARRAY_H 7 | #define ALLIB_DATASTRUCT_ARRAY_DYNARRAY_H 8 | #include "..\..\stdafx.h" 9 | #include "..\..\Algorithm\Sort\Sort.h" 10 | 11 | // svn 12 | namespace AlLib 13 | { 14 | namespace DataStruct 15 | { 16 | namespace Array 17 | { 18 | // 突破固定容量数组限制 19 | // 容量可以动态增加和减少的数组 20 | // 适合作为管理动态元素集合的容器 21 | template 22 | class DynArray 23 | { 24 | public: 25 | DynArray(); 26 | DynArray(int nInitialSize_, const T& nInitialValue_); 27 | virtual ~DynArray(); 28 | 29 | void Add(const T& value_); 30 | void AddRange(const DynArray& arrItems_); 31 | void Insert(int nIndex_, const T& value_); 32 | void DeleteByIndex(int nIndex_ = -1); 33 | void DeleteByValue(const T& value_); 34 | void DeleteAll(); 35 | bool SetValue(int nIndex_, const T& value_); 36 | bool GetValue(int nIndex_, T& value_) const; 37 | 38 | int GetSize() const; 39 | int GetCapacity() const; 40 | int Find(std::function fun_) const; 41 | 42 | T& operator[](int nIndex_); 43 | const T& operator[](int nIndex_) const; 44 | DynArray(const DynArray& arrElements_); 45 | DynArray& operator=(const DynArray& arrElements_); 46 | DynArray Sort(std::function fun_ = [](const T& nT1_, const T& nT2_)->int 47 | { 48 | if (nT1_ < nT2_) 49 | { 50 | return -1; 51 | } 52 | else if (nT1_ > nT2_) 53 | { 54 | return 1; 55 | } 56 | else 57 | { 58 | return 0; 59 | } 60 | }) const; 61 | void Sort(std::function fun_ = [](const T& nT1_, const T& nT2_)->int 62 | { 63 | if (nT1_ < nT2_) 64 | { 65 | return -1; 66 | } 67 | else if (nT1_ > nT2_) 68 | { 69 | return 1; 70 | } 71 | else 72 | { 73 | return 0; 74 | } 75 | }); 76 | 77 | void Reset(); 78 | private: 79 | void Check(int nSize_); 80 | void Shrink(); 81 | 82 | private: 83 | T* m_pSource;// 当T为引用类型时,sizeof无法获得其真实大小 84 | int m_nSize; 85 | int m_nCapacity; 86 | 87 | std::allocator m_alloc; 88 | }; 89 | 90 | template 91 | DynArray::DynArray(const DynArray& arrElements_) 92 | : m_pSource(nullptr), m_nSize(0), m_nCapacity(0) 93 | { 94 | m_pSource = m_alloc.allocate(arrElements_.m_nCapacity); 95 | if (m_pSource == nullptr) 96 | { 97 | throw "out of memory"; 98 | } 99 | else 100 | { 101 | m_nSize = arrElements_.m_nSize; 102 | m_nCapacity = arrElements_.m_nCapacity; 103 | std::uninitialized_copy_n(arrElements_.m_pSource, arrElements_.m_nSize, m_pSource); 104 | } 105 | } 106 | 107 | template 108 | DynArray& DynArray::operator=(const DynArray& arrElements_) 109 | { 110 | if (&arrElements_ == this) 111 | { 112 | return *this; 113 | } 114 | 115 | this->~DynArray(); 116 | m_pSource = m_alloc.allocate(arrElements_.m_nCapacity); 117 | if (m_pSource == nullptr) 118 | { 119 | throw "out of memory"; 120 | } 121 | else 122 | { 123 | m_nSize = arrElements_.m_nSize; 124 | m_nCapacity = arrElements_.m_nCapacity; 125 | std::uninitialized_copy_n(arrElements_.m_pSource, arrElements_.m_nSize, m_pSource); 126 | } 127 | 128 | return *this; 129 | } 130 | 131 | template 132 | int DynArray::Find(std::function fun_) const 133 | { 134 | int _nPosIndex = -1; 135 | for (int _i = 0; _i < m_nSize; _i++) 136 | { 137 | if (fun_(*(m_pSource + _i))) 138 | { 139 | _nPosIndex = _i; 140 | break; 141 | } 142 | } 143 | 144 | return _nPosIndex; 145 | } 146 | 147 | template 148 | DynArray::DynArray() 149 | :m_pSource(nullptr), m_nSize(0), m_nCapacity(0) 150 | { 151 | m_pSource = m_alloc.allocate(100); 152 | if (m_pSource == nullptr) 153 | { 154 | throw "out of memory"; 155 | } 156 | else 157 | { 158 | m_nSize = 0; 159 | m_nCapacity = 100; 160 | } 161 | } 162 | 163 | template 164 | DynArray::DynArray(int nInitialSize_, const T& nInitialValue_) 165 | :m_pSource(nullptr), m_nSize(0), m_nCapacity(0) 166 | { 167 | m_pSource = m_alloc.allocate(nInitialSize_ * 2); 168 | if (m_pSource == nullptr) 169 | { 170 | throw "out of memory"; 171 | } 172 | else 173 | { 174 | m_nSize = nInitialSize_; 175 | m_nCapacity = nInitialSize_ * 2; 176 | std::uninitialized_fill_n(m_pSource, m_nSize, nInitialValue_); 177 | } 178 | } 179 | 180 | template 181 | void DynArray::Reset() 182 | { 183 | this->~DynArray(); 184 | m_pSource = m_alloc.allocate(100); 185 | if (m_pSource == nullptr) 186 | { 187 | throw "out of memory"; 188 | } 189 | else 190 | { 191 | m_nSize = 0; 192 | m_nCapacity = 100; 193 | } 194 | } 195 | 196 | template 197 | DynArray::~DynArray() 198 | { 199 | if (m_pSource == nullptr) 200 | { 201 | return; 202 | } 203 | 204 | T* _pEnd = m_pSource + m_nSize; 205 | while (_pEnd != m_pSource) 206 | { 207 | m_alloc.destroy(--_pEnd); 208 | } 209 | 210 | // 内存释放 211 | m_alloc.deallocate(m_pSource, m_nCapacity); 212 | m_pSource = nullptr; 213 | m_nSize = 0; 214 | m_nCapacity = 0; 215 | } 216 | 217 | template 218 | void DynArray::Add(const T& value_) 219 | { 220 | Check(m_nSize + 1); 221 | T* _pEnd = m_pSource + m_nSize; 222 | m_alloc.construct(_pEnd, value_); 223 | m_nSize++; 224 | } 225 | 226 | template 227 | void DynArray::AddRange(const DynArray& arrItems_) 228 | { 229 | Check(m_nSize + arrItems_.GetSize()); 230 | for (int _i = 0; _i < arrItems_.m_nSize; _i++) 231 | { 232 | m_alloc.construct(m_pSource + m_nSize + _i, *(arrItems_.m_pSource + _i)); 233 | } 234 | 235 | m_nSize += arrItems_.m_nSize; 236 | } 237 | 238 | template 239 | void DynArray::Check(int nSize_) 240 | { 241 | if (m_nCapacity >= nSize_) 242 | { 243 | return; 244 | } 245 | 246 | int _nSize = m_nSize; 247 | int _nCapacity = nSize_ * 2; 248 | T* _pSource = m_alloc.allocate(_nCapacity); 249 | if (_pSource == nullptr) 250 | { 251 | throw "out of memory"; 252 | } 253 | else 254 | { 255 | std::uninitialized_copy_n(m_pSource, m_nSize, _pSource); 256 | this->~DynArray(); 257 | m_pSource = _pSource; 258 | m_nSize = _nSize; 259 | m_nCapacity = _nCapacity; 260 | } 261 | } 262 | 263 | template 264 | void DynArray::Insert(int nIndex_, const T& value_) 265 | { 266 | if (nIndex_ > m_nSize) 267 | { 268 | throw "Insert position error"; 269 | } 270 | 271 | Check(m_nSize + 1); 272 | m_alloc.construct(m_pSource + m_nSize, value_); 273 | 274 | for (int _i = m_nSize - 1; _i >= nIndex_; _i--) 275 | { 276 | *(m_pSource + _i + 1) = *(m_pSource + _i); 277 | } 278 | 279 | *(m_pSource + nIndex_) = value_; 280 | m_nSize++; 281 | } 282 | 283 | template 284 | void DynArray::DeleteByIndex(int nIndex_) 285 | { 286 | if (nIndex_ < 0 287 | || nIndex_ >= m_nSize) 288 | { 289 | return; 290 | } 291 | 292 | // 前移 293 | for (int _i = nIndex_ + 1; _i < m_nSize; _i++) 294 | { 295 | *(m_pSource + _i - 1) = *(m_pSource + _i); 296 | } 297 | 298 | m_alloc.destroy(m_pSource + m_nSize - 1); 299 | m_nSize--; 300 | if (m_nSize <= m_nCapacity / 4) 301 | { 302 | Shrink(); 303 | } 304 | } 305 | 306 | template 307 | void DynArray::DeleteByValue(const T& value_) 308 | { 309 | int _nIndex = Find([value_](const T& nT_)->bool 310 | { 311 | if (nT_ == value_) 312 | { 313 | return true; 314 | } 315 | else 316 | { 317 | return false; 318 | } 319 | }); 320 | 321 | if (_nIndex == -1) 322 | { 323 | return; 324 | } 325 | 326 | DeleteByIndex(_nIndex); 327 | } 328 | 329 | template 330 | void DynArray::Shrink() 331 | { 332 | int _nSize = m_nSize; 333 | int _nCapacity = (m_nCapacity / 2) > 100 ? (m_nCapacity / 2) : 100; 334 | T* _pSource = m_alloc.allocate(_nCapacity); 335 | if (_pSource == nullptr) 336 | { 337 | throw "out of memory"; 338 | } 339 | else 340 | { 341 | std::uninitialized_copy_n(m_pSource, m_nSize, _pSource); 342 | this->~DynArray(); 343 | m_pSource = _pSource; 344 | m_nSize = _nSize; 345 | m_nCapacity = _nCapacity; 346 | } 347 | } 348 | 349 | template 350 | void DynArray::DeleteAll() 351 | { 352 | Reset(); 353 | } 354 | 355 | template 356 | bool DynArray::SetValue(int nIndex_, const T& value_) 357 | { 358 | if (nIndex_ < 0 359 | || nIndex_ >= m_nSize) 360 | { 361 | return false; 362 | } 363 | 364 | *(m_pSource + nIndex_) = value_; 365 | return true; 366 | } 367 | 368 | template 369 | bool DynArray::GetValue(int nIndex_, T& value_) const 370 | { 371 | if (nIndex_ < 0 372 | || nIndex_ >= m_nSize) 373 | { 374 | return false; 375 | } 376 | 377 | value_ = *(m_pSource + nIndex_); 378 | return true; 379 | } 380 | 381 | template 382 | int DynArray::GetSize() const 383 | { 384 | return m_nSize; 385 | } 386 | 387 | template 388 | int DynArray::GetCapacity() const 389 | { 390 | return m_nCapacity; 391 | } 392 | 393 | template 394 | T& DynArray::operator[](int nIndex_) 395 | { 396 | if (nIndex_ < 0 397 | || nIndex_ >= m_nSize) 398 | { 399 | throw "index is error"; 400 | } 401 | 402 | return *(m_pSource + nIndex_); 403 | } 404 | 405 | template 406 | const T& DynArray::operator[](int nIndex_) const 407 | { 408 | if (nIndex_ < 0 409 | || nIndex_ >= m_nSize) 410 | { 411 | throw "index is error"; 412 | } 413 | 414 | return *(m_pSource + nIndex_); 415 | } 416 | 417 | template 418 | typename DynArray DynArray::Sort(std::function fun_) const 419 | { 420 | DynArray _arr = *this; 421 | if (m_nSize <= 1) 422 | { 423 | return _arr; 424 | } 425 | 426 | Algorithm::Sort::Helper::QuickSort(_arr.m_pSource, 427 | _arr.m_pSource + m_nSize, 428 | fun_) 429 | return _arr; 430 | } 431 | 432 | template 433 | void DynArray::Sort(std::function fun_) 434 | { 435 | if (m_nSize <= 1) 436 | { 437 | return; 438 | } 439 | 440 | Algorithm::Sort::Helper::QuickSort(m_pSource, 441 | m_pSource + m_nSize, 442 | fun_); 443 | return; 444 | } 445 | } 446 | } 447 | } 448 | #endif 449 | 450 | 451 | 452 | -------------------------------------------------------------------------------- /DataStruct/Graph/Graph.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/DataStruct/Graph/Graph.h -------------------------------------------------------------------------------- /DataStruct/Hash/ListHash.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef ALLIB_DATASTRUCT_HASH_LISTHASH_H 7 | #define ALLIB_DATASTRUCT_HASH_LISTHASH_H 8 | #include "..\..\stdafx.h" 9 | #include "..\Array\DynArray.h" 10 | #include "..\List\DoubleList.h" 11 | namespace AlLib 12 | { 13 | namespace DataStruct 14 | { 15 | namespace Hash 16 | { 17 | template 18 | class ListHash 19 | { 20 | public: 21 | class Pair 22 | { 23 | public: 24 | Pair() 25 | { 26 | } 27 | 28 | Pair(const Key& nKey_, const Value& nValue_) 29 | { 30 | m_nKey = nKey_; 31 | m_nValue = nValue_; 32 | } 33 | 34 | ~Pair() 35 | { 36 | } 37 | public: 38 | Key m_nKey; 39 | Value m_nValue; 40 | }; 41 | 42 | public: 43 | ListHash(int nSoltNums_, 44 | std::function hashFun_ = [](const Key& nKey_, int nSlotsNum_)->int 45 | { 46 | return (nKey_ % nSlotsNum_); 47 | }); 48 | ~ListHash(); 49 | ListHash(const ListHash& hash_); 50 | ListHash& operator=(const ListHash& hash_); 51 | 52 | void Insert(const Pair& nPair_); 53 | void Delete(const Key& nKey_); 54 | bool Search(const Key& nKey_, Value& nValue_) const; 55 | private: 56 | Array::DynArray*> m_arrSlots; 57 | std::function m_fHashFun; 58 | }; 59 | 60 | template 61 | ListHash::ListHash(int nSoltNums_, std::function hashFun_) 62 | :m_arrSlots(nSoltNums_, nullptr), m_fHashFun(hashFun_) 63 | { 64 | for (int _i = 0; _i < nSoltNums_; _i++) 65 | { 66 | List::DoubleList* _pList = nullptr; 67 | try 68 | { 69 | _pList = new List::DoubleList(); 70 | } 71 | catch (...) 72 | { 73 | _pList = nullptr; 74 | throw "out of memory"; 75 | } 76 | 77 | m_arrSlots[_i] = _pList; 78 | } 79 | } 80 | 81 | template 82 | ListHash::ListHash(const ListHash& hash_) 83 | : m_arrSlots(hash_.m_arrSlots.GetSize(), nullptr), m_fHashFun(hash_.m_fHashFun) 84 | { 85 | int _nSize = hash_.m_arrSlots.GetSize(); 86 | for (int _i = 0; _i < _nSize; _i++) 87 | { 88 | List::DoubleList* _pList = nullptr; 89 | try 90 | { 91 | _pList = new List::DoubleList(hash_.m_arrSlots[_i]); 92 | } 93 | catch (...) 94 | { 95 | _pList = nullptr; 96 | throw "out of memory"; 97 | } 98 | 99 | m_arrSlots[_i] = _pList; 100 | } 101 | } 102 | 103 | template 104 | typename ListHash& ListHash::operator=(const ListHash& hash_) 105 | { 106 | if (this == &hash_) 107 | { 108 | return *this; 109 | } 110 | 111 | this->~ListHash(); 112 | int _nSize = hash_.m_arrSlots.GetSize(); 113 | m_arrSlots = Array::DynArray*>(_nSize, nullptr); 114 | for (int _i = 0; _i < _nSize; _i++) 115 | { 116 | List::DoubleList* _pList = nullptr; 117 | try 118 | { 119 | _pList = new List::DoubleList(hash_.m_arrSlots[_i]); 120 | } 121 | catch (...) 122 | { 123 | _pList = nullptr; 124 | throw "out of memory"; 125 | } 126 | 127 | m_arrSlots[_i] = _pList; 128 | } 129 | 130 | m_fHashFun = hash_.m_fHashFun; 131 | return *this; 132 | } 133 | 134 | template 135 | ListHash::~ListHash() 136 | { 137 | int _nSize = m_arrSlots.GetSize(); 138 | for (int _i = 0; _i < _nSize; _i++) 139 | { 140 | delete m_arrSlots[_i]; 141 | m_arrSlots[_i] = nullptr; 142 | } 143 | } 144 | 145 | template 146 | void ListHash::Insert(const Pair& nPair_) 147 | { 148 | int _nSize = m_arrSlots.GetSize(); 149 | int _nIndex = m_fHashFun(nPair_.m_nKey, _nSize); 150 | List::DoubleList *_pList = m_arrSlots[_i]; 151 | _pList->Add(nPair_); 152 | } 153 | 154 | template 155 | bool ListHash::Search(const Key& nKey_, Value& nValue_) const 156 | { 157 | int _nSize = m_arrSlots.GetSize(); 158 | int _nIndex = m_fHashFun(nKey_, _nSize); 159 | List::DoubleList *_pList = m_arrSlots[_nIndex]; 160 | List::DoubleList::Node* _pNode = _pList->Find([nKey_](const Pair& nPair_)->bool 161 | { 162 | if (nPair_.m_nKey == nKey_) 163 | { 164 | return true; 165 | } 166 | else 167 | { 168 | return false; 169 | } 170 | }); 171 | 172 | if (_pNode == nullptr) 173 | { 174 | return false; 175 | } 176 | else 177 | { 178 | Pair _nPair = _pNode->GetValue(); 179 | nValue_ = _nPair.m_nValue; 180 | return true; 181 | } 182 | } 183 | 184 | template 185 | void ListHash::Delete(const Key& nKey_) 186 | { 187 | int _nSize = m_arrSlots.GetSize(); 188 | int _nIndex = m_fHashFun(nKey_, _nSize); 189 | List::DoubleList *_pList = m_arrSlots[_nIndex]; 190 | List::DoubleList::Node* _pNode = _pList->Find([nKey_](const Pair& nPair_)->bool 191 | { 192 | if (nPair_.m_nKey == nKey_) 193 | { 194 | return true; 195 | } 196 | else 197 | { 198 | return false; 199 | } 200 | }); 201 | 202 | if (_pNode) 203 | { 204 | _pList->Delete(_pNode); 205 | } 206 | } 207 | } 208 | } 209 | } 210 | #endif 211 | -------------------------------------------------------------------------------- /DataStruct/Heap/MinHeap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/DataStruct/Heap/MinHeap.h -------------------------------------------------------------------------------- /DataStruct/List/DoubleList.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/DataStruct/List/DoubleList.h -------------------------------------------------------------------------------- /DataStruct/Queue/DynQueue.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_DATASTRUCT_QUEUE_DYNQUEUE_H 7 | #define AILIB_DATASTRUCT_QUEUE_DYNQUEUE_H 8 | #include "..\..\stdafx.h" 9 | #include "..\List\DoubleList.h" 10 | 11 | namespace AlLib 12 | { 13 | namespace DataStruct 14 | { 15 | namespace Queue 16 | { 17 | template 18 | class DynQueue 19 | { 20 | public: 21 | DynQueue(); 22 | virtual ~DynQueue(); 23 | 24 | DynQueue(const DynQueue& dqA_); 25 | DynQueue& operator=(const DynQueue& dqA_); 26 | 27 | void In(const T& nValue_); 28 | T Out(); 29 | T Peek() const; 30 | bool IsEmpty() const; 31 | List::DoubleList GetList() const 32 | { 33 | return m_List; 34 | } 35 | private: 36 | List::DoubleList m_List; 37 | }; 38 | 39 | template 40 | DynQueue::DynQueue() 41 | { 42 | } 43 | 44 | template 45 | DynQueue::~DynQueue() 46 | { 47 | } 48 | 49 | template 50 | DynQueue::DynQueue(const DynQueue& dqA_) 51 | { 52 | m_List = dqA_.m_List; 53 | } 54 | 55 | template 56 | typename DynQueue& DynQueue::operator=(const DynQueue& dqA_) 57 | { 58 | if (this == &dqA_) 59 | { 60 | return *this; 61 | } 62 | 63 | m_List = dqA_.m_List; 64 | return *this; 65 | } 66 | 67 | template 68 | void DynQueue::In(const T& nValue_) 69 | { 70 | m_List.InsertLast(nValue_); 71 | } 72 | 73 | template 74 | T DynQueue::Out() 75 | { 76 | if (IsEmpty()) 77 | { 78 | throw "queue is empty"; 79 | } 80 | 81 | List::DoubleList::Node* _pFirst = m_List.GetFirst(); 82 | T _nValue = _pFirst->GetValue(); 83 | m_List.DeleteFirst(); 84 | return _nValue; 85 | } 86 | 87 | template 88 | T DynQueue::Peek() const 89 | { 90 | if (IsEmpty()) 91 | { 92 | throw "queue is empty"; 93 | } 94 | 95 | List::DoubleList::Node* _pFirst = m_List.GetFirst(); 96 | return _pFirst->GetValue(); 97 | } 98 | 99 | template 100 | bool DynQueue::IsEmpty() const 101 | { 102 | return m_List.IsEmpty(); 103 | } 104 | } 105 | } 106 | } 107 | #endif 108 | 109 | -------------------------------------------------------------------------------- /DataStruct/Queue/MinPriorityQueue.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_DATASTRUCT_QUEUE_PRIORITYQUEUE_H 7 | #define AILIB_DATASTRUCT_QUEUE_PRIORITYQUEUE_H 8 | #include "..\..\stdafx.h" 9 | #include "..\Heap\MinHeap.h" 10 | 11 | namespace AlLib 12 | { 13 | namespace DataStruct 14 | { 15 | namespace Queue 16 | { 17 | template 18 | class MinPriorityQueue 19 | { 20 | public: 21 | MinPriorityQueue(); 22 | virtual ~MinPriorityQueue(); 23 | 24 | MinPriorityQueue(const MinPriorityQueue& dqA_); 25 | MinPriorityQueue& operator=(const MinPriorityQueue& dqA_); 26 | 27 | void In(const T& nValue_); 28 | T Out(); 29 | T Peek() const; 30 | bool IsEmpty() const; 31 | Heap::MinHeap GetHeap() const 32 | { 33 | return m_mhHeap; 34 | } 35 | private: 36 | Heap::MinHeap m_mhHeap; 37 | }; 38 | 39 | template 40 | MinPriorityQueue::MinPriorityQueue() 41 | { 42 | } 43 | 44 | template 45 | MinPriorityQueue::~MinPriorityQueue() 46 | { 47 | } 48 | 49 | template 50 | MinPriorityQueue::MinPriorityQueue(const MinPriorityQueue& mpqA_) 51 | { 52 | m_mhHeap = mpqA_.m_mhHeap; 53 | } 54 | 55 | template 56 | typename MinPriorityQueue& MinPriorityQueue::operator=(const MinPriorityQueue& mpqA_) 57 | { 58 | if (this == &mpqA_) 59 | { 60 | return *this; 61 | } 62 | 63 | m_mhHeap = mpqA_.m_mhHeap; 64 | return *this; 65 | } 66 | 67 | template 68 | void MinPriorityQueue::In(const T& nValue_) 69 | { 70 | m_mhHeap.Add(nValue_); 71 | } 72 | 73 | template 74 | typename T MinPriorityQueue::Out() 75 | { 76 | if (IsEmpty()) 77 | { 78 | throw "queue is empty"; 79 | } 80 | 81 | T _nValue = m_mhHeap.Get(1); 82 | m_mhHeap.Delete(1); 83 | return _nValue; 84 | } 85 | 86 | template 87 | typename T MinPriorityQueue::Peek() const 88 | { 89 | if (IsEmpty()) 90 | { 91 | throw "queue is empty"; 92 | } 93 | 94 | return m_mhHeap.Get(1); 95 | } 96 | 97 | template 98 | bool MinPriorityQueue::IsEmpty() const 99 | { 100 | return m_mhHeap.GetSize() <= 1; 101 | } 102 | } 103 | } 104 | } 105 | #endif 106 | 107 | -------------------------------------------------------------------------------- /DataStruct/Stack/DynStack.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef ALLIB_DATASTRUCT_STACK_STACK_H 7 | #define ALLIB_DATASTRUCT_STACK_STACK_H 8 | #include "..\..\stdafx.h" 9 | #include "..\Array\DynArray.h" 10 | 11 | namespace AlLib 12 | { 13 | namespace DataStruct 14 | { 15 | namespace Stack 16 | { 17 | template 18 | class DynStack 19 | { 20 | public: 21 | DynStack(); 22 | DynStack(const DynStack& sA_); 23 | DynStack& operator=(const DynStack& sA_); 24 | virtual ~DynStack(); 25 | 26 | void Push(const T& nNewValue_); 27 | T Pop(); 28 | void Reset(); 29 | T Peek(unsigned int nReverseIndex_ = 0) const; 30 | bool IsEmpty() const; 31 | int GetSize() const; 32 | Array::DynArray GetArray() const 33 | { 34 | return m_arrValues; 35 | } 36 | private: 37 | Array::DynArray m_arrValues; 38 | }; 39 | 40 | template 41 | DynStack::DynStack() 42 | { 43 | 44 | } 45 | 46 | template 47 | DynStack::DynStack(const DynStack& sA_) 48 | { 49 | m_arrValues = sA_.m_arrValues; 50 | } 51 | 52 | template 53 | typename DynStack& DynStack::operator=(const DynStack& sA_) 54 | { 55 | m_arrValues = sA_.m_arrValues; 56 | return *this; 57 | } 58 | 59 | template 60 | DynStack::~DynStack() 61 | { 62 | 63 | } 64 | 65 | template 66 | void DynStack::Push(const T& nNewValue_) 67 | { 68 | m_arrValues.Add(nNewValue_); 69 | } 70 | 71 | template 72 | T DynStack::Pop() 73 | { 74 | if (IsEmpty()) 75 | { 76 | throw "stack is empty"; 77 | } 78 | 79 | int _nSize = m_arrValues.GetSize(); 80 | T _nTopValue = m_arrValues[_nSize - 1]; 81 | m_arrValues.DeleteByIndex(_nSize - 1); 82 | return _nTopValue; 83 | } 84 | 85 | template 86 | void DynStack::Reset() 87 | { 88 | m_arrValues.Reset(); 89 | } 90 | 91 | template 92 | T DynStack::Peek(unsigned int nReverseIndex_) const 93 | { 94 | if ((unsigned int)m_arrValues.GetSize() < (nReverseIndex_ + 1)) 95 | { 96 | throw "out of size"; 97 | } 98 | 99 | int _nSize = m_arrValues.GetSize(); 100 | if (nReverseIndex_ >= (unsigned int)_nSize) 101 | { 102 | throw "the position to peek is not exist"; 103 | } 104 | 105 | return m_arrValues[_nSize - 1 - nReverseIndex_]; 106 | } 107 | 108 | template 109 | bool DynStack::IsEmpty() const 110 | { 111 | return m_arrValues.GetSize() == 0; 112 | } 113 | 114 | template 115 | int DynStack::GetSize() const 116 | { 117 | return m_arrValues.GetSize(); 118 | } 119 | } 120 | } 121 | } 122 | #endif 123 | 124 | 125 | -------------------------------------------------------------------------------- /DataStruct/Tree/SortedBalanceBinaryTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/DataStruct/Tree/SortedBalanceBinaryTree.h -------------------------------------------------------------------------------- /DataStruct/Tree/SortedBinaryTree.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_DATASTRUCT_TREE_SORTEDBINARYTREE_H 7 | #define AILIB_DATASTRUCT_TREE_SORTEDBINARYTREE_H 8 | #include "..\..\stdafx.h" 9 | #include "..\Array\DynArray.h" 10 | namespace AlLib 11 | { 12 | namespace DataStruct 13 | { 14 | namespace Tree 15 | { 16 | template 17 | class SortedBinaryTree 18 | { 19 | public: 20 | class Pair 21 | { 22 | public: 23 | Pair() 24 | { 25 | } 26 | 27 | Pair(const Key& key_, const Value& value_) 28 | { 29 | m_nKey = key_; 30 | m_nValue = value_; 31 | } 32 | 33 | ~Pair() 34 | { 35 | } 36 | 37 | public: 38 | Key m_nKey; 39 | Value m_nValue; 40 | }; 41 | 42 | class Node 43 | { 44 | public: 45 | Value GetValue() 46 | { 47 | return m_nPair.m_nValue; 48 | } 49 | 50 | Key GetKey() 51 | { 52 | return m_nPair.m_nKey; 53 | } 54 | 55 | void SetValue(const Value& nValue_) 56 | { 57 | m_nPair.m_nValue = nValue_; 58 | } 59 | 60 | Pair GetPair() 61 | { 62 | return m_nPair; 63 | } 64 | 65 | private: 66 | Node() 67 | { 68 | m_pParent = nullptr; 69 | m_pLeftChild = nullptr; 70 | m_pRightChild = nullptr; 71 | } 72 | 73 | Node(const Pair& nPair_) 74 | { 75 | m_nPair = nPair_; 76 | m_pParent = nullptr; 77 | m_pLeftChild = nullptr; 78 | m_pRightChild = nullptr; 79 | } 80 | 81 | ~Node() 82 | { 83 | } 84 | 85 | private: 86 | Node* m_pParent; 87 | Node* m_pLeftChild; 88 | Node* m_pRightChild; 89 | Pair m_nPair; 90 | friend class SortedBinaryTree; 91 | }; 92 | 93 | SortedBinaryTree(); 94 | ~SortedBinaryTree(); 95 | 96 | SortedBinaryTree(const SortedBinaryTree& minTree_); 97 | SortedBinaryTree& operator=(const SortedBinaryTree& minTree_); 98 | 99 | void PreVisit(std::function travelFunction_, Node* pRoot_ = nullptr) const; 100 | void PostVisit(std::function travelFunction_, Node* pRoot_ = nullptr) const; 101 | void Visit(std::function travelFunction_, Node* pRoot_ = nullptr) const; 102 | 103 | Node* Min(Node* pRoot_ = nullptr) const; 104 | Node* Max(Node* pRoot_ = nullptr) const; 105 | 106 | Node* Pre(Node *pNode_) const; 107 | Node* Suc(Node *pNode_) const; 108 | 109 | bool Search(const Key& key_, Value& value_, Node* pRoot_ = nullptr) const; 110 | Node* Search(const Key& key_, Node* pRoot_ = nullptr) const; 111 | bool Add(const Pair& nPair_); 112 | void Delete(const Key& key_, Node* pRoot_ = nullptr); 113 | void DeleteAll(); 114 | 115 | Node* GetRoot() const; 116 | Array::DynArray GetArray() const 117 | { 118 | Array::DynArray _arrContain; 119 | PreVisit([&_arrContain](Node* pNode_) 120 | { 121 | _arrContain.Add(pNode_->m_nPair); 122 | }); 123 | 124 | return _arrContain; 125 | } 126 | 127 | /*void DeleteEx(const Key& key_, Node* pRoot_ = nullptr);*/ 128 | private: 129 | void Delete(Node* pNode_); 130 | private: 131 | Node* m_pRoot; 132 | }; 133 | 134 | template 135 | SortedBinaryTree::SortedBinaryTree() 136 | { 137 | m_pRoot = nullptr; 138 | } 139 | 140 | template 141 | SortedBinaryTree::~SortedBinaryTree() 142 | { 143 | Array::DynArray _arrNodes; 144 | PreVisit([&_arrNodes](Node* pNode_) 145 | { 146 | _arrNodes.Add(pNode_); 147 | }); 148 | 149 | int _nSize = _arrNodes.GetSize(); 150 | for (int _i = 0; _i < _nSize; _i++) 151 | { 152 | delete _arrNodes[_i]; 153 | _arrNodes[_i] = nullptr; 154 | } 155 | 156 | m_pRoot = nullptr; 157 | } 158 | 159 | template 160 | void SortedBinaryTree::PreVisit( 161 | std::function travelFunction_, 162 | Node* pRoot_) const 163 | { 164 | Node *_pRoot = nullptr; 165 | if (pRoot_ == nullptr) 166 | { 167 | _pRoot = m_pRoot; 168 | } 169 | else 170 | { 171 | _pRoot = pRoot_; 172 | } 173 | 174 | if (_pRoot == nullptr) 175 | { 176 | return; 177 | } 178 | 179 | travelFunction_(_pRoot); 180 | if (_pRoot->m_pLeftChild != nullptr) 181 | { 182 | PreVisit(travelFunction_, _pRoot->m_pLeftChild); 183 | } 184 | 185 | if (_pRoot->m_pRightChild != nullptr) 186 | { 187 | PreVisit(travelFunction_, _pRoot->m_pRightChild); 188 | } 189 | } 190 | 191 | template 192 | void SortedBinaryTree::PostVisit( 193 | std::function travelFunction_, 194 | Node* pRoot_) const 195 | { 196 | Node *_pRoot = nullptr; 197 | if (pRoot_ == nullptr) 198 | { 199 | _pRoot = m_pRoot; 200 | } 201 | else 202 | { 203 | _pRoot = pRoot_; 204 | } 205 | 206 | if (_pRoot == nullptr) 207 | { 208 | return; 209 | } 210 | 211 | if (_pRoot->m_pLeftChild != nullptr) 212 | { 213 | PostVisit(travelFunction_, _pRoot->m_pLeftChild); 214 | } 215 | 216 | if (_pRoot->m_pRightChild != nullptr) 217 | { 218 | PostVisit(travelFunction_, _pRoot->m_pRightChild); 219 | } 220 | 221 | travelFunction_(_pRoot); 222 | } 223 | 224 | template 225 | void SortedBinaryTree::Visit( 226 | std::function travelFunction_, 227 | Node* pRoot_) const 228 | { 229 | Node *_pRoot = nullptr; 230 | if (pRoot_ == nullptr) 231 | { 232 | _pRoot = m_pRoot; 233 | } 234 | else 235 | { 236 | _pRoot = pRoot_; 237 | } 238 | 239 | if (_pRoot == nullptr) 240 | { 241 | return; 242 | } 243 | 244 | if (_pRoot->m_pLeftChild != nullptr) 245 | { 246 | Visit(travelFunction_, _pRoot->m_pLeftChild); 247 | } 248 | 249 | travelFunction_(_pRoot); 250 | if (_pRoot->m_pRightChild != nullptr) 251 | { 252 | Visit(travelFunction_, _pRoot->m_pRightChild); 253 | } 254 | } 255 | 256 | template 257 | typename SortedBinaryTree::Node* SortedBinaryTree::Min(Node* pRoot_) const 258 | { 259 | Node *_pRoot = nullptr; 260 | if (pRoot_ == nullptr) 261 | { 262 | _pRoot = m_pRoot; 263 | } 264 | else 265 | { 266 | _pRoot = pRoot_; 267 | } 268 | 269 | if (_pRoot == nullptr) 270 | { 271 | return nullptr; 272 | } 273 | 274 | if (_pRoot->m_pLeftChild != nullptr) 275 | { 276 | return Min(_pRoot->m_pLeftChild); 277 | } 278 | else 279 | { 280 | return _pRoot; 281 | } 282 | } 283 | 284 | template 285 | typename SortedBinaryTree::Node* SortedBinaryTree::Max(Node* pRoot_) const 286 | { 287 | Node *_pRoot = nullptr; 288 | if (pRoot_ == nullptr) 289 | { 290 | _pRoot = m_pRoot; 291 | } 292 | else 293 | { 294 | _pRoot = pRoot_; 295 | } 296 | 297 | if (_pRoot == nullptr) 298 | { 299 | return nullptr; 300 | } 301 | 302 | if (_pRoot->m_pRightChild != nullptr) 303 | { 304 | return Max(_pRoot->m_pRightChild); 305 | } 306 | else 307 | { 308 | return _pRoot; 309 | } 310 | } 311 | 312 | template 313 | bool SortedBinaryTree::Search(const Key& key_, Value& value_, Node* pRoot_) const 314 | { 315 | Node *_pRoot = nullptr; 316 | if (pRoot_ == nullptr) 317 | { 318 | _pRoot = m_pRoot; 319 | } 320 | else 321 | { 322 | _pRoot = pRoot_; 323 | } 324 | 325 | if (_pRoot == nullptr) 326 | { 327 | return false; 328 | } 329 | 330 | if (_pRoot->m_nPair.m_nKey == key_) 331 | { 332 | value_ = _pRoot->m_nPair.m_nValue; 333 | return true; 334 | } 335 | else if (_pRoot->m_nPair.m_nKey < key_) 336 | { 337 | if (_pRoot->m_pRightChild != nullptr) 338 | { 339 | return Search(key_, value_, _pRoot->m_pRightChild); 340 | } 341 | else 342 | { 343 | return false; 344 | } 345 | } 346 | else 347 | { 348 | if (_pRoot->m_pLeftChild != nullptr) 349 | { 350 | return Search(key_, value_, _pRoot->m_pLeftChild); 351 | } 352 | else 353 | { 354 | return false; 355 | } 356 | } 357 | } 358 | 359 | template 360 | typename SortedBinaryTree::Node* SortedBinaryTree::Search(const Key& key_, Node* pRoot_) const 361 | { 362 | Node *_pRoot = nullptr; 363 | if (pRoot_ == nullptr) 364 | { 365 | _pRoot = m_pRoot; 366 | } 367 | else 368 | { 369 | _pRoot = pRoot_; 370 | } 371 | 372 | if (_pRoot == nullptr) 373 | { 374 | return nullptr; 375 | } 376 | 377 | if (_pRoot->m_nPair.m_nKey == key_) 378 | { 379 | return _pRoot; 380 | } 381 | else if (_pRoot->m_nPair.m_nKey < key_) 382 | { 383 | if (_pRoot->m_pRightChild != nullptr) 384 | { 385 | return Search(key_, _pRoot->m_pRightChild); 386 | } 387 | else 388 | { 389 | return nullptr; 390 | } 391 | } 392 | else 393 | { 394 | if (_pRoot->m_pLeftChild != nullptr) 395 | { 396 | return Search(key_, _pRoot->m_pLeftChild); 397 | } 398 | else 399 | { 400 | return nullptr; 401 | } 402 | } 403 | } 404 | 405 | template 406 | void SortedBinaryTree::Delete(const Key& key_, Node* pRoot_) 407 | { 408 | Node *_pRoot = nullptr; 409 | if (pRoot_ == nullptr) 410 | { 411 | _pRoot = m_pRoot; 412 | } 413 | else 414 | { 415 | _pRoot = pRoot_; 416 | } 417 | 418 | if (_pRoot == nullptr) 419 | { 420 | return; 421 | } 422 | 423 | Node* _pNode = Search(key_, _pRoot); 424 | if (_pNode == nullptr) 425 | { 426 | return; 427 | } 428 | 429 | Delete(_pNode); 430 | } 431 | 432 | template 433 | void SortedBinaryTree::Delete(Node* pNode_) 434 | { 435 | Node* _pNode = pNode_; 436 | if (_pNode == nullptr) 437 | { 438 | throw "input error"; 439 | } 440 | 441 | if (_pNode->m_pLeftChild == nullptr 442 | && _pNode->m_pRightChild == nullptr) 443 | { 444 | if (_pNode->m_pParent == nullptr) 445 | { 446 | delete _pNode; 447 | _pNode = nullptr; 448 | m_pRoot = nullptr; 449 | return; 450 | } 451 | 452 | if (_pNode->m_pParent->m_pLeftChild == _pNode) 453 | { 454 | _pNode->m_pParent->m_pLeftChild = nullptr; 455 | delete _pNode; 456 | _pNode = nullptr; 457 | return; 458 | } 459 | else 460 | { 461 | _pNode->m_pParent->m_pRightChild = nullptr; 462 | delete _pNode; 463 | _pNode = nullptr; 464 | return; 465 | } 466 | } 467 | else if (_pNode->m_pLeftChild != nullptr 468 | && _pNode->m_pRightChild == nullptr) 469 | { 470 | Node *_pMovingNode = Max(_pNode->m_pLeftChild); 471 | _pNode->m_nPair = _pMovingNode->m_nPair; 472 | Delete(_pMovingNode); 473 | } 474 | else if (_pNode->m_pLeftChild == nullptr 475 | && _pNode->m_pRightChild != nullptr) 476 | { 477 | Node *_pMovingNode = Min(_pNode->m_pRightChild); 478 | _pNode->m_nPair = _pMovingNode->m_nPair; 479 | Delete(_pMovingNode); 480 | } 481 | else 482 | { 483 | Node *_pMovingNode = Max(_pNode->m_pLeftChild); 484 | _pNode->m_nPair = _pMovingNode->m_nPair; 485 | Delete(_pMovingNode); 486 | } 487 | 488 | return; 489 | } 490 | 491 | /*template 492 | void SortedBinaryTree::Delete(const Key& key_, Node* pRoot_) 493 | { 494 | Node *_pRoot = nullptr; 495 | if (pRoot_ == nullptr) 496 | { 497 | _pRoot = m_pRoot; 498 | } 499 | else 500 | { 501 | _pRoot = pRoot_; 502 | } 503 | 504 | if (_pRoot == nullptr) 505 | { 506 | return; 507 | } 508 | 509 | Node* _pNode = Search(key_, _pRoot); 510 | if (_pNode == nullptr) 511 | { 512 | return; 513 | } 514 | 515 | if (_pNode->m_pLeftChild == nullptr 516 | && _pNode->m_pRightChild == nullptr) 517 | { 518 | if (_pNode->m_pParent == nullptr) 519 | { 520 | delete _pNode; 521 | _pNode = nullptr; 522 | m_pRoot = nullptr; 523 | return; 524 | } 525 | 526 | if (_pNode->m_pParent->m_pLeftChild == _pNode) 527 | { 528 | _pNode->m_pParent->m_pLeftChild = nullptr; 529 | delete _pNode; 530 | _pNode = nullptr; 531 | return; 532 | } 533 | else 534 | { 535 | _pNode->m_pParent->m_pRightChild = nullptr; 536 | delete _pNode; 537 | _pNode = nullptr; 538 | return; 539 | } 540 | } 541 | else if (_pNode->m_pLeftChild != nullptr 542 | && _pNode->m_pRightChild == nullptr) 543 | { 544 | Node *_pMovingNode = Max(_pNode->m_pLeftChild); 545 | _pNode->m_nPair = _pMovingNode->m_nPair; 546 | Delete(_pMovingNode->m_nPair.m_nKey, _pNode->m_pLeftChild); 547 | } 548 | else if (_pNode->m_pLeftChild == nullptr 549 | && _pNode->m_pRightChild != nullptr) 550 | { 551 | Node *_pMovingNode = Min(_pNode->m_pRightChild); 552 | _pNode->m_nPair = _pMovingNode->m_nPair; 553 | Delete(_pMovingNode->m_nPair.m_nKey, _pNode->m_pRightChild); 554 | } 555 | else 556 | { 557 | Node *_pMovingNode = Max(_pNode->m_pLeftChild); 558 | _pNode->m_nPair = _pMovingNode->m_nPair; 559 | Delete(_pMovingNode->m_nPair.m_nKey, _pNode->m_pLeftChild); 560 | } 561 | 562 | return; 563 | }*/ 564 | 565 | template 566 | void SortedBinaryTree::DeleteAll() 567 | { 568 | this->~SortedBinaryTree(); 569 | } 570 | 571 | template 572 | typename SortedBinaryTree::Node* SortedBinaryTree::Pre(Node *pNode_) const 573 | { 574 | if (pNode_ == nullptr) 575 | { 576 | return nullptr; 577 | } 578 | 579 | if (pNode_->m_pLeftChild != nullptr) 580 | { 581 | return Max(pNode_->m_pLeftChild); 582 | } 583 | else 584 | { 585 | while (true) 586 | { 587 | if (pNode_->m_pParent == nullptr) 588 | { 589 | return nullptr; 590 | } 591 | else if (pNode_->m_pParent->m_pLeftChild == pNode_) 592 | { 593 | pNode_ = pNode_->m_pParent; 594 | } 595 | else 596 | { 597 | return pNode_->m_pParent; 598 | } 599 | } 600 | } 601 | } 602 | 603 | template 604 | typename SortedBinaryTree::Node* SortedBinaryTree::Suc(Node *pNode_) const 605 | { 606 | if (pNode_ == nullptr) 607 | { 608 | return nullptr; 609 | } 610 | 611 | if (pNode_->m_pRightChild != nullptr) 612 | { 613 | return Min(pNode_->m_pRightChild); 614 | } 615 | else 616 | { 617 | while (true) 618 | { 619 | if (pNode_->m_pParent == nullptr) 620 | { 621 | return nullptr; 622 | } 623 | else if (pNode_->m_pParent->m_pRightChild == pNode_) 624 | { 625 | pNode_ = pNode_->m_pParent; 626 | } 627 | else 628 | { 629 | return pNode_->m_pParent; 630 | } 631 | } 632 | } 633 | } 634 | 635 | template 636 | bool SortedBinaryTree::Add(const Pair& nPair_) 637 | { 638 | Node *_pNewNode = nullptr; 639 | try 640 | { 641 | _pNewNode = new Node(); 642 | } 643 | catch (...) 644 | { 645 | _pNewNode = nullptr; 646 | throw "out of memory"; 647 | } 648 | 649 | _pNewNode->m_nPair = nPair_; 650 | if (m_pRoot == nullptr) 651 | { 652 | m_pRoot = _pNewNode; 653 | return true; 654 | } 655 | 656 | Node* _pNode = m_pRoot; 657 | while (true) 658 | { 659 | if (_pNode->m_nPair.m_nKey > _pNewNode->m_nPair.m_nKey) 660 | { 661 | if (_pNode->m_pLeftChild == nullptr) 662 | { 663 | _pNode->m_pLeftChild = _pNewNode; 664 | _pNewNode->m_pParent = _pNode; 665 | return true; 666 | } 667 | else 668 | { 669 | _pNode = _pNode->m_pLeftChild; 670 | } 671 | } 672 | else if (_pNode->m_nPair.m_nKey < _pNewNode->m_nPair.m_nKey) 673 | { 674 | if (_pNode->m_pRightChild == nullptr) 675 | { 676 | _pNode->m_pRightChild = _pNewNode; 677 | _pNewNode->m_pParent = _pNode; 678 | return true; 679 | } 680 | else 681 | { 682 | _pNode = _pNode->m_pRightChild; 683 | } 684 | } 685 | else 686 | { 687 | if (_pNewNode) 688 | { 689 | delete _pNewNode; 690 | _pNewNode = nullptr; 691 | } 692 | 693 | return false; 694 | } 695 | } 696 | } 697 | 698 | template 699 | typename SortedBinaryTree::Node* SortedBinaryTree::GetRoot() const 700 | { 701 | return m_pRoot; 702 | } 703 | 704 | template 705 | SortedBinaryTree::SortedBinaryTree(const SortedBinaryTree& minTree_) 706 | :m_pRoot(nullptr) 707 | { 708 | Array::DynArray _arrPairs = minTree_.GetArray(); 709 | int _nSize = _arrPairs.GetSize(); 710 | for (int _i = 0; _i < _nSize; _i++) 711 | { 712 | Add(_arrPairs[_i]); 713 | } 714 | } 715 | 716 | template 717 | typename SortedBinaryTree& SortedBinaryTree::operator=(const SortedBinaryTree& minTree_) 718 | { 719 | if (this == &minTree_) 720 | { 721 | return *this; 722 | } 723 | 724 | this->~SortedBinaryTree(); 725 | Array::DynArray _arrPairs = minTree_.GetArray(); 726 | int _nSize = _arrPairs.GetSize(); 727 | for (int _i = 0; _i < _nSize; _i++) 728 | { 729 | Add(_arrPairs[_i]); 730 | } 731 | 732 | return *this; 733 | } 734 | } 735 | } 736 | } 737 | 738 | #endif 739 | -------------------------------------------------------------------------------- /Design/Divide/Divide.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Divide/Divide.txt -------------------------------------------------------------------------------- /Design/DynamicPlanning/DynamicPlanning.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/DynamicPlanning/DynamicPlanning.txt -------------------------------------------------------------------------------- /Design/DynamicPlanning/MaxProfit.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/DynamicPlanning/MaxProfit.cpp -------------------------------------------------------------------------------- /Design/DynamicPlanning/MaxProfit.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_DESIGN_DP_MAXPROFIT_H 7 | #define AILIB_DESIGN_DP_MAXPROFIT_H 8 | #include "..\..\stdafx.h" 9 | 10 | extern "C" ALGORITHMLIB double CalculateMaxProfit1(int nLength_, const double* pPriceTable_, int nNum_); 11 | extern "C" ALGORITHMLIB double CalculateMaxProfit2(int nLength_, const double* pPriceTable_, double * pMaxProfitTable_, int nNum_); 12 | extern "C" ALGORITHMLIB double CalculateMaxProfitByIterration(int nLength_, const double* pPriceTable_, int nNum_); 13 | #endif 14 | -------------------------------------------------------------------------------- /Design/DynamicPlanning/MinExpectTimes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/DynamicPlanning/MinExpectTimes.cpp -------------------------------------------------------------------------------- /Design/DynamicPlanning/MinExpectTimes.h: -------------------------------------------------------------------------------- 1 | // Author : XuBenHao 2 | // Version : 1.0.0 3 | // Mail : xbh370970843@163.com 4 | // Copyright : XuBenHao 2020 - 2030 5 | 6 | #ifndef AILIB_DESIGN_DP_MINEXPECTTIMES_H 7 | #define AILIB_DESIGN_DP_MINEXPECTTIMES_H 8 | #include "..\..\stdafx.h" 9 | extern "C" ALGORITHMLIB double CalculateMinExpectTimes( 10 | const double* pArr_, 11 | int nS_, 12 | int nE_); 13 | 14 | #endif 15 | 16 | -------------------------------------------------------------------------------- /Design/Greedy/ActionsPlanning.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Greedy/ActionsPlanning.cpp -------------------------------------------------------------------------------- /Design/Greedy/ActionsPlanning.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Greedy/ActionsPlanning.h -------------------------------------------------------------------------------- /Design/Greedy/GreedyAlgorithm.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Greedy/GreedyAlgorithm.txt -------------------------------------------------------------------------------- /Design/Greedy/HufManAlgorithm.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Greedy/HufManAlgorithm.h -------------------------------------------------------------------------------- /Design/Greedy/ZeroOnePack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Greedy/ZeroOnePack.cpp -------------------------------------------------------------------------------- /Design/Greedy/ZeroOnePack.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Greedy/ZeroOnePack.h -------------------------------------------------------------------------------- /Design/Iterator/Iteration.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/Design/Iterator/Iteration.txt -------------------------------------------------------------------------------- /External/Math/Data/ConstValue.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_DATA_CONSTVALUE_H 2 | #define MATH_DATA_CONSTVALUE_H 3 | #include "..\Stdafx.h" 4 | namespace Math 5 | { 6 | #define PI 3.14159265 7 | } 8 | #endif -------------------------------------------------------------------------------- /External/Math/Data/Helper.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_DATA_HELPER_H 2 | #define MATH_DATA_HELPER_H 3 | #include "..\Stdafx.h" 4 | namespace Math 5 | { 6 | class Helper 7 | { 8 | public: 9 | template 10 | static T Max(const T& t1_, const T& t2_); 11 | template 12 | static T Min(const T& t1_, const T& t2_); 13 | }; 14 | 15 | template 16 | typename T Helper::Max(const T& t1_, const T& t2_) 17 | { 18 | if (t1_ < t2_) 19 | { 20 | return t2_; 21 | } 22 | else 23 | { 24 | return t1_; 25 | } 26 | } 27 | 28 | template 29 | typename T Helper::Min(const T& t1_, const T& t2_) 30 | { 31 | if (t1_ < t2_) 32 | { 33 | return t1_; 34 | } 35 | else 36 | { 37 | return t2_; 38 | } 39 | } 40 | 41 | } 42 | #endif -------------------------------------------------------------------------------- /External/Math/Data/Point.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_DATA_POINT_H 2 | #define MATH_DATA_POINT_H 3 | #include "..\Stdafx.h" 4 | namespace Math 5 | { 6 | template 7 | class Point 8 | { 9 | public: 10 | Point(); 11 | ~Point(); 12 | 13 | bool operator==(const Point& po_); 14 | bool operator!=(const Point& po_); 15 | Point operator+(const Point& po_); 16 | Point operator-(const Point& po_); 17 | Point operator*(double nRatio_); 18 | Point operator/(double nDivide_); 19 | 20 | public: 21 | double m_nPos[N]; 22 | }; 23 | 24 | template 25 | Point::Point() 26 | { 27 | for (int _i = 0; _i < N; _i++) 28 | { 29 | m_nPos[_i] = 0.0; 30 | } 31 | } 32 | 33 | template 34 | Point::~Point() 35 | { 36 | 37 | } 38 | 39 | template 40 | bool Point::operator==(const Point& po_) 41 | { 42 | bool _bIdentity = true; 43 | for (int _i = 0; _i < N; _i++) 44 | { 45 | if (m_nPos[_i] != po_.m_nPos[_i]) 46 | { 47 | _bIdentity = false; 48 | break; 49 | } 50 | } 51 | 52 | return _bIdentity; 53 | } 54 | 55 | template 56 | bool Point::operator!=(const Point& po_) 57 | { 58 | return !operator==(po_); 59 | } 60 | 61 | template 62 | Point Point::operator+(const Point& po_) 63 | { 64 | Point _poP; 65 | for (int _i = 0; _i < N; _i++) 66 | { 67 | _poP.m_nPos[_i] = m_nPos[_i] + po_.m_nPos[_i]; 68 | } 69 | 70 | return _poP; 71 | } 72 | 73 | template 74 | Point Point::operator-(const Point& po_) 75 | { 76 | Point _poP; 77 | for (int _i = 0; _i < N; _i++) 78 | { 79 | _poP.m_nPos[_i] = m_nPos[_i] - po_.m_nPos[_i]; 80 | } 81 | 82 | return _poP; 83 | } 84 | 85 | template 86 | Point Point::operator*(double nRatio_) 87 | { 88 | Point _poP; 89 | for (int _i = 0; _i < N; _i++) 90 | { 91 | _poP.m_nPos[_i] = m_nPos[_i] * nRatio_; 92 | } 93 | 94 | return _poP; 95 | } 96 | 97 | template 98 | Point Point::operator/(double nDivide_) 99 | { 100 | if (nDivide_ == 0.0) 101 | { 102 | throw "cannot be divided by zero"; 103 | } 104 | 105 | Point _poP; 106 | for (int _i = 0; _i < N; _i++) 107 | { 108 | _poP.m_nPos[_i] = m_nPos[_i] / nDivide_; 109 | } 110 | 111 | return _poP; 112 | } 113 | } 114 | #endif 115 | -------------------------------------------------------------------------------- /External/Math/Data/Vector.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/External/Math/Data/Vector.h -------------------------------------------------------------------------------- /External/Math/Math.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_MATH_H 2 | #define MATH_MATH_H 3 | #include "Data/Point.h" 4 | #include "Data/Vector.h" 5 | #include "Data/Helper.h" 6 | #include "Data/ConstValue.h" 7 | #endif 8 | 9 | -------------------------------------------------------------------------------- /External/Math/Stdafx.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_STDAFX_H 2 | #define MATH_STDAFX_H 3 | #ifdef MATHLIB 4 | #else 5 | #define MATHLIB __declspec(dllimport) 6 | #endif 7 | #endif 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | 24 | -------------------------------------------------------------------------------- /External/PlaneGeometry/External/Math/Data/Point.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_DATA_POINT_H 2 | #define MATH_DATA_POINT_H 3 | #include "..\Stdafx.h" 4 | namespace Math 5 | { 6 | template 7 | class Point 8 | { 9 | public: 10 | Point(); 11 | ~Point(); 12 | 13 | public: 14 | double m_nPos[N]; 15 | }; 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /External/PlaneGeometry/External/Math/Data/Vector.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xubenhao/Algorithm/f35301a3c3a8f6d7ee7bfb623294ebb3f5f22664/External/PlaneGeometry/External/Math/Data/Vector.h -------------------------------------------------------------------------------- /External/PlaneGeometry/External/Math/Math.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_MATH_H 2 | #define MATH_MATH_H 3 | #include "Data/Point.h" 4 | #include "Data/Vector.h" 5 | #endif 6 | 7 | -------------------------------------------------------------------------------- /External/PlaneGeometry/External/Math/Stdafx.h: -------------------------------------------------------------------------------- 1 | #ifndef MATH_STDAFX_H 2 | #define MATH_STDAFX_H 3 | #ifdef MATHLIB 4 | #else 5 | #define MATHLIB __declspec(dllimport) 6 | #endif 7 | #endif 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | 24 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Geometry/Geometry.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_GEOMETRY_GEOMETRY_H 2 | #define PLANEGEOMETRY_GEOMETRY_GEOMETRY_H 3 | #include "..\Stdafx.h" 4 | namespace PlaneGeometry 5 | { 6 | extern "C" class PLANEGEOMETRYLIB Geometry 7 | { 8 | public: 9 | Geometry(); 10 | ~Geometry(); 11 | 12 | private: 13 | 14 | }; 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Geometry/LineGeometry.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_GEOMETRY_LINEGEOMETRY_H 2 | #define PLANEGEOMETRY_GEOMETRY_LINEGEOMETRY_H 3 | #include "..\Stdafx.h" 4 | #include "Geometry.h" 5 | namespace PlaneGeometry 6 | { 7 | extern "C" class PLANEGEOMETRYLIB LineGeometry : Geometry 8 | { 9 | public: 10 | LineGeometry(); 11 | ~LineGeometry(); 12 | Math::Vector<2> GetVector() const; 13 | public: 14 | Math::Point<2> m_poStart; 15 | Math::Point<2> m_poEnd; 16 | }; 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Geometry/PathGeometry.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_GEOMETRY_PATHGEOMETRY_H 2 | #define PLANEGEOMETRY_GEOMETRY_PATHGEOMETRY_H 3 | #include "..\Stdafx.h" 4 | #include "Geometry.h" 5 | namespace PlaneGeometry 6 | { 7 | extern "C" class PLANEGEOMETRYLIB PathGeometry : Geometry 8 | { 9 | public: 10 | PathGeometry(); 11 | ~PathGeometry(); 12 | 13 | private: 14 | }; 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /External/PlaneGeometry/PlaneGeometry.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_PLANEGEOMETRY_H 2 | #define PLANEGEOMETRY_PLANEGEOMETRY_H 3 | #include "Geometry/Geometry.h" 4 | #include "Geometry/LineGeometry.h" 5 | #include "Geometry/PathGeometry.h" 6 | 7 | #include "Segment/Segment.h" 8 | #include "Segment/LineSegment.h" 9 | #include "Segment/ArcSegment.h" 10 | #endif 11 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Segment/ArcSegment.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_SEGMENT_ARCSEGMENT_H 2 | #define PLANEGEOMETRY_SEGMENT_ARCSEGMENT_H 3 | #include "..\Stdafx.h" 4 | #include "Segment.h" 5 | namespace PlaneGeometry 6 | { 7 | extern "C" class PLANEGEOMETRYLIB ArcSegment : Segment 8 | { 9 | public: 10 | ArcSegment(); 11 | ~ArcSegment(); 12 | }; 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Segment/LineSegment.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_SEGMENT_LINESEGMENT_H 2 | #define PLANEGEOMETRY_SEGMENT_LINESEGMENT_H 3 | #include "..\Stdafx.h" 4 | #include "Segment.h" 5 | namespace PlaneGeometry 6 | { 7 | extern "C" class PLANEGEOMETRYLIB LineSegment : Segment 8 | { 9 | public: 10 | LineSegment(); 11 | ~LineSegment(); 12 | 13 | }; 14 | } 15 | #endif 16 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Segment/Segment.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_SEGMENT_SEGMENT_H 2 | #define PLANEGEOMETRY_SEGMENT_SEGMENT_H 3 | #include "..\Stdafx.h" 4 | namespace PlaneGeometry 5 | { 6 | extern "C" class PLANEGEOMETRYLIB Segment 7 | { 8 | public: 9 | Segment(); 10 | virtual ~Segment(); 11 | 12 | private: 13 | 14 | }; 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /External/PlaneGeometry/Stdafx.h: -------------------------------------------------------------------------------- 1 | #ifndef PLANEGEOMETRY_STDAFX_H 2 | #define PLANEGEOMETRY_STDAFX_H 3 | #ifdef PLANEGEOMETRYLIB 4 | #else 5 | #define PLANEGEOMETRYLIB __declspec(dllimport) 6 | #endif 7 | 8 | #include "External/Math/Math.h" 9 | #endif 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 XuBenHao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 数据结构与算法库 2 | ## 数据结构 3 | ### 动态数组 4 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Array/DynArray.h 5 | ### 动态栈 6 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Stack/DynStack.h 7 | ### 双向链表 8 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/List/DoubleList.h 9 | ### 最小堆 10 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Heap/MinHeap.h 11 | ### 队列 12 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Queue/DynQueue.h 13 | ### 基于最小堆的优先队列 14 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Queue/MinPriorityQueue.h 15 | ### 基于数组-链表的哈希表 16 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Hash/ListHash.h 17 | ### 二叉搜索树 18 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Tree/SortedBinaryTree.h 19 | ### 二叉搜索平衡树/红黑树 20 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Tree/SortedBalanceBinaryTree.h 21 | ### 图 22 | https://github.com/xubenhao/Algorithm/blob/master/DataStruct/Graph/Graph.h 23 | ## 算法 24 | ### 二分搜索 25 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Search/Find.h 26 | ### 快速排序 27 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Sort/Sort.h 28 | ### 归并排序 29 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Sort/Sort.h 30 | ### 图的广度优先搜索 31 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/BreadthFirstVisit.h 32 | ### 图的深度优先搜索 33 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/DepthFirstVisit.h 34 | ### 拓扑排序 35 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/TopologySort.h 36 | ### 强连通分量 37 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/StrongConnectGraph.h 38 | ### 图的转置 39 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/ReverseGraph.h 40 | ### 图单源最短路径 41 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/ShorestPath.h 42 | ### 无向连通图的最小生成树 43 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/MinGenerateTree.h 44 | ### 无反向边流量图的最大流 45 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Graph/MaxStream.h 46 | ### 基于自动机的字符串模式匹配 47 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Char/CharMatch.h 48 | ### 基于KMP的字符串模式匹配 49 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Char/CharMatch.h 50 | ### 线段与线段交点判断 51 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Geometry/SegmentIntersect.h 52 | ### 点集中两点的最短距离 53 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Geometry/MinDistance.h 54 | ### 线段集合的相交检测 55 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Geometry/SegmentIntersect.h 56 | ### 点集的凸包 57 | https://github.com/xubenhao/Algorithm/blob/master/Algorithm/Geometry/ConvexHull.h 58 | ## 算法设计思想 59 | ### 分治 60 | https://github.com/xubenhao/Algorithm/tree/master/Design/Divide 61 | ### 迭代 62 | https://github.com/xubenhao/Algorithm/tree/master/Design/Iterator 63 | ### 动态规划 64 | https://github.com/xubenhao/Algorithm/tree/master/Design/DynamicPlanning 65 | ### 贪心 66 | https://github.com/xubenhao/Algorithm/tree/master/Design/Greedy 67 | -------------------------------------------------------------------------------- /stdafx.cpp: -------------------------------------------------------------------------------- 1 | #include "Stdafx.h" 2 | // use svn as version manage 3 | -------------------------------------------------------------------------------- /stdafx.h: -------------------------------------------------------------------------------- 1 | #ifndef ALGORITHM_STDAFX_H 2 | #define ALGORITHM_STDAFX_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | 18 | #include "External/Math/Math.h" 19 | #include "External/PlaneGeometry/PlaneGeometry.h" 20 | #ifdef ALGORITHMLIB 21 | #else 22 | #define ALGORITHMLIB __declspec(dllimport) 23 | #endif 24 | #endif 25 | --------------------------------------------------------------------------------